creo-flow-extensions-base 1.0.0-dev.3 → 1.0.0-dev.5
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/lib/config/dev/index.js +1 -1
- package/lib/config/index.js +1 -1
- package/lib/config/prod/index.js +1 -1
- package/lib/config/qa/index.js +1 -1
- package/lib/context/index.d.ts +4 -3
- package/lib/context/index.js +28 -7
- package/lib/createExtension/index.d.ts +1 -32
- package/lib/createExtension/index.js +48 -25
- package/lib/enum/index.d.ts +5 -0
- package/lib/enum/index.js +7 -1
- package/lib/error/ErrorLevels.d.ts +4 -0
- package/lib/error/ErrorLevels.js +8 -0
- package/lib/error/index.d.ts +5 -2
- package/lib/error/index.js +13 -5
- package/lib/error/interface.d.ts +6 -0
- package/lib/error/interface.js +2 -0
- package/lib/executionContext/index.d.ts +4 -0
- package/lib/executionContext/index.js +10 -0
- package/lib/interfaces/index.d.ts +27 -0
- package/lib/interfaces/index.js +2 -0
- package/lib/logger/index.js +6 -2
- package/package.json +1 -1
package/lib/config/dev/index.js
CHANGED
package/lib/config/index.js
CHANGED
package/lib/config/prod/index.js
CHANGED
package/lib/config/qa/index.js
CHANGED
package/lib/context/index.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { IDeleteContextToRedis, IGetContextFromRedis, IGetContextFromS3, IWriteContext, IWriteContextToRedis, IWriteContextToS3 } from './interfaces';
|
|
2
|
-
|
|
2
|
+
import { ExecutionContext } from "../executionContext";
|
|
3
|
+
export declare class ContextService {
|
|
4
|
+
private context;
|
|
5
|
+
constructor(context: ExecutionContext);
|
|
3
6
|
getContext({ methodProps, token, urls }: {
|
|
4
7
|
methodProps: any;
|
|
5
8
|
token: string;
|
|
@@ -14,5 +17,3 @@ declare class ContextService {
|
|
|
14
17
|
sendContextToS3({ data, url }: IWriteContextToS3): Promise<void>;
|
|
15
18
|
deleteContextFromRedis({ contextKey, token }: IDeleteContextToRedis): Promise<void>;
|
|
16
19
|
}
|
|
17
|
-
export declare const contextService: ContextService;
|
|
18
|
-
export {};
|
package/lib/context/index.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
6
|
+
exports.ContextService = void 0;
|
|
7
7
|
const api_1 = require("../api");
|
|
8
8
|
const utils_1 = require("./utils");
|
|
9
9
|
const axios_1 = __importDefault(require("axios"));
|
|
@@ -11,8 +11,13 @@ const error_1 = require("../error");
|
|
|
11
11
|
const v8_1 = require("v8");
|
|
12
12
|
const zlib_1 = __importDefault(require("zlib"));
|
|
13
13
|
const logger_1 = require("../logger");
|
|
14
|
+
const ErrorLevels_1 = require("../error/ErrorLevels");
|
|
14
15
|
const redisSizeLimit = 1024 * 100; // 0.1mb
|
|
15
16
|
class ContextService {
|
|
17
|
+
context;
|
|
18
|
+
constructor(context) {
|
|
19
|
+
this.context = context;
|
|
20
|
+
}
|
|
16
21
|
async getContext({ methodProps, token, urls }) {
|
|
17
22
|
try {
|
|
18
23
|
const contextReadKeys = (0, utils_1.getContextKeys)(methodProps);
|
|
@@ -43,15 +48,23 @@ class ContextService {
|
|
|
43
48
|
});
|
|
44
49
|
return context;
|
|
45
50
|
}
|
|
46
|
-
catch (
|
|
51
|
+
catch (error) {
|
|
47
52
|
logger_1.logger.error({
|
|
48
53
|
message: 'Error while getting context',
|
|
49
54
|
location: this.constructor.name,
|
|
50
55
|
payload: {
|
|
51
|
-
errorMessage:
|
|
56
|
+
errorMessage: error.message,
|
|
52
57
|
}
|
|
53
58
|
});
|
|
54
|
-
|
|
59
|
+
const isCaughtError = error instanceof error_1.ExecutionError;
|
|
60
|
+
if (isCaughtError) {
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
throw new error_1.ExecutionError({
|
|
64
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
65
|
+
message: `Something went wrong while getting context`,
|
|
66
|
+
nodeName: this.context.nodeTitle
|
|
67
|
+
});
|
|
55
68
|
}
|
|
56
69
|
}
|
|
57
70
|
async writeContext({ contextKey, data, token, url }) {
|
|
@@ -94,7 +107,11 @@ class ContextService {
|
|
|
94
107
|
errorMessage: e.message,
|
|
95
108
|
}
|
|
96
109
|
});
|
|
97
|
-
throw new error_1.
|
|
110
|
+
throw new error_1.ExecutionError({
|
|
111
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
112
|
+
message: `Error while writing context by key ${contextKey}`,
|
|
113
|
+
nodeName: this.context.nodeTitle
|
|
114
|
+
});
|
|
98
115
|
}
|
|
99
116
|
}
|
|
100
117
|
async getContextByKeys({ contextKeys, token, urls }) {
|
|
@@ -146,7 +163,11 @@ class ContextService {
|
|
|
146
163
|
});
|
|
147
164
|
return result;
|
|
148
165
|
}
|
|
149
|
-
throw
|
|
166
|
+
throw new error_1.ExecutionError({
|
|
167
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
168
|
+
message: `Error while getting context by key ${contextKey}`,
|
|
169
|
+
nodeName: this.context.nodeTitle
|
|
170
|
+
});
|
|
150
171
|
}
|
|
151
172
|
}
|
|
152
173
|
async getContextFromRedis({ contextKey, token }) {
|
|
@@ -175,4 +196,4 @@ class ContextService {
|
|
|
175
196
|
await api_1.ownApi.delete(`flows/${flowId}/contexts/${flowContextId}/${contextKey}`, { headers: { 'context-token': token } });
|
|
176
197
|
}
|
|
177
198
|
}
|
|
178
|
-
exports.
|
|
199
|
+
exports.ContextService = ContextService;
|
|
@@ -1,36 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { INodeDescriptor } from '../nodes';
|
|
3
|
-
import { ExtensionTypes } from '../enum';
|
|
4
|
-
export interface ICreateExtensionParams {
|
|
5
|
-
nodes: INodeDescriptor[];
|
|
6
|
-
connections?: IConnectionSchema[];
|
|
7
|
-
title: string;
|
|
8
|
-
icon: string;
|
|
9
|
-
type: ExtensionTypes;
|
|
10
|
-
}
|
|
11
|
-
declare enum EventTypes {
|
|
12
|
-
EXECUTE = "execute",
|
|
13
|
-
GET_EXTENSION_INFO = "getExtensionInfo",
|
|
14
|
-
GET_FULL_NODE = "getFullNode"
|
|
15
|
-
}
|
|
16
|
-
interface IEvent {
|
|
17
|
-
method?: {
|
|
18
|
-
name: string;
|
|
19
|
-
uri: string;
|
|
20
|
-
databaseName: string;
|
|
21
|
-
collectionName: string;
|
|
22
|
-
query?: string;
|
|
23
|
-
};
|
|
24
|
-
writeContextUrl: string;
|
|
25
|
-
readContextUrls: {
|
|
26
|
-
[contextKey: string]: string;
|
|
27
|
-
};
|
|
28
|
-
type: EventTypes;
|
|
29
|
-
contextKey: string;
|
|
30
|
-
flowContextToken: string;
|
|
31
|
-
}
|
|
1
|
+
import { ICreateExtensionParams, IEvent } from "../interfaces";
|
|
32
2
|
export declare const createExtension: (extension: ICreateExtensionParams) => (event: IEvent) => Promise<{
|
|
33
3
|
statusCode: number;
|
|
34
4
|
body: string;
|
|
35
5
|
}>;
|
|
36
|
-
export {};
|
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createExtension = void 0;
|
|
4
|
-
const enum_1 = require("../
|
|
5
|
-
const
|
|
4
|
+
const enum_1 = require("../enum");
|
|
5
|
+
const enum_2 = require("../fields/enum");
|
|
6
6
|
const error_1 = require("../error");
|
|
7
7
|
const logger_1 = require("../logger");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
EventTypes["GET_EXTENSION_INFO"] = "getExtensionInfo";
|
|
12
|
-
EventTypes["GET_FULL_NODE"] = "getFullNode";
|
|
13
|
-
})(EventTypes || (EventTypes = {}));
|
|
8
|
+
const ErrorLevels_1 = require("../error/ErrorLevels");
|
|
9
|
+
const context_1 = require("../context");
|
|
10
|
+
const executionContext_1 = require("../executionContext");
|
|
14
11
|
// @TODO: add dependencies between fields for smart rendering. Now on each field change we need to get node info;
|
|
15
12
|
// Disable node select fields without options.
|
|
16
13
|
const createExtension = (extension) => async (event) => {
|
|
14
|
+
const executionContext = new executionContext_1.ExecutionContext(event.nodeTitle);
|
|
15
|
+
const contextService = new context_1.ContextService(executionContext);
|
|
17
16
|
try {
|
|
18
17
|
const EventCallbacks = {
|
|
19
|
-
[EventTypes.EXECUTE]: execute,
|
|
20
|
-
[EventTypes.GET_EXTENSION_INFO]: getExtensionInfo,
|
|
21
|
-
[EventTypes.GET_FULL_NODE]: getFullNode,
|
|
18
|
+
[enum_1.EventTypes.EXECUTE]: (event) => execute(event, extension, contextService),
|
|
19
|
+
[enum_1.EventTypes.GET_EXTENSION_INFO]: getExtensionInfo,
|
|
20
|
+
[enum_1.EventTypes.GET_FULL_NODE]: getFullNode,
|
|
22
21
|
};
|
|
23
22
|
const eventCallback = EventCallbacks[event.type];
|
|
24
23
|
const result = await eventCallback(event, extension);
|
|
@@ -27,12 +26,12 @@ const createExtension = (extension) => async (event) => {
|
|
|
27
26
|
catch (e) {
|
|
28
27
|
return {
|
|
29
28
|
statusCode: 500,
|
|
30
|
-
body: JSON.stringify({ message: `An error occurred ${e.message}` })
|
|
29
|
+
body: JSON.stringify({ errorDetails: { message: `An error occurred ${e.message}` } })
|
|
31
30
|
};
|
|
32
31
|
}
|
|
33
32
|
};
|
|
34
33
|
exports.createExtension = createExtension;
|
|
35
|
-
const execute = async (event, extension) => {
|
|
34
|
+
const execute = async (event, extension, contextService) => {
|
|
36
35
|
try {
|
|
37
36
|
const methodProps = event?.method;
|
|
38
37
|
const method = methodProps?.name ? extension.nodes.find(({ name }) => name === methodProps.name) : undefined;
|
|
@@ -46,21 +45,29 @@ const execute = async (event, extension) => {
|
|
|
46
45
|
}
|
|
47
46
|
});
|
|
48
47
|
if (!method) {
|
|
49
|
-
throw new
|
|
48
|
+
throw new error_1.ExecutionError({
|
|
49
|
+
message: 'Unknown extension method',
|
|
50
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
51
|
+
nodeName: event.nodeTitle
|
|
52
|
+
});
|
|
50
53
|
}
|
|
51
54
|
;
|
|
52
55
|
const contextWriteKey = event?.contextKey;
|
|
53
56
|
if (!contextWriteKey) {
|
|
54
|
-
throw new
|
|
57
|
+
throw new error_1.ExecutionError({
|
|
58
|
+
message: 'Context key is not provided',
|
|
59
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
60
|
+
nodeName: event.nodeTitle
|
|
61
|
+
});
|
|
55
62
|
}
|
|
56
63
|
const contextToken = event?.flowContextToken;
|
|
57
64
|
if (!contextToken) {
|
|
58
65
|
throw new Error('Context token is not provided');
|
|
59
66
|
}
|
|
60
|
-
const context = await
|
|
67
|
+
const context = await contextService.getContext({ methodProps, token: event.flowContextToken, urls: readContextUrls });
|
|
61
68
|
const response = await method.func(methodProps, context);
|
|
62
69
|
if (writeContextUrl && event.flowContextToken) {
|
|
63
|
-
await
|
|
70
|
+
await contextService.writeContext({ contextKey: contextWriteKey, data: response, token: event.flowContextToken, url: writeContextUrl });
|
|
64
71
|
}
|
|
65
72
|
logger_1.logger.info({
|
|
66
73
|
message: `Extension successfully executed, extension: ${extension.title}`,
|
|
@@ -82,14 +89,20 @@ const execute = async (event, extension) => {
|
|
|
82
89
|
}
|
|
83
90
|
});
|
|
84
91
|
// TODO: Use for better error handling.
|
|
85
|
-
const isCaughtError = error instanceof error_1.
|
|
92
|
+
const isCaughtError = error instanceof error_1.ExecutionError;
|
|
86
93
|
const statusCode = isCaughtError ? 400 : 500;
|
|
87
|
-
const
|
|
94
|
+
const errorDetails = isCaughtError
|
|
95
|
+
? error.getErrorDetails()
|
|
96
|
+
: {
|
|
97
|
+
message: `An execution error occurred`,
|
|
98
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
99
|
+
nodeName: event.nodeTitle
|
|
100
|
+
};
|
|
88
101
|
const logs = [...logger_1.logger.getLogs()];
|
|
89
102
|
logger_1.logger.clearLogs();
|
|
90
103
|
return {
|
|
91
104
|
statusCode,
|
|
92
|
-
body: JSON.stringify({
|
|
105
|
+
body: JSON.stringify({ errorDetails, logs }),
|
|
93
106
|
};
|
|
94
107
|
}
|
|
95
108
|
};
|
|
@@ -104,10 +117,14 @@ const getFullNode = async (event, extension) => {
|
|
|
104
117
|
const methodProps = event?.method;
|
|
105
118
|
const method = methodProps?.name ? extension.nodes.find(({ name }) => name === methodProps.name) : undefined;
|
|
106
119
|
if (!method) {
|
|
107
|
-
throw new error_1.
|
|
120
|
+
throw new error_1.ExecutionError({
|
|
121
|
+
message: 'Unknown extension method',
|
|
122
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
123
|
+
nodeName: event.nodeTitle
|
|
124
|
+
});
|
|
108
125
|
}
|
|
109
126
|
const fullFields = await Promise.all(method.fields.map(async (field) => {
|
|
110
|
-
const isSelect = field.type ===
|
|
127
|
+
const isSelect = field.type === enum_2.FieldTypes.SELECT;
|
|
111
128
|
if (!isSelect)
|
|
112
129
|
return field;
|
|
113
130
|
const options = field?.getOptions ? await field.getOptions(methodProps) : [];
|
|
@@ -119,12 +136,18 @@ const getFullNode = async (event, extension) => {
|
|
|
119
136
|
};
|
|
120
137
|
}
|
|
121
138
|
catch (error) {
|
|
122
|
-
const isCaughtError = error instanceof error_1.
|
|
139
|
+
const isCaughtError = error instanceof error_1.ExecutionError;
|
|
123
140
|
const statusCode = isCaughtError ? 400 : 500;
|
|
124
|
-
const
|
|
141
|
+
const errorDetails = isCaughtError
|
|
142
|
+
? error.getErrorDetails()
|
|
143
|
+
: {
|
|
144
|
+
message: 'An receiving node info error occurred',
|
|
145
|
+
errorLevel: ErrorLevels_1.ErrorLevels.BASE_LIB_LEVEL,
|
|
146
|
+
nodeName: event.nodeTitle
|
|
147
|
+
};
|
|
125
148
|
return {
|
|
126
149
|
statusCode,
|
|
127
|
-
body: JSON.stringify({
|
|
150
|
+
body: JSON.stringify({ errorDetails }),
|
|
128
151
|
};
|
|
129
152
|
}
|
|
130
153
|
};
|
package/lib/enum/index.d.ts
CHANGED
package/lib/enum/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ExtensionTypes = void 0;
|
|
3
|
+
exports.EventTypes = exports.ExtensionTypes = void 0;
|
|
4
4
|
var ExtensionTypes;
|
|
5
5
|
(function (ExtensionTypes) {
|
|
6
6
|
ExtensionTypes["DB_CONNECTION"] = "db_connection";
|
|
@@ -9,3 +9,9 @@ var ExtensionTypes;
|
|
|
9
9
|
ExtensionTypes["PARALLEL"] = "parallel";
|
|
10
10
|
ExtensionTypes["CODE"] = "code";
|
|
11
11
|
})(ExtensionTypes || (exports.ExtensionTypes = ExtensionTypes = {}));
|
|
12
|
+
var EventTypes;
|
|
13
|
+
(function (EventTypes) {
|
|
14
|
+
EventTypes["EXECUTE"] = "execute";
|
|
15
|
+
EventTypes["GET_EXTENSION_INFO"] = "getExtensionInfo";
|
|
16
|
+
EventTypes["GET_FULL_NODE"] = "getFullNode";
|
|
17
|
+
})(EventTypes || (exports.EventTypes = EventTypes = {}));
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ErrorLevels = void 0;
|
|
4
|
+
var ErrorLevels;
|
|
5
|
+
(function (ErrorLevels) {
|
|
6
|
+
ErrorLevels["BASE_LIB_LEVEL"] = "baseLibLevel";
|
|
7
|
+
ErrorLevels["EXTENSION_LEVEL"] = "extensionLevel";
|
|
8
|
+
})(ErrorLevels || (exports.ErrorLevels = ErrorLevels = {}));
|
package/lib/error/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { IExecutionError } from "./interface";
|
|
2
|
+
export declare class ExecutionError extends Error {
|
|
3
|
+
errorInfo: IExecutionError;
|
|
4
|
+
constructor(props: IExecutionError);
|
|
5
|
+
getErrorDetails(): IExecutionError;
|
|
3
6
|
}
|
package/lib/error/index.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
class
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
exports.ExecutionError = void 0;
|
|
4
|
+
class ExecutionError extends Error {
|
|
5
|
+
errorInfo;
|
|
6
|
+
constructor(props) {
|
|
7
|
+
super(props.message);
|
|
7
8
|
this.name = this.constructor.name;
|
|
9
|
+
this.errorInfo = props;
|
|
10
|
+
if (Error.captureStackTrace) {
|
|
11
|
+
Error.captureStackTrace(this, this.constructor);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
getErrorDetails() {
|
|
15
|
+
return this.errorInfo;
|
|
8
16
|
}
|
|
9
17
|
}
|
|
10
|
-
exports.
|
|
18
|
+
exports.ExecutionError = ExecutionError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExecutionContext = void 0;
|
|
4
|
+
class ExecutionContext {
|
|
5
|
+
nodeTitle;
|
|
6
|
+
constructor(nodeTitle) {
|
|
7
|
+
this.nodeTitle = nodeTitle;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.ExecutionContext = ExecutionContext;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { INodeDescriptor } from "../nodes";
|
|
2
|
+
import { IConnectionSchema } from "../connection";
|
|
3
|
+
import { EventTypes, ExtensionTypes } from "../enum";
|
|
4
|
+
export interface ICreateExtensionParams {
|
|
5
|
+
nodes: INodeDescriptor[];
|
|
6
|
+
connections?: IConnectionSchema[];
|
|
7
|
+
title: string;
|
|
8
|
+
icon: string;
|
|
9
|
+
type: ExtensionTypes;
|
|
10
|
+
}
|
|
11
|
+
export interface IEvent {
|
|
12
|
+
method?: {
|
|
13
|
+
name: string;
|
|
14
|
+
uri: string;
|
|
15
|
+
databaseName: string;
|
|
16
|
+
collectionName: string;
|
|
17
|
+
query?: string;
|
|
18
|
+
};
|
|
19
|
+
writeContextUrl: string;
|
|
20
|
+
readContextUrls: {
|
|
21
|
+
[contextKey: string]: string;
|
|
22
|
+
};
|
|
23
|
+
type: EventTypes;
|
|
24
|
+
contextKey: string;
|
|
25
|
+
flowContextToken: string;
|
|
26
|
+
nodeTitle: string;
|
|
27
|
+
}
|
package/lib/logger/index.js
CHANGED
|
@@ -13,10 +13,14 @@ class Logger {
|
|
|
13
13
|
this.logs = [];
|
|
14
14
|
}
|
|
15
15
|
info({ location, message, payload }) {
|
|
16
|
-
|
|
16
|
+
const logData = { level: LogLevels.INFO, location, message, payload, timestamp: this.getCurrentTimestamp() };
|
|
17
|
+
console.log(logData);
|
|
18
|
+
this.logs.push(logData);
|
|
17
19
|
}
|
|
18
20
|
error({ location, message, payload }) {
|
|
19
|
-
|
|
21
|
+
const logData = { level: LogLevels.ERROR, location, message, payload, timestamp: this.getCurrentTimestamp() };
|
|
22
|
+
console.error(logData);
|
|
23
|
+
this.logs.push(logData);
|
|
20
24
|
}
|
|
21
25
|
getLogs() {
|
|
22
26
|
return this.logs;
|