@@ -76,7 +76,7 @@ class CoCreateLazyLoader {
7676
7777 async executeScriptWithTimeout ( name , data ) {
7878 try {
79- if ( this . modules [ name ] . initialize ) {
79+ if ( this . modules [ name ] . initialize || this . modules [ name ] . initialize === '' ) {
8080 if ( data . req )
8181 data = await this . webhooks ( this . modules [ name ] , data , name )
8282 else
@@ -159,18 +159,11 @@ class CoCreateLazyLoader {
159159 throw new Error ( `Missing ${ name } key in organization apis object` ) ;
160160
161161 const service = require ( config . path ) ;
162- const instance = new service [ config . initialize ] ( key ) ;
163-
164- let method = instance
165- for ( let i = 0 ; i < methodPath . length ; i ++ ) {
166- method = method [ methodPath [ i ] ]
167- if ( method === undefined ) {
168- throw new Error ( `Method ${ methodPath [ i ] } not found using ${ data . method } .` ) ;
169- }
170- }
171-
172- if ( typeof method !== 'function' )
173- throw new Error ( `Method ${ data . method } is not a function.` ) ;
162+ let instance
163+ if ( config . initialize )
164+ instance = new service [ config . initialize ] ( key ) ;
165+ else
166+ instance = new service ( key ) ;
174167
175168 let params = [ ] , mainParam = false
176169 for ( let i = 0 ; true ; i ++ ) {
@@ -191,7 +184,7 @@ class CoCreateLazyLoader {
191184 // execute = await processOperators(data, execute);
192185 // }
193186
194- data [ name ] = await method . apply ( instance , params ) ;
187+ data [ name ] = await executeMethod ( data . method , methodPath , instance , params )
195188
196189 // let execute = webhook.events[eventName];
197190 // if (execute) {
@@ -272,18 +265,13 @@ class CoCreateLazyLoader {
272265 event = JSON . parse ( rawBody )
273266 } else {
274267 const service = require ( config . path ) ;
275- const instance = new service [ config . initialize ] ( key ) ;
276- const methodPath = method . split ( '.' )
277- let property = instance
278-
279- for ( let i = 0 ; i < methodPath . length ; i ++ ) {
280- property = property [ methodPath [ i ] ]
281- if ( property === undefined ) {
282- throw new Error ( `Method ${ methodPath [ i ] } not found using ${ data . method } .` ) ;
283- }
284- }
268+ let instance
269+ if ( config . initialize )
270+ instance = new service [ config . initialize ] ( key ) ;
271+ else
272+ instance = new service ( key ) ;
285273
286- event = await property . apply ( instance , parameters ) ;
274+ event = await executeMethod ( name + '.' + method , methodPath , instance , parameters )
287275 }
288276
289277 let eventName = getValueFromObject ( event , nameKey )
@@ -379,6 +367,45 @@ async function processOperator(data, event, operator, context) {
379367 return operator ;
380368}
381369
370+ async function executeMethod ( method , methodPath , instance , params ) {
371+ try {
372+ switch ( methodPath . length ) {
373+ case 1 :
374+ return await instance [ methodPath [ 0 ] ] ( ...params )
375+ case 2 :
376+ return await instance [ methodPath [ 0 ] ] [ methodPath [ 1 ] ] ( ...params ) ;
377+ case 3 :
378+ return await instance [ methodPath [ 0 ] ] [ methodPath [ 1 ] ] [ methodPath [ 2 ] ] ( ...params ) ;
379+ case 4 :
380+ return await instance [ methodPath [ 0 ] ] [ methodPath [ 1 ] ] [ methodPath [ 2 ] ] [ methodPath [ 3 ] ] ( ...params ) ;
381+ case 5 :
382+ return await instance [ methodPath [ 0 ] ] [ methodPath [ 1 ] ] [ methodPath [ 2 ] ] [ methodPath [ 3 ] ] [ methodPath [ 4 ] ] ( ...params ) ;
383+ case 6 :
384+ return await instance [ methodPath [ 0 ] ] [ methodPath [ 1 ] ] [ methodPath [ 2 ] ] [ methodPath [ 3 ] ] [ methodPath [ 4 ] ] [ methodPath [ 5 ] ] ( ...params ) ;
385+ case 7 :
386+ return await instance [ methodPath [ 0 ] ] [ methodPath [ 1 ] ] [ methodPath [ 2 ] ] [ methodPath [ 3 ] ] [ methodPath [ 4 ] ] [ methodPath [ 5 ] ] [ methodPath [ 6 ] ] ( ...params ) ;
387+ case 8 :
388+ return await instance [ methodPath [ 0 ] ] [ methodPath [ 1 ] ] [ methodPath [ 2 ] ] [ methodPath [ 3 ] ] [ methodPath [ 4 ] ] [ methodPath [ 5 ] ] [ methodPath [ 6 ] ] [ methodPath [ 7 ] ] ( ...params ) ;
389+ default :
390+ const methodName = methodPath . pop ( ) ;
391+ let Method = instance
392+ for ( let i = 0 ; i < methodPath . length ; i ++ ) {
393+ Method = Method [ methodPath [ i ] ]
394+ if ( Method === undefined ) {
395+ throw new Error ( `Method ${ methodPath [ i ] } not found using ${ method } .` ) ;
396+ }
397+ }
398+
399+ if ( typeof Method [ methodName ] !== 'function' )
400+ throw new Error ( `Method ${ method } is not a function.` ) ;
401+
402+ return await Method [ methodName ] ( ...params )
403+ }
404+ } catch ( error ) {
405+ throw new Error ( `Method ${ method } not found.` ) ;
406+ }
407+ }
408+
382409function getModuleDependencies ( modulePath ) {
383410 let moduleObj = require . cache [ modulePath ] ;
384411 if ( ! moduleObj ) {
0 commit comments