Skip to content

Commit ebe70db

Browse files
committed
Added config argument into the NEWAPI() and NEWAIMODEL().
1 parent d7475ca commit ebe70db

File tree

4 files changed

+87
-36
lines changed

4 files changed

+87
-36
lines changed

aimodel.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function AI(model) {
77

88
t.options = {};
99
t.options.callback = NOOP;
10+
t.config = {};
1011

1112
t.payload = {};
1213
t.payload.model = model;
@@ -39,6 +40,13 @@ AI.prototype.message = function(role, content, merge) {
3940
return t;
4041
};
4142

43+
AI.prototype.configure = function(opt) {
44+
const t = this;
45+
for (let key in opt)
46+
t.config[key] = opt[key];
47+
return t;
48+
};
49+
4250
AI.prototype.system = function(content, merge) {
4351
return this.message('system', content, merge);
4452
};
@@ -85,10 +93,16 @@ AI.prototype.callback = function(fn) {
8593
// Internal function
8694
AI.prototype.run = function() {
8795
const t = this;
88-
let fn = F.aimodels[t.payload.model];
89-
if (fn)
90-
fn(t);
91-
else
96+
let ai = F.aimodels[t.payload.model];
97+
if (ai) {
98+
if (ai.config) {
99+
for (let key in ai.config) {
100+
if (t.config[key] === undefined)
101+
t.config[key] = ai.config[key];
102+
}
103+
}
104+
ai.callback(t, t.options.callback);
105+
} else
92106
t.options.callback('AI model not found.');
93107
return t;
94108
};
@@ -97,8 +111,14 @@ exports.exec = function(model) {
97111
return new AI(model);
98112
};
99113

100-
exports.newai = function (model, callback) {
114+
exports.newai = function(model, config, callback) {
115+
116+
if (typeof(config) === 'function') {
117+
callback = config;
118+
config = null;
119+
}
120+
101121
const models = model.split(/,/).trim();
102122
for (const m of models)
103-
F.aimodels[m] = callback;
123+
F.aimodels[m] = { config, callback };
104124
};

api.js

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,29 @@ const REG_TEXT = /^text\/(html|plain|xml)/;
1010
var cache = {};
1111

1212
// Registers a new API type
13-
exports.newapi = function(type, callback) {
13+
exports.newapi = function(type, config, callback) {
1414

15-
if (typeof(type) === 'function') {
15+
let t = typeof(type);
16+
17+
if (t === 'function') {
1618
callback = type;
1719
type = 'default';
20+
config = null;
21+
} else if (t === 'object') {
22+
callback = config;
23+
config = type;
24+
type = 'default';
25+
}
26+
27+
if (typeof(config) === 'function') {
28+
callback = config;
29+
config = null;
1830
}
1931

2032
if (type.indexOf(',') !== -1) {
2133
var arr = type.split(',').trim();
2234
for (var m of arr)
23-
exports.newapi(m, callback);
35+
exports.newapi(m, config, callback);
2436
return;
2537
}
2638

@@ -30,15 +42,17 @@ exports.newapi = function(type, callback) {
3042
cache[lower] = lower;
3143

3244
if (callback)
33-
F.apiservices[lower] = callback;
45+
F.apiservices[lower] = { config, callback };
3446
else
3547
delete F.apiservices[lower];
3648

3749
};
3850

3951
function APIOptions(api) {
40-
this.api = api;
41-
this.retries = 0;
52+
const t = this;
53+
t.api = api;
54+
t.retries = 0;
55+
t.config = {};
4256
}
4357

4458
APIOptions.prototype.retry = function() {
@@ -58,8 +72,8 @@ APICallProto.output = function(type) {
5872
};
5973

6074
APICallProto.promise = function($) {
61-
var t = this;
62-
var promise = new Promise(function(resolve, reject) {
75+
const t = this;
76+
const promise = new Promise(function(resolve, reject) {
6377
t.$callback = function(err, response) {
6478
if (err) {
6579
if ($ && $.invalid) {
@@ -75,7 +89,7 @@ APICallProto.promise = function($) {
7589
};
7690

7791
APICallProto.audit = function($, message, type) {
78-
var t = this;
92+
const t = this;
7993
t.$audit = function() {
8094
// Dynamic arguments
8195
if (message)
@@ -85,8 +99,15 @@ APICallProto.audit = function($, message, type) {
8599
return t;
86100
};
87101

102+
APICallProto.configure = function(opt) {
103+
const t = this;
104+
for (let key in opt)
105+
t.options.config[key] = opt[key];
106+
return t;
107+
};
108+
88109
APICallProto.done = function($, callback) {
89-
var t = this;
110+
const t = this;
90111
t.$callback = function(err, response) {
91112
if (err)
92113
$.invalid(err);
@@ -119,12 +140,12 @@ APICallProto.controller = function($) {
119140

120141
APICallProto.file = function(filename, path, name) {
121142

122-
var t = this;
143+
const t = this;
123144

124145
if (!t.options.files)
125146
t.options.files = [];
126147

127-
var obj = { name: name || ('file' + t.options.files.length), filename: filename, path: path };
148+
const obj = { name: name || ('file' + t.options.files.length), filename: filename, path: path };
128149

129150
if (t.options.files)
130151
t.options.files.push(obj);
@@ -146,14 +167,14 @@ APICallProto.logerror = function() {
146167
};
147168

148169
APICallProto.callback = APICallProto.pipe = function($) {
149-
var t = this;
170+
const t = this;
150171
t.$callback = typeof($) === 'function' ? $ : $.callback();
151172
return t;
152173
};
153174

154175
APICallProto.evaluate = function(err, response) {
155176

156-
var t = this;
177+
const t = this;
157178
if (!err && t.$error) {
158179
if (t.$error_reverse) {
159180
if (response)
@@ -184,16 +205,24 @@ APICallProto.evaluate = function(err, response) {
184205
};
185206

186207
function execapi(api) {
187-
var conn = F.apiservices[cache[api.options.name]] || F.apiservices['*'];
188-
if (conn)
189-
conn.call(api, api.options, (err, response) => api.evaluate(err, response));
190-
else
208+
const conn = F.apiservices[cache[api.options.name]] || F.apiservices['*'];
209+
if (conn) {
210+
211+
if (conn.config) {
212+
for (let key in conn.config) {
213+
if (api.options.config[key] === undefined)
214+
api.options.config[key] = conn.config[key];
215+
}
216+
}
217+
218+
conn.callback.call(api, api.options, (err, response) => api.evaluate(err, response));
219+
} else
191220
api.evaluate('API is not initialized');
192221
}
193222

194223
// Executes API
195224
exports.exec = function(name, schema, data, $) {
196-
var api = new APICall();
225+
const api = new APICall();
197226
api.options.name = cache[name] || name;
198227
api.options.schema = schema;
199228
api.options.data = data;
@@ -212,7 +241,7 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
212241
if (opt.data && typeof(opt.data) !== 'object')
213242
opt.data = { value: opt.data };
214243

215-
var req = {};
244+
const req = {};
216245

217246
req.method = 'POST';
218247
req.url = 'https://' + F.config.$tapiurl + '.api.totaljs.com/' + opt.schema + '/';
@@ -226,7 +255,7 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
226255
req.type = 'json';
227256
req.timeout = 60000;
228257
req.keepalive = true;
229-
req.headers = { 'x-token': opt.token || F.config.totalapi || F.config.secret_totalapi || F.config.$tapisecret || '-', 'x-app': encodeURIComponent(F.config.name) };
258+
req.headers = { 'x-token': opt.token || opt.config.token || F.config.totalapi || F.config.secret_totalapi || F.config.$tapisecret || '-', 'x-app': encodeURIComponent(F.config.name) };
230259
req.custom = true;
231260

232261
req.callback = function(err, response) {
@@ -236,7 +265,7 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
236265
return;
237266
}
238267

239-
var buffer = [];
268+
const buffer = [];
240269

241270
// Error
242271
if (response.status > 200) {
@@ -256,7 +285,8 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
256285
if (opt.output === 'base64') {
257286
output = output.toString('base64');
258287
} else if (opt.output !== 'binary' && opt.output !== 'buffer') {
259-
var type = response.headers['content-type'];
288+
289+
const type = response.headers['content-type'];
260290

261291
if (REG_BINARY.test(type)) {
262292
next(null, output);
@@ -292,20 +322,20 @@ exports.newapi('TotalAPI,TAPI', function(opt, next) {
292322
}
293323

294324
var type = (response.headers['content-type'] || '').toLowerCase();
295-
var index = type.lastIndexOf(';');
325+
const index = type.lastIndexOf(';');
296326
if (index !== -1)
297327
type = type.substring(0, index);
298328

299-
var ext = type ? F.TUtils.getExtensionFromContentType(type) : 'bin';
300-
var id = fsdata[1] || UID();
301-
var filename = fsdata[2] || id + '.' + ext;
329+
const ext = type ? F.TUtils.getExtensionFromContentType(type) : 'bin';
330+
const id = fsdata[1] || UID();
331+
const filename = fsdata[2] || id + '.' + ext;
302332

303333
response.stream.pause();
304334
fs.save(id, filename, response.stream, next);
305335
return;
306336
}
307337

308-
var writer = F.Fs.createWriteStream(opt.output);
338+
const writer = F.Fs.createWriteStream(opt.output);
309339
response.stream.pipe(writer);
310340
F.cleanup(writer, () => opt.next(null, opt.output));
311341
};

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- added `exec` command to the remote editing functionality
2626
- always set `cwd` directory for the current executed script
2727
- added `timeout {Number}` property into the `NEWACTION` method
28+
- extended `NEWAPI(type, [config], config)` by adding the `config` argument
2829

2930
========================
3031
0.0.15

global.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ global.TMSCLIENT = F.TTMS.client;
250250

251251
// API
252252
global.API = (name, schema, data, $) => F.TApi.exec(name, schema, data, $);
253-
global.NEWAPI = (name, callback) => F.TApi.newapi(name, callback);
253+
global.NEWAPI = (name, config, callback) => F.TApi.newapi(name, config, callback);
254254

255255
// AI
256256
global.AIMODEL = (name, schema, data, $) => F.TAIModel.exec(name, schema, data, $);
257-
global.NEWAIMODEL = (name, callback) => F.TAIModel.newai(name, callback);
257+
global.NEWAIMODEL = (name, config, callback) => F.TAIModel.newai(name, config, callback);
258258

259259
// NoSQL
260260
global.NOSQL = F.TNoSQL.nosql;

0 commit comments

Comments
 (0)