@@ -46,10 +46,12 @@ class CoCreateLazyLoader {
4646 try {
4747 const valideUrl = new URL ( `http://${ req . headers . host } ${ req . url } ` ) ;
4848 const hostname = valideUrl . hostname ;
49-
50- let organization = await this . crud . getHost ( hostname ) ;
51- if ( organization . error )
49+ let organization
50+ try {
51+ organization = await this . crud . getOrganization ( { host : hostname } ) ;
52+ } catch {
5253 return this . files . send ( req , res , this . crud , organization , valideUrl )
54+ }
5355
5456 if ( valideUrl . pathname . startsWith ( '/webhooks/' ) ) {
5557 let name = req . url . split ( '/' ) [ 2 ] ; // Assuming URL structure is /webhook/name/...
@@ -75,7 +77,7 @@ class CoCreateLazyLoader {
7577 try {
7678 if ( this . modules [ name ] . initialize ) {
7779 if ( data . req )
78- data = await this . webhooks ( data )
80+ data = await this . webhooks ( this . modules [ name ] , data , name )
7981 else
8082 data = await this . api ( this . modules [ name ] , data )
8183 } else {
@@ -94,17 +96,18 @@ class CoCreateLazyLoader {
9496 }
9597
9698 if ( this . modules [ name ] . content ) {
97- data . apis = await this . getApiKey ( data . organization_id , name )
99+ data . apis = await this . getApiKey ( data , name )
98100 data . crud = this . crud
99101 data = await this . modules [ name ] . content . send ( data )
100102 delete data . apis
101103 delete data . crud
102- if ( data . socket )
103- this . wsManager . send ( data )
104104 } else
105105 return
106106 }
107107
108+ if ( data . socket )
109+ this . wsManager . send ( data )
110+
108111 if ( this . modules [ name ] . unload === false || this . modules [ name ] . unload === 'false' )
109112 return
110113 else if ( this . modules [ name ] . unload === true || this . modules [ name ] . unload === 'true' )
@@ -134,34 +137,25 @@ class CoCreateLazyLoader {
134137 this . modules [ name ] . timeout = timeout
135138 }
136139 } catch ( error ) {
137- console . log ( error )
140+ data . error = error . message
141+ if ( data . req ) {
142+ data . res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } ) ;
143+ data . res . end ( `Lazyload Error: ${ error . message } ` ) ;
144+ } if ( data . socket )
145+ this . wsManager . send ( data )
138146 }
139147 }
140148
141- async getApiKey ( organization_id , name ) {
142- let organization = await this . crud . getOrganization ( organization_id ) ;
143- if ( organization . error )
144- return organization . error
145- if ( ! organization . apis )
146- return { error : 'Missing apis object in organization object' }
147- if ( ! organization . apis [ name ] )
148- return { error : `Missing ${ name } in organization apis object` }
149- return organization . apis [ name ]
150- }
151-
152149 async api ( config , data ) {
153150 try {
154151 const methodPath = data . method . split ( '.' )
155152 const name = methodPath . shift ( )
156153
157- const apis = await this . getApiKey ( data . organization_id , name )
158- if ( apis . error )
159- return data . error = apis . error
160-
154+ const apis = await this . getApiKey ( data , name )
161155 const environment = data . environment || 'production' ;
162- const key = apis [ environment ] ;
156+ const key = apis [ environment ] . key ;
163157 if ( ! key )
164- return data . error = `Missing ${ name } key in organization apis object`
158+ throw new Error ( `Missing ${ name } key in organization apis object` ) ;
165159
166160 const service = require ( config . path ) ;
167161 const instance = new service [ config . initialize ] ( key ) ;
@@ -170,40 +164,39 @@ class CoCreateLazyLoader {
170164 for ( let i = 0 ; i < methodPath . length ; i ++ ) {
171165 method = method [ methodPath [ i ] ]
172166 if ( method === undefined ) {
173- return data . error = `Method ${ methodPath [ i ] } not found using ${ data . method } .`
167+ throw new Error ( `Method ${ methodPath [ i ] } not found using ${ data . method } .` ) ;
174168 }
175169 }
176170
177171 if ( typeof method !== 'function' )
178- return data . error = `Method ${ data . method } is not a function.`
172+ throw new Error ( `Method ${ data . method } is not a function.` ) ;
179173
180- return data . postmark = await method . apply ( instance , [ data [ name ] ] ) ;
174+ data . postmark = await method . apply ( instance , [ data [ name ] ] ) ;
175+ return data
181176 } catch ( error ) {
182- return data . error = error . message
177+ data . error = error . message
178+ return data
183179 }
184180 }
185181
186- async webhooks ( config , data ) {
182+ async webhooks ( config , data , name ) {
187183 try {
188- const apis = await this . getApiKey ( data . organization_id , name )
189- if ( apis . error )
190- return data . error = apis . error
191-
184+ const apis = await this . getApiKey ( data , name )
192185 let environment = data . environment || 'production' ;
193186 if ( data . host . startsWith ( 'dev.' ) || data . host . startsWith ( 'test.' ) )
194187 environment = 'test'
195188
196- const key = apis [ environment ] ;
189+ const key = apis [ environment ] . key ;
197190 if ( ! key )
198- return data . error = `Missing ${ name } key in organization apis object`
191+ throw new Error ( `Missing ${ name } key in organization apis object` ) ;
199192
200193 let name = data . req . url . split ( '/' ) ;
201194 name = name [ 3 ] || name [ 2 ] || name [ 1 ]
202195
203196 // TODO: webhook secert could be a key pair
204197 const webhookSecret = data . apis [ environment ] . webhooks [ name ] ;
205198 if ( webhookSecret !== req . headers [ name ] )
206- return data . error = `Webhook secret failed for ${ name } . Unauthorized access attempt.` ;
199+ throw new Error ( `Webhook secret failed for ${ name } . Unauthorized access attempt.` ) ;
207200
208201 let rawBody = '' ;
209202 await new Promise ( ( resolve , reject ) => {
@@ -232,10 +225,22 @@ class CoCreateLazyLoader {
232225 } catch ( error ) {
233226 data . error = error . message
234227 data . res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } ) ;
235- data . res . end ( `Webhook Error: ${ err . message } ` ) ;
228+ data . res . end ( error . message ) ;
236229 return data
237230 }
238231 }
232+
233+ async getApiKey ( data , name ) {
234+ let organization = await this . crud . getOrganization ( data ) ;
235+ if ( organization . error )
236+ throw new Error ( organization . error ) ;
237+ if ( ! organization . apis )
238+ throw new Error ( 'Missing apis object in organization object' ) ;
239+ if ( ! organization . apis [ name ] )
240+ throw new Error ( `Missing ${ name } in organization apis object` ) ;
241+ return organization . apis [ name ]
242+ }
243+
239244}
240245
241246function getModuleDependencies ( modulePath ) {
0 commit comments