@adminide-stack/form-builder-core 5.1.4-alpha.66 → 5.1.4-alpha.82
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/inngest/generateFunctionCode.d.ts +1 -0
- package/lib/inngest/generateFunctionCode.d.ts.map +1 -1
- package/lib/inngest/generateFunctionCode.js +72 -3
- package/lib/inngest/generateFunctionCode.js.map +1 -1
- package/lib/utils/json.d.ts +3 -0
- package/lib/utils/json.d.ts.map +1 -0
- package/lib/utils/json.js +43 -0
- package/lib/utils/json.js.map +1 -0
- package/package.json +2 -2
- package/src/index.ts +2 -1
- package/src/inngest/generateFunctionCode.ts +99 -2
- package/src/inngest/stepGenerator.ts +1 -1
- package/src/utils/json.ts +50 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [5.1.4-alpha.82](https://github.com/CDEBase/forms-stack/compare/v5.1.4-alpha.81...v5.1.4-alpha.82) (2025-10-25)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @adminide-stack/form-builder-core
|
|
9
|
+
|
|
10
|
+
## [5.1.4-alpha.81](https://github.com/CDEBase/forms-stack/compare/v5.1.4-alpha.80...v5.1.4-alpha.81) (2025-10-24)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @adminide-stack/form-builder-core
|
|
13
|
+
|
|
6
14
|
## [5.1.4-alpha.66](https://github.com/CDEBase/forms-stack/compare/v5.1.4-alpha.65...v5.1.4-alpha.66) (2025-10-16)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @adminide-stack/form-builder-core
|
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './inngest/interfaces/types';
|
|
2
2
|
export * from './inngest/generateFunctionCode';
|
|
3
|
-
export { generateFromExtractedFunctions } from './inngest/stepGenerator';
|
|
3
|
+
export { generateFromExtractedFunctions, extractFunctionBody } from './inngest/stepGenerator';
|
|
4
4
|
export * from './inngest/monacoAutocompleteIntegration';
|
|
5
|
+
export * from './utils/json';
|
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,OAAO,EAAE,8BAA8B,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,4BAA4B,CAAC;AAC3C,cAAc,gCAAgC,CAAC;AAC/C,OAAO,EAAE,8BAA8B,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9F,cAAc,yCAAyC,CAAC;AACxD,cAAc,cAAc,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{generateFunctionCode,generateHandlerBody,generateStepFromFunction,generateStepFunctionsFromDB,wrapStepsInInngestFunction}from'./inngest/generateFunctionCode.js';export{cleanStepCode,extractStepVarName,generateFromExtractedFunctions}from'./inngest/stepGenerator.js';export{getAvailableDefinitions,installLibraryForAutocomplete,setupMonacoAutocomplete,setupStepAutocomplete,updateAutocompleteConfig}from'./inngest/monacoAutocompleteIntegration.js';//# sourceMappingURL=index.js.map
|
|
1
|
+
export{generateFunctionCode,generateHandlerBody,generateHandlerBodyWithReturn,generateStepFromFunction,generateStepFunctionsFromDB,wrapStepsInInngestFunction}from'./inngest/generateFunctionCode.js';export{cleanStepCode,extractFunctionBody,extractStepVarName,generateFromExtractedFunctions}from'./inngest/stepGenerator.js';export{getAvailableDefinitions,installLibraryForAutocomplete,setupMonacoAutocomplete,setupStepAutocomplete,updateAutocompleteConfig}from'./inngest/monacoAutocompleteIntegration.js';export{flatten,unflatten}from'./utils/json.js';//# sourceMappingURL=index.js.map
|
|
@@ -2,6 +2,7 @@ import type { InngestFunctionDef, ExtractedFunction, StepFunction, GeneratedFunc
|
|
|
2
2
|
export { cleanStepCode, extractStepVarName } from './stepGenerator';
|
|
3
3
|
export declare function generateFunctionCode(def: InngestFunctionDef): string;
|
|
4
4
|
export declare function generateHandlerBody(def: InngestFunctionDef): string;
|
|
5
|
+
export declare function generateHandlerBodyWithReturn(def: InngestFunctionDef): string;
|
|
5
6
|
export declare function generateStepFunctionsFromDB(functionId: string, events: string[], extractedFunctions: ExtractedFunction[]): GeneratedFunctionResult;
|
|
6
7
|
export declare function wrapStepsInInngestFunction(functionDef: InngestFunctionDef, stepFunctions: StepFunction[]): string;
|
|
7
8
|
export declare function generateStepFromFunction(stepFunction: StepFunction, index: number): string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateFunctionCode.d.ts","sourceRoot":"","sources":["../../src/inngest/generateFunctionCode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,kBAAkB,EAElB,iBAAiB,EACjB,YAAY,EACZ,uBAAuB,EAC1B,MAAM,oBAAoB,CAAC;AAS5B,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"generateFunctionCode.d.ts","sourceRoot":"","sources":["../../src/inngest/generateFunctionCode.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACR,kBAAkB,EAElB,iBAAiB,EACjB,YAAY,EACZ,uBAAuB,EAC1B,MAAM,oBAAoB,CAAC;AAS5B,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAyEpE,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,kBAAkB,GAAG,MAAM,CAWpE;AAGD,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,kBAAkB,GAAG,MAAM,CAInE;AAiDD,wBAAgB,6BAA6B,CAAC,GAAG,EAAE,kBAAkB,GAAG,MAAM,CAkC7E;AAGD,wBAAgB,2BAA2B,CACvC,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EAAE,EAChB,kBAAkB,EAAE,iBAAiB,EAAE,GACxC,uBAAuB,CAazB;AAGD,wBAAgB,0BAA0B,CAAC,WAAW,EAAE,kBAAkB,EAAE,aAAa,EAAE,YAAY,EAAE,GAAG,MAAM,CAWjH;AAGD,wBAAgB,wBAAwB,CAAC,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAoB1F"}
|
|
@@ -23,6 +23,16 @@ function formatCode(code) {
|
|
|
23
23
|
// Basic formatting - in production, you'd use a proper formatter
|
|
24
24
|
return code.split('\n').map(line => line.trimEnd()).join('\n').replace(/\n{3,}/g, '\n\n'); // Remove excessive blank lines
|
|
25
25
|
}
|
|
26
|
+
function sanitizeDirectStepCode(code) {
|
|
27
|
+
if (!code) {
|
|
28
|
+
return code;
|
|
29
|
+
}
|
|
30
|
+
return code
|
|
31
|
+
// Remove inline object returns (e.g. return { success: true };)
|
|
32
|
+
.replace(/\n\s*return\s*\{[\s\S]*?\};?/g, '')
|
|
33
|
+
// Remove simple expression returns (e.g. return result;)
|
|
34
|
+
.replace(/\n\s*return\s+[^\n;]+;?/g, '');
|
|
35
|
+
}
|
|
26
36
|
function generateStepBlock(step, position) {
|
|
27
37
|
const stepVar = (step.code || '').match(/const\s+(\w+)\s*=/)?.[1] || `step_${position}`;
|
|
28
38
|
// If caller supplied code, embed it inside step.run wrapper by default for consistency
|
|
@@ -34,7 +44,7 @@ ${body}`;
|
|
|
34
44
|
}
|
|
35
45
|
const inner = extractFunctionBody(step.code || '') || 'return { success: true };';
|
|
36
46
|
return `// ${step.description || step.name || `Step ${position}`}
|
|
37
|
-
const ${stepVar}_result = await step.run('${stepVar}', async (
|
|
47
|
+
const ${stepVar}_result = await step.run('${stepVar}', async () => {
|
|
38
48
|
${indent(inner, 2)}
|
|
39
49
|
});`;
|
|
40
50
|
}
|
|
@@ -57,6 +67,65 @@ function generateHandlerBody(def) {
|
|
|
57
67
|
// Footer removed: callers should append their own consolidated return as needed
|
|
58
68
|
return `${stepBlocks}`;
|
|
59
69
|
}
|
|
70
|
+
function escapeSingleQuotes(value) {
|
|
71
|
+
return value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
72
|
+
}
|
|
73
|
+
function resolveStepVarName(step, index) {
|
|
74
|
+
const directMatch = (step.code || '').match(/const\s+(\w+)\s*=/);
|
|
75
|
+
if (directMatch) {
|
|
76
|
+
return directMatch[1];
|
|
77
|
+
}
|
|
78
|
+
if (typeof step.id === 'string' && step.id.trim()) {
|
|
79
|
+
return step.id;
|
|
80
|
+
}
|
|
81
|
+
return `step_${index + 1}`;
|
|
82
|
+
}
|
|
83
|
+
function resolveResultExpression(step, varName) {
|
|
84
|
+
switch (step.type) {
|
|
85
|
+
case 'sleep':
|
|
86
|
+
return 'sleepResult';
|
|
87
|
+
case 'sendEvent':
|
|
88
|
+
return 'eventResult';
|
|
89
|
+
case 'waitForEvent':
|
|
90
|
+
return 'waitResult';
|
|
91
|
+
default:
|
|
92
|
+
return `${varName}_result`;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function sanitizeSteps(def) {
|
|
96
|
+
const sanitizedSteps = def.steps.map(step => {
|
|
97
|
+
if (step.type === 'sleep' || step.type === 'sendEvent' || step.type === 'waitForEvent') {
|
|
98
|
+
return {
|
|
99
|
+
...step,
|
|
100
|
+
code: sanitizeDirectStepCode(step.code || '')
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
return step;
|
|
104
|
+
});
|
|
105
|
+
return {
|
|
106
|
+
...def,
|
|
107
|
+
steps: sanitizedSteps
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function generateHandlerBodyWithReturn(def) {
|
|
111
|
+
const sanitizedDef = sanitizeSteps(def);
|
|
112
|
+
const body = generateHandlerBody(sanitizedDef).trimEnd();
|
|
113
|
+
const resultLines = sanitizedDef.steps.map((step, index) => {
|
|
114
|
+
const varName = resolveStepVarName(step, index);
|
|
115
|
+
const resultExpr = resolveResultExpression(step, varName);
|
|
116
|
+
if (!resultExpr) {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
return ` ${varName}: ${resultExpr},`;
|
|
120
|
+
}).filter(line => Boolean(line));
|
|
121
|
+
const escapedId = escapeSingleQuotes(def.id || 'new-function');
|
|
122
|
+
const returnLines = ['return {', ` functionId: '${escapedId}',`, ...resultLines, ' timestamp: new Date().toISOString()', '};'];
|
|
123
|
+
const returnBlock = returnLines.join('\n');
|
|
124
|
+
if (!body) {
|
|
125
|
+
return returnBlock;
|
|
126
|
+
}
|
|
127
|
+
return `${body}\n\n${returnBlock}`;
|
|
128
|
+
}
|
|
60
129
|
// Generate Inngest function from database-extracted functions
|
|
61
130
|
function generateStepFunctionsFromDB(functionId, events, extractedFunctions) {
|
|
62
131
|
if (!extractedFunctions || extractedFunctions.length === 0) {
|
|
@@ -93,5 +162,5 @@ function generateStepFromFunction(stepFunction, index) {
|
|
|
93
162
|
if (hasDirectOps) {
|
|
94
163
|
return `// ${stepFunction.name || `Step ${index}`}\n${cleanedBody}`;
|
|
95
164
|
}
|
|
96
|
-
return `// ${stepFunction.name || `Step ${index}`}\nconst ${stepVar}_result = await step.run('${stepVar}', async (
|
|
97
|
-
}export{cleanStepCode,extractStepVarName,generateFunctionCode,generateHandlerBody,generateStepFromFunction,generateStepFunctionsFromDB,wrapStepsInInngestFunction};//# sourceMappingURL=generateFunctionCode.js.map
|
|
165
|
+
return `// ${stepFunction.name || `Step ${index}`}\nconst ${stepVar}_result = await step.run('${stepVar}', async () => {\n${indent(cleanedBody, 2)}\n});`;
|
|
166
|
+
}export{cleanStepCode,extractStepVarName,generateFunctionCode,generateHandlerBody,generateHandlerBodyWithReturn,generateStepFromFunction,generateStepFunctionsFromDB,wrapStepsInInngestFunction};//# sourceMappingURL=generateFunctionCode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateFunctionCode.js","sources":["../../src/inngest/generateFunctionCode.ts"],"sourcesContent":[null],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generateFunctionCode.js","sources":["../../src/inngest/generateFunctionCode.ts"],"sourcesContent":[null],"names":[],"mappings":"qHAuGA;AAqDA,SAAA;AAqCA,EAAA,OAAA,+CACI,CAAA,IAAA,CAAU,IAAE,CAAA;AAmBhB;AAcA,SAAA,oBAAgB,CAAA,IAAA,EAAA,EAAA,EAAA,KAAA,EAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../src/utils/json.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,SAAS,SAAK,EAAE,GAAG,GAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAM,2BAoBtG;AAED,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,MA2BrD"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
function flatten(obj, parentKey = '', res = {}) {
|
|
2
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
3
|
+
const newKey = parentKey ? `${parentKey}.${key}` : key;
|
|
4
|
+
if (Array.isArray(value)) {
|
|
5
|
+
value.forEach((item, index) => {
|
|
6
|
+
if (item !== null && typeof item === 'object') {
|
|
7
|
+
flatten(item, `${newKey}.${index}`, res);
|
|
8
|
+
} else {
|
|
9
|
+
res[`${newKey}.${index}`] = item;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
} else if (value !== null && typeof value === 'object') {
|
|
13
|
+
flatten(value, newKey, res);
|
|
14
|
+
} else {
|
|
15
|
+
res[newKey] = value;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return res;
|
|
19
|
+
}
|
|
20
|
+
function unflatten(obj) {
|
|
21
|
+
const result = {};
|
|
22
|
+
if (!obj || typeof obj !== 'object') return result;
|
|
23
|
+
for (const [flatKey, value] of Object.entries(obj)) {
|
|
24
|
+
const keys = flatKey.split('.');
|
|
25
|
+
keys.reduce((acc, cur, i) => {
|
|
26
|
+
const isLast = i === keys.length - 1;
|
|
27
|
+
const nextKey = keys[i + 1];
|
|
28
|
+
const curIsIndex = !Number.isNaN(Number(cur));
|
|
29
|
+
const nextIsIndex = !Number.isNaN(Number(nextKey));
|
|
30
|
+
// If current key is numeric, treat parent as an array
|
|
31
|
+
if (curIsIndex) {
|
|
32
|
+
if (!Array.isArray(acc)) return [];
|
|
33
|
+
}
|
|
34
|
+
if (isLast) {
|
|
35
|
+
acc[cur] = value;
|
|
36
|
+
} else if (acc[cur] == null) {
|
|
37
|
+
acc[cur] = nextIsIndex ? [] : {};
|
|
38
|
+
}
|
|
39
|
+
return acc[cur];
|
|
40
|
+
}, result);
|
|
41
|
+
}
|
|
42
|
+
return result;
|
|
43
|
+
}export{flatten,unflatten};//# sourceMappingURL=json.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json.js","sources":["../../src/utils/json.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA,SAAA,OAAA,CAAgB,GAAA,EAAA,cAAmB,EAAC,GAAA,GAAM,EAAE,EAAA;AAsB5C,EAAA,MAAA,CAAA,OAAA,CAAA,GAAA,CAAA,CAAA,OAAgB,CAAA,CAAA,CAAA,GAAU,EAAG,KAAE,CAAA,KAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@adminide-stack/form-builder-core",
|
|
3
|
-
"version": "5.1.4-alpha.
|
|
3
|
+
"version": "5.1.4-alpha.82",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -24,5 +24,5 @@
|
|
|
24
24
|
"publishConfig": {
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "d2f703901231f55e85e7d721754f8746cd2753cc"
|
|
28
28
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './inngest/interfaces/types';
|
|
2
2
|
export * from './inngest/generateFunctionCode';
|
|
3
|
-
export { generateFromExtractedFunctions } from './inngest/stepGenerator';
|
|
3
|
+
export { generateFromExtractedFunctions, extractFunctionBody } from './inngest/stepGenerator';
|
|
4
4
|
export * from './inngest/monacoAutocompleteIntegration';
|
|
5
|
+
export * from './utils/json';
|
|
@@ -54,6 +54,20 @@ function formatCode(code: string): string {
|
|
|
54
54
|
.replace(/\n{3,}/g, '\n\n'); // Remove excessive blank lines
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
function sanitizeDirectStepCode(code: string): string {
|
|
58
|
+
if (!code) {
|
|
59
|
+
return code;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
code
|
|
64
|
+
// Remove inline object returns (e.g. return { success: true };)
|
|
65
|
+
.replace(/\n\s*return\s*\{[\s\S]*?\};?/g, '')
|
|
66
|
+
// Remove simple expression returns (e.g. return result;)
|
|
67
|
+
.replace(/\n\s*return\s+[^\n;]+;?/g, '')
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
57
71
|
function generateStepBlock(step: InngestStep, position: number): string {
|
|
58
72
|
const stepVar = (step.code || '').match(/const\s+(\w+)\s*=/)?.[1] || `step_${position}`;
|
|
59
73
|
|
|
@@ -67,7 +81,7 @@ ${body}`;
|
|
|
67
81
|
|
|
68
82
|
const inner = extractFunctionBody(step.code || '') || 'return { success: true };';
|
|
69
83
|
return `// ${step.description || step.name || `Step ${position}`}
|
|
70
|
-
const ${stepVar}_result = await step.run('${stepVar}', async (
|
|
84
|
+
const ${stepVar}_result = await step.run('${stepVar}', async () => {
|
|
71
85
|
${indent(inner, 2)}
|
|
72
86
|
});`;
|
|
73
87
|
}
|
|
@@ -93,6 +107,89 @@ export function generateHandlerBody(def: InngestFunctionDef): string {
|
|
|
93
107
|
return `${stepBlocks}`;
|
|
94
108
|
}
|
|
95
109
|
|
|
110
|
+
function escapeSingleQuotes(value: string): string {
|
|
111
|
+
return value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function resolveStepVarName(step: InngestStep, index: number): string {
|
|
115
|
+
const directMatch = (step.code || '').match(/const\s+(\w+)\s*=/);
|
|
116
|
+
if (directMatch) {
|
|
117
|
+
return directMatch[1];
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (typeof step.id === 'string' && step.id.trim()) {
|
|
121
|
+
return step.id;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return `step_${index + 1}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function resolveResultExpression(step: InngestStep, varName: string): string {
|
|
128
|
+
switch (step.type) {
|
|
129
|
+
case 'sleep':
|
|
130
|
+
return 'sleepResult';
|
|
131
|
+
case 'sendEvent':
|
|
132
|
+
return 'eventResult';
|
|
133
|
+
case 'waitForEvent':
|
|
134
|
+
return 'waitResult';
|
|
135
|
+
default:
|
|
136
|
+
return `${varName}_result`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function sanitizeSteps(def: InngestFunctionDef): InngestFunctionDef {
|
|
141
|
+
const sanitizedSteps = def.steps.map((step) => {
|
|
142
|
+
if (step.type === 'sleep' || step.type === 'sendEvent' || step.type === 'waitForEvent') {
|
|
143
|
+
return {
|
|
144
|
+
...step,
|
|
145
|
+
code: sanitizeDirectStepCode(step.code || ''),
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
return step;
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
...def,
|
|
153
|
+
steps: sanitizedSteps,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function generateHandlerBodyWithReturn(def: InngestFunctionDef): string {
|
|
158
|
+
const sanitizedDef = sanitizeSteps(def);
|
|
159
|
+
const body = generateHandlerBody(sanitizedDef).trimEnd();
|
|
160
|
+
|
|
161
|
+
const resultLines = sanitizedDef.steps
|
|
162
|
+
.map((step, index) => {
|
|
163
|
+
const varName = resolveStepVarName(step, index);
|
|
164
|
+
const resultExpr = resolveResultExpression(step, varName);
|
|
165
|
+
|
|
166
|
+
if (!resultExpr) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return ` ${varName}: ${resultExpr},`;
|
|
171
|
+
})
|
|
172
|
+
.filter((line): line is string => Boolean(line));
|
|
173
|
+
|
|
174
|
+
const escapedId = escapeSingleQuotes(def.id || 'new-function');
|
|
175
|
+
|
|
176
|
+
const returnLines = [
|
|
177
|
+
'return {',
|
|
178
|
+
` functionId: '${escapedId}',`,
|
|
179
|
+
...resultLines,
|
|
180
|
+
' timestamp: new Date().toISOString()',
|
|
181
|
+
'};',
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
const returnBlock = returnLines.join('\n');
|
|
185
|
+
|
|
186
|
+
if (!body) {
|
|
187
|
+
return returnBlock;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return `${body}\n\n${returnBlock}`;
|
|
191
|
+
}
|
|
192
|
+
|
|
96
193
|
// Generate Inngest function from database-extracted functions
|
|
97
194
|
export function generateStepFunctionsFromDB(
|
|
98
195
|
functionId: string,
|
|
@@ -147,5 +244,5 @@ export function generateStepFromFunction(stepFunction: StepFunction, index: numb
|
|
|
147
244
|
|
|
148
245
|
return `// ${
|
|
149
246
|
stepFunction.name || `Step ${index}`
|
|
150
|
-
}\nconst ${stepVar}_result = await step.run('${stepVar}', async (
|
|
247
|
+
}\nconst ${stepVar}_result = await step.run('${stepVar}', async () => {\n${indent(cleanedBody, 2)}\n});`;
|
|
151
248
|
}
|
|
@@ -463,7 +463,7 @@ export function transformStepForExecution(
|
|
|
463
463
|
|
|
464
464
|
// For other step types, wrap in step.run()
|
|
465
465
|
return `// ${stepLabel}
|
|
466
|
-
const ${stepFunction.varName}_result = await step.run('${stepFunction.varName}', async (
|
|
466
|
+
const ${stepFunction.varName}_result = await step.run('${stepFunction.varName}', async () => {
|
|
467
467
|
${cleanedBody}
|
|
468
468
|
});`;
|
|
469
469
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export function flatten(obj: Record<string, unknown>, parentKey = '', res: Record<string, unknown> = {}) {
|
|
2
|
+
Object.entries(obj).forEach(([key, value]) => {
|
|
3
|
+
const newKey = parentKey ? `${parentKey}.${key}` : key;
|
|
4
|
+
|
|
5
|
+
if (Array.isArray(value)) {
|
|
6
|
+
value.forEach((item, index) => {
|
|
7
|
+
if (item !== null && typeof item === 'object') {
|
|
8
|
+
flatten(item, `${newKey}.${index}`, res);
|
|
9
|
+
} else {
|
|
10
|
+
res[`${newKey}.${index}`] = item;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
} else if (value !== null && typeof value === 'object') {
|
|
14
|
+
flatten(value as Record<string, unknown>, newKey, res);
|
|
15
|
+
} else {
|
|
16
|
+
res[newKey] = value;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return res;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function unflatten(obj: Record<string, unknown>) {
|
|
24
|
+
const result = {};
|
|
25
|
+
if (!obj || typeof obj !== 'object') return result;
|
|
26
|
+
for (const [flatKey, value] of Object.entries(obj)) {
|
|
27
|
+
const keys = flatKey.split('.');
|
|
28
|
+
keys.reduce((acc, cur, i) => {
|
|
29
|
+
const isLast = i === keys.length - 1;
|
|
30
|
+
const nextKey = keys[i + 1];
|
|
31
|
+
const curIsIndex = !Number.isNaN(Number(cur));
|
|
32
|
+
const nextIsIndex = !Number.isNaN(Number(nextKey));
|
|
33
|
+
|
|
34
|
+
// If current key is numeric, treat parent as an array
|
|
35
|
+
if (curIsIndex) {
|
|
36
|
+
if (!Array.isArray(acc)) return [];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (isLast) {
|
|
40
|
+
acc[cur] = value;
|
|
41
|
+
} else if (acc[cur] == null) {
|
|
42
|
+
acc[cur] = nextIsIndex ? [] : {};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return acc[cur];
|
|
46
|
+
}, result);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return result;
|
|
50
|
+
}
|