@@ -37,48 +37,53 @@ if (!String.prototype.matchAll) {
3737 } ;
3838}
3939
40+ const types = [ 'Datacondition' , 'Eventcondition' , 'Events' , 'Functions' , 'Retries' , 'Switchstate' ] ;
41+
4042interface BuilderExtension {
43+ import ?: string ;
4144 preValidate : string ;
4245}
4346/** Stores additional code that needs to be added to builders depending on their type */
4447const buildersExtensions : { [ key : string ] : BuilderExtension } = {
4548 Callbackstate : {
46- preValidate : `\r\n data.type = 'callback';` ,
47- } ,
48- Databasedswitch : {
49- preValidate : `\r\n data.type = 'switch';` ,
49+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
50+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
5051 } ,
5152 Delaystate : {
52- preValidate : `\r\n data.type = 'delay';` ,
53- } ,
54- Eventbasedswitch : {
55- preValidate : `\r\n data.type = 'switch';` ,
53+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
54+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
5655 } ,
5756 Eventstate : {
58- preValidate : `\r\n data.type = 'event';` ,
57+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
58+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
5959 } ,
6060 Foreachstate : {
61- preValidate : `\r\n data.type = 'foreach';
62- \r\n //FIXME https://github.com/serverlessworkflow/sdk-typescript/issues/95
63- \r\n data.usedForCompensation = data.usedForCompensation || false;` ,
61+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
62+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
6463 } ,
6564 Injectstate : {
66- preValidate : `\r\n data.type = 'inject';` ,
65+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
66+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
6767 } ,
6868 Operationstate : {
69- preValidate : `\r\n data.type = 'operation';` ,
69+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
70+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
7071 } ,
7172 Parallelstate : {
72- preValidate : `\r\n data.type = 'parallel';` ,
73+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
74+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
7375 } ,
7476 Subflowstate : {
75- preValidate : `\r\n data.type = 'subflow';` ,
77+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
78+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
7679 } ,
77- Function : {
78- preValidate : `\r\n data.type = data.type || 'rest';` ,
80+ Defaultdef : {
81+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
82+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
7983 } ,
80- Eventdef : {
81- preValidate : `\r\n data.kind = data.kind || 'consumed';` ,
84+ Error : {
85+ import : `import { setEndValueIfNoTransition } from '../definitions/utils';` ,
86+ preValidate : `\r\n setEndValueIfNoTransition(model);` ,
8287 } ,
8388} ;
8489
@@ -125,16 +130,21 @@ const createBuilder = async (destDir: string, dataType: string): Promise<void> =
125130 `import { Builder, builder } from '../builder';
126131import { Specification } from '../definitions';
127132import { validate } from '../utils';
133+ ${ extension ?. import ? extension . import : '' }
128134
129135/**
130136 * The internal function used by the builder proxy to validate and return its underlying object
131137 * @param {Specification.${ dataType } } data The underlying object
132138 * @returns {Specification.${ dataType } } The validated underlying object
133139 */
134140function ${ camelType } BuildingFn(data: Specification.${ dataType } ): (() => Specification.${ dataType } ) {
135- return () => {${ extension ?. preValidate ? extension . preValidate : '' }
136- validate('${ dataType } ', data);
137- return data;
141+ return () => {
142+ const model = new Specification.${ dataType } (data);
143+
144+ ${ extension ?. preValidate ? extension . preValidate : '' }
145+
146+ validate('${ dataType } ', model);
147+ return model;
138148 };
139149}
140150
@@ -159,11 +169,11 @@ export function ${camelType}Builder(): Builder<Specification.${dataType}> {
159169 * @param {string } dataType The type to create the builders index for
160170 * @returns {void }
161171 */
162- const createIndex = async ( destDir : string , types : string [ ] ) : Promise < void > => {
172+ const createIndex = async ( destDir : string , classes : string [ ] ) : Promise < void > => {
163173 try {
164174 const indexCode : string =
165175 fileHeader +
166- types . reduce ( ( acc , t ) => acc + `export * from './${ toKebabCase ( toCamelCase ( t ) ) + '-builder' } ';\n` , '' ) ;
176+ classes . reduce ( ( acc , t ) => acc + `export * from './${ toKebabCase ( toCamelCase ( t ) ) + '-builder' } ';\n` , '' ) ;
167177 const indexFile = path . resolve ( destDir , 'index.ts' ) ;
168178 await writeFile ( indexFile , indexCode ) ;
169179 return Promise . resolve ( ) ;
@@ -183,9 +193,11 @@ const generate = async (source: string, destDir: string): Promise<void> => {
183193 await reset ( destDir ) ;
184194 const extractor : RegExp = / e x p o r t \w * ( \w * ) / g;
185195 const definition : string = await readFile ( source , 'utf-8' ) ;
186- const types : string [ ] = [ ...definition . matchAll ( extractor ) ] . map ( ( [ , type ] ) => type ) ;
187- await Promise . all ( types . map ( createBuilder . bind ( null , destDir ) ) ) ;
188- createIndex ( destDir , types ) ;
196+ const classes : string [ ] = [ ...definition . matchAll ( extractor ) ]
197+ . map ( ( [ , type ] ) => type )
198+ . filter ( ( cl ) => ! types . includes ( cl ) ) ;
199+ await Promise . all ( classes . map ( createBuilder . bind ( null , destDir ) ) ) ;
200+ createIndex ( destDir , classes ) ;
189201 return Promise . resolve ( ) ;
190202 } catch ( ex ) {
191203 return Promise . reject ( ex ) ;
0 commit comments