@byted-apaas/server-sdk-node 0.0.1-beta.3
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/LICENSE +7 -0
- package/NOTICE +3 -0
- package/README.md +1 -0
- package/application/application.d.ts +27 -0
- package/application/application.js +2 -0
- package/application/application.ts +40 -0
- package/application/function/function.d.ts +14 -0
- package/application/function/function.js +21 -0
- package/application/function/function.ts +37 -0
- package/application/impl/common.d.ts +2 -0
- package/application/impl/common.js +50 -0
- package/application/impl/common.ts +50 -0
- package/application/impl/impl.d.ts +16 -0
- package/application/impl/impl.js +38 -0
- package/application/impl/impl.ts +48 -0
- package/constants/constants.d.ts +10 -0
- package/constants/constants.js +15 -0
- package/constants/constants.ts +15 -0
- package/context/context.d.ts +74 -0
- package/context/context.js +25 -0
- package/context/context.ts +130 -0
- package/context/db/db.d.ts +73 -0
- package/context/db/db.js +4 -0
- package/context/db/db.ts +80 -0
- package/context/db/impl/IObject.d.ts +251 -0
- package/context/db/impl/IObject.js +5 -0
- package/context/db/impl/IObject.ts +247 -0
- package/context/db/impl/db.d.ts +61 -0
- package/context/db/impl/db.js +86 -0
- package/context/db/impl/db.ts +95 -0
- package/context/db/impl/object.d.ts +92 -0
- package/context/db/impl/object.js +524 -0
- package/context/db/impl/object.ts +631 -0
- package/context/db/impl/oql/ioql.d.ts +6 -0
- package/context/db/impl/oql/ioql.js +4 -0
- package/context/db/impl/oql/ioql.ts +9 -0
- package/context/db/impl/oql/oql.d.ts +8 -0
- package/context/db/impl/oql/oql.js +21 -0
- package/context/db/impl/oql/oql.ts +25 -0
- package/context/db/impl/order.d.ts +9 -0
- package/context/db/impl/order.js +15 -0
- package/context/db/impl/order.ts +19 -0
- package/context/db/impl/propertiesStore.d.ts +8 -0
- package/context/db/impl/propertiesStore.js +29 -0
- package/context/db/impl/propertiesStore.ts +31 -0
- package/context/db/impl/queryBuilder.d.ts +29 -0
- package/context/db/impl/queryBuilder.js +78 -0
- package/context/db/impl/queryBuilder.ts +106 -0
- package/context/db/impl/transaction/index.d.ts +20 -0
- package/context/db/impl/transaction/index.js +277 -0
- package/context/db/impl/transaction/index.ts +340 -0
- package/context/db/impl/transaction/operation.d.ts +27 -0
- package/context/db/impl/transaction/operation.js +69 -0
- package/context/db/impl/transaction/operation.ts +72 -0
- package/context/db/impl/transaction.d.ts +67 -0
- package/context/db/impl/transaction.js +4 -0
- package/context/db/impl/transaction.ts +79 -0
- package/context/globalConfig/globalConfig.d.ts +7 -0
- package/context/globalConfig/globalConfig.js +16 -0
- package/context/globalConfig/globalConfig.ts +14 -0
- package/context/metadata/components/common.d.ts +6 -0
- package/context/metadata/components/common.js +17 -0
- package/context/metadata/components/common.ts +22 -0
- package/context/metadata/components/components.d.ts +12 -0
- package/context/metadata/components/components.js +19 -0
- package/context/metadata/components/components.ts +22 -0
- package/context/metadata/components/desktop/list.d.ts +16 -0
- package/context/metadata/components/desktop/list.js +402 -0
- package/context/metadata/components/desktop/list.ts +457 -0
- package/context/metadata/components/desktop/recordDetail.d.ts +11 -0
- package/context/metadata/components/desktop/recordDetail.js +53 -0
- package/context/metadata/components/desktop/recordDetail.ts +67 -0
- package/context/metadata/components/mobile/list.d.ts +24 -0
- package/context/metadata/components/mobile/list.js +145 -0
- package/context/metadata/components/mobile/list.ts +179 -0
- package/context/metadata/metadata.d.ts +61 -0
- package/context/metadata/metadata.js +103 -0
- package/context/metadata/metadata.ts +178 -0
- package/context/metadata/objects/fields.d.ts +130 -0
- package/context/metadata/objects/fields.js +334 -0
- package/context/metadata/objects/fields.ts +457 -0
- package/context/metadata/objects/fields.util.d.ts +40 -0
- package/context/metadata/objects/fields.util.js +319 -0
- package/context/metadata/objects/fields.util.ts +360 -0
- package/context/metadata/objects/objects.d.ts +11 -0
- package/context/metadata/objects/objects.js +20 -0
- package/context/metadata/objects/objects.ts +30 -0
- package/context/metadata/types/common.d.ts +6 -0
- package/context/metadata/types/common.js +9 -0
- package/context/metadata/types/common.ts +13 -0
- package/context/metadata/types/components.d.ts +90 -0
- package/context/metadata/types/components.js +4 -0
- package/context/metadata/types/components.ts +108 -0
- package/context/metadata/types/objects.d.ts +226 -0
- package/context/metadata/types/objects.js +4 -0
- package/context/metadata/types/objects.ts +254 -0
- package/context/msg/msg.d.ts +38 -0
- package/context/msg/msg.js +35 -0
- package/context/msg/msg.ts +52 -0
- package/context/resources/IResources.d.ts +66 -0
- package/context/resources/IResources.js +2 -0
- package/context/resources/IResources.ts +73 -0
- package/context/resources/impl/resources.d.ts +42 -0
- package/context/resources/impl/resources.js +150 -0
- package/context/resources/impl/resources.ts +173 -0
- package/context/tasks/tasks.d.ts +33 -0
- package/context/tasks/tasks.js +139 -0
- package/context/tasks/tasks.ts +159 -0
- package/data/index.d.ts +27 -0
- package/data/index.js +4 -0
- package/data/index.ts +30 -0
- package/hooks/api.d.ts +61 -0
- package/hooks/api.js +327 -0
- package/hooks/api.ts +341 -0
- package/hooks/hooks.d.ts +1 -0
- package/hooks/hooks.js +88 -0
- package/hooks/hooks.ts +100 -0
- package/kunlun/kunlun.d.ts +55 -0
- package/kunlun/kunlun.js +58 -0
- package/kunlun/kunlun.ts +72 -0
- package/kunlun/operator/IOperator.d.ts +158 -0
- package/kunlun/operator/IOperator.js +4 -0
- package/kunlun/operator/IOperator.ts +166 -0
- package/kunlun/operator/impl/expression.d.ts +47 -0
- package/kunlun/operator/impl/expression.js +56 -0
- package/kunlun/operator/impl/expression.ts +74 -0
- package/kunlun/operator/impl/logic.d.ts +50 -0
- package/kunlun/operator/impl/logic.js +509 -0
- package/kunlun/operator/impl/logic.ts +598 -0
- package/kunlun/operator/impl/logicV2.d.ts +58 -0
- package/kunlun/operator/impl/logicV2.js +200 -0
- package/kunlun/operator/impl/logicV2.ts +254 -0
- package/kunlun/operator/impl/operator.d.ts +179 -0
- package/kunlun/operator/impl/operator.js +222 -0
- package/kunlun/operator/impl/operator.ts +258 -0
- package/kunlun/operator/impl/operatorV2.d.ts +180 -0
- package/kunlun/operator/impl/operatorV2.js +216 -0
- package/kunlun/operator/impl/operatorV2.ts +249 -0
- package/lib/core.d.ts +4 -0
- package/lib/core.js +19 -0
- package/lib/core.ts +7 -0
- package/package.json +50 -0
- package/request/common.d.ts +13 -0
- package/request/common.js +87 -0
- package/request/common.ts +91 -0
- package/request/constants.d.ts +10 -0
- package/request/constants.js +25 -0
- package/request/constants.ts +25 -0
- package/request/faasinfra.d.ts +8 -0
- package/request/faasinfra.js +358 -0
- package/request/faasinfra.ts +393 -0
- package/request/innerapi.d.ts +67 -0
- package/request/innerapi.js +1412 -0
- package/request/innerapi.ts +1648 -0
- package/request/interface.d.ts +95 -0
- package/request/interface.js +41 -0
- package/request/interface.ts +169 -0
- package/request/openapi.d.ts +69 -0
- package/request/openapi.js +1014 -0
- package/request/openapi.ts +1223 -0
- package/types/types.d.ts +79 -0
- package/types/types.js +4 -0
- package/types/types.ts +108 -0
package/hooks/api.ts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
import * as common from "@byted-apaas/server-common-node";
|
|
2
|
+
import {IGfCtx} from "@byted-apaas/server-common-node/context/IContext";
|
|
3
|
+
import {InvalidParamError} from "@byted-apaas/server-common-node/utils/exceptions";
|
|
4
|
+
|
|
5
|
+
import * as api from "../hooks/api";
|
|
6
|
+
import * as metadataApi from '../context/metadata/metadata'
|
|
7
|
+
|
|
8
|
+
import {Tasks} from "../context/tasks/tasks";
|
|
9
|
+
import {DB} from "../context/db/impl/db";
|
|
10
|
+
import {_Resources} from "../context/resources/impl/resources";
|
|
11
|
+
import {Message} from "../context/msg/msg";
|
|
12
|
+
import * as Request from "../request/interface"
|
|
13
|
+
import * as constants from "../constants/constants";
|
|
14
|
+
import {getRequireFunc} from "@byted-apaas/server-common-node/utils/utils";
|
|
15
|
+
import {IMetaData, MetaData} from "../context/metadata/metadata";
|
|
16
|
+
import {
|
|
17
|
+
ExecuteOptions,
|
|
18
|
+
ExecutionInfo,
|
|
19
|
+
ExecutionResult,
|
|
20
|
+
FlowTaskInfo,
|
|
21
|
+
RevokeExecutionOptions
|
|
22
|
+
} from "@byted-apaas/server-common-node/context/workflow/workflow";
|
|
23
|
+
|
|
24
|
+
const nodeCls = require("node-cls");
|
|
25
|
+
const nodeClsCtx = common.nodeClsCtx;
|
|
26
|
+
const exceptions = common.exceptions;
|
|
27
|
+
const utils = common.utils;
|
|
28
|
+
const checkUtils = common.checkUtils;
|
|
29
|
+
const functionSources = common.constants.functionSources;
|
|
30
|
+
|
|
31
|
+
type invokeGlobalFuncT = (funcNames: string) => { invoke: (params?: any) => Promise<any>; }
|
|
32
|
+
|
|
33
|
+
// 同步调用云函数
|
|
34
|
+
export function getFunctionSync(context?: any, logger?: common.Logger): invokeGlobalFuncT {
|
|
35
|
+
return (funcName: string) => {
|
|
36
|
+
if (utils.isOnlineDev()) {
|
|
37
|
+
let oldFuncName = funcName;
|
|
38
|
+
if (!utils.isMicroservice()) {
|
|
39
|
+
funcName = utils.handleFuncName(null, funcName);
|
|
40
|
+
if (!funcName) {
|
|
41
|
+
throw new exceptions.InvalidParamError(`The function (${oldFuncName}) does not exist, please confirm ${oldFuncName} exists and has been deployed.`);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
funcName = undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let idOrName: any = {};
|
|
48
|
+
// 2.0 通过 apiAlias 触发
|
|
49
|
+
if (funcName === oldFuncName) {
|
|
50
|
+
idOrName = {
|
|
51
|
+
APIName: funcName,
|
|
52
|
+
isInvokeByAPIName: true,
|
|
53
|
+
}
|
|
54
|
+
} else if (utils.isMicroservice()) {
|
|
55
|
+
// 微服务
|
|
56
|
+
idOrName = {
|
|
57
|
+
APIName: oldFuncName,
|
|
58
|
+
isInvokeByAPIName: true,
|
|
59
|
+
}
|
|
60
|
+
} else { // 1.0 通过 apiID 触发
|
|
61
|
+
idOrName = {
|
|
62
|
+
APIId: funcName,
|
|
63
|
+
isInvokeByAPIName: false,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
invoke: async (params?: any) => {
|
|
69
|
+
// 微服务的场景
|
|
70
|
+
if (utils.isMicroservice()) {
|
|
71
|
+
return await Request.GetInstance().invokeFuncSync(idOrName, params)
|
|
72
|
+
} else {
|
|
73
|
+
let func = utils.getGlobalFunc(null, funcName);
|
|
74
|
+
if (!func) {
|
|
75
|
+
// 改为远程调用
|
|
76
|
+
return await Request.GetInstance().invokeFuncSync(idOrName, params)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
let loopMasks = [funcName];
|
|
80
|
+
let reqCtx = nodeCls.get("loopCtx");
|
|
81
|
+
if (reqCtx && reqCtx.loopMasks && Array.isArray(reqCtx.loopMasks)) {
|
|
82
|
+
if (reqCtx.loopMasks.includes(funcName)) {
|
|
83
|
+
throw new exceptions.InvalidParamError(`Task execution forms a loop.`);
|
|
84
|
+
}
|
|
85
|
+
loopMasks.push(...reqCtx.loopMasks);
|
|
86
|
+
}
|
|
87
|
+
// 修改 source 全局函数
|
|
88
|
+
let ctx = nodeCls.create("loopCtx");
|
|
89
|
+
ctx.loopMasks = loopMasks;
|
|
90
|
+
return await ctx.run(async () => {
|
|
91
|
+
const dynCtx = nodeClsCtx.getDynCtx();
|
|
92
|
+
dynCtx.source = functionSources.globalFunction;
|
|
93
|
+
return await dynCtx.run(async () => {
|
|
94
|
+
return await common.hooks.invoke(func, params, context as any, logger);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return {
|
|
103
|
+
invoke: async (params?: any) => {
|
|
104
|
+
let func = getRequireFunc()(funcName);
|
|
105
|
+
if (!func) {
|
|
106
|
+
throw new exceptions.InvalidParamError(`The function (${funcName}) does not exist.`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let loopMasks = [funcName];
|
|
110
|
+
let reqCtx = nodeCls.get("loopCtx");
|
|
111
|
+
if (reqCtx && reqCtx.loopMasks && Array.isArray(reqCtx.loopMasks)) {
|
|
112
|
+
if (reqCtx.loopMasks.includes(funcName)) {
|
|
113
|
+
throw new exceptions.InvalidParamError(`Task execution forms a loop.`);
|
|
114
|
+
}
|
|
115
|
+
loopMasks.push(...reqCtx.loopMasks);
|
|
116
|
+
}
|
|
117
|
+
// 修改 source 全局函数
|
|
118
|
+
let ctx = nodeCls.create("loopCtx");
|
|
119
|
+
ctx.loopMasks = loopMasks;
|
|
120
|
+
return await ctx.run(async () => {
|
|
121
|
+
const dynCtx = nodeClsCtx.getDynCtx();
|
|
122
|
+
dynCtx.source = functionSources.globalFunction;
|
|
123
|
+
return await dynCtx.run(async () => {
|
|
124
|
+
return await common.hooks.invoke(func, params, context as any, logger);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 设置请求头
|
|
133
|
+
export function setRespHeader() {
|
|
134
|
+
return function () {
|
|
135
|
+
switch (arguments.length) {
|
|
136
|
+
case 1:
|
|
137
|
+
if (checkUtils.isObject(arguments[0])) {
|
|
138
|
+
try {
|
|
139
|
+
for (let k of Object.keys(arguments[0])) {
|
|
140
|
+
utils.setHttpHeaders(k, toString(arguments[0][k]));
|
|
141
|
+
}
|
|
142
|
+
} catch (err) {
|
|
143
|
+
throw new exceptions.InternalError(err.message);
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
throw new exceptions.InvalidParamError("setResponseHeaders param is not a object");
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
case 2:
|
|
150
|
+
try {
|
|
151
|
+
utils.setHttpHeaders(arguments[0], toString(arguments[1]));
|
|
152
|
+
} catch (err) {
|
|
153
|
+
throw new exceptions.InternalError(err.message);
|
|
154
|
+
}
|
|
155
|
+
break;
|
|
156
|
+
default:
|
|
157
|
+
throw new exceptions.InvalidParamError("the number of setResponseHeaders param is 1 or 2");
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export function toString(value: any) {
|
|
163
|
+
if (value instanceof Array) {
|
|
164
|
+
let arr = [];
|
|
165
|
+
for (let v of value) {
|
|
166
|
+
if (typeof v === "string") {
|
|
167
|
+
arr.push(v);
|
|
168
|
+
} else {
|
|
169
|
+
arr.push(utils.stringify(v));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
return arr.join(", ");
|
|
173
|
+
} else if (typeof value === "string") {
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
return utils.stringify(value);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export function setRedirectLocation(context: any) {
|
|
180
|
+
return function (location: string) {
|
|
181
|
+
context.setResponseHeader(constants.httpHeaderKeys.location, location);
|
|
182
|
+
context.setResponseHeader(constants.httpHeaderKeys.statusCode, String(constants.httpStatusCodes.redirect));
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// 请求方法
|
|
187
|
+
export function method() {
|
|
188
|
+
return utils.getHttpMethod();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export function mountMetadata(context: any) {
|
|
192
|
+
// 1.挂载metadata
|
|
193
|
+
context.metadata = metadataApi.metadata(context);
|
|
194
|
+
|
|
195
|
+
// 2.挂载metaType
|
|
196
|
+
if (!global.kunlun) {
|
|
197
|
+
(global as any).kunlun = {};
|
|
198
|
+
}
|
|
199
|
+
(global as any).kunlun.metaType = metadataApi.metaType();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function mountWorkflowFeature(context: any) {
|
|
203
|
+
if (context && context.workflow) {
|
|
204
|
+
context.workflow.updateVariable = common.updateVariable(context, Request.GetInstance().updateWorkflowVariables);
|
|
205
|
+
} else {
|
|
206
|
+
context.workflow = {};
|
|
207
|
+
}
|
|
208
|
+
// 此处挂载保证了 terminate 存在
|
|
209
|
+
context.workflow.terminate = terminateWorkflow();
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export function terminateWorkflow() {
|
|
213
|
+
return async (workflowInstanceId: number, options: any) => {
|
|
214
|
+
let operator: number = utils.getUser()._id, reason: string = null;
|
|
215
|
+
if (options && options.reason) {
|
|
216
|
+
// instanceof 改动5
|
|
217
|
+
// if (options.reason instanceof common.fieldType.Multilingual) {
|
|
218
|
+
if (common.fieldType.isKFieldTypeMultilingual(options.reason)) {
|
|
219
|
+
reason = options.reason.toString();
|
|
220
|
+
} else {
|
|
221
|
+
reason = new common.fieldType.Multilingual(options.reason).toString();
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
await Request.GetInstance().terminateWorkflowInstance(workflowInstanceId, operator, reason);
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function mockFlowFeature(context: any) {
|
|
230
|
+
if (!context.flow) {
|
|
231
|
+
context.flow = {};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
context.flow.updateVariable = common.updateVariable(context, Request.GetInstance().updateWorkflowVariables);
|
|
235
|
+
|
|
236
|
+
context.flow.getExecutionUserTaskInfo = mockGetExecutionUserTaskInfo();
|
|
237
|
+
|
|
238
|
+
context.flow.execute = mockExecuteFlow();
|
|
239
|
+
context.flow.revokeExecution = mockRevokeExecution();
|
|
240
|
+
context.flow.getExecutionInfo = mockGetExecutionInfo();
|
|
241
|
+
return context.flow;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
function mockGetExecutionUserTaskInfo() {
|
|
245
|
+
return async (executionId: bigint): Promise<FlowTaskInfo[]> => {
|
|
246
|
+
if (!executionId || typeof executionId !== "number") {
|
|
247
|
+
throw new InvalidParamError(`param's type (${executionId}) need number, but is ${typeof executionId}`);
|
|
248
|
+
}
|
|
249
|
+
return await Request.GetInstance().getExecutionUserTaskInfo(executionId);
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function mockExecuteFlow() {
|
|
254
|
+
return async (APIName: string, options?: ExecuteOptions): Promise<ExecutionResult> => {
|
|
255
|
+
if (!APIName || typeof APIName !== "string") {
|
|
256
|
+
throw new InvalidParamError(`param's type (${APIName}) need string, but is ${typeof APIName}`);
|
|
257
|
+
}
|
|
258
|
+
if (options && options.params && checkUtils.isNotObject(options.params)) {
|
|
259
|
+
throw new InvalidParamError(`options.params's type (${options}) need object, but is ${Array.isArray(options) ? "array" : typeof options.params}`);
|
|
260
|
+
}
|
|
261
|
+
if (!options) {
|
|
262
|
+
options = {} as any;
|
|
263
|
+
}
|
|
264
|
+
return await Request.GetInstance().executeFlow(APIName, options);
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function mockRevokeExecution() {
|
|
269
|
+
return async (executionId: number, options: RevokeExecutionOptions): Promise<void> => {
|
|
270
|
+
if (!executionId || typeof executionId !== "number") {
|
|
271
|
+
throw new InvalidParamError(`param's type (${executionId}) is need number, but is ${typeof executionId}`);
|
|
272
|
+
}
|
|
273
|
+
return await Request.GetInstance().revokeExecution(executionId, options);
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function mockGetExecutionInfo() {
|
|
278
|
+
return async (executionId: number): Promise<ExecutionInfo> => {
|
|
279
|
+
if (!executionId || typeof executionId !== "number") {
|
|
280
|
+
throw new InvalidParamError(`param's type (${executionId}) is not matched to number, but is ${typeof executionId}`);
|
|
281
|
+
}
|
|
282
|
+
return await Request.GetInstance().getExecutionInfo(executionId);
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function invokeMicroservice() {
|
|
287
|
+
/**
|
|
288
|
+
* 微服务
|
|
289
|
+
* @param apiName 微服务的名称
|
|
290
|
+
*/
|
|
291
|
+
return function (apiName: string) {
|
|
292
|
+
if (!apiName || typeof (apiName) !== "string") {
|
|
293
|
+
throw new InvalidParamError(`The apiName (${apiName}) is invalid and should be a non-empty field string.`);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return {
|
|
297
|
+
/**
|
|
298
|
+
* 同步调用微服务
|
|
299
|
+
* @param path 请求路径,如:/ping
|
|
300
|
+
* @param method 请求方法,默认为 GET
|
|
301
|
+
* @param params 请求参数,默认为 undefined
|
|
302
|
+
*/
|
|
303
|
+
invoke: async function (path: string, method: string = "GET", params: any = undefined) {
|
|
304
|
+
if (!path || typeof (path) !== "string") {
|
|
305
|
+
throw new InvalidParamError(`The path (${path}) is invalid and should be a non-empty field string.`);
|
|
306
|
+
}
|
|
307
|
+
if (!method || typeof (method) !== "string") {
|
|
308
|
+
throw new InvalidParamError(`The method (${method}) is invalid and should be a non-empty field string.`);
|
|
309
|
+
}
|
|
310
|
+
return await Request.GetFaaSInfraInstance().invokeMicroserviceSync(apiName, path, method, params);
|
|
311
|
+
},
|
|
312
|
+
/**
|
|
313
|
+
* 异步调用微服务
|
|
314
|
+
* @param path 请求路径,如:/ping
|
|
315
|
+
* @param method 请求方法,默认为 GET
|
|
316
|
+
* @param params 请求参数,默认为 undefined
|
|
317
|
+
*/
|
|
318
|
+
invokeAsync: async function (path: string, method: string = "GET", params: any = undefined) {
|
|
319
|
+
if (!path || typeof (path) !== "string") {
|
|
320
|
+
throw new InvalidParamError(`The path (${path}) is invalid and should be a non-empty field string.`);
|
|
321
|
+
}
|
|
322
|
+
if (!method || typeof (method) !== "string") {
|
|
323
|
+
throw new InvalidParamError(`The method (${method}) is invalid and should be a non-empty field string.`);
|
|
324
|
+
}
|
|
325
|
+
return await Request.GetFaaSInfraInstance().invokeMicroserviceAsync(apiName, path, method, params);
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
export const db = new DB<IGfCtx>();
|
|
332
|
+
export const resources = new _Resources();
|
|
333
|
+
export const msg = new Message();
|
|
334
|
+
export const tasks = new Tasks();
|
|
335
|
+
export const cloudfunction = api.getFunctionSync();
|
|
336
|
+
export const microservice = api.invokeMicroservice();
|
|
337
|
+
export const workflow = {
|
|
338
|
+
terminate: terminateWorkflow(),
|
|
339
|
+
};
|
|
340
|
+
export const metaData: IMetaData<{}> = new MetaData<{}>(null)
|
|
341
|
+
export {Application} from "../application/impl/impl";
|
package/hooks/hooks.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/hooks/hooks.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const resources_1 = require("../context/resources/impl/resources");
|
|
4
|
+
const tasks_1 = require("../context/tasks/tasks");
|
|
5
|
+
const msg_1 = require("../context/msg/msg");
|
|
6
|
+
const server_common_node_1 = require("@byted-apaas/server-common-node");
|
|
7
|
+
const api = require("./api");
|
|
8
|
+
const operator_1 = require("../kunlun/operator/impl/operator");
|
|
9
|
+
const constants_1 = require("@byted-apaas/server-common-node/constants/constants");
|
|
10
|
+
const db_1 = require("../context/db/impl/db");
|
|
11
|
+
const common_1 = require("../request/common");
|
|
12
|
+
/**
|
|
13
|
+
* 根据 key 获取对应的全局变量
|
|
14
|
+
* @param key 全局变量的 key
|
|
15
|
+
*/
|
|
16
|
+
function getVar(key) {
|
|
17
|
+
return server_common_node_1.utils.getGlobalVariableByKey(key);
|
|
18
|
+
}
|
|
19
|
+
function pre(params, context, logger, requireFunc) {
|
|
20
|
+
// 初始化 kunlun
|
|
21
|
+
if (!global.kunlun) {
|
|
22
|
+
// 缺省的情况,先对 kunlun 进行初始化
|
|
23
|
+
global.kunlun = {};
|
|
24
|
+
}
|
|
25
|
+
global.kunlun.operator = new operator_1.Operator();
|
|
26
|
+
global.kunlun.getVar = getVar;
|
|
27
|
+
global.kunlun.tool.getTenantInfo = common_1.getTenantInfo;
|
|
28
|
+
// 对 context 下的能力进行初始化
|
|
29
|
+
mountContext(context, logger, requireFunc);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* mountContext 函数用于初始化 context 在 core 包中实现的部分,包括:
|
|
33
|
+
* - db
|
|
34
|
+
* - (global function 场景下): setRespHeader, setRedirectLocation
|
|
35
|
+
* - task, function, msg, resources
|
|
36
|
+
* @param context 系统代理函数中经由 common 创建的用户 context,根据 source 分为不同类型
|
|
37
|
+
* @param logger Logger
|
|
38
|
+
* @param requireFunc
|
|
39
|
+
*/
|
|
40
|
+
function mountContext(context, logger, requireFunc) {
|
|
41
|
+
const source = server_common_node_1.utils.getSource();
|
|
42
|
+
// 1. 根据不同调用场景差异化初始化内容
|
|
43
|
+
// - db 是否包含 currentObject,transaction 是否包含 currentObject
|
|
44
|
+
// - global function 场景需要初始化 setRespHeader 和 RedirectLocation 方法,以及 method 属性
|
|
45
|
+
switch (source) {
|
|
46
|
+
case constants_1.functionSources.workflow:
|
|
47
|
+
context.db = new db_1.DBWithCurrentObject(context.objectApiName);
|
|
48
|
+
break;
|
|
49
|
+
case constants_1.functionSources.globalFunction:
|
|
50
|
+
context.db = new db_1.DBWithCurrentObject(null);
|
|
51
|
+
// set response headers
|
|
52
|
+
context.setResponseHeader = api.setRespHeader();
|
|
53
|
+
context.setRedirectLocation = api.setRedirectLocation(context);
|
|
54
|
+
// 暴露context.method属性
|
|
55
|
+
context.method = server_common_node_1.utils.getHttpMethod();
|
|
56
|
+
break;
|
|
57
|
+
case constants_1.functionSources.recordAutomation:
|
|
58
|
+
context.db = new db_1.DBWithCurrentObject(context.objectApiName);
|
|
59
|
+
break;
|
|
60
|
+
case constants_1.functionSources.scheduleAutomation:
|
|
61
|
+
context.db = new db_1.DBWithCurrentObject(null);
|
|
62
|
+
break;
|
|
63
|
+
default:
|
|
64
|
+
throw new server_common_node_1.exceptions.InvalidParamError(`This npm package does not contain this source ${source}`);
|
|
65
|
+
}
|
|
66
|
+
// 2. 初始化 core 包中需要初始化的内容,因此强转为 IContext 类型
|
|
67
|
+
// - function
|
|
68
|
+
// - tasks
|
|
69
|
+
// - msg
|
|
70
|
+
// - resources
|
|
71
|
+
// - microservice
|
|
72
|
+
// - getVar
|
|
73
|
+
context.function = api.getFunctionSync(context, logger);
|
|
74
|
+
context.tasks = new tasks_1.Tasks(context.function);
|
|
75
|
+
context.msg = new msg_1.Message();
|
|
76
|
+
context.resources = new resources_1._Resources();
|
|
77
|
+
context.getVar = getVar;
|
|
78
|
+
context.microservice = api.invokeMicroservice();
|
|
79
|
+
api.mountWorkflowFeature(context);
|
|
80
|
+
// 挂载 metadata
|
|
81
|
+
api.mountMetadata(context);
|
|
82
|
+
api.mockFlowFeature(context);
|
|
83
|
+
// 防止 context.function 被串改
|
|
84
|
+
Object.freeze(context.function);
|
|
85
|
+
}
|
|
86
|
+
module.exports = {
|
|
87
|
+
pre,
|
|
88
|
+
};
|
package/hooks/hooks.ts
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import * as common from "@byted-apaas/server-common-node";
|
|
2
|
+
import { IContext } from "../context/context";
|
|
3
|
+
import { _Resources } from "../context/resources/impl/resources";
|
|
4
|
+
import { Tasks } from "../context/tasks/tasks";
|
|
5
|
+
import { Message } from "../context/msg/msg";
|
|
6
|
+
import { exceptions, utils } from '@byted-apaas/server-common-node'
|
|
7
|
+
import * as api from './api'
|
|
8
|
+
import { Operator } from "../kunlun/operator/impl/operator";
|
|
9
|
+
import { functionSources } from "@byted-apaas/server-common-node/constants/constants";
|
|
10
|
+
import { IWfCtx, IGfCtx, IRaCtx, ISaCtx } from '@byted-apaas/server-common-node/context/IContext'
|
|
11
|
+
import { DBWithCurrentObject } from "../context/db/impl/db";
|
|
12
|
+
import { getTenantInfo } from "../request/common";
|
|
13
|
+
|
|
14
|
+
type AllICtx = IGfCtx | IWfCtx | IRaCtx | ISaCtx;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 根据 key 获取对应的全局变量
|
|
18
|
+
* @param key 全局变量的 key
|
|
19
|
+
*/
|
|
20
|
+
function getVar(key: string): string | number | number[] {
|
|
21
|
+
return utils.getGlobalVariableByKey(key);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function pre(params: any, context: AllICtx, logger: common.Logger, requireFunc: Function) {
|
|
25
|
+
// 初始化 kunlun
|
|
26
|
+
if (!global.kunlun) {
|
|
27
|
+
// 缺省的情况,先对 kunlun 进行初始化
|
|
28
|
+
(global as any).kunlun = {}
|
|
29
|
+
}
|
|
30
|
+
global.kunlun.operator = new Operator();
|
|
31
|
+
((global.kunlun as any).getVar as any) = getVar;
|
|
32
|
+
((global.kunlun as any).tool as any).getTenantInfo = getTenantInfo as any;
|
|
33
|
+
// 对 context 下的能力进行初始化
|
|
34
|
+
mountContext(context, logger, requireFunc);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* mountContext 函数用于初始化 context 在 core 包中实现的部分,包括:
|
|
39
|
+
* - db
|
|
40
|
+
* - (global function 场景下): setRespHeader, setRedirectLocation
|
|
41
|
+
* - task, function, msg, resources
|
|
42
|
+
* @param context 系统代理函数中经由 common 创建的用户 context,根据 source 分为不同类型
|
|
43
|
+
* @param logger Logger
|
|
44
|
+
* @param requireFunc
|
|
45
|
+
*/
|
|
46
|
+
function mountContext(context: AllICtx, logger: common.Logger, requireFunc: Function) {
|
|
47
|
+
const source: string = utils.getSource();
|
|
48
|
+
|
|
49
|
+
// 1. 根据不同调用场景差异化初始化内容
|
|
50
|
+
// - db 是否包含 currentObject,transaction 是否包含 currentObject
|
|
51
|
+
// - global function 场景需要初始化 setRespHeader 和 RedirectLocation 方法,以及 method 属性
|
|
52
|
+
switch (source) {
|
|
53
|
+
case functionSources.workflow:
|
|
54
|
+
(context as IContext<IWfCtx> & IWfCtx).db = new DBWithCurrentObject<IWfCtx>((context as IWfCtx).objectApiName)
|
|
55
|
+
break;
|
|
56
|
+
case functionSources.globalFunction:
|
|
57
|
+
(context as IContext<IGfCtx> & IGfCtx).db = new DBWithCurrentObject<IGfCtx>(null);
|
|
58
|
+
|
|
59
|
+
// set response headers
|
|
60
|
+
(context as IGfCtx).setResponseHeader = api.setRespHeader();
|
|
61
|
+
(context as IGfCtx).setRedirectLocation = api.setRedirectLocation(context as IGfCtx);
|
|
62
|
+
|
|
63
|
+
// 暴露context.method属性
|
|
64
|
+
(context as IGfCtx).method = utils.getHttpMethod();
|
|
65
|
+
break;
|
|
66
|
+
case functionSources.recordAutomation:
|
|
67
|
+
(context as IContext<IRaCtx> & IRaCtx).db = new DBWithCurrentObject<IRaCtx>((context as IRaCtx).objectApiName)
|
|
68
|
+
break;
|
|
69
|
+
case functionSources.scheduleAutomation:
|
|
70
|
+
(context as IContext<ISaCtx> & ISaCtx).db = new DBWithCurrentObject<ISaCtx>(null)
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
throw new exceptions.InvalidParamError(`This npm package does not contain this source ${source}`)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 2. 初始化 core 包中需要初始化的内容,因此强转为 IContext 类型
|
|
77
|
+
// - function
|
|
78
|
+
// - tasks
|
|
79
|
+
// - msg
|
|
80
|
+
// - resources
|
|
81
|
+
// - microservice
|
|
82
|
+
// - getVar
|
|
83
|
+
(context as unknown as IContext<null>).function = api.getFunctionSync(context, logger);
|
|
84
|
+
(context as unknown as IContext<null>).tasks = new Tasks((context as unknown as IContext<null>).function);
|
|
85
|
+
(context as unknown as IContext<null>).msg = new Message();
|
|
86
|
+
(context as unknown as IContext<null>).resources = new _Resources();
|
|
87
|
+
((context as unknown as IContext<null>).getVar as any) = getVar;
|
|
88
|
+
(context as any).microservice = api.invokeMicroservice();
|
|
89
|
+
api.mountWorkflowFeature(context);
|
|
90
|
+
// 挂载 metadata
|
|
91
|
+
api.mountMetadata(context);
|
|
92
|
+
api.mockFlowFeature(context as any);
|
|
93
|
+
|
|
94
|
+
// 防止 context.function 被串改
|
|
95
|
+
Object.freeze((context as unknown as IContext<null>).function);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = {
|
|
99
|
+
pre,
|
|
100
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { IOperator } from './operator/IOperator';
|
|
2
|
+
import { GMTOffset as GMT } from "@byted-apaas/server-common-node/constants/timeZone";
|
|
3
|
+
import { area2code } from "@byted-apaas/server-common-node/constants/countryCode";
|
|
4
|
+
import { ITool, IToolsExtra } from "@byted-apaas/server-common-node/kunlun/tool/Itool";
|
|
5
|
+
import { errCodes } from "@byted-apaas/server-common-node/utils/exceptions";
|
|
6
|
+
import * as common from "@byted-apaas/server-common-node";
|
|
7
|
+
declare global {
|
|
8
|
+
export namespace kunlun {
|
|
9
|
+
/**
|
|
10
|
+
* 指令操作符
|
|
11
|
+
*/
|
|
12
|
+
let operator: IOperator;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export declare namespace kunlun {
|
|
16
|
+
/**
|
|
17
|
+
* 引用时区信息
|
|
18
|
+
*/
|
|
19
|
+
const GMTOffset: typeof GMT;
|
|
20
|
+
/**
|
|
21
|
+
* 引用国际区号
|
|
22
|
+
*/
|
|
23
|
+
const countryCode: typeof area2code;
|
|
24
|
+
/**
|
|
25
|
+
* 工具方法
|
|
26
|
+
*/
|
|
27
|
+
let tool: ITool & IToolsExtra;
|
|
28
|
+
/**
|
|
29
|
+
* 生成复杂数据结构
|
|
30
|
+
*/
|
|
31
|
+
namespace type {
|
|
32
|
+
export import Multilingual = common.fieldType.Multilingual;
|
|
33
|
+
export import Phone = common.fieldType.Phone;
|
|
34
|
+
export import Date = common.fieldType.Date;
|
|
35
|
+
export import DateTime = common.fieldType.DateTime;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 错误码
|
|
39
|
+
*/
|
|
40
|
+
const errorCode: {
|
|
41
|
+
/**
|
|
42
|
+
* 限流错误码
|
|
43
|
+
*/
|
|
44
|
+
RATE_LIMIT_ERROR: errCodes;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 根据 key 获取对应的全局变量
|
|
48
|
+
* @param key 全局变量的 key
|
|
49
|
+
*/
|
|
50
|
+
function getVar(key: string): Promise<string>;
|
|
51
|
+
/**
|
|
52
|
+
* 指令操作符
|
|
53
|
+
*/
|
|
54
|
+
const operator: IOperator;
|
|
55
|
+
}
|
package/kunlun/kunlun.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright 2022 ByteDance Ltd. and/or its affiliates
|
|
3
|
+
// SPDX-License-Identifier: MIT
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
exports.kunlun = void 0;
|
|
6
|
+
const timeZone_1 = require("@byted-apaas/server-common-node/constants/timeZone");
|
|
7
|
+
const countryCode_1 = require("@byted-apaas/server-common-node/constants/countryCode");
|
|
8
|
+
const tool_1 = require("@byted-apaas/server-common-node/kunlun/tool/impl/tool");
|
|
9
|
+
const exceptions_1 = require("@byted-apaas/server-common-node/utils/exceptions");
|
|
10
|
+
const operator_1 = require("./operator/impl/operator");
|
|
11
|
+
const common = require("@byted-apaas/server-common-node");
|
|
12
|
+
const Request = require("../request/interface");
|
|
13
|
+
var kunlun;
|
|
14
|
+
(function (kunlun) {
|
|
15
|
+
/**
|
|
16
|
+
* 引用时区信息
|
|
17
|
+
*/
|
|
18
|
+
kunlun.GMTOffset = timeZone_1.GMTOffset;
|
|
19
|
+
/**
|
|
20
|
+
* 引用国际区号
|
|
21
|
+
*/
|
|
22
|
+
kunlun.countryCode = countryCode_1.area2code;
|
|
23
|
+
/**
|
|
24
|
+
* 工具方法
|
|
25
|
+
*/
|
|
26
|
+
kunlun.tool = new tool_1.Tool();
|
|
27
|
+
/**
|
|
28
|
+
* 生成复杂数据结构
|
|
29
|
+
*/
|
|
30
|
+
let type;
|
|
31
|
+
(function (type) {
|
|
32
|
+
type.Multilingual = common.fieldType.Multilingual;
|
|
33
|
+
type.Phone = common.fieldType.Phone;
|
|
34
|
+
type.Date = common.fieldType.Date;
|
|
35
|
+
type.DateTime = common.fieldType.DateTime;
|
|
36
|
+
})(type = kunlun.type || (kunlun.type = {}));
|
|
37
|
+
/**
|
|
38
|
+
* 错误码
|
|
39
|
+
*/
|
|
40
|
+
kunlun.errorCode = {
|
|
41
|
+
/**
|
|
42
|
+
* 限流错误码
|
|
43
|
+
*/
|
|
44
|
+
RATE_LIMIT_ERROR: exceptions_1.errCodes.RATE_LIMIT_ERROR,
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* 根据 key 获取对应的全局变量
|
|
48
|
+
* @param key 全局变量的 key
|
|
49
|
+
*/
|
|
50
|
+
async function getVar(key) {
|
|
51
|
+
return await Request.GetInstance().getGlobalConfigByKey(key);
|
|
52
|
+
}
|
|
53
|
+
kunlun.getVar = getVar;
|
|
54
|
+
/**
|
|
55
|
+
* 指令操作符
|
|
56
|
+
*/
|
|
57
|
+
kunlun.operator = new operator_1.Operator();
|
|
58
|
+
})(kunlun = exports.kunlun || (exports.kunlun = {}));
|