@rainbow-o23/n3 1.0.43 → 1.0.45
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/README.md +12 -0
- package/index.cjs +75 -37
- package/index.js +75 -37
- package/lib/http/types.d.ts +4 -4
- package/lib/step/abstract-fragmentary-pipeline-step.d.ts +4 -4
- package/lib/step/parallel-step-sets.d.ts +2 -2
- package/package.json +6 -6
- package/rollup.config.base.js +1 -1
- package/src/lib/error-codes.ts +24 -1
- package/src/lib/http/fetch-step.ts +9 -9
- package/src/lib/http/types.ts +4 -4
- package/src/lib/step/abstract-fragmentary-pipeline-step.ts +24 -22
- package/src/lib/step/conditional-step-sets.ts +2 -2
- package/src/lib/step/each-step-sets.ts +1 -1
- package/src/lib/step/parallel-step-sets.ts +5 -5
- package/src/lib/step/routes-step-sets.ts +2 -2
- package/src/lib/step/utils.ts +18 -0
package/README.md
CHANGED
|
@@ -30,6 +30,18 @@ When using scripts, pay attention to the usage of variables. Typically:
|
|
|
30
30
|
- `$helpers` represents function supporting and can be used in all snippets,
|
|
31
31
|
- `$options` represents a set of data, usually in error handles.
|
|
32
32
|
|
|
33
|
+
### Typescript support
|
|
34
|
+
|
|
35
|
+
In dynamic snippet, TypeScript syntax can also be used. Currently, `o23/n3` is compiled using ES2022 syntax. It is important to note that
|
|
36
|
+
dynamic script fragments are function bodies, so `import`/`export` syntax is not supported. Moreover, they are compiled in loose mode, and
|
|
37
|
+
the compilation process does not report any errors. Additionally, for script security reasons, the following keywords or classes are also
|
|
38
|
+
not supported.
|
|
39
|
+
|
|
40
|
+
- `process`
|
|
41
|
+
- `global`
|
|
42
|
+
- `eval`
|
|
43
|
+
- `Function`
|
|
44
|
+
|
|
33
45
|
## Basic Steps
|
|
34
46
|
|
|
35
47
|
### Fragmentary
|
package/index.cjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var typeorm = require('typeorm');
|
|
4
3
|
var n1 = require('@rainbow-o23/n1');
|
|
4
|
+
var typeorm = require('typeorm');
|
|
5
|
+
var ts = require('typescript');
|
|
5
6
|
var fetch = require('node-fetch');
|
|
6
7
|
|
|
7
8
|
const ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY = 'O03-00001';
|
|
@@ -24,6 +25,26 @@ const ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = 'O03-00017';
|
|
|
24
25
|
const ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = 'O03-00018';
|
|
25
26
|
const ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = 'O03-00019';
|
|
26
27
|
const ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = 'O03-00020';
|
|
28
|
+
n1.ErrorCodes.ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY = ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY;
|
|
29
|
+
n1.ErrorCodes.ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY = ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY;
|
|
30
|
+
n1.ErrorCodes.ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND = ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND;
|
|
31
|
+
n1.ErrorCodes.ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND = ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND;
|
|
32
|
+
n1.ErrorCodes.ERR_TYPEORM_DATASOURCE_NOT_FOUND = ERR_TYPEORM_DATASOURCE_NOT_FOUND;
|
|
33
|
+
n1.ErrorCodes.ERR_TYPEORM_ENTITY_NOT_FOUND = ERR_TYPEORM_ENTITY_NOT_FOUND;
|
|
34
|
+
n1.ErrorCodes.ERR_TYPEORM_SQL_NOT_EMPTY = ERR_TYPEORM_SQL_NOT_EMPTY;
|
|
35
|
+
n1.ErrorCodes.ERR_TYPEORM_TRANSACTION_NOT_FOUND = ERR_TYPEORM_TRANSACTION_NOT_FOUND;
|
|
36
|
+
n1.ErrorCodes.ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY = ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY;
|
|
37
|
+
n1.ErrorCodes.ERR_FETCH_ERROR = ERR_FETCH_ERROR;
|
|
38
|
+
n1.ErrorCodes.ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED = ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED;
|
|
39
|
+
n1.ErrorCodes.ERR_EACH_FRAGMENT_NOT_ANY_ARRAY = ERR_EACH_FRAGMENT_NOT_ANY_ARRAY;
|
|
40
|
+
n1.ErrorCodes.ERR_PIPELINE_STEP_REF_NOT_EMPTY = ERR_PIPELINE_STEP_REF_NOT_EMPTY;
|
|
41
|
+
n1.ErrorCodes.ERR_PIPELINE_STEP_REF_NOT_FOUND = ERR_PIPELINE_STEP_REF_NOT_FOUND;
|
|
42
|
+
n1.ErrorCodes.ERR_PIPELINE_REF_NOT_EMPTY = ERR_PIPELINE_REF_NOT_EMPTY;
|
|
43
|
+
n1.ErrorCodes.ERR_PIPELINE_REF_NOT_FOUND = ERR_PIPELINE_REF_NOT_FOUND;
|
|
44
|
+
n1.ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL;
|
|
45
|
+
n1.ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS;
|
|
46
|
+
n1.ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL;
|
|
47
|
+
n1.ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION;
|
|
27
48
|
|
|
28
49
|
class AbstractTypeOrmDataSource {
|
|
29
50
|
_name;
|
|
@@ -360,6 +381,21 @@ class Utils {
|
|
|
360
381
|
return creators.createDefault();
|
|
361
382
|
}
|
|
362
383
|
else if (typeof snippet === 'string') {
|
|
384
|
+
const transpiled = ts.transpileModule(snippet, {
|
|
385
|
+
compilerOptions: {
|
|
386
|
+
target: ts.ScriptTarget.ES2022,
|
|
387
|
+
jsx: ts.JsxEmit.None,
|
|
388
|
+
strict: false,
|
|
389
|
+
noEmitOnError: true,
|
|
390
|
+
esModuleInterop: true,
|
|
391
|
+
module: ts.ModuleKind.ES2022,
|
|
392
|
+
suppressOutputPathCheck: false,
|
|
393
|
+
skipLibCheck: true,
|
|
394
|
+
skipDefaultLibCheck: true,
|
|
395
|
+
moduleResolution: ts.ModuleResolutionKind.Node16
|
|
396
|
+
}
|
|
397
|
+
});
|
|
398
|
+
snippet = transpiled.outputText;
|
|
363
399
|
const variableNames = creators.getVariableNames() ?? [];
|
|
364
400
|
if (creators.async) {
|
|
365
401
|
const func = new AsyncFunction(...variableNames, ...AvoidNames, snippet);
|
|
@@ -466,8 +502,10 @@ class AbstractFragmentaryPipelineStep extends n1.AbstractPipelineStep {
|
|
|
466
502
|
else {
|
|
467
503
|
this._mergeRequest = options.mergeRequest ?? false;
|
|
468
504
|
}
|
|
469
|
-
this._fromRequestFunc = Utils.
|
|
470
|
-
createDefault: () => ($factor, _$request, _$helpers, _$) =>
|
|
505
|
+
this._fromRequestFunc = Utils.createAsyncFunction(this.getFromRequestSnippet(), {
|
|
506
|
+
createDefault: () => async ($factor, _$request, _$helpers, _$) => {
|
|
507
|
+
return $factor;
|
|
508
|
+
},
|
|
471
509
|
getVariableNames: () => this.generateFromRequestVariableNames(),
|
|
472
510
|
error: (e) => {
|
|
473
511
|
this.getLogger().error(`Failed on create function for from request transformer, snippet is [${this.getFromRequestSnippet()}].`);
|
|
@@ -547,24 +585,24 @@ class AbstractFragmentaryPipelineStep extends n1.AbstractPipelineStep {
|
|
|
547
585
|
const funcOrSnippet = this.getToResponseSnippet();
|
|
548
586
|
if (funcOrSnippet == null || (typeof funcOrSnippet === 'string' && funcOrSnippet.trim().length === 0)) {
|
|
549
587
|
if (this.useUnboxMerging()) {
|
|
550
|
-
return ($result, $request, _$helpers, _$) => {
|
|
588
|
+
return async ($result, $request, _$helpers, _$) => {
|
|
551
589
|
return { ...$request.content, ...$result };
|
|
552
590
|
};
|
|
553
591
|
}
|
|
554
592
|
else if (this.hasMergeKey()) {
|
|
555
|
-
return ($result, $request, _$helpers, _$) => {
|
|
593
|
+
return async ($result, $request, _$helpers, _$) => {
|
|
556
594
|
return { ...$request.content, [this.getMergeKey()]: $result };
|
|
557
595
|
};
|
|
558
596
|
}
|
|
559
597
|
else {
|
|
560
|
-
return ($result, _$request, _$helpers, _$) => {
|
|
598
|
+
return async ($result, _$request, _$helpers, _$) => {
|
|
561
599
|
return $result;
|
|
562
600
|
};
|
|
563
601
|
}
|
|
564
602
|
}
|
|
565
603
|
else if (typeof funcOrSnippet === 'string') {
|
|
566
|
-
const func = Utils.
|
|
567
|
-
createDefault: () => {
|
|
604
|
+
const func = Utils.createAsyncFunction(funcOrSnippet, {
|
|
605
|
+
createDefault: async () => {
|
|
568
606
|
throw new n1.UncatchableError(ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, 'Cannot create perform func on empty snippet.');
|
|
569
607
|
},
|
|
570
608
|
getVariableNames: () => this.generateToResponseVariableNames(),
|
|
@@ -574,13 +612,13 @@ class AbstractFragmentaryPipelineStep extends n1.AbstractPipelineStep {
|
|
|
574
612
|
}
|
|
575
613
|
});
|
|
576
614
|
if (this.useUnboxMerging()) {
|
|
577
|
-
return ($result, $request, $helpers, $) => {
|
|
578
|
-
const r = func($result, $request, $helpers, $);
|
|
615
|
+
return async ($result, $request, $helpers, $) => {
|
|
616
|
+
const r = await func($result, $request, $helpers, $);
|
|
579
617
|
return { ...$request.content, ...r };
|
|
580
618
|
};
|
|
581
619
|
}
|
|
582
620
|
else if (this.hasMergeKey()) {
|
|
583
|
-
return ($result, $request, $helpers, $) => {
|
|
621
|
+
return async ($result, $request, $helpers, $) => {
|
|
584
622
|
const r = func($result, $request, $helpers, $);
|
|
585
623
|
return { ...$request.content, [this.getMergeKey()]: r };
|
|
586
624
|
};
|
|
@@ -590,13 +628,13 @@ class AbstractFragmentaryPipelineStep extends n1.AbstractPipelineStep {
|
|
|
590
628
|
}
|
|
591
629
|
}
|
|
592
630
|
else if (this.useUnboxMerging()) {
|
|
593
|
-
return ($result, $request, $helpers, $) => {
|
|
631
|
+
return async ($result, $request, $helpers, $) => {
|
|
594
632
|
const r = funcOrSnippet($result, $request, $helpers, $);
|
|
595
633
|
return { ...$request.content, ...r };
|
|
596
634
|
};
|
|
597
635
|
}
|
|
598
636
|
else if (this.hasMergeKey()) {
|
|
599
|
-
return ($result, $request, $helpers, $) => {
|
|
637
|
+
return async ($result, $request, $helpers, $) => {
|
|
600
638
|
const r = funcOrSnippet($result, $request, $helpers, $);
|
|
601
639
|
return { ...$request.content, [this.getMergeKey()]: r };
|
|
602
640
|
};
|
|
@@ -605,13 +643,13 @@ class AbstractFragmentaryPipelineStep extends n1.AbstractPipelineStep {
|
|
|
605
643
|
return funcOrSnippet;
|
|
606
644
|
}
|
|
607
645
|
}
|
|
608
|
-
getFromInput($factor, $request) {
|
|
646
|
+
async getFromInput($factor, $request) {
|
|
609
647
|
const $helpers = this.getHelpers();
|
|
610
648
|
return this._fromRequestFunc($factor, $request, $helpers, $helpers);
|
|
611
649
|
}
|
|
612
|
-
setToOutput($result, $request) {
|
|
650
|
+
async setToOutput($result, $request) {
|
|
613
651
|
const $helpers = this.getHelpers();
|
|
614
|
-
return { content: this._toResponseFunc($result, $request, $helpers, $helpers) };
|
|
652
|
+
return { content: await this._toResponseFunc($result, $request, $helpers, $helpers) };
|
|
615
653
|
}
|
|
616
654
|
buildStepOptions() {
|
|
617
655
|
return { config: this.getConfig(), logger: this.getLogger() };
|
|
@@ -697,18 +735,18 @@ class AbstractFragmentaryPipelineStep extends n1.AbstractPipelineStep {
|
|
|
697
735
|
async performAndCatch(request, perform) {
|
|
698
736
|
let fragment = null;
|
|
699
737
|
try {
|
|
700
|
-
fragment = this.getFromInput(request.content, request);
|
|
738
|
+
fragment = await this.getFromInput(request.content, request);
|
|
701
739
|
return await perform(fragment);
|
|
702
740
|
}
|
|
703
741
|
catch (e) {
|
|
704
742
|
const result = await this.handleError(fragment, request, e);
|
|
705
|
-
return this.setToOutput(result, request);
|
|
743
|
+
return await this.setToOutput(result, request);
|
|
706
744
|
}
|
|
707
745
|
}
|
|
708
746
|
async perform(request) {
|
|
709
747
|
return await this.performAndCatch(request, async (fragment) => {
|
|
710
748
|
const result = await this.doPerform(fragment, request);
|
|
711
|
-
return this.setToOutput(result, request);
|
|
749
|
+
return await this.setToOutput(result, request);
|
|
712
750
|
});
|
|
713
751
|
}
|
|
714
752
|
}
|
|
@@ -781,8 +819,8 @@ class ParallelPipelineStepSets extends PipelineStepSets {
|
|
|
781
819
|
constructor(options) {
|
|
782
820
|
super(options);
|
|
783
821
|
this._cloneDataSnippet = options.cloneData;
|
|
784
|
-
this._cloneData = Utils.
|
|
785
|
-
createDefault: () => ($factor, _$request, _$helpers, _$) => {
|
|
822
|
+
this._cloneData = Utils.createAsyncFunction(this.getCloneDataSnippet(), {
|
|
823
|
+
createDefault: () => async ($factor, _$request, _$helpers, _$) => {
|
|
786
824
|
return $factor;
|
|
787
825
|
},
|
|
788
826
|
getVariableNames: () => ['$factor', '$request', ...this.getHelpersVariableNames()],
|
|
@@ -799,7 +837,7 @@ class ParallelPipelineStepSets extends PipelineStepSets {
|
|
|
799
837
|
raceOne() {
|
|
800
838
|
return this._race;
|
|
801
839
|
}
|
|
802
|
-
cloneDataForEach($factor, $request) {
|
|
840
|
+
async cloneDataForEach($factor, $request) {
|
|
803
841
|
if ($factor == null) {
|
|
804
842
|
return null;
|
|
805
843
|
}
|
|
@@ -814,7 +852,7 @@ class ParallelPipelineStepSets extends PipelineStepSets {
|
|
|
814
852
|
return steps.map(async (step) => {
|
|
815
853
|
return await this.measurePerformance(traceId, 'STEP', step.constructor.name)
|
|
816
854
|
.execute(async () => {
|
|
817
|
-
const eachData = this.cloneDataForEach(data, request);
|
|
855
|
+
const eachData = await this.cloneDataForEach(data, request);
|
|
818
856
|
const eachRequest = { content: eachData, $context: { ...context, traceId } };
|
|
819
857
|
this.traceStepIn(traceId, step, request);
|
|
820
858
|
const response = await step.perform(eachRequest);
|
|
@@ -882,7 +920,7 @@ class EachPipelineStepSets extends PipelineStepSets {
|
|
|
882
920
|
}
|
|
883
921
|
results.push(result);
|
|
884
922
|
}
|
|
885
|
-
return this.setToOutput(results, request);
|
|
923
|
+
return await this.setToOutput(results, request);
|
|
886
924
|
});
|
|
887
925
|
}
|
|
888
926
|
}
|
|
@@ -924,7 +962,7 @@ class ConditionalPipelineStepSets extends PipelineStepSets {
|
|
|
924
962
|
const checked = await this.check(fragment, request);
|
|
925
963
|
if (checked) {
|
|
926
964
|
const result = await this.doPerform(request.content, request);
|
|
927
|
-
return this.setToOutput(result, request);
|
|
965
|
+
return await this.setToOutput(result, request);
|
|
928
966
|
}
|
|
929
967
|
const otherwiseStepBuilders = this.getOtherwiseStepBuilders();
|
|
930
968
|
if (otherwiseStepBuilders != null) {
|
|
@@ -932,7 +970,7 @@ class ConditionalPipelineStepSets extends PipelineStepSets {
|
|
|
932
970
|
...this.buildStepOptions(), name: this.getName(), steps: otherwiseStepBuilders
|
|
933
971
|
});
|
|
934
972
|
const result = await sets.perform(request);
|
|
935
|
-
return this.setToOutput(result.content, request);
|
|
973
|
+
return await this.setToOutput(result.content, request);
|
|
936
974
|
}
|
|
937
975
|
else {
|
|
938
976
|
return request;
|
|
@@ -1000,7 +1038,7 @@ class RoutesPipelineStepSets extends AbstractFragmentaryPipelineStep {
|
|
|
1000
1038
|
...this.buildStepOptions(), name: this.getName(), steps
|
|
1001
1039
|
});
|
|
1002
1040
|
const result = await sets.perform(request);
|
|
1003
|
-
return this.setToOutput(result.content, request);
|
|
1041
|
+
return await this.setToOutput(result.content, request);
|
|
1004
1042
|
}
|
|
1005
1043
|
}
|
|
1006
1044
|
}
|
|
@@ -1010,7 +1048,7 @@ class RoutesPipelineStepSets extends AbstractFragmentaryPipelineStep {
|
|
|
1010
1048
|
...this.buildStepOptions(), name: this.getName(), steps: otherwiseStepBuilders
|
|
1011
1049
|
});
|
|
1012
1050
|
const result = await sets.perform(request);
|
|
1013
|
-
return this.setToOutput(result.content, request);
|
|
1051
|
+
return await this.setToOutput(result.content, request);
|
|
1014
1052
|
}
|
|
1015
1053
|
else {
|
|
1016
1054
|
return request;
|
|
@@ -1257,8 +1295,8 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1257
1295
|
?? config.getNumber(`endpoints.${this.getEndpointSystemCode()}.global.timeout`, -1);
|
|
1258
1296
|
this._endpointTimeout = this._endpointTimeout > 0 ? this._endpointTimeout * 1000 : -1;
|
|
1259
1297
|
this._urlGenerateSnippet = options.urlGenerate;
|
|
1260
|
-
this._urlGenerateFunc = Utils.
|
|
1261
|
-
createDefault: () => ($endpointUrl, _$factor, _$request, _$helpers, _$) => $endpointUrl,
|
|
1298
|
+
this._urlGenerateFunc = Utils.createAsyncFunction(this.getUrlGenerateSnippet(), {
|
|
1299
|
+
createDefault: () => async ($endpointUrl, _$factor, _$request, _$helpers, _$) => $endpointUrl,
|
|
1262
1300
|
getVariableNames: () => this.getUrlGenerateVariableName(),
|
|
1263
1301
|
error: (e) => {
|
|
1264
1302
|
this.getLogger().error(`Failed on create function for url generate, snippet is [${this.getUrlGenerateSnippet()}].`);
|
|
@@ -1266,8 +1304,8 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1266
1304
|
}
|
|
1267
1305
|
});
|
|
1268
1306
|
this._headersGenerateSnippet = options.headersGenerate;
|
|
1269
|
-
this._headersGenerateFunc = Utils.
|
|
1270
|
-
createDefault: () => (_$factor, _$request, _$helpers, _$) => (void 0),
|
|
1307
|
+
this._headersGenerateFunc = Utils.createAsyncFunction(this.getHeadersGenerateSnippet(), {
|
|
1308
|
+
createDefault: () => async (_$factor, _$request, _$helpers, _$) => (void 0),
|
|
1271
1309
|
getVariableNames: () => this.getHeadersGenerateVariableNames(),
|
|
1272
1310
|
error: (e) => {
|
|
1273
1311
|
this.getLogger().error(`Failed on create function for request headers generate, snippet is [${this.getHeadersGenerateSnippet()}].`);
|
|
@@ -1276,8 +1314,8 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1276
1314
|
});
|
|
1277
1315
|
this._bodyUsed = options.bodyUsed;
|
|
1278
1316
|
this._bodyGenerateSnippet = options.bodyGenerate;
|
|
1279
|
-
this._bodyGenerateFunc = Utils.
|
|
1280
|
-
createDefault: () => ($factor, _$request, _$helpers, _$) => {
|
|
1317
|
+
this._bodyGenerateFunc = Utils.createAsyncFunction(this.getBodyGenerateSnippet(), {
|
|
1318
|
+
createDefault: () => async ($factor, _$request, _$helpers, _$) => {
|
|
1281
1319
|
return ($factor == null || (typeof $factor === 'string' && $factor.length === 0)) ? (void 0) : JSON.stringify($factor);
|
|
1282
1320
|
},
|
|
1283
1321
|
getVariableNames: () => this.getBodyGenerateVariableNames(),
|
|
@@ -1409,14 +1447,14 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1409
1447
|
const $helpers = this.getHelpers();
|
|
1410
1448
|
let url = '';
|
|
1411
1449
|
try {
|
|
1412
|
-
url = this._urlGenerateFunc(this.getEndpointUrl(), data, request, $helpers, $helpers);
|
|
1450
|
+
url = await this._urlGenerateFunc(this.getEndpointUrl(), data, request, $helpers, $helpers);
|
|
1413
1451
|
const method = this.getEndpointMethod();
|
|
1414
1452
|
const staticHeaders = this.getEndpointHeaders() ?? {};
|
|
1415
|
-
const headers = this._headersGenerateFunc(data, request, $helpers, $helpers) ?? {};
|
|
1453
|
+
const headers = await this._headersGenerateFunc(data, request, $helpers, $helpers) ?? {};
|
|
1416
1454
|
let body;
|
|
1417
1455
|
const bodyUsed = this.isBodyUsed();
|
|
1418
1456
|
if (bodyUsed === true || (bodyUsed == null && method !== 'get')) {
|
|
1419
|
-
body = this._bodyGenerateFunc(data, request, $helpers, $helpers);
|
|
1457
|
+
body = await this._bodyGenerateFunc(data, request, $helpers, $helpers);
|
|
1420
1458
|
}
|
|
1421
1459
|
else {
|
|
1422
1460
|
body = (void 0);
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { ErrorCodes, UncatchableError, AbstractPipelineStep, CatchableError, ExposedUncatchableError, ERR_UNKNOWN, PipelineRepository, StepHelpersUtils } from '@rainbow-o23/n1';
|
|
1
2
|
import { DataSource } from 'typeorm';
|
|
2
|
-
import
|
|
3
|
+
import ts, { ScriptTarget, JsxEmit, ModuleKind, ModuleResolutionKind } from 'typescript';
|
|
3
4
|
import fetch from 'node-fetch';
|
|
4
5
|
|
|
5
6
|
const ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY = 'O03-00001';
|
|
@@ -22,6 +23,26 @@ const ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = 'O03-00017';
|
|
|
22
23
|
const ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = 'O03-00018';
|
|
23
24
|
const ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = 'O03-00019';
|
|
24
25
|
const ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = 'O03-00020';
|
|
26
|
+
ErrorCodes.ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY = ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY;
|
|
27
|
+
ErrorCodes.ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY = ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY;
|
|
28
|
+
ErrorCodes.ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND = ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND;
|
|
29
|
+
ErrorCodes.ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND = ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND;
|
|
30
|
+
ErrorCodes.ERR_TYPEORM_DATASOURCE_NOT_FOUND = ERR_TYPEORM_DATASOURCE_NOT_FOUND;
|
|
31
|
+
ErrorCodes.ERR_TYPEORM_ENTITY_NOT_FOUND = ERR_TYPEORM_ENTITY_NOT_FOUND;
|
|
32
|
+
ErrorCodes.ERR_TYPEORM_SQL_NOT_EMPTY = ERR_TYPEORM_SQL_NOT_EMPTY;
|
|
33
|
+
ErrorCodes.ERR_TYPEORM_TRANSACTION_NOT_FOUND = ERR_TYPEORM_TRANSACTION_NOT_FOUND;
|
|
34
|
+
ErrorCodes.ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY = ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY;
|
|
35
|
+
ErrorCodes.ERR_FETCH_ERROR = ERR_FETCH_ERROR;
|
|
36
|
+
ErrorCodes.ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED = ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED;
|
|
37
|
+
ErrorCodes.ERR_EACH_FRAGMENT_NOT_ANY_ARRAY = ERR_EACH_FRAGMENT_NOT_ANY_ARRAY;
|
|
38
|
+
ErrorCodes.ERR_PIPELINE_STEP_REF_NOT_EMPTY = ERR_PIPELINE_STEP_REF_NOT_EMPTY;
|
|
39
|
+
ErrorCodes.ERR_PIPELINE_STEP_REF_NOT_FOUND = ERR_PIPELINE_STEP_REF_NOT_FOUND;
|
|
40
|
+
ErrorCodes.ERR_PIPELINE_REF_NOT_EMPTY = ERR_PIPELINE_REF_NOT_EMPTY;
|
|
41
|
+
ErrorCodes.ERR_PIPELINE_REF_NOT_FOUND = ERR_PIPELINE_REF_NOT_FOUND;
|
|
42
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL;
|
|
43
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS;
|
|
44
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL;
|
|
45
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION;
|
|
25
46
|
|
|
26
47
|
class AbstractTypeOrmDataSource {
|
|
27
48
|
_name;
|
|
@@ -358,6 +379,21 @@ class Utils {
|
|
|
358
379
|
return creators.createDefault();
|
|
359
380
|
}
|
|
360
381
|
else if (typeof snippet === 'string') {
|
|
382
|
+
const transpiled = ts.transpileModule(snippet, {
|
|
383
|
+
compilerOptions: {
|
|
384
|
+
target: ScriptTarget.ES2022,
|
|
385
|
+
jsx: JsxEmit.None,
|
|
386
|
+
strict: false,
|
|
387
|
+
noEmitOnError: true,
|
|
388
|
+
esModuleInterop: true,
|
|
389
|
+
module: ModuleKind.ES2022,
|
|
390
|
+
suppressOutputPathCheck: false,
|
|
391
|
+
skipLibCheck: true,
|
|
392
|
+
skipDefaultLibCheck: true,
|
|
393
|
+
moduleResolution: ModuleResolutionKind.Node16
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
snippet = transpiled.outputText;
|
|
361
397
|
const variableNames = creators.getVariableNames() ?? [];
|
|
362
398
|
if (creators.async) {
|
|
363
399
|
const func = new AsyncFunction(...variableNames, ...AvoidNames, snippet);
|
|
@@ -464,8 +500,10 @@ class AbstractFragmentaryPipelineStep extends AbstractPipelineStep {
|
|
|
464
500
|
else {
|
|
465
501
|
this._mergeRequest = options.mergeRequest ?? false;
|
|
466
502
|
}
|
|
467
|
-
this._fromRequestFunc = Utils.
|
|
468
|
-
createDefault: () => ($factor, _$request, _$helpers, _$) =>
|
|
503
|
+
this._fromRequestFunc = Utils.createAsyncFunction(this.getFromRequestSnippet(), {
|
|
504
|
+
createDefault: () => async ($factor, _$request, _$helpers, _$) => {
|
|
505
|
+
return $factor;
|
|
506
|
+
},
|
|
469
507
|
getVariableNames: () => this.generateFromRequestVariableNames(),
|
|
470
508
|
error: (e) => {
|
|
471
509
|
this.getLogger().error(`Failed on create function for from request transformer, snippet is [${this.getFromRequestSnippet()}].`);
|
|
@@ -545,24 +583,24 @@ class AbstractFragmentaryPipelineStep extends AbstractPipelineStep {
|
|
|
545
583
|
const funcOrSnippet = this.getToResponseSnippet();
|
|
546
584
|
if (funcOrSnippet == null || (typeof funcOrSnippet === 'string' && funcOrSnippet.trim().length === 0)) {
|
|
547
585
|
if (this.useUnboxMerging()) {
|
|
548
|
-
return ($result, $request, _$helpers, _$) => {
|
|
586
|
+
return async ($result, $request, _$helpers, _$) => {
|
|
549
587
|
return { ...$request.content, ...$result };
|
|
550
588
|
};
|
|
551
589
|
}
|
|
552
590
|
else if (this.hasMergeKey()) {
|
|
553
|
-
return ($result, $request, _$helpers, _$) => {
|
|
591
|
+
return async ($result, $request, _$helpers, _$) => {
|
|
554
592
|
return { ...$request.content, [this.getMergeKey()]: $result };
|
|
555
593
|
};
|
|
556
594
|
}
|
|
557
595
|
else {
|
|
558
|
-
return ($result, _$request, _$helpers, _$) => {
|
|
596
|
+
return async ($result, _$request, _$helpers, _$) => {
|
|
559
597
|
return $result;
|
|
560
598
|
};
|
|
561
599
|
}
|
|
562
600
|
}
|
|
563
601
|
else if (typeof funcOrSnippet === 'string') {
|
|
564
|
-
const func = Utils.
|
|
565
|
-
createDefault: () => {
|
|
602
|
+
const func = Utils.createAsyncFunction(funcOrSnippet, {
|
|
603
|
+
createDefault: async () => {
|
|
566
604
|
throw new UncatchableError(ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, 'Cannot create perform func on empty snippet.');
|
|
567
605
|
},
|
|
568
606
|
getVariableNames: () => this.generateToResponseVariableNames(),
|
|
@@ -572,13 +610,13 @@ class AbstractFragmentaryPipelineStep extends AbstractPipelineStep {
|
|
|
572
610
|
}
|
|
573
611
|
});
|
|
574
612
|
if (this.useUnboxMerging()) {
|
|
575
|
-
return ($result, $request, $helpers, $) => {
|
|
576
|
-
const r = func($result, $request, $helpers, $);
|
|
613
|
+
return async ($result, $request, $helpers, $) => {
|
|
614
|
+
const r = await func($result, $request, $helpers, $);
|
|
577
615
|
return { ...$request.content, ...r };
|
|
578
616
|
};
|
|
579
617
|
}
|
|
580
618
|
else if (this.hasMergeKey()) {
|
|
581
|
-
return ($result, $request, $helpers, $) => {
|
|
619
|
+
return async ($result, $request, $helpers, $) => {
|
|
582
620
|
const r = func($result, $request, $helpers, $);
|
|
583
621
|
return { ...$request.content, [this.getMergeKey()]: r };
|
|
584
622
|
};
|
|
@@ -588,13 +626,13 @@ class AbstractFragmentaryPipelineStep extends AbstractPipelineStep {
|
|
|
588
626
|
}
|
|
589
627
|
}
|
|
590
628
|
else if (this.useUnboxMerging()) {
|
|
591
|
-
return ($result, $request, $helpers, $) => {
|
|
629
|
+
return async ($result, $request, $helpers, $) => {
|
|
592
630
|
const r = funcOrSnippet($result, $request, $helpers, $);
|
|
593
631
|
return { ...$request.content, ...r };
|
|
594
632
|
};
|
|
595
633
|
}
|
|
596
634
|
else if (this.hasMergeKey()) {
|
|
597
|
-
return ($result, $request, $helpers, $) => {
|
|
635
|
+
return async ($result, $request, $helpers, $) => {
|
|
598
636
|
const r = funcOrSnippet($result, $request, $helpers, $);
|
|
599
637
|
return { ...$request.content, [this.getMergeKey()]: r };
|
|
600
638
|
};
|
|
@@ -603,13 +641,13 @@ class AbstractFragmentaryPipelineStep extends AbstractPipelineStep {
|
|
|
603
641
|
return funcOrSnippet;
|
|
604
642
|
}
|
|
605
643
|
}
|
|
606
|
-
getFromInput($factor, $request) {
|
|
644
|
+
async getFromInput($factor, $request) {
|
|
607
645
|
const $helpers = this.getHelpers();
|
|
608
646
|
return this._fromRequestFunc($factor, $request, $helpers, $helpers);
|
|
609
647
|
}
|
|
610
|
-
setToOutput($result, $request) {
|
|
648
|
+
async setToOutput($result, $request) {
|
|
611
649
|
const $helpers = this.getHelpers();
|
|
612
|
-
return { content: this._toResponseFunc($result, $request, $helpers, $helpers) };
|
|
650
|
+
return { content: await this._toResponseFunc($result, $request, $helpers, $helpers) };
|
|
613
651
|
}
|
|
614
652
|
buildStepOptions() {
|
|
615
653
|
return { config: this.getConfig(), logger: this.getLogger() };
|
|
@@ -695,18 +733,18 @@ class AbstractFragmentaryPipelineStep extends AbstractPipelineStep {
|
|
|
695
733
|
async performAndCatch(request, perform) {
|
|
696
734
|
let fragment = null;
|
|
697
735
|
try {
|
|
698
|
-
fragment = this.getFromInput(request.content, request);
|
|
736
|
+
fragment = await this.getFromInput(request.content, request);
|
|
699
737
|
return await perform(fragment);
|
|
700
738
|
}
|
|
701
739
|
catch (e) {
|
|
702
740
|
const result = await this.handleError(fragment, request, e);
|
|
703
|
-
return this.setToOutput(result, request);
|
|
741
|
+
return await this.setToOutput(result, request);
|
|
704
742
|
}
|
|
705
743
|
}
|
|
706
744
|
async perform(request) {
|
|
707
745
|
return await this.performAndCatch(request, async (fragment) => {
|
|
708
746
|
const result = await this.doPerform(fragment, request);
|
|
709
|
-
return this.setToOutput(result, request);
|
|
747
|
+
return await this.setToOutput(result, request);
|
|
710
748
|
});
|
|
711
749
|
}
|
|
712
750
|
}
|
|
@@ -779,8 +817,8 @@ class ParallelPipelineStepSets extends PipelineStepSets {
|
|
|
779
817
|
constructor(options) {
|
|
780
818
|
super(options);
|
|
781
819
|
this._cloneDataSnippet = options.cloneData;
|
|
782
|
-
this._cloneData = Utils.
|
|
783
|
-
createDefault: () => ($factor, _$request, _$helpers, _$) => {
|
|
820
|
+
this._cloneData = Utils.createAsyncFunction(this.getCloneDataSnippet(), {
|
|
821
|
+
createDefault: () => async ($factor, _$request, _$helpers, _$) => {
|
|
784
822
|
return $factor;
|
|
785
823
|
},
|
|
786
824
|
getVariableNames: () => ['$factor', '$request', ...this.getHelpersVariableNames()],
|
|
@@ -797,7 +835,7 @@ class ParallelPipelineStepSets extends PipelineStepSets {
|
|
|
797
835
|
raceOne() {
|
|
798
836
|
return this._race;
|
|
799
837
|
}
|
|
800
|
-
cloneDataForEach($factor, $request) {
|
|
838
|
+
async cloneDataForEach($factor, $request) {
|
|
801
839
|
if ($factor == null) {
|
|
802
840
|
return null;
|
|
803
841
|
}
|
|
@@ -812,7 +850,7 @@ class ParallelPipelineStepSets extends PipelineStepSets {
|
|
|
812
850
|
return steps.map(async (step) => {
|
|
813
851
|
return await this.measurePerformance(traceId, 'STEP', step.constructor.name)
|
|
814
852
|
.execute(async () => {
|
|
815
|
-
const eachData = this.cloneDataForEach(data, request);
|
|
853
|
+
const eachData = await this.cloneDataForEach(data, request);
|
|
816
854
|
const eachRequest = { content: eachData, $context: { ...context, traceId } };
|
|
817
855
|
this.traceStepIn(traceId, step, request);
|
|
818
856
|
const response = await step.perform(eachRequest);
|
|
@@ -880,7 +918,7 @@ class EachPipelineStepSets extends PipelineStepSets {
|
|
|
880
918
|
}
|
|
881
919
|
results.push(result);
|
|
882
920
|
}
|
|
883
|
-
return this.setToOutput(results, request);
|
|
921
|
+
return await this.setToOutput(results, request);
|
|
884
922
|
});
|
|
885
923
|
}
|
|
886
924
|
}
|
|
@@ -922,7 +960,7 @@ class ConditionalPipelineStepSets extends PipelineStepSets {
|
|
|
922
960
|
const checked = await this.check(fragment, request);
|
|
923
961
|
if (checked) {
|
|
924
962
|
const result = await this.doPerform(request.content, request);
|
|
925
|
-
return this.setToOutput(result, request);
|
|
963
|
+
return await this.setToOutput(result, request);
|
|
926
964
|
}
|
|
927
965
|
const otherwiseStepBuilders = this.getOtherwiseStepBuilders();
|
|
928
966
|
if (otherwiseStepBuilders != null) {
|
|
@@ -930,7 +968,7 @@ class ConditionalPipelineStepSets extends PipelineStepSets {
|
|
|
930
968
|
...this.buildStepOptions(), name: this.getName(), steps: otherwiseStepBuilders
|
|
931
969
|
});
|
|
932
970
|
const result = await sets.perform(request);
|
|
933
|
-
return this.setToOutput(result.content, request);
|
|
971
|
+
return await this.setToOutput(result.content, request);
|
|
934
972
|
}
|
|
935
973
|
else {
|
|
936
974
|
return request;
|
|
@@ -998,7 +1036,7 @@ class RoutesPipelineStepSets extends AbstractFragmentaryPipelineStep {
|
|
|
998
1036
|
...this.buildStepOptions(), name: this.getName(), steps
|
|
999
1037
|
});
|
|
1000
1038
|
const result = await sets.perform(request);
|
|
1001
|
-
return this.setToOutput(result.content, request);
|
|
1039
|
+
return await this.setToOutput(result.content, request);
|
|
1002
1040
|
}
|
|
1003
1041
|
}
|
|
1004
1042
|
}
|
|
@@ -1008,7 +1046,7 @@ class RoutesPipelineStepSets extends AbstractFragmentaryPipelineStep {
|
|
|
1008
1046
|
...this.buildStepOptions(), name: this.getName(), steps: otherwiseStepBuilders
|
|
1009
1047
|
});
|
|
1010
1048
|
const result = await sets.perform(request);
|
|
1011
|
-
return this.setToOutput(result.content, request);
|
|
1049
|
+
return await this.setToOutput(result.content, request);
|
|
1012
1050
|
}
|
|
1013
1051
|
else {
|
|
1014
1052
|
return request;
|
|
@@ -1255,8 +1293,8 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1255
1293
|
?? config.getNumber(`endpoints.${this.getEndpointSystemCode()}.global.timeout`, -1);
|
|
1256
1294
|
this._endpointTimeout = this._endpointTimeout > 0 ? this._endpointTimeout * 1000 : -1;
|
|
1257
1295
|
this._urlGenerateSnippet = options.urlGenerate;
|
|
1258
|
-
this._urlGenerateFunc = Utils.
|
|
1259
|
-
createDefault: () => ($endpointUrl, _$factor, _$request, _$helpers, _$) => $endpointUrl,
|
|
1296
|
+
this._urlGenerateFunc = Utils.createAsyncFunction(this.getUrlGenerateSnippet(), {
|
|
1297
|
+
createDefault: () => async ($endpointUrl, _$factor, _$request, _$helpers, _$) => $endpointUrl,
|
|
1260
1298
|
getVariableNames: () => this.getUrlGenerateVariableName(),
|
|
1261
1299
|
error: (e) => {
|
|
1262
1300
|
this.getLogger().error(`Failed on create function for url generate, snippet is [${this.getUrlGenerateSnippet()}].`);
|
|
@@ -1264,8 +1302,8 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1264
1302
|
}
|
|
1265
1303
|
});
|
|
1266
1304
|
this._headersGenerateSnippet = options.headersGenerate;
|
|
1267
|
-
this._headersGenerateFunc = Utils.
|
|
1268
|
-
createDefault: () => (_$factor, _$request, _$helpers, _$) => (void 0),
|
|
1305
|
+
this._headersGenerateFunc = Utils.createAsyncFunction(this.getHeadersGenerateSnippet(), {
|
|
1306
|
+
createDefault: () => async (_$factor, _$request, _$helpers, _$) => (void 0),
|
|
1269
1307
|
getVariableNames: () => this.getHeadersGenerateVariableNames(),
|
|
1270
1308
|
error: (e) => {
|
|
1271
1309
|
this.getLogger().error(`Failed on create function for request headers generate, snippet is [${this.getHeadersGenerateSnippet()}].`);
|
|
@@ -1274,8 +1312,8 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1274
1312
|
});
|
|
1275
1313
|
this._bodyUsed = options.bodyUsed;
|
|
1276
1314
|
this._bodyGenerateSnippet = options.bodyGenerate;
|
|
1277
|
-
this._bodyGenerateFunc = Utils.
|
|
1278
|
-
createDefault: () => ($factor, _$request, _$helpers, _$) => {
|
|
1315
|
+
this._bodyGenerateFunc = Utils.createAsyncFunction(this.getBodyGenerateSnippet(), {
|
|
1316
|
+
createDefault: () => async ($factor, _$request, _$helpers, _$) => {
|
|
1279
1317
|
return ($factor == null || (typeof $factor === 'string' && $factor.length === 0)) ? (void 0) : JSON.stringify($factor);
|
|
1280
1318
|
},
|
|
1281
1319
|
getVariableNames: () => this.getBodyGenerateVariableNames(),
|
|
@@ -1407,14 +1445,14 @@ class FetchPipelineStep extends AbstractFragmentaryPipelineStep {
|
|
|
1407
1445
|
const $helpers = this.getHelpers();
|
|
1408
1446
|
let url = '';
|
|
1409
1447
|
try {
|
|
1410
|
-
url = this._urlGenerateFunc(this.getEndpointUrl(), data, request, $helpers, $helpers);
|
|
1448
|
+
url = await this._urlGenerateFunc(this.getEndpointUrl(), data, request, $helpers, $helpers);
|
|
1411
1449
|
const method = this.getEndpointMethod();
|
|
1412
1450
|
const staticHeaders = this.getEndpointHeaders() ?? {};
|
|
1413
|
-
const headers = this._headersGenerateFunc(data, request, $helpers, $helpers) ?? {};
|
|
1451
|
+
const headers = await this._headersGenerateFunc(data, request, $helpers, $helpers) ?? {};
|
|
1414
1452
|
let body;
|
|
1415
1453
|
const bodyUsed = this.isBodyUsed();
|
|
1416
1454
|
if (bodyUsed === true || (bodyUsed == null && method !== 'get')) {
|
|
1417
|
-
body = this._bodyGenerateFunc(data, request, $helpers, $helpers);
|
|
1455
|
+
body = await this._bodyGenerateFunc(data, request, $helpers, $helpers);
|
|
1418
1456
|
}
|
|
1419
1457
|
else {
|
|
1420
1458
|
body = (void 0);
|
package/lib/http/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { PipelineStepData, PipelineStepHelpers, Undefinable } from '@rainbow-o23/n1';
|
|
2
2
|
import { Response } from 'node-fetch';
|
|
3
|
-
export type HttpGenerateUrl<In, InFragment> = ($endpointUrl: string, $factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => string
|
|
4
|
-
export type HttpGenerateHeaders<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Undefinable<Record<string, string
|
|
5
|
-
export type HttpGenerateBody<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => any
|
|
3
|
+
export type HttpGenerateUrl<In, InFragment> = ($endpointUrl: string, $factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<string>;
|
|
4
|
+
export type HttpGenerateHeaders<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<Undefinable<Record<string, string>>>;
|
|
5
|
+
export type HttpGenerateBody<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<any>;
|
|
6
6
|
export type HttpGenerateResponse<In, InFragment> = ($response: Response, $factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<any>;
|
|
7
7
|
export type HttpUnpredictedErrorCode = `0${number}`;
|
|
8
8
|
export declare const HttpUnknownErrorCode: HttpUnpredictedErrorCode;
|
|
@@ -18,4 +18,4 @@ export interface HttpErrorHandleOptions<In, InFragment> {
|
|
|
18
18
|
$factor: InFragment;
|
|
19
19
|
$request: PipelineStepData<In>;
|
|
20
20
|
}
|
|
21
|
-
export type HttpHandleError<In, InFragment, OutFragment> = (options: HttpErrorHandleOptions<In, InFragment>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<OutFragment> | never;
|
|
21
|
+
export type HttpHandleError<In, InFragment, OutFragment> = ($options: HttpErrorHandleOptions<In, InFragment>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<OutFragment> | never;
|
|
@@ -12,8 +12,8 @@ export interface FragmentaryPipelineStepOptions<In = PipelineStepPayload, Out =
|
|
|
12
12
|
any?: ScriptFuncOrBody<HandleAnyError<In, InFragment, OutFragment>> | Array<PipelineStepBuilder>;
|
|
13
13
|
};
|
|
14
14
|
}
|
|
15
|
-
export type GetInFragmentFromRequestFunc<In, InFragment> = ($factor: In, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => InFragment
|
|
16
|
-
export type SetOutFragmentToResponseFunc<In, Out, OutFragment> = ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Out
|
|
15
|
+
export type GetInFragmentFromRequestFunc<In, InFragment> = ($factor: In, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<InFragment>;
|
|
16
|
+
export type SetOutFragmentToResponseFunc<In, Out, OutFragment> = ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<Out>;
|
|
17
17
|
export declare abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out> extends AbstractPipelineStep<In, Out> {
|
|
18
18
|
private readonly _fromRequestSnippet;
|
|
19
19
|
private readonly _toResponseSnippet;
|
|
@@ -32,8 +32,8 @@ export declare abstract class AbstractFragmentaryPipelineStep<In = PipelineStepP
|
|
|
32
32
|
protected generateFromRequestVariableNames(): Array<string>;
|
|
33
33
|
protected generateToResponseVariableNames(): Array<string>;
|
|
34
34
|
protected createToResponseFunc(): SetOutFragmentToResponseFunc<In, Out, OutFragment>;
|
|
35
|
-
protected getFromInput($factor: In, $request: PipelineStepData<In>): InFragment
|
|
36
|
-
protected setToOutput($result: OutFragment, $request: PipelineStepData<In>): PipelineStepData<Out
|
|
35
|
+
protected getFromInput($factor: In, $request: PipelineStepData<In>): Promise<InFragment>;
|
|
36
|
+
protected setToOutput($result: OutFragment, $request: PipelineStepData<In>): Promise<PipelineStepData<Out>>;
|
|
37
37
|
protected abstract doPerform(data: InFragment, request: PipelineStepData<In>): Promise<OutFragment>;
|
|
38
38
|
protected buildStepOptions(): Pick<PipelineStepOptions, 'config' | 'logger'>;
|
|
39
39
|
protected createErrorHandleContext(request: PipelineStepData<In>): PipelineStepSetsContext;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { PipelineStepData, PipelineStepHelpers, PipelineStepPayload } from '@rainbow-o23/n1';
|
|
2
2
|
import { PipelineStepSets, PipelineStepSetsOptions } from './step-sets';
|
|
3
3
|
import { ScriptFuncOrBody } from './types';
|
|
4
|
-
export type CloneDataFunc<In, InFragment, EachInFragment = InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => EachInFragment
|
|
4
|
+
export type CloneDataFunc<In, InFragment, EachInFragment = InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<EachInFragment>;
|
|
5
5
|
export interface ParallelPipelineStepSetsOptions<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out, EachInFragment = InFragment> extends PipelineStepSetsOptions<In, Out, InFragment, OutFragment> {
|
|
6
6
|
cloneData?: ScriptFuncOrBody<CloneDataFunc<In, InFragment, EachInFragment>>;
|
|
7
7
|
race?: boolean;
|
|
@@ -13,6 +13,6 @@ export declare class ParallelPipelineStepSets<In = PipelineStepPayload, Out = Pi
|
|
|
13
13
|
constructor(options: ParallelPipelineStepSetsOptions<In, Out, InFragment, OutFragment, EachInFragment>);
|
|
14
14
|
getCloneDataSnippet(): ScriptFuncOrBody<CloneDataFunc<In, InFragment, EachInFragment>>;
|
|
15
15
|
raceOne(): boolean;
|
|
16
|
-
protected cloneDataForEach($factor: InFragment, $request: PipelineStepData<In>): EachInFragment
|
|
16
|
+
protected cloneDataForEach($factor: InFragment, $request: PipelineStepData<In>): Promise<EachInFragment>;
|
|
17
17
|
protected doPerform(data: InFragment, request: PipelineStepData<In>): Promise<OutFragment>;
|
|
18
18
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rainbow-o23/n3",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.45",
|
|
4
4
|
"description": "o23 pipelines",
|
|
5
5
|
"main": "index.cjs",
|
|
6
6
|
"module": "index.js",
|
|
@@ -21,9 +21,10 @@
|
|
|
21
21
|
"url": "https://github.com/InsureMO/rainbow-o23/issues"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@rainbow-o23/n1": "1.0.
|
|
24
|
+
"@rainbow-o23/n1": "1.0.45",
|
|
25
25
|
"node-fetch": "2.6.7",
|
|
26
|
-
"typeorm": "^0.3.17"
|
|
26
|
+
"typeorm": "^0.3.17",
|
|
27
|
+
"typescript": "5.5.4"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@babel/core": "^7.23.9",
|
|
@@ -48,8 +49,7 @@
|
|
|
48
49
|
"rollup": "^3.7.0",
|
|
49
50
|
"rollup-plugin-tslint": "^0.2.2",
|
|
50
51
|
"rollup-plugin-typescript2": "^0.34.1",
|
|
51
|
-
"tslib": "^2.4.1"
|
|
52
|
-
"typescript": "5.1.6"
|
|
52
|
+
"tslib": "^2.4.1"
|
|
53
53
|
},
|
|
54
54
|
"jest": {
|
|
55
55
|
"moduleFileExtensions": [
|
|
@@ -74,7 +74,7 @@
|
|
|
74
74
|
"testEnvironment": "node"
|
|
75
75
|
},
|
|
76
76
|
"volta": {
|
|
77
|
-
"node": "
|
|
77
|
+
"node": "20.17.0",
|
|
78
78
|
"yarn": "1.22.21"
|
|
79
79
|
}
|
|
80
80
|
}
|
package/rollup.config.base.js
CHANGED
|
@@ -25,7 +25,7 @@ export const buildConfig = (lint) => {
|
|
|
25
25
|
external(id) {
|
|
26
26
|
return ["@rainbow-o23/", "typeorm/"].some(scope => id.startsWith(scope))
|
|
27
27
|
|| [
|
|
28
|
-
"typeorm", "reflect-metadata", "node-fetch"
|
|
28
|
+
"typescript", "typeorm", "reflect-metadata", "node-fetch"
|
|
29
29
|
].includes(id);
|
|
30
30
|
}
|
|
31
31
|
};
|
package/src/lib/error-codes.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {O23ReservedErrorCode} from '@rainbow-o23/n1';
|
|
1
|
+
import {ErrorCodes, O23ReservedErrorCode} from '@rainbow-o23/n1';
|
|
2
2
|
|
|
3
3
|
export const ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY: O23ReservedErrorCode = 'O03-00001';
|
|
4
4
|
export const ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY: O23ReservedErrorCode = 'O03-00002';
|
|
5
5
|
export const ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND: O23ReservedErrorCode = 'O03-00003';
|
|
6
6
|
export const ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND: O23ReservedErrorCode = 'O03-00004';
|
|
7
7
|
export const ERR_TYPEORM_DATASOURCE_NOT_FOUND: O23ReservedErrorCode = 'O03-00005';
|
|
8
|
+
/** @deprecated, typeorm entity related steps are removed from 1.0.42 */
|
|
8
9
|
export const ERR_TYPEORM_ENTITY_NOT_FOUND: O23ReservedErrorCode = 'O03-00006';
|
|
9
10
|
export const ERR_TYPEORM_SQL_NOT_EMPTY: O23ReservedErrorCode = 'O03-00007';
|
|
10
11
|
export const ERR_TYPEORM_TRANSACTION_NOT_FOUND: O23ReservedErrorCode = 'O03-00008';
|
|
@@ -20,3 +21,25 @@ export const ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL: O23ReservedErrorCode = 'O03
|
|
|
20
21
|
export const ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS: O23ReservedErrorCode = 'O03-00018';
|
|
21
22
|
export const ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL: O23ReservedErrorCode = 'O03-00019';
|
|
22
23
|
export const ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION: O23ReservedErrorCode = 'O03-00020';
|
|
24
|
+
|
|
25
|
+
ErrorCodes.ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY = ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY;
|
|
26
|
+
ErrorCodes.ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY = ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY;
|
|
27
|
+
ErrorCodes.ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND = ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND;
|
|
28
|
+
ErrorCodes.ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND = ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND;
|
|
29
|
+
ErrorCodes.ERR_TYPEORM_DATASOURCE_NOT_FOUND = ERR_TYPEORM_DATASOURCE_NOT_FOUND;
|
|
30
|
+
/** @deprecated, typeorm entity related steps are removed from 1.0.42 */
|
|
31
|
+
ErrorCodes.ERR_TYPEORM_ENTITY_NOT_FOUND = ERR_TYPEORM_ENTITY_NOT_FOUND;
|
|
32
|
+
ErrorCodes.ERR_TYPEORM_SQL_NOT_EMPTY = ERR_TYPEORM_SQL_NOT_EMPTY;
|
|
33
|
+
ErrorCodes.ERR_TYPEORM_TRANSACTION_NOT_FOUND = ERR_TYPEORM_TRANSACTION_NOT_FOUND;
|
|
34
|
+
ErrorCodes.ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY = ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY;
|
|
35
|
+
ErrorCodes.ERR_FETCH_ERROR = ERR_FETCH_ERROR;
|
|
36
|
+
ErrorCodes.ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED = ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED;
|
|
37
|
+
ErrorCodes.ERR_EACH_FRAGMENT_NOT_ANY_ARRAY = ERR_EACH_FRAGMENT_NOT_ANY_ARRAY;
|
|
38
|
+
ErrorCodes.ERR_PIPELINE_STEP_REF_NOT_EMPTY = ERR_PIPELINE_STEP_REF_NOT_EMPTY;
|
|
39
|
+
ErrorCodes.ERR_PIPELINE_STEP_REF_NOT_FOUND = ERR_PIPELINE_STEP_REF_NOT_FOUND;
|
|
40
|
+
ErrorCodes.ERR_PIPELINE_REF_NOT_EMPTY = ERR_PIPELINE_REF_NOT_EMPTY;
|
|
41
|
+
ErrorCodes.ERR_PIPELINE_REF_NOT_FOUND = ERR_PIPELINE_REF_NOT_FOUND;
|
|
42
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL;
|
|
43
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS = ERR_PIPELINE_SNIPPET_CANNOT_USE_PROCESS;
|
|
44
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL = ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL;
|
|
45
|
+
ErrorCodes.ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION = ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION;
|
|
@@ -67,9 +67,9 @@ export class FetchPipelineStep<In = PipelineStepPayload, Out = PipelineStepPaylo
|
|
|
67
67
|
// to millisecond
|
|
68
68
|
this._endpointTimeout = this._endpointTimeout > 0 ? this._endpointTimeout * 1000 : -1;
|
|
69
69
|
this._urlGenerateSnippet = options.urlGenerate;
|
|
70
|
-
this._urlGenerateFunc = Utils.
|
|
70
|
+
this._urlGenerateFunc = Utils.createAsyncFunction(this.getUrlGenerateSnippet(), {
|
|
71
71
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
72
|
-
createDefault: () => ($endpointUrl: string, _$factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => $endpointUrl,
|
|
72
|
+
createDefault: () => async ($endpointUrl: string, _$factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => $endpointUrl,
|
|
73
73
|
getVariableNames: () => this.getUrlGenerateVariableName(),
|
|
74
74
|
error: (e: Error) => {
|
|
75
75
|
this.getLogger().error(`Failed on create function for url generate, snippet is [${this.getUrlGenerateSnippet()}].`);
|
|
@@ -77,9 +77,9 @@ export class FetchPipelineStep<In = PipelineStepPayload, Out = PipelineStepPaylo
|
|
|
77
77
|
}
|
|
78
78
|
});
|
|
79
79
|
this._headersGenerateSnippet = options.headersGenerate;
|
|
80
|
-
this._headersGenerateFunc = Utils.
|
|
80
|
+
this._headersGenerateFunc = Utils.createAsyncFunction(this.getHeadersGenerateSnippet(), {
|
|
81
81
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
82
|
-
createDefault: () => (_$factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => (void 0),
|
|
82
|
+
createDefault: () => async (_$factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => (void 0),
|
|
83
83
|
getVariableNames: () => this.getHeadersGenerateVariableNames(),
|
|
84
84
|
error: (e: Error) => {
|
|
85
85
|
this.getLogger().error(`Failed on create function for request headers generate, snippet is [${this.getHeadersGenerateSnippet()}].`);
|
|
@@ -88,9 +88,9 @@ export class FetchPipelineStep<In = PipelineStepPayload, Out = PipelineStepPaylo
|
|
|
88
88
|
});
|
|
89
89
|
this._bodyUsed = options.bodyUsed;
|
|
90
90
|
this._bodyGenerateSnippet = options.bodyGenerate;
|
|
91
|
-
this._bodyGenerateFunc = Utils.
|
|
91
|
+
this._bodyGenerateFunc = Utils.createAsyncFunction(this.getBodyGenerateSnippet(), {
|
|
92
92
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
93
|
-
createDefault: () => ($factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => {
|
|
93
|
+
createDefault: () => async ($factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => {
|
|
94
94
|
return ($factor == null || (typeof $factor === 'string' && $factor.length === 0)) ? (void 0) : JSON.stringify($factor);
|
|
95
95
|
},
|
|
96
96
|
getVariableNames: () => this.getBodyGenerateVariableNames(),
|
|
@@ -251,15 +251,15 @@ export class FetchPipelineStep<In = PipelineStepPayload, Out = PipelineStepPaylo
|
|
|
251
251
|
const $helpers = this.getHelpers();
|
|
252
252
|
let url = '';
|
|
253
253
|
try {
|
|
254
|
-
url = this._urlGenerateFunc(this.getEndpointUrl(), data, request, $helpers, $helpers);
|
|
254
|
+
url = await this._urlGenerateFunc(this.getEndpointUrl(), data, request, $helpers, $helpers);
|
|
255
255
|
const method = this.getEndpointMethod();
|
|
256
256
|
const staticHeaders = this.getEndpointHeaders() ?? {};
|
|
257
|
-
const headers = this._headersGenerateFunc(data, request, $helpers, $helpers) ?? {};
|
|
257
|
+
const headers = await this._headersGenerateFunc(data, request, $helpers, $helpers) ?? {};
|
|
258
258
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
259
259
|
let body: any;
|
|
260
260
|
const bodyUsed = this.isBodyUsed();
|
|
261
261
|
if (bodyUsed === true || (bodyUsed == null && method !== 'get')) {
|
|
262
|
-
body = this._bodyGenerateFunc(data, request, $helpers, $helpers);
|
|
262
|
+
body = await this._bodyGenerateFunc(data, request, $helpers, $helpers);
|
|
263
263
|
} else {
|
|
264
264
|
body = (void 0);
|
|
265
265
|
}
|
package/src/lib/http/types.ts
CHANGED
|
@@ -2,11 +2,11 @@ import {PipelineStepData, PipelineStepHelpers, Undefinable} from '@rainbow-o23/n
|
|
|
2
2
|
import {Response} from 'node-fetch';
|
|
3
3
|
|
|
4
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
5
|
-
export type HttpGenerateUrl<In, InFragment> = ($endpointUrl: string, $factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => string
|
|
5
|
+
export type HttpGenerateUrl<In, InFragment> = ($endpointUrl: string, $factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<string>;
|
|
6
6
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
-
export type HttpGenerateHeaders<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Undefinable<Record<string, string
|
|
7
|
+
export type HttpGenerateHeaders<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<Undefinable<Record<string, string>>>;
|
|
8
8
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
-
export type HttpGenerateBody<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => any
|
|
9
|
+
export type HttpGenerateBody<In, InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<any>;
|
|
10
10
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
11
|
export type HttpGenerateResponse<In, InFragment> = ($response: Response, $factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<any>;
|
|
12
12
|
|
|
@@ -30,4 +30,4 @@ export interface HttpErrorHandleOptions<In, InFragment> {
|
|
|
30
30
|
$request: PipelineStepData<In>;
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
export type HttpHandleError<In, InFragment, OutFragment> = (options: HttpErrorHandleOptions<In, InFragment>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<OutFragment> | never;
|
|
33
|
+
export type HttpHandleError<In, InFragment, OutFragment> = ($options: HttpErrorHandleOptions<In, InFragment>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<OutFragment> | never;
|
|
@@ -43,11 +43,11 @@ export interface FragmentaryPipelineStepOptions<In = PipelineStepPayload, Out =
|
|
|
43
43
|
/**
|
|
44
44
|
* parameter names could be change {@link AbstractFragmentaryPipelineStep#generateFromRequestVariableNames}
|
|
45
45
|
*/
|
|
46
|
-
export type GetInFragmentFromRequestFunc<In, InFragment> = ($factor: In, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => InFragment
|
|
46
|
+
export type GetInFragmentFromRequestFunc<In, InFragment> = ($factor: In, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<InFragment>;
|
|
47
47
|
/**
|
|
48
48
|
* parameter names could be change {@link AbstractFragmentaryPipelineStep#generateToResponseVariableNames}
|
|
49
49
|
*/
|
|
50
|
-
export type SetOutFragmentToResponseFunc<In, Out, OutFragment> = ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Out
|
|
50
|
+
export type SetOutFragmentToResponseFunc<In, Out, OutFragment> = ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<Out>;
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
53
|
* deal with fragment data from request, and put result into response.
|
|
@@ -86,9 +86,11 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
86
86
|
} else {
|
|
87
87
|
this._mergeRequest = options.mergeRequest ?? false;
|
|
88
88
|
}
|
|
89
|
-
this._fromRequestFunc = Utils.
|
|
89
|
+
this._fromRequestFunc = Utils.createAsyncFunction(this.getFromRequestSnippet(), {
|
|
90
90
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
91
|
-
createDefault: () => ($factor: In, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): InFragment =>
|
|
91
|
+
createDefault: () => async ($factor: In, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Promise<InFragment> => {
|
|
92
|
+
return $factor as unknown as InFragment;
|
|
93
|
+
},
|
|
92
94
|
getVariableNames: () => this.generateFromRequestVariableNames(),
|
|
93
95
|
error: (e: Error) => {
|
|
94
96
|
this.getLogger().error(`Failed on create function for from request transformer, snippet is [${this.getFromRequestSnippet()}].`);
|
|
@@ -196,24 +198,24 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
196
198
|
if (funcOrSnippet == null || (typeof funcOrSnippet === 'string' && funcOrSnippet.trim().length === 0)) {
|
|
197
199
|
if (this.useUnboxMerging()) {
|
|
198
200
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
199
|
-
return ($result: OutFragment, $request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Out => {
|
|
201
|
+
return async ($result: OutFragment, $request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Promise<Out> => {
|
|
200
202
|
return {...$request.content, ...$result} as Out;
|
|
201
203
|
};
|
|
202
204
|
} else if (this.hasMergeKey()) {
|
|
203
205
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
204
|
-
return ($result: OutFragment, $request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Out => {
|
|
206
|
+
return async ($result: OutFragment, $request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Promise<Out> => {
|
|
205
207
|
return {...$request.content, [this.getMergeKey()]: $result} as Out;
|
|
206
208
|
};
|
|
207
209
|
} else {
|
|
208
210
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
209
|
-
return ($result: OutFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Out => {
|
|
211
|
+
return async ($result: OutFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Promise<Out> => {
|
|
210
212
|
return $result as unknown as Out;
|
|
211
213
|
};
|
|
212
214
|
}
|
|
213
215
|
} else if (typeof funcOrSnippet === 'string') {
|
|
214
216
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
215
|
-
const func
|
|
216
|
-
createDefault: ()
|
|
217
|
+
const func = Utils.createAsyncFunction(funcOrSnippet, {
|
|
218
|
+
createDefault: async () => {
|
|
217
219
|
throw new UncatchableError(ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY, 'Cannot create perform func on empty snippet.');
|
|
218
220
|
},
|
|
219
221
|
getVariableNames: () => this.generateToResponseVariableNames(),
|
|
@@ -221,14 +223,14 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
221
223
|
this.getLogger().error(`Failed on create function for snippet[${funcOrSnippet}].`);
|
|
222
224
|
throw e;
|
|
223
225
|
}
|
|
224
|
-
})
|
|
226
|
+
}) as unknown as SetOutFragmentToResponseFunc<In, Out, OutFragment>;
|
|
225
227
|
if (this.useUnboxMerging()) {
|
|
226
|
-
return ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Out => {
|
|
227
|
-
const r = func($result, $request, $helpers, $);
|
|
228
|
+
return async ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Promise<Out> => {
|
|
229
|
+
const r = await func($result, $request, $helpers, $);
|
|
228
230
|
return {...$request.content, ...r};
|
|
229
231
|
};
|
|
230
232
|
} else if (this.hasMergeKey()) {
|
|
231
|
-
return ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Out => {
|
|
233
|
+
return async ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Promise<Out> => {
|
|
232
234
|
const r = func($result, $request, $helpers, $);
|
|
233
235
|
return {...$request.content, [this.getMergeKey()]: r} as Out;
|
|
234
236
|
};
|
|
@@ -236,12 +238,12 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
236
238
|
return func as SetOutFragmentToResponseFunc<In, Out, OutFragment>;
|
|
237
239
|
}
|
|
238
240
|
} else if (this.useUnboxMerging()) {
|
|
239
|
-
return ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Out => {
|
|
241
|
+
return async ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Promise<Out> => {
|
|
240
242
|
const r = funcOrSnippet($result, $request, $helpers, $);
|
|
241
|
-
return {...$request.content, ...r};
|
|
243
|
+
return {...$request.content, ...r} as Out;
|
|
242
244
|
};
|
|
243
245
|
} else if (this.hasMergeKey()) {
|
|
244
|
-
return ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Out => {
|
|
246
|
+
return async ($result: OutFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Promise<Out> => {
|
|
245
247
|
const r = funcOrSnippet($result, $request, $helpers, $);
|
|
246
248
|
return {...$request.content, [this.getMergeKey()]: r} as Out;
|
|
247
249
|
};
|
|
@@ -253,7 +255,7 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
253
255
|
/**
|
|
254
256
|
* default get request content
|
|
255
257
|
*/
|
|
256
|
-
protected getFromInput($factor: In, $request: PipelineStepData<In>): InFragment {
|
|
258
|
+
protected async getFromInput($factor: In, $request: PipelineStepData<In>): Promise<InFragment> {
|
|
257
259
|
const $helpers = this.getHelpers();
|
|
258
260
|
return this._fromRequestFunc($factor, $request, $helpers, $helpers);
|
|
259
261
|
}
|
|
@@ -261,9 +263,9 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
261
263
|
/**
|
|
262
264
|
* default set to response content
|
|
263
265
|
*/
|
|
264
|
-
protected setToOutput($result: OutFragment, $request: PipelineStepData<In>): PipelineStepData<Out
|
|
266
|
+
protected async setToOutput($result: OutFragment, $request: PipelineStepData<In>): Promise<PipelineStepData<Out>> {
|
|
265
267
|
const $helpers = this.getHelpers();
|
|
266
|
-
return {content: this._toResponseFunc($result, $request, $helpers, $helpers)};
|
|
268
|
+
return {content: await this._toResponseFunc($result, $request, $helpers, $helpers)};
|
|
267
269
|
}
|
|
268
270
|
|
|
269
271
|
/**
|
|
@@ -360,19 +362,19 @@ export abstract class AbstractFragmentaryPipelineStep<In = PipelineStepPayload,
|
|
|
360
362
|
public async performAndCatch(request: PipelineStepData<In>, perform: (data: InFragment) => Promise<PipelineStepData<Out>>): Promise<PipelineStepData<Out>> {
|
|
361
363
|
let fragment = null;
|
|
362
364
|
try {
|
|
363
|
-
fragment = this.getFromInput(request.content, request);
|
|
365
|
+
fragment = await this.getFromInput(request.content, request);
|
|
364
366
|
return await perform(fragment);
|
|
365
367
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
366
368
|
} catch (e: any) {
|
|
367
369
|
const result = await this.handleError(fragment, request, e);
|
|
368
|
-
return this.setToOutput(result, request);
|
|
370
|
+
return await this.setToOutput(result, request);
|
|
369
371
|
}
|
|
370
372
|
}
|
|
371
373
|
|
|
372
374
|
public async perform(request: PipelineStepData<In>): Promise<PipelineStepData<Out>> {
|
|
373
375
|
return await this.performAndCatch(request, async (fragment) => {
|
|
374
376
|
const result = await this.doPerform(fragment, request);
|
|
375
|
-
return this.setToOutput(result, request);
|
|
377
|
+
return await this.setToOutput(result, request);
|
|
376
378
|
});
|
|
377
379
|
}
|
|
378
380
|
}
|
|
@@ -67,7 +67,7 @@ export class ConditionalPipelineStepSets<In = PipelineStepPayload, Out = Pipelin
|
|
|
67
67
|
const checked = await this.check(fragment, request);
|
|
68
68
|
if (checked) {
|
|
69
69
|
const result = await this.doPerform(request.content as unknown as InFragment, request);
|
|
70
|
-
return this.setToOutput(result, request);
|
|
70
|
+
return await this.setToOutput(result, request);
|
|
71
71
|
}
|
|
72
72
|
// noinspection DuplicatedCode
|
|
73
73
|
const otherwiseStepBuilders = this.getOtherwiseStepBuilders();
|
|
@@ -77,7 +77,7 @@ export class ConditionalPipelineStepSets<In = PipelineStepPayload, Out = Pipelin
|
|
|
77
77
|
...this.buildStepOptions(), name: this.getName(), steps: otherwiseStepBuilders
|
|
78
78
|
});
|
|
79
79
|
const result = await sets.perform(request);
|
|
80
|
-
return this.setToOutput(result.content, request);
|
|
80
|
+
return await this.setToOutput(result.content, request);
|
|
81
81
|
} else {
|
|
82
82
|
// otherwise route not declared
|
|
83
83
|
return request as unknown as PipelineStepData<Out>;
|
|
@@ -74,7 +74,7 @@ export class EachPipelineStepSets<In = PipelineStepPayload, Out = PipelineStepPa
|
|
|
74
74
|
results.push(result);
|
|
75
75
|
}
|
|
76
76
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
-
return this.setToOutput(results as any, request);
|
|
77
|
+
return await this.setToOutput(results as any, request);
|
|
78
78
|
});
|
|
79
79
|
}
|
|
80
80
|
}
|
|
@@ -3,7 +3,7 @@ import {PipelineStepSets, PipelineStepSetsContext, PipelineStepSetsOptions} from
|
|
|
3
3
|
import {ScriptFuncOrBody} from './types';
|
|
4
4
|
import {Utils} from './utils';
|
|
5
5
|
|
|
6
|
-
export type CloneDataFunc<In, InFragment, EachInFragment = InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => EachInFragment
|
|
6
|
+
export type CloneDataFunc<In, InFragment, EachInFragment = InFragment> = ($factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<EachInFragment>;
|
|
7
7
|
|
|
8
8
|
export interface ParallelPipelineStepSetsOptions<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out, EachInFragment = InFragment>
|
|
9
9
|
extends PipelineStepSetsOptions<In, Out, InFragment, OutFragment> {
|
|
@@ -23,9 +23,9 @@ export class ParallelPipelineStepSets<In = PipelineStepPayload, Out = PipelineSt
|
|
|
23
23
|
public constructor(options: ParallelPipelineStepSetsOptions<In, Out, InFragment, OutFragment, EachInFragment>) {
|
|
24
24
|
super(options);
|
|
25
25
|
this._cloneDataSnippet = options.cloneData;
|
|
26
|
-
this._cloneData = Utils.
|
|
26
|
+
this._cloneData = Utils.createAsyncFunction(this.getCloneDataSnippet(), {
|
|
27
27
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
28
|
-
createDefault: () => ($factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): EachInFragment => {
|
|
28
|
+
createDefault: () => async ($factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Promise<EachInFragment> => {
|
|
29
29
|
return $factor as unknown as EachInFragment;
|
|
30
30
|
},
|
|
31
31
|
getVariableNames: () => ['$factor', '$request', ...this.getHelpersVariableNames()], //this.generateFromRequestVariableNames(),
|
|
@@ -51,7 +51,7 @@ export class ParallelPipelineStepSets<In = PipelineStepPayload, Out = PipelineSt
|
|
|
51
51
|
/**
|
|
52
52
|
* default get request content
|
|
53
53
|
*/
|
|
54
|
-
protected cloneDataForEach($factor: InFragment, $request: PipelineStepData<In>): EachInFragment {
|
|
54
|
+
protected async cloneDataForEach($factor: InFragment, $request: PipelineStepData<In>): Promise<EachInFragment> {
|
|
55
55
|
if ($factor == null) {
|
|
56
56
|
return null;
|
|
57
57
|
}
|
|
@@ -68,7 +68,7 @@ export class ParallelPipelineStepSets<In = PipelineStepPayload, Out = PipelineSt
|
|
|
68
68
|
return steps.map(async step => {
|
|
69
69
|
return await this.measurePerformance(traceId, 'STEP', step.constructor.name)
|
|
70
70
|
.execute(async () => {
|
|
71
|
-
const eachData = this.cloneDataForEach(data, request);
|
|
71
|
+
const eachData = await this.cloneDataForEach(data, request);
|
|
72
72
|
const eachRequest = {content: eachData, $context: {...context, traceId}};
|
|
73
73
|
this.traceStepIn(traceId, step, request);
|
|
74
74
|
const response = await step.perform(eachRequest);
|
|
@@ -98,7 +98,7 @@ export class RoutesPipelineStepSets<In = PipelineStepPayload, Out = PipelineStep
|
|
|
98
98
|
...this.buildStepOptions(), name: this.getName(), steps
|
|
99
99
|
});
|
|
100
100
|
const result = await sets.perform(request);
|
|
101
|
-
return this.setToOutput(result.content, request);
|
|
101
|
+
return await this.setToOutput(result.content, request);
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
}
|
|
@@ -110,7 +110,7 @@ export class RoutesPipelineStepSets<In = PipelineStepPayload, Out = PipelineStep
|
|
|
110
110
|
...this.buildStepOptions(), name: this.getName(), steps: otherwiseStepBuilders
|
|
111
111
|
});
|
|
112
112
|
const result = await sets.perform(request);
|
|
113
|
-
return this.setToOutput(result.content, request);
|
|
113
|
+
return await this.setToOutput(result.content, request);
|
|
114
114
|
} else {
|
|
115
115
|
// otherwise route not declared
|
|
116
116
|
return request as unknown as PipelineStepData<Out>;
|
package/src/lib/step/utils.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {Nullable, UncatchableError, Undefinable} from '@rainbow-o23/n1';
|
|
2
|
+
import ts, {JsxEmit, ModuleKind, ModuleResolutionKind, ScriptTarget} from 'typescript';
|
|
2
3
|
import {
|
|
3
4
|
ERR_PIPELINE_SNIPPET_CANNOT_USE_EVAL,
|
|
4
5
|
ERR_PIPELINE_SNIPPET_CANNOT_USE_FUNCTION,
|
|
@@ -57,6 +58,23 @@ export class Utils {
|
|
|
57
58
|
// // noinspection ExceptionCaughtLocallyJS
|
|
58
59
|
// throw new UncatchableError(ERR_PIPELINE_SNIPPET_CANNOT_USE_GLOBAL, '"global" is not allowed in dynamic snippet.');
|
|
59
60
|
// }
|
|
61
|
+
// transpiled by typescript
|
|
62
|
+
const transpiled = ts.transpileModule(snippet, {
|
|
63
|
+
compilerOptions: {
|
|
64
|
+
target: ScriptTarget.ES2022,
|
|
65
|
+
jsx: JsxEmit.None, // no jsx
|
|
66
|
+
strict: false,
|
|
67
|
+
noEmitOnError: true, // ignore errors
|
|
68
|
+
esModuleInterop: true,
|
|
69
|
+
module: ModuleKind.ES2022,
|
|
70
|
+
suppressOutputPathCheck: false,
|
|
71
|
+
skipLibCheck: true,
|
|
72
|
+
skipDefaultLibCheck: true,
|
|
73
|
+
moduleResolution: ModuleResolutionKind.Node16 // default use node 16
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
snippet = transpiled.outputText;
|
|
77
|
+
// build function
|
|
60
78
|
const variableNames = creators.getVariableNames() ?? [];
|
|
61
79
|
if (creators.async) {
|
|
62
80
|
const func = new AsyncFunction(...variableNames, ...AvoidNames, snippet);
|