Skip to content

Commit 973f07a

Browse files
committed
fix: added processOperators and processOperator to lazyLoader class
1 parent c1f17b4 commit 973f07a

File tree

1 file changed

+63
-54
lines changed

1 file changed

+63
-54
lines changed

src/server.js

+63-54
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ class CoCreateLazyLoader {
275275

276276
const methodPath = method.split('.')
277277

278-
await processOperators(data, '', parameters);
278+
await this.processOperators(data, '', parameters);
279279

280280
event = await executeMethod(method, methodPath, instance, parameters)
281281
}
@@ -290,7 +290,7 @@ class CoCreateLazyLoader {
290290

291291
let execute = webhook.events[eventName];
292292
if (execute) {
293-
execute = await processOperators(data, event, execute);
293+
execute = await this.processOperators(data, event, execute);
294294
}
295295

296296
data.res.writeHead(200, { 'Content-Type': 'application/json' });
@@ -304,6 +304,67 @@ class CoCreateLazyLoader {
304304
}
305305
}
306306

307+
async processOperators(data, event, execute, parent = null, parentKey = null) {
308+
if (Array.isArray(execute)) {
309+
for (let index = 0; index < execute.length; index++) {
310+
execute[index] = await this.processOperators(data, event, execute[index], execute, index);
311+
}
312+
} else if (typeof execute === 'object' && execute !== null) {
313+
for (let key of Object.keys(execute)) {
314+
if (key.startsWith('$')) {
315+
const operatorResult = await this.processOperator(data, event, key, execute[key]);
316+
if (parent && operatorResult !== null && parentKey !== null) {
317+
parent[parentKey] = operatorResult;
318+
await this.processOperators(data, event, parent[parentKey], parent, parentKey);
319+
}
320+
} else {
321+
execute[key] = await this.processOperators(data, event, execute[key], execute, key);
322+
}
323+
}
324+
} else {
325+
return await this.processOperator(data, event, execute);
326+
}
327+
return execute;
328+
}
329+
330+
async processOperator(data, event, operator, context) {
331+
let result
332+
if (operator.startsWith('$data.')) {
333+
return getValueFromObject(data, operator.substring(6))
334+
} else if (operator.startsWith('$req')) {
335+
return getValueFromObject(data, operator.substring(1))
336+
} else if (operator.startsWith('$header')) {
337+
return getValueFromObject(data.req, operator.substring(1))
338+
} else if (operator.startsWith('$rawBody')) {
339+
return getValueFromObject(data, operator.substring(1))
340+
} else if (operator.startsWith('$crud')) {
341+
context = await this.processOperators(data, event, context);
342+
result = await this.crud.send(context)
343+
if (operator.startsWith('$crud.'))
344+
result = getValueFromObject(operator, operator.substring(6))
345+
return await this.processOperators(data, event, result);
346+
} else if (operator.startsWith('$socket')) {
347+
context = await this.processOperators(data, event, context);
348+
result = await this.socket.send(context)
349+
if (operator.startsWith('$socket.'))
350+
result = getValueFromObject(operator, operator.substring(6))
351+
return await this.processOperators(data, event, result);
352+
} else if (operator.startsWith('$api')) {
353+
context = await this.processOperators(data, event, context);
354+
let name = context.method.split('.')[0]
355+
result = this.executeScriptWithTimeout(name, context)
356+
if (operator.startsWith('$api.'))
357+
result = getValueFromObject(event, operator.substring(5))
358+
return await this.processOperators(data, event, result);
359+
} else if (operator.startsWith('$event')) {
360+
if (operator.startsWith('$event.'))
361+
result = getValueFromObject(event, operator.substring(7))
362+
return await this.processOperators(data, event, result);
363+
}
364+
365+
return operator;
366+
}
367+
307368
async getApiKey(data, name) {
308369
let organization = await this.crud.getOrganization(data);
309370
if (organization.error)
@@ -317,28 +378,6 @@ class CoCreateLazyLoader {
317378

318379
}
319380

320-
async function processOperators(data, event, execute, parent = null, parentKey = null) {
321-
if (Array.isArray(execute)) {
322-
for (let index = 0; index < execute.length; index++) {
323-
execute[index] = await processOperators(data, event, execute[index], execute, index);
324-
}
325-
} else if (typeof execute === 'object' && execute !== null) {
326-
for (let key of Object.keys(execute)) {
327-
if (key.startsWith('$')) {
328-
const operatorResult = await processOperator(data, event, key, execute[key]);
329-
if (parent && operatorResult !== null && parentKey !== null) {
330-
parent[parentKey] = operatorResult;
331-
await processOperators(data, event, parent[parentKey], parent, parentKey);
332-
}
333-
} else {
334-
execute[key] = await processOperators(data, event, execute[key], execute, key);
335-
}
336-
}
337-
} else {
338-
return await processOperator(data, event, execute);
339-
}
340-
return execute;
341-
}
342381

343382
// async function processOperators(data, event, execute, parent = null, parentKey = null) {
344383
// if (Array.isArray(execute)) {
@@ -369,36 +408,6 @@ async function processOperators(data, event, execute, parent = null, parentKey =
369408
// }
370409
// }
371410

372-
async function processOperator(data, event, operator, context) {
373-
if (operator.startsWith('$data.')) {
374-
operator = getValueFromObject(data, operator.substring(6))
375-
} else if (operator.startsWith('$req')) {
376-
console.log(operator.substring(1))
377-
operator = getValueFromObject(data, operator.substring(1))
378-
} else if (operator.startsWith('$header')) {
379-
operator = getValueFromObject(data.req, operator.substring(1))
380-
} else if (operator.startsWith('$rawBody')) {
381-
operator = getValueFromObject(data, operator.substring(1))
382-
} else if (operator.startsWith('$crud')) {
383-
operator = await data.crud.send(context)
384-
let name = operator.method.split('.')[0]
385-
operator = operator[name]
386-
} else if (operator.startsWith('$socket')) {
387-
operator = await data.socket.send(context)
388-
let name = operator.method.split('.')[0]
389-
operator = operator[name]
390-
} else if (operator.startsWith('$api')) {
391-
let name = context.method.split('.')[0]
392-
operator = this.executeScriptWithTimeout(name, context)
393-
} else if (operator.startsWith('$webhook.')) {
394-
operator = getValueFromObject(webhook, operator.substring(9))
395-
} else if (operator.startsWith('$event.')) {
396-
operator = getValueFromObject(event, operator.substring(7))
397-
}
398-
399-
return operator;
400-
}
401-
402411
async function executeMethod(method, methodPath, instance, params) {
403412
try {
404413
switch (methodPath.length) {

0 commit comments

Comments
 (0)