@@ -46,10 +46,12 @@ class CoCreateLazyLoader {
46
46
try {
47
47
const valideUrl = new URL ( `http://${ req . headers . host } ${ req . url } ` ) ;
48
48
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 {
52
53
return this . files . send ( req , res , this . crud , organization , valideUrl )
54
+ }
53
55
54
56
if ( valideUrl . pathname . startsWith ( '/webhooks/' ) ) {
55
57
let name = req . url . split ( '/' ) [ 2 ] ; // Assuming URL structure is /webhook/name/...
@@ -75,7 +77,7 @@ class CoCreateLazyLoader {
75
77
try {
76
78
if ( this . modules [ name ] . initialize ) {
77
79
if ( data . req )
78
- data = await this . webhooks ( data )
80
+ data = await this . webhooks ( this . modules [ name ] , data , name )
79
81
else
80
82
data = await this . api ( this . modules [ name ] , data )
81
83
} else {
@@ -94,17 +96,18 @@ class CoCreateLazyLoader {
94
96
}
95
97
96
98
if ( this . modules [ name ] . content ) {
97
- data . apis = await this . getApiKey ( data . organization_id , name )
99
+ data . apis = await this . getApiKey ( data , name )
98
100
data . crud = this . crud
99
101
data = await this . modules [ name ] . content . send ( data )
100
102
delete data . apis
101
103
delete data . crud
102
- if ( data . socket )
103
- this . wsManager . send ( data )
104
104
} else
105
105
return
106
106
}
107
107
108
+ if ( data . socket )
109
+ this . wsManager . send ( data )
110
+
108
111
if ( this . modules [ name ] . unload === false || this . modules [ name ] . unload === 'false' )
109
112
return
110
113
else if ( this . modules [ name ] . unload === true || this . modules [ name ] . unload === 'true' )
@@ -134,34 +137,25 @@ class CoCreateLazyLoader {
134
137
this . modules [ name ] . timeout = timeout
135
138
}
136
139
} 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 )
138
146
}
139
147
}
140
148
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
-
152
149
async api ( config , data ) {
153
150
try {
154
151
const methodPath = data . method . split ( '.' )
155
152
const name = methodPath . shift ( )
156
153
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 )
161
155
const environment = data . environment || 'production' ;
162
- const key = apis [ environment ] ;
156
+ const key = apis [ environment ] . key ;
163
157
if ( ! key )
164
- return data . error = `Missing ${ name } key in organization apis object`
158
+ throw new Error ( `Missing ${ name } key in organization apis object` ) ;
165
159
166
160
const service = require ( config . path ) ;
167
161
const instance = new service [ config . initialize ] ( key ) ;
@@ -170,40 +164,39 @@ class CoCreateLazyLoader {
170
164
for ( let i = 0 ; i < methodPath . length ; i ++ ) {
171
165
method = method [ methodPath [ i ] ]
172
166
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 } .` ) ;
174
168
}
175
169
}
176
170
177
171
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.` ) ;
179
173
180
- return data . postmark = await method . apply ( instance , [ data [ name ] ] ) ;
174
+ data . postmark = await method . apply ( instance , [ data [ name ] ] ) ;
175
+ return data
181
176
} catch ( error ) {
182
- return data . error = error . message
177
+ data . error = error . message
178
+ return data
183
179
}
184
180
}
185
181
186
- async webhooks ( config , data ) {
182
+ async webhooks ( config , data , name ) {
187
183
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 )
192
185
let environment = data . environment || 'production' ;
193
186
if ( data . host . startsWith ( 'dev.' ) || data . host . startsWith ( 'test.' ) )
194
187
environment = 'test'
195
188
196
- const key = apis [ environment ] ;
189
+ const key = apis [ environment ] . key ;
197
190
if ( ! key )
198
- return data . error = `Missing ${ name } key in organization apis object`
191
+ throw new Error ( `Missing ${ name } key in organization apis object` ) ;
199
192
200
193
let name = data . req . url . split ( '/' ) ;
201
194
name = name [ 3 ] || name [ 2 ] || name [ 1 ]
202
195
203
196
// TODO: webhook secert could be a key pair
204
197
const webhookSecret = data . apis [ environment ] . webhooks [ name ] ;
205
198
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.` ) ;
207
200
208
201
let rawBody = '' ;
209
202
await new Promise ( ( resolve , reject ) => {
@@ -232,10 +225,22 @@ class CoCreateLazyLoader {
232
225
} catch ( error ) {
233
226
data . error = error . message
234
227
data . res . writeHead ( 400 , { 'Content-Type' : 'text/plain' } ) ;
235
- data . res . end ( `Webhook Error: ${ err . message } ` ) ;
228
+ data . res . end ( error . message ) ;
236
229
return data
237
230
}
238
231
}
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
+
239
244
}
240
245
241
246
function getModuleDependencies ( modulePath ) {
0 commit comments