@rainbow-o23/n3 0.1.1
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/.babelrc +11 -0
- package/.eslintrc +23 -0
- package/README.md +584 -0
- package/index.cjs +2283 -0
- package/index.d.ts +5 -0
- package/index.js +2229 -0
- package/lib/error-codes.d.ts +17 -0
- package/lib/http/fetch-step.d.ts +53 -0
- package/lib/http/index.d.ts +2 -0
- package/lib/http/types.d.ts +21 -0
- package/lib/step/abstract-fragmentary-pipeline-step.d.ts +44 -0
- package/lib/step/async-step-sets.d.ts +5 -0
- package/lib/step/conditional-step-sets.d.ts +23 -0
- package/lib/step/delete-property-step.d.ts +11 -0
- package/lib/step/each-step-sets.d.ts +14 -0
- package/lib/step/get-property-step.d.ts +11 -0
- package/lib/step/index.d.ts +14 -0
- package/lib/step/ref-pipeline-step.d.ts +15 -0
- package/lib/step/ref-step-step.d.ts +14 -0
- package/lib/step/routes-step-sets.d.ts +30 -0
- package/lib/step/snippet-step.d.ts +19 -0
- package/lib/step/snowflake-step.d.ts +6 -0
- package/lib/step/step-sets.d.ts +19 -0
- package/lib/step/types.d.ts +13 -0
- package/lib/step/utils.d.ts +23 -0
- package/lib/typeorm/abstract-datasource.d.ts +22 -0
- package/lib/typeorm/better-sqlite3-datasource.d.ts +7 -0
- package/lib/typeorm/datasource-manager.d.ts +25 -0
- package/lib/typeorm/index.d.ts +7 -0
- package/lib/typeorm/mssql-datasource.d.ts +6 -0
- package/lib/typeorm/mysql-datasource.d.ts +6 -0
- package/lib/typeorm/oracle-datasource.d.ts +6 -0
- package/lib/typeorm/pgsql-datasource.d.ts +6 -0
- package/lib/typeorm-step/abstract-typeorm-by-sql-step.d.ts +62 -0
- package/lib/typeorm-step/abstract-typeorm-load-by-sql-step.d.ts +10 -0
- package/lib/typeorm-step/abstract-typeorm-step.d.ts +30 -0
- package/lib/typeorm-step/index.d.ts +12 -0
- package/lib/typeorm-step/type-orm-transactional-step-sets.d.ts +22 -0
- package/lib/typeorm-step/typeorm-bulk-save-by-sql-step.d.ts +9 -0
- package/lib/typeorm-step/typeorm-by-snippet-step.d.ts +21 -0
- package/lib/typeorm-step/typeorm-load-entity-by-id-step.d.ts +12 -0
- package/lib/typeorm-step/typeorm-load-many-by-sql-step.d.ts +6 -0
- package/lib/typeorm-step/typeorm-load-one-by-sql-step.d.ts +6 -0
- package/lib/typeorm-step/typeorm-save-by-sql-step.d.ts +9 -0
- package/lib/typeorm-step/typeorm-save-entity-step.d.ts +32 -0
- package/lib/typeorm-step/types.d.ts +22 -0
- package/package.json +74 -0
- package/rollup.config.base.js +33 -0
- package/rollup.config.ci.js +3 -0
- package/rollup.config.js +3 -0
- package/src/index.ts +7 -0
- package/src/lib/error-codes.ts +18 -0
- package/src/lib/http/fetch-step.ts +290 -0
- package/src/lib/http/index.ts +3 -0
- package/src/lib/http/types.ts +33 -0
- package/src/lib/step/abstract-fragmentary-pipeline-step.ts +367 -0
- package/src/lib/step/async-step-sets.ts +14 -0
- package/src/lib/step/conditional-step-sets.ts +115 -0
- package/src/lib/step/delete-property-step.ts +33 -0
- package/src/lib/step/each-step-sets.ts +80 -0
- package/src/lib/step/get-property-step.ts +34 -0
- package/src/lib/step/index.ts +18 -0
- package/src/lib/step/ref-pipeline-step.ts +65 -0
- package/src/lib/step/ref-step-step.ts +59 -0
- package/src/lib/step/routes-step-sets.ts +147 -0
- package/src/lib/step/snippet-step.ts +80 -0
- package/src/lib/step/snowflake-step.ts +16 -0
- package/src/lib/step/step-sets.ts +99 -0
- package/src/lib/step/types.ts +23 -0
- package/src/lib/step/utils.ts +109 -0
- package/src/lib/typeorm/abstract-datasource.ts +58 -0
- package/src/lib/typeorm/better-sqlite3-datasource.ts +29 -0
- package/src/lib/typeorm/datasource-manager.ts +104 -0
- package/src/lib/typeorm/index.ts +9 -0
- package/src/lib/typeorm/mssql-datasource.ts +131 -0
- package/src/lib/typeorm/mysql-datasource.ts +35 -0
- package/src/lib/typeorm/oracle-datasource.ts +27 -0
- package/src/lib/typeorm/pgsql-datasource.ts +25 -0
- package/src/lib/typeorm-step/abstract-typeorm-by-sql-step.ts +556 -0
- package/src/lib/typeorm-step/abstract-typeorm-load-by-sql-step.ts +31 -0
- package/src/lib/typeorm-step/abstract-typeorm-step.ts +241 -0
- package/src/lib/typeorm-step/index.ts +17 -0
- package/src/lib/typeorm-step/type-orm-transactional-step-sets.ts +129 -0
- package/src/lib/typeorm-step/typeorm-bulk-save-by-sql-step.ts +29 -0
- package/src/lib/typeorm-step/typeorm-by-snippet-step.ts +83 -0
- package/src/lib/typeorm-step/typeorm-load-entity-by-id-step.ts +35 -0
- package/src/lib/typeorm-step/typeorm-load-many-by-sql-step.ts +13 -0
- package/src/lib/typeorm-step/typeorm-load-one-by-sql-step.ts +13 -0
- package/src/lib/typeorm-step/typeorm-save-by-sql-step.ts +24 -0
- package/src/lib/typeorm-step/typeorm-save-entity-step.ts +109 -0
- package/src/lib/typeorm-step/types.ts +25 -0
- package/test/step/snippet-step.test.ts +21 -0
- package/test/step/snowflake-step.test.ts +22 -0
- package/test/step/typeorm-by-sql-autonomous.test.ts +117 -0
- package/test/step/typeorm-by-sql-transactional.test.ts +215 -0
- package/test/step/typeorm-entity.test.ts +50 -0
- package/tsconfig.json +36 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { PipelineStepData, PipelineStepPayload, Undefinable } from '@rainbow-o23/n1';
|
|
2
|
+
import { QueryRunner } from 'typeorm';
|
|
3
|
+
import { PipelineStepSets, PipelineStepSetsContext, PipelineStepSetsOptions } from '../step';
|
|
4
|
+
import { TypeOrmDataSource } from '../typeorm';
|
|
5
|
+
import { TypeOrmDataSourceName, TypeOrmTransactionalContext, TypeOrmTransactionKey, TypeOrmTransactionName } from './types';
|
|
6
|
+
export interface TypeOrmTransactionalPipelineStepSetsOptions extends PipelineStepSetsOptions {
|
|
7
|
+
dataSourceName: TypeOrmDataSourceName;
|
|
8
|
+
transactionName?: TypeOrmTransactionName;
|
|
9
|
+
}
|
|
10
|
+
export declare class TypeOrmTransactionalPipelineStepSets<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out> extends PipelineStepSets<In, Out, InFragment, OutFragment> {
|
|
11
|
+
private readonly _dataSourceName;
|
|
12
|
+
private readonly _transactionName;
|
|
13
|
+
constructor(options: TypeOrmTransactionalPipelineStepSetsOptions);
|
|
14
|
+
getDataSourceName(): TypeOrmDataSourceName;
|
|
15
|
+
getTransactionName(): TypeOrmTransactionName;
|
|
16
|
+
getTransactionKey(): TypeOrmTransactionKey;
|
|
17
|
+
protected findDataSourceOrThrow(): Promise<Undefinable<TypeOrmDataSource>>;
|
|
18
|
+
protected createRunner(): Promise<[TypeOrmDataSource, QueryRunner]>;
|
|
19
|
+
protected attachMineToInternalContext(inheritedContext: PipelineStepSetsContext, _request: PipelineStepData<In>): Promise<TypeOrmTransactionalContext>;
|
|
20
|
+
private getRunner;
|
|
21
|
+
protected performWithContext(request: PipelineStepData<In>, run: (request: PipelineStepData<In>, context: PipelineStepSetsContext) => Promise<OutFragment>): Promise<OutFragment>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PipelineStepData, PipelineStepPayload, Undefinable } from '@rainbow-o23/n1';
|
|
2
|
+
import { AbstractTypeOrmBySQLPipelineStep, TypeOrmBasis } from './abstract-typeorm-by-sql-step';
|
|
3
|
+
import { TypeOrmBulkWrittenResult, TypeOrmEntityToSave, TypeOrmEntityValue } from './types';
|
|
4
|
+
export interface TypeOrmBulkSaveBasis extends TypeOrmBasis {
|
|
5
|
+
items?: Array<Array<TypeOrmEntityValue> | TypeOrmEntityToSave>;
|
|
6
|
+
}
|
|
7
|
+
export declare class TypeOrmBulkSaveBySQLPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, OutFragment extends TypeOrmBulkWrittenResult = TypeOrmBulkWrittenResult> extends AbstractTypeOrmBySQLPipelineStep<In, Out, Undefinable<TypeOrmBulkSaveBasis>, OutFragment> {
|
|
8
|
+
protected doPerform(basis: Undefinable<TypeOrmBulkSaveBasis>, request: PipelineStepData<In>): Promise<OutFragment>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PipelineStepData, PipelineStepHelpers, PipelineStepPayload } from '@rainbow-o23/n1';
|
|
2
|
+
import { QueryRunner } from 'typeorm';
|
|
3
|
+
import { ScriptFuncOrBody } from '../step';
|
|
4
|
+
import { AbstractTypeOrmPipelineStep, TypeOrmPipelineStepOptions } from './abstract-typeorm-step';
|
|
5
|
+
export type TypeOrmPerformWithInFragment<InFragment, OutFragment> = ($runner: QueryRunner, $factor: InFragment, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<OutFragment>;
|
|
6
|
+
export type TypeOrmPerformWithoutInFragment<OutFragment> = ($runner: QueryRunner, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<OutFragment>;
|
|
7
|
+
export type TypeOrmPerformFunc<InFragment, OutFragment> = TypeOrmPerformWithInFragment<InFragment, OutFragment> | TypeOrmPerformWithoutInFragment<OutFragment>;
|
|
8
|
+
export interface TypeOrmBySnippetPipelineStepOptions<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out> extends TypeOrmPipelineStepOptions<In, Out, InFragment, OutFragment> {
|
|
9
|
+
snippet: ScriptFuncOrBody<TypeOrmPerformFunc<InFragment, OutFragment>>;
|
|
10
|
+
}
|
|
11
|
+
export declare class TypeOrmBySnippetPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out> extends AbstractTypeOrmPipelineStep<In, Out, InFragment, OutFragment> {
|
|
12
|
+
private readonly _snippet;
|
|
13
|
+
private readonly _func;
|
|
14
|
+
constructor(options: TypeOrmBySnippetPipelineStepOptions<In, Out, InFragment, OutFragment>);
|
|
15
|
+
getSnippet(): ScriptFuncOrBody<TypeOrmPerformFunc<InFragment, OutFragment>>;
|
|
16
|
+
protected generateVariableNames(): Array<string>;
|
|
17
|
+
protected doPerform(basis: InFragment, request: PipelineStepData<In>): Promise<OutFragment>;
|
|
18
|
+
protected isInFragmentIgnored(): boolean;
|
|
19
|
+
protected getRunnerVariableName(): string;
|
|
20
|
+
protected getInFragmentVariableName(): string;
|
|
21
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { PipelineStepData, PipelineStepPayload, Undefinable } from '@rainbow-o23/n1';
|
|
2
|
+
import { AbstractTypeOrmPipelineStep, TypeOrmPipelineStepOptions } from './abstract-typeorm-step';
|
|
3
|
+
import { TypeOrmEntityName, TypeOrmEntityToLoad, TypeOrmIdType } from './types';
|
|
4
|
+
export interface TypeOrmLoadEntityByIdPipelineStepOptions<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = TypeOrmIdType, OutFragment = Out> extends TypeOrmPipelineStepOptions<In, Out, InFragment, OutFragment> {
|
|
5
|
+
entityName: TypeOrmEntityName;
|
|
6
|
+
}
|
|
7
|
+
export declare class TypeOrmLoadEntityByIdPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, OutFragment = TypeOrmEntityToLoad> extends AbstractTypeOrmPipelineStep<In, Out, TypeOrmIdType, OutFragment> {
|
|
8
|
+
private readonly _entityName;
|
|
9
|
+
constructor(options: TypeOrmLoadEntityByIdPipelineStepOptions<In, Out, TypeOrmIdType, OutFragment>);
|
|
10
|
+
getEntityName(): TypeOrmEntityName;
|
|
11
|
+
protected doPerform(id: TypeOrmIdType, request: PipelineStepData<In>): Promise<Undefinable<OutFragment>>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PipelineStepPayload } from '@rainbow-o23/n1';
|
|
2
|
+
import { AbstractTypeOrmLoadBySQLPipelineStep } from './abstract-typeorm-load-by-sql-step';
|
|
3
|
+
import { TypeOrmEntityToLoad } from './types';
|
|
4
|
+
export declare class TypeOrmLoadManyBySQLPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, OutFragment = Array<TypeOrmEntityToLoad>> extends AbstractTypeOrmLoadBySQLPipelineStep<In, Out, OutFragment> {
|
|
5
|
+
protected getDataFromResultSet(rst: Array<TypeOrmEntityToLoad>): Promise<OutFragment>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { PipelineStepPayload } from '@rainbow-o23/n1';
|
|
2
|
+
import { AbstractTypeOrmLoadBySQLPipelineStep } from './abstract-typeorm-load-by-sql-step';
|
|
3
|
+
import { TypeOrmEntityToLoad } from './types';
|
|
4
|
+
export declare class TypeOrmLoadOneBySQLPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, OutFragment = TypeOrmEntityToLoad> extends AbstractTypeOrmLoadBySQLPipelineStep<In, Out, OutFragment> {
|
|
5
|
+
protected getDataFromResultSet(rst: Array<TypeOrmEntityToLoad>): Promise<OutFragment>;
|
|
6
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PipelineStepData, PipelineStepPayload, Undefinable } from '@rainbow-o23/n1';
|
|
2
|
+
import { AbstractTypeOrmBySQLPipelineStep, TypeOrmBasis } from './abstract-typeorm-by-sql-step';
|
|
3
|
+
import { TypeOrmEntityToSave, TypeOrmEntityValue, TypeOrmWrittenResult } from './types';
|
|
4
|
+
export interface TypeOrmSaveBasis extends TypeOrmBasis {
|
|
5
|
+
values?: Array<TypeOrmEntityValue> | TypeOrmEntityToSave;
|
|
6
|
+
}
|
|
7
|
+
export declare class TypeOrmSaveBySQLPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, OutFragment = TypeOrmWrittenResult> extends AbstractTypeOrmBySQLPipelineStep<In, Out, Undefinable<TypeOrmSaveBasis>, OutFragment> {
|
|
8
|
+
protected doPerform(basis: Undefinable<TypeOrmSaveBasis>, request: PipelineStepData<In>): Promise<OutFragment>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { O23ExternalErrorCode, PipelineStepData, PipelineStepPayload, Undefinable } from '@rainbow-o23/n1';
|
|
2
|
+
import { DeepPartial, ObjectLiteral } from 'typeorm';
|
|
3
|
+
import { ScriptFuncOrBody } from '../step';
|
|
4
|
+
import { AbstractTypeOrmPipelineStep, TypeOrmPipelineStepOptions } from './abstract-typeorm-step';
|
|
5
|
+
import { TypeOrmEntityName } from './types';
|
|
6
|
+
export type EntityToSave = DeepPartial<ObjectLiteral>;
|
|
7
|
+
export type UniquenessCheckResult = {
|
|
8
|
+
pass: true;
|
|
9
|
+
} | {
|
|
10
|
+
pass: false;
|
|
11
|
+
code: O23ExternalErrorCode;
|
|
12
|
+
message: string;
|
|
13
|
+
};
|
|
14
|
+
export type UniquenessCheckFunc<EntityToSave> = (given: EntityToSave, existing: EntityToSave) => UniquenessCheckResult;
|
|
15
|
+
export interface TypeOrmSaveEntityPipelineStepOptions<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = EntityToSave, OutFragment = EntityToSave> extends TypeOrmPipelineStepOptions<In, Out, InFragment, OutFragment> {
|
|
16
|
+
entityName: TypeOrmEntityName;
|
|
17
|
+
fillIdBySnowflake?: boolean;
|
|
18
|
+
uniquenessCheckSnippet?: ScriptFuncOrBody<UniquenessCheckFunc<InFragment>>;
|
|
19
|
+
}
|
|
20
|
+
export declare class TypeOrmSaveEntityPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = EntityToSave, OutFragment = EntityToSave> extends AbstractTypeOrmPipelineStep<In, Out, InFragment, OutFragment> {
|
|
21
|
+
private readonly _entityName;
|
|
22
|
+
private readonly _fillIdBySnowflake;
|
|
23
|
+
private readonly _uniquenessCheckSnippet?;
|
|
24
|
+
private readonly _uniquenessCheckFunc?;
|
|
25
|
+
constructor(options: TypeOrmSaveEntityPipelineStepOptions<In, Out, InFragment, OutFragment>);
|
|
26
|
+
getEntityName(): TypeOrmEntityName;
|
|
27
|
+
isFillIdBySnowflake(): boolean;
|
|
28
|
+
getUniquenessCheckSnippet(): Undefinable<ScriptFuncOrBody<UniquenessCheckFunc<InFragment>>>;
|
|
29
|
+
needUniquenessCheck(): boolean;
|
|
30
|
+
protected generateUniquenessCheckVariableNames(): Array<string>;
|
|
31
|
+
protected doPerform(entity: InFragment, request: PipelineStepData<In>): Promise<OutFragment>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DeepPartial, ObjectLiteral, QueryRunner } from 'typeorm';
|
|
2
|
+
import { PipelineStepSetsContext } from '../step';
|
|
3
|
+
import { TypeOrmDataSource } from '../typeorm';
|
|
4
|
+
export type TypeOrmIdType = string | number | bigint;
|
|
5
|
+
export type TypeOrmDataSourceName = string;
|
|
6
|
+
export type TypeOrmTransactionName = string;
|
|
7
|
+
export type TypeOrmTransactionKey = `${TypeOrmDataSourceName}.${TypeOrmTransactionName}`;
|
|
8
|
+
export type TypeOrmEntityName = string;
|
|
9
|
+
export type TypeOrmEntityValue = string | number | bigint | boolean | Date | null | undefined;
|
|
10
|
+
export type TypeOrmEntityToLoad = DeepPartial<ObjectLiteral>;
|
|
11
|
+
export type TypeOrmEntityToSave = DeepPartial<ObjectLiteral>;
|
|
12
|
+
export type TypeOrmSql = string;
|
|
13
|
+
export type TypeOrmIdOfInserted = TypeOrmIdType;
|
|
14
|
+
export type TypeOrmIdsOfInserted = Array<TypeOrmIdOfInserted>;
|
|
15
|
+
export type TypeOrmCountOfAffected = number;
|
|
16
|
+
export type TypeOrmCountsOfAffected = Array<TypeOrmCountOfAffected>;
|
|
17
|
+
export type TypeOrmWrittenResult = TypeOrmIdOfInserted | TypeOrmCountOfAffected;
|
|
18
|
+
export type TypeOrmBulkWrittenResult = TypeOrmIdsOfInserted | TypeOrmCountsOfAffected;
|
|
19
|
+
export declare const DEFAULT_TRANSACTION_NAME: TypeOrmTransactionName;
|
|
20
|
+
export interface TypeOrmTransactionalContext extends PipelineStepSetsContext {
|
|
21
|
+
$trans: Record<TypeOrmTransactionKey, [TypeOrmDataSource, QueryRunner]>;
|
|
22
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rainbow-o23/n3",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "o23 pipelines",
|
|
5
|
+
"main": "index.cjs",
|
|
6
|
+
"module": "index.js",
|
|
7
|
+
"types": "index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "rollup -c",
|
|
11
|
+
"build:ci": "rollup -c rollup.config.ci.js",
|
|
12
|
+
"test": "jest"
|
|
13
|
+
},
|
|
14
|
+
"license": "UNLICENSED",
|
|
15
|
+
"author": "Rainbow Team",
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@rainbow-o23/n1": "0.1.1",
|
|
18
|
+
"@theinternetfolks/snowflake": "^1.3.0",
|
|
19
|
+
"node-fetch": "2.6.1",
|
|
20
|
+
"typeorm": "^0.3.17"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@babel/core": "^7.20.5",
|
|
24
|
+
"@babel/preset-env": "^7.20.2",
|
|
25
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
26
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
27
|
+
"@rollup/plugin-eslint": "^9.0.3",
|
|
28
|
+
"@types/better-sqlite3": "^7.6.6",
|
|
29
|
+
"@types/events": "^3.0.1",
|
|
30
|
+
"@types/node": "18.16.12",
|
|
31
|
+
"@types/node-fetch": "2.6.4",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "^5.46.0",
|
|
33
|
+
"@typescript-eslint/parser": "^5.46.0",
|
|
34
|
+
"better-sqlite3": "^9.0.0",
|
|
35
|
+
"eslint": "^8.29.0",
|
|
36
|
+
"mssql": "^10.0.1",
|
|
37
|
+
"mysql2": "^3.6.2",
|
|
38
|
+
"oracledb": "^6.2.0",
|
|
39
|
+
"pg": "^8.11.3",
|
|
40
|
+
"pg-query-stream": "^4.5.3",
|
|
41
|
+
"reflect-metadata": "^0.1.13",
|
|
42
|
+
"rollup": "^3.7.0",
|
|
43
|
+
"rollup-plugin-tslint": "^0.2.2",
|
|
44
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
45
|
+
"tslib": "^2.4.1",
|
|
46
|
+
"typescript": "5.1.6"
|
|
47
|
+
},
|
|
48
|
+
"jest": {
|
|
49
|
+
"moduleFileExtensions": [
|
|
50
|
+
"js",
|
|
51
|
+
"json",
|
|
52
|
+
"ts"
|
|
53
|
+
],
|
|
54
|
+
"testRegex": "(/test/.*\\.(test|spec))\\.[tj]sx?$",
|
|
55
|
+
"testPathIgnorePatterns": [
|
|
56
|
+
"/node_modules/"
|
|
57
|
+
],
|
|
58
|
+
"transform": {
|
|
59
|
+
"^.+\\.(t|j)s$": "ts-jest"
|
|
60
|
+
},
|
|
61
|
+
"collectCoverageFrom": [
|
|
62
|
+
"src/**/*.(t|j)s"
|
|
63
|
+
],
|
|
64
|
+
"coverageDirectory": "./coverage",
|
|
65
|
+
"coverageReporters": [
|
|
66
|
+
"html"
|
|
67
|
+
],
|
|
68
|
+
"testEnvironment": "node"
|
|
69
|
+
},
|
|
70
|
+
"volta": {
|
|
71
|
+
"node": "18.19.0",
|
|
72
|
+
"yarn": "1.22.21"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {babel} from '@rollup/plugin-babel';
|
|
2
|
+
import eslint from '@rollup/plugin-eslint';
|
|
3
|
+
import typescript from 'rollup-plugin-typescript2';
|
|
4
|
+
|
|
5
|
+
export const buildConfig = (lint) => {
|
|
6
|
+
// ['./index.d.ts', './index.js', './index.cjs', './lib'].forEach(f => {
|
|
7
|
+
// const cwd = path.resolve(process.cwd(), f);
|
|
8
|
+
// if (fs.existsSync(cwd)) {
|
|
9
|
+
// fs.rmSync(cwd, {recursive: true, force: true});
|
|
10
|
+
// }
|
|
11
|
+
// });
|
|
12
|
+
|
|
13
|
+
return {
|
|
14
|
+
input: './src/index.ts',
|
|
15
|
+
output: [
|
|
16
|
+
{format: 'es', dir: '.'},
|
|
17
|
+
{format: 'cjs', file: './index.cjs'}
|
|
18
|
+
],
|
|
19
|
+
plugins: [
|
|
20
|
+
lint ? eslint({exclude: ['../node_modules/**', 'node_modules/**']}) : null,
|
|
21
|
+
// lint ? tslint({exclude: ['../node_modules/**', 'node_modules/**']}) : null,
|
|
22
|
+
typescript({clean: true}),
|
|
23
|
+
babel({babelHelpers: 'bundled'})
|
|
24
|
+
].filter(x => x != null),
|
|
25
|
+
external(id) {
|
|
26
|
+
return ["@rainbow-o23/", "typeorm/"].some(scope => id.startsWith(scope))
|
|
27
|
+
|| [
|
|
28
|
+
"typeorm", "reflect-metadata",
|
|
29
|
+
"@theinternetfolks/snowflake", "node-fetch"
|
|
30
|
+
].includes(id);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
};
|
package/rollup.config.js
ADDED
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {O23ReservedErrorCode} from '@rainbow-o23/n1';
|
|
2
|
+
|
|
3
|
+
export const ERR_PIPELINE_STEP_SNIPPET_NOT_EMPTY: O23ReservedErrorCode = 'O03-00001';
|
|
4
|
+
export const ERR_PIPELINE_STEP_CONDITIONAL_SNIPPET_NOT_EMPTY: O23ReservedErrorCode = 'O03-00002';
|
|
5
|
+
export const ERR_TYPEORM_DATASOURCE_TYPE_NOT_FOUND: O23ReservedErrorCode = 'O03-00003';
|
|
6
|
+
export const ERR_TYPEORM_DATASOURCE_CREATOR_NOT_FOUND: O23ReservedErrorCode = 'O03-00004';
|
|
7
|
+
export const ERR_TYPEORM_DATASOURCE_NOT_FOUND: O23ReservedErrorCode = 'O03-00005';
|
|
8
|
+
export const ERR_TYPEORM_ENTITY_NOT_FOUND: O23ReservedErrorCode = 'O03-00006';
|
|
9
|
+
export const ERR_TYPEORM_SQL_NOT_EMPTY: O23ReservedErrorCode = 'O03-00007';
|
|
10
|
+
export const ERR_TYPEORM_TRANSACTION_NOT_FOUND: O23ReservedErrorCode = 'O03-00008';
|
|
11
|
+
export const ERR_TYPEORM_STEP_SNIPPET_NOT_EMPTY: O23ReservedErrorCode = 'O03-00009';
|
|
12
|
+
export const ERR_FETCH_ERROR: O23ReservedErrorCode = 'O03-00010';
|
|
13
|
+
export const ERR_PIPELINE_STEP_METHOD_NOT_SUPPORTED: O23ReservedErrorCode = 'O03-00011';
|
|
14
|
+
export const ERR_EACH_FRAGMENT_NOT_ANY_ARRAY: O23ReservedErrorCode = 'O03-00012';
|
|
15
|
+
export const ERR_PIPELINE_STEP_REF_NOT_EMPTY: O23ReservedErrorCode = 'O03-00013';
|
|
16
|
+
export const ERR_PIPELINE_STEP_REF_NOT_FOUND: O23ReservedErrorCode = 'O03-00014';
|
|
17
|
+
export const ERR_PIPELINE_REF_NOT_EMPTY: O23ReservedErrorCode = 'O03-00015';
|
|
18
|
+
export const ERR_PIPELINE_REF_NOT_FOUND: O23ReservedErrorCode = 'O03-00016';
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
import {PipelineStepData, PipelineStepHelpers, PipelineStepPayload, UncatchableError} from '@rainbow-o23/n1';
|
|
2
|
+
import fetch, {Response} from 'node-fetch';
|
|
3
|
+
import {ERR_FETCH_ERROR} from '../error-codes';
|
|
4
|
+
import {AbstractFragmentaryPipelineStep, FragmentaryPipelineStepOptions, ScriptFuncOrBody, Utils} from '../step';
|
|
5
|
+
import {
|
|
6
|
+
HttpAbortErrorCode,
|
|
7
|
+
HttpErrorCode,
|
|
8
|
+
HttpErrorHandleOptions,
|
|
9
|
+
HttpGenerateBody,
|
|
10
|
+
HttpGenerateHeaders,
|
|
11
|
+
HttpGenerateResponse,
|
|
12
|
+
HttpGenerateUrl,
|
|
13
|
+
HttpHandleError,
|
|
14
|
+
HttpUnknownErrorCode
|
|
15
|
+
} from './types';
|
|
16
|
+
|
|
17
|
+
export interface FetchPipelineStepOptions<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out>
|
|
18
|
+
extends FragmentaryPipelineStepOptions<In, Out, InFragment, OutFragment> {
|
|
19
|
+
endpointSystemCode: string;
|
|
20
|
+
endpointName: string;
|
|
21
|
+
urlGenerate?: ScriptFuncOrBody<HttpGenerateUrl<In, InFragment>>;
|
|
22
|
+
headersGenerate?: ScriptFuncOrBody<HttpGenerateHeaders<In, InFragment>>;
|
|
23
|
+
bodyGenerate?: ScriptFuncOrBody<HttpGenerateBody<In, InFragment>>;
|
|
24
|
+
responseGenerate?: ScriptFuncOrBody<HttpGenerateResponse<In, InFragment>>;
|
|
25
|
+
responseErrorHandles?: {
|
|
26
|
+
[key: HttpErrorCode]: ScriptFuncOrBody<HttpHandleError<In, InFragment, OutFragment>>;
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class FetchPipelineStep<In = PipelineStepPayload, Out = PipelineStepPayload, InFragment = In, OutFragment = Out>
|
|
31
|
+
extends AbstractFragmentaryPipelineStep<In, Out, InFragment, OutFragment> {
|
|
32
|
+
private readonly _endpointSystemCode: string;
|
|
33
|
+
private readonly _endpointName: string;
|
|
34
|
+
private readonly _endpointUrl: string;
|
|
35
|
+
private readonly _endpointMethod: string;
|
|
36
|
+
private readonly _endpointHeaders: Record<string, string>;
|
|
37
|
+
private readonly _endpointTimeout: number;
|
|
38
|
+
private readonly _urlGenerateSnippet: ScriptFuncOrBody<HttpGenerateUrl<In, InFragment>>;
|
|
39
|
+
private readonly _urlGenerateFunc: HttpGenerateUrl<In, InFragment>;
|
|
40
|
+
private readonly _headersGenerateSnippet: ScriptFuncOrBody<HttpGenerateHeaders<In, InFragment>>;
|
|
41
|
+
private readonly _headersGenerateFunc: HttpGenerateHeaders<In, InFragment>;
|
|
42
|
+
private readonly _bodyUsed: boolean;
|
|
43
|
+
private readonly _bodyGenerateSnippet: ScriptFuncOrBody<HttpGenerateBody<In, InFragment>>;
|
|
44
|
+
private readonly _bodyGenerateFunc: HttpGenerateBody<In, InFragment>;
|
|
45
|
+
private readonly _responseGenerateSnippet: ScriptFuncOrBody<HttpGenerateResponse<In, InFragment>>;
|
|
46
|
+
private readonly _responseGenerateFunc: HttpGenerateResponse<In, InFragment>;
|
|
47
|
+
private readonly _responseErrorHandleFunc: HttpHandleError<In, InFragment, OutFragment>;
|
|
48
|
+
|
|
49
|
+
public constructor(options: FetchPipelineStepOptions<In, Out, InFragment, OutFragment>) {
|
|
50
|
+
super(options);
|
|
51
|
+
const config = this.getConfig();
|
|
52
|
+
this._endpointSystemCode = options.endpointSystemCode;
|
|
53
|
+
this._endpointName = options.endpointName;
|
|
54
|
+
const endpointKey = this.getEndpointKey();
|
|
55
|
+
this._endpointUrl = config.getString(`endpoints.${endpointKey}.url`);
|
|
56
|
+
this._endpointMethod = config.getString(`endpoints.${endpointKey}.method`, 'POST').toLowerCase();
|
|
57
|
+
this._endpointHeaders = this.generateEndpointHeaders(
|
|
58
|
+
config.getString(`endpoints.${endpointKey}.headers`),
|
|
59
|
+
this.generateEndpointHeaders(config.getString(`endpoints.${this.getEndpointSystemCode()}.global.headers`)));
|
|
60
|
+
// in second
|
|
61
|
+
this._endpointTimeout = config.getNumber(`endpoints.${endpointKey}.timeout`)
|
|
62
|
+
?? config.getNumber(`endpoints.${this.getEndpointSystemCode()}.global.timeout`, -1);
|
|
63
|
+
// to millisecond
|
|
64
|
+
this._endpointTimeout = this._endpointTimeout > 0 ? this._endpointTimeout * 1000 : -1;
|
|
65
|
+
this._urlGenerateSnippet = options.urlGenerate;
|
|
66
|
+
this._urlGenerateFunc = Utils.createSyncFunction(this.getUrlGenerateSnippet(), {
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
68
|
+
createDefault: () => ($endpointUrl: string, _$factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => $endpointUrl,
|
|
69
|
+
getVariableNames: () => this.getUrlGenerateVariableName(),
|
|
70
|
+
error: (e: Error) => {
|
|
71
|
+
this.getLogger().error(`Failed on create function for url generate, snippet is [${this.getUrlGenerateSnippet()}].`);
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
this._headersGenerateSnippet = options.headersGenerate;
|
|
76
|
+
this._headersGenerateFunc = Utils.createSyncFunction(this.getHeadersGenerateSnippet(), {
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
78
|
+
createDefault: () => (_$factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => (void 0),
|
|
79
|
+
getVariableNames: () => this.getHeadersGenerateVariableNames(),
|
|
80
|
+
error: (e: Error) => {
|
|
81
|
+
this.getLogger().error(`Failed on create function for request headers generate, snippet is [${this.getHeadersGenerateSnippet()}].`);
|
|
82
|
+
throw e;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
this._bodyUsed = config.getBoolean(`endpoints.${endpointKey}.body.used`, true);
|
|
86
|
+
this._bodyGenerateSnippet = options.bodyGenerate;
|
|
87
|
+
this._bodyGenerateFunc = Utils.createSyncFunction(this.getBodyGenerateSnippet(), {
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
89
|
+
createDefault: () => ($factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => {
|
|
90
|
+
return ($factor == null || (typeof $factor === 'string' && $factor.length === 0)) ? (void 0) : JSON.stringify($factor);
|
|
91
|
+
},
|
|
92
|
+
getVariableNames: () => this.getBodyGenerateVariableNames(),
|
|
93
|
+
error: (e: Error) => {
|
|
94
|
+
this.getLogger().error(`Failed on create function for request body generate, snippet is [${this.getBodyGenerateSnippet()}].`);
|
|
95
|
+
throw e;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
this._responseGenerateSnippet = options.responseGenerate;
|
|
99
|
+
this._responseGenerateFunc = Utils.createAsyncFunction(this.getResponseGenerateSnippet(), {
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
101
|
+
createDefault: () => async ($response: Response, _$factor: InFragment, _$request: PipelineStepData<In>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers) => {
|
|
102
|
+
return await $response.json();
|
|
103
|
+
},
|
|
104
|
+
getVariableNames: () => this.getResponseGenerateVariableName(),
|
|
105
|
+
error: (e: Error) => {
|
|
106
|
+
this.getLogger().error(`Failed on create function for response generate, snippet is [${this.getResponseGenerateSnippet()}].`);
|
|
107
|
+
throw e;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
if (options.responseErrorHandles) {
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
112
|
+
const defaultHandler = async (options: HttpErrorHandleOptions<In, InFragment>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Promise<OutFragment> | never => {
|
|
113
|
+
throw new UncatchableError(ERR_FETCH_ERROR, `Error[${options.$errorCode}] caught when fetch data from remote[${options.$url}].`);
|
|
114
|
+
};
|
|
115
|
+
const createDefaultHandler = () => defaultHandler;
|
|
116
|
+
const getVariableNames = () => this.getErrorHandlerVariableName();
|
|
117
|
+
const handlers: Record<HttpErrorCode, HttpHandleError<In, InFragment, OutFragment>> = Object.keys(options.responseErrorHandles).reduce((handlers, status) => {
|
|
118
|
+
handlers[status] = Utils.createAsyncFunction(options.responseErrorHandles[status], {
|
|
119
|
+
createDefault: createDefaultHandler, getVariableNames,
|
|
120
|
+
error: (e: Error) => {
|
|
121
|
+
this.getLogger().error(`Failed on create function for response error handler[${status}], snippet is [${options.responseErrorHandles[status]}].`);
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
return handlers;
|
|
126
|
+
}, {});
|
|
127
|
+
this._responseErrorHandleFunc = async ($options: HttpErrorHandleOptions<In, InFragment>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers): Promise<OutFragment> | never => {
|
|
128
|
+
const {$errorCode} = $options;
|
|
129
|
+
const givenErrorHandler = handlers[$errorCode];
|
|
130
|
+
if (givenErrorHandler == null) {
|
|
131
|
+
return await defaultHandler($options, $helpers, $);
|
|
132
|
+
} else {
|
|
133
|
+
return await givenErrorHandler($options, $helpers, $);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
} else {
|
|
137
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
138
|
+
this._responseErrorHandleFunc = async ($options: HttpErrorHandleOptions<In, InFragment>, _$helpers: PipelineStepHelpers, _$: PipelineStepHelpers): Promise<OutFragment> | never => {
|
|
139
|
+
throw new UncatchableError(ERR_FETCH_ERROR, `Error[${$options.$errorCode}] caught when fetch data from remote[${$options.$url}].`);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
public getEndpointSystemCode(): string {
|
|
145
|
+
return this._endpointSystemCode;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public getEndpointName(): string {
|
|
149
|
+
return this._endpointName;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public getEndpointKey(): string {
|
|
153
|
+
return `${this.getEndpointSystemCode()}.${this.getEndpointName()}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
public getEndpointUrl(): string {
|
|
157
|
+
return this._endpointUrl;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
public getEndpointMethod(): string {
|
|
161
|
+
return this._endpointMethod;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
public getEndpointHeaders(): Record<string, string> {
|
|
165
|
+
return this._endpointHeaders;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
protected generateEndpointHeaders(headers?: string, base?: Record<string, string>): Record<string, string> {
|
|
169
|
+
return `${headers || ''}`.split(';')
|
|
170
|
+
.map(x => x.trim())
|
|
171
|
+
.filter(x => x.length !== 0)
|
|
172
|
+
.map(x => x.split('='))
|
|
173
|
+
.map(([key, value]) => [key.trim(), value])
|
|
174
|
+
.filter(([key]) => key.length !== 0)
|
|
175
|
+
.reduce((map, [key, value]) => {
|
|
176
|
+
map[key] = value;
|
|
177
|
+
return map;
|
|
178
|
+
}, base ?? {});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
public getEndpointTimeout(): number {
|
|
182
|
+
return this._endpointTimeout;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
public needTimeout(): boolean {
|
|
186
|
+
return this._endpointTimeout > 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
public getUrlGenerateSnippet(): ScriptFuncOrBody<HttpGenerateUrl<In, InFragment>> {
|
|
190
|
+
return this._urlGenerateSnippet;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* override this method when want to use another variable name rather than "$endpointUrl", "$factor", "$request"
|
|
195
|
+
*/
|
|
196
|
+
protected getUrlGenerateVariableName(): Array<string> {
|
|
197
|
+
return ['$endpointUrl', '$factor', '$request', ...this.getHelpersVariableNames()];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
public getHeadersGenerateSnippet(): ScriptFuncOrBody<HttpGenerateHeaders<In, InFragment>> {
|
|
201
|
+
return this._headersGenerateSnippet;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* override this method when want to use another variable name rather than "$endpointUrl", "$factor", "$request"
|
|
206
|
+
*/
|
|
207
|
+
protected getHeadersGenerateVariableNames(): Array<string> {
|
|
208
|
+
return ['$factor', '$request', ...this.getHelpersVariableNames()];
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
protected isBodyUsed(): boolean {
|
|
212
|
+
return this._bodyUsed;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
public getBodyGenerateSnippet(): ScriptFuncOrBody<HttpGenerateBody<In, InFragment>> {
|
|
216
|
+
return this._bodyGenerateSnippet;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* override this method when want to use another variable name rather than "$endpointUrl", "$factor", "$request"
|
|
221
|
+
*/
|
|
222
|
+
protected getBodyGenerateVariableNames(): Array<string> {
|
|
223
|
+
return ['$factor', '$request', ...this.getHelpersVariableNames()];
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
public getResponseGenerateSnippet(): ScriptFuncOrBody<HttpGenerateResponse<In, InFragment>> {
|
|
227
|
+
return this._responseGenerateSnippet;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
protected getResponseGenerateVariableName(): Array<string> {
|
|
231
|
+
return ['$response', '$factor', '$request', ...this.getHelpersVariableNames()];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
protected getErrorHandlerVariableName(): Array<string> {
|
|
235
|
+
return ['$options', ...this.getHelpersVariableNames()];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
protected async doPerform(data: InFragment, request: PipelineStepData<In>): Promise<OutFragment> {
|
|
239
|
+
const $helpers = this.getHelpers();
|
|
240
|
+
let url = '';
|
|
241
|
+
try {
|
|
242
|
+
url = this._urlGenerateFunc(this.getEndpointUrl(), data, request, $helpers, $helpers);
|
|
243
|
+
const method = this.getEndpointMethod();
|
|
244
|
+
const staticHeaders = this.getEndpointHeaders() ?? {};
|
|
245
|
+
const headers = this._headersGenerateFunc(data, request, $helpers, $helpers) ?? {};
|
|
246
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
247
|
+
let body: any;
|
|
248
|
+
if (this.isBodyUsed()) {
|
|
249
|
+
body = this._bodyGenerateFunc(data, request, $helpers, $helpers);
|
|
250
|
+
} else {
|
|
251
|
+
body = (void 0);
|
|
252
|
+
}
|
|
253
|
+
if (body != null && typeof body !== 'string') {
|
|
254
|
+
body = JSON.stringify(body);
|
|
255
|
+
}
|
|
256
|
+
const response = await fetch(url, {
|
|
257
|
+
method, headers: {...staticHeaders, ...headers}, body,
|
|
258
|
+
signal: this.needTimeout() ? (() => {
|
|
259
|
+
const controller = new AbortController();
|
|
260
|
+
setTimeout(() => controller.abort(), this.getEndpointTimeout());
|
|
261
|
+
return controller.signal;
|
|
262
|
+
})() : (void 0)
|
|
263
|
+
});
|
|
264
|
+
const status = response.status;
|
|
265
|
+
if (status >= 400) {
|
|
266
|
+
return await this._responseErrorHandleFunc({
|
|
267
|
+
$url: url, $factor: data, $request: request, $response: response,
|
|
268
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
269
|
+
// @ts-ignore
|
|
270
|
+
$errorCode: `${status}`
|
|
271
|
+
}, $helpers, $helpers);
|
|
272
|
+
} else {
|
|
273
|
+
return await this._responseGenerateFunc(response, data, request, $helpers, $helpers);
|
|
274
|
+
}
|
|
275
|
+
} catch (e) {
|
|
276
|
+
if (e instanceof DOMException || e.name === 'AbortError') {
|
|
277
|
+
return await this._responseErrorHandleFunc({
|
|
278
|
+
$url: url, $factor: data, $request: request, $errorCode: HttpAbortErrorCode
|
|
279
|
+
}, $helpers, $helpers);
|
|
280
|
+
} else if (e instanceof UncatchableError) {
|
|
281
|
+
// uncatchable error is thrown manually, do not handle it again.
|
|
282
|
+
throw e;
|
|
283
|
+
} else {
|
|
284
|
+
return await this._responseErrorHandleFunc({
|
|
285
|
+
$url: url, $factor: data, $request: request, $errorCode: HttpUnknownErrorCode
|
|
286
|
+
}, $helpers, $helpers);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {PipelineStepData, PipelineStepHelpers, Undefinable} from '@rainbow-o23/n1';
|
|
2
|
+
import {Response} from 'node-fetch';
|
|
3
|
+
|
|
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;
|
|
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>>;
|
|
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;
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
export type HttpGenerateResponse<In, InFragment> = ($response: Response, $factor: InFragment, $request: PipelineStepData<In>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<any>;
|
|
12
|
+
|
|
13
|
+
export type HttpUnpredictedErrorCode = `0${number}`;
|
|
14
|
+
export const HttpUnknownErrorCode: HttpUnpredictedErrorCode = '000';
|
|
15
|
+
export type HttpClientErrorCode = `4${number}`;
|
|
16
|
+
export type HttpServerErrorCode = `5${number}`;
|
|
17
|
+
export type HttpCustomizedErrorCode = `6${number}`;
|
|
18
|
+
export const HttpAbortErrorCode: HttpCustomizedErrorCode = '600';
|
|
19
|
+
export type HttpErrorCode =
|
|
20
|
+
HttpUnpredictedErrorCode
|
|
21
|
+
| HttpClientErrorCode
|
|
22
|
+
| HttpServerErrorCode
|
|
23
|
+
| HttpCustomizedErrorCode;
|
|
24
|
+
|
|
25
|
+
export interface HttpErrorHandleOptions<In, InFragment> {
|
|
26
|
+
$errorCode: HttpErrorCode;
|
|
27
|
+
$url: string;
|
|
28
|
+
$response?: Response;
|
|
29
|
+
$factor: InFragment;
|
|
30
|
+
$request: PipelineStepData<In>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type HttpHandleError<In, InFragment, OutFragment> = (options: HttpErrorHandleOptions<In, InFragment>, $helpers: PipelineStepHelpers, $: PipelineStepHelpers) => Promise<OutFragment> | never;
|