@@ -10,17 +10,29 @@ const REG_TEXT = /^text\/(html|plain|xml)/;
1010var 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
3951function 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
4458APIOptions . prototype . retry = function ( ) {
@@ -58,8 +72,8 @@ APICallProto.output = function(type) {
5872} ;
5973
6074APICallProto . 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
7791APICallProto . 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+
88109APICallProto . 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
120141APICallProto . 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
148169APICallProto . callback = APICallProto . pipe = function ( $ ) {
149- var t = this ;
170+ const t = this ;
150171 t . $callback = typeof ( $ ) === 'function' ? $ : $ . callback ( ) ;
151172 return t ;
152173} ;
153174
154175APICallProto . 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
186207function 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
195224exports . 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 } ;
0 commit comments