@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.
Files changed (163) hide show
  1. package/LICENSE +7 -0
  2. package/NOTICE +3 -0
  3. package/README.md +1 -0
  4. package/application/application.d.ts +27 -0
  5. package/application/application.js +2 -0
  6. package/application/application.ts +40 -0
  7. package/application/function/function.d.ts +14 -0
  8. package/application/function/function.js +21 -0
  9. package/application/function/function.ts +37 -0
  10. package/application/impl/common.d.ts +2 -0
  11. package/application/impl/common.js +50 -0
  12. package/application/impl/common.ts +50 -0
  13. package/application/impl/impl.d.ts +16 -0
  14. package/application/impl/impl.js +38 -0
  15. package/application/impl/impl.ts +48 -0
  16. package/constants/constants.d.ts +10 -0
  17. package/constants/constants.js +15 -0
  18. package/constants/constants.ts +15 -0
  19. package/context/context.d.ts +74 -0
  20. package/context/context.js +25 -0
  21. package/context/context.ts +130 -0
  22. package/context/db/db.d.ts +73 -0
  23. package/context/db/db.js +4 -0
  24. package/context/db/db.ts +80 -0
  25. package/context/db/impl/IObject.d.ts +251 -0
  26. package/context/db/impl/IObject.js +5 -0
  27. package/context/db/impl/IObject.ts +247 -0
  28. package/context/db/impl/db.d.ts +61 -0
  29. package/context/db/impl/db.js +86 -0
  30. package/context/db/impl/db.ts +95 -0
  31. package/context/db/impl/object.d.ts +92 -0
  32. package/context/db/impl/object.js +524 -0
  33. package/context/db/impl/object.ts +631 -0
  34. package/context/db/impl/oql/ioql.d.ts +6 -0
  35. package/context/db/impl/oql/ioql.js +4 -0
  36. package/context/db/impl/oql/ioql.ts +9 -0
  37. package/context/db/impl/oql/oql.d.ts +8 -0
  38. package/context/db/impl/oql/oql.js +21 -0
  39. package/context/db/impl/oql/oql.ts +25 -0
  40. package/context/db/impl/order.d.ts +9 -0
  41. package/context/db/impl/order.js +15 -0
  42. package/context/db/impl/order.ts +19 -0
  43. package/context/db/impl/propertiesStore.d.ts +8 -0
  44. package/context/db/impl/propertiesStore.js +29 -0
  45. package/context/db/impl/propertiesStore.ts +31 -0
  46. package/context/db/impl/queryBuilder.d.ts +29 -0
  47. package/context/db/impl/queryBuilder.js +78 -0
  48. package/context/db/impl/queryBuilder.ts +106 -0
  49. package/context/db/impl/transaction/index.d.ts +20 -0
  50. package/context/db/impl/transaction/index.js +277 -0
  51. package/context/db/impl/transaction/index.ts +340 -0
  52. package/context/db/impl/transaction/operation.d.ts +27 -0
  53. package/context/db/impl/transaction/operation.js +69 -0
  54. package/context/db/impl/transaction/operation.ts +72 -0
  55. package/context/db/impl/transaction.d.ts +67 -0
  56. package/context/db/impl/transaction.js +4 -0
  57. package/context/db/impl/transaction.ts +79 -0
  58. package/context/globalConfig/globalConfig.d.ts +7 -0
  59. package/context/globalConfig/globalConfig.js +16 -0
  60. package/context/globalConfig/globalConfig.ts +14 -0
  61. package/context/metadata/components/common.d.ts +6 -0
  62. package/context/metadata/components/common.js +17 -0
  63. package/context/metadata/components/common.ts +22 -0
  64. package/context/metadata/components/components.d.ts +12 -0
  65. package/context/metadata/components/components.js +19 -0
  66. package/context/metadata/components/components.ts +22 -0
  67. package/context/metadata/components/desktop/list.d.ts +16 -0
  68. package/context/metadata/components/desktop/list.js +402 -0
  69. package/context/metadata/components/desktop/list.ts +457 -0
  70. package/context/metadata/components/desktop/recordDetail.d.ts +11 -0
  71. package/context/metadata/components/desktop/recordDetail.js +53 -0
  72. package/context/metadata/components/desktop/recordDetail.ts +67 -0
  73. package/context/metadata/components/mobile/list.d.ts +24 -0
  74. package/context/metadata/components/mobile/list.js +145 -0
  75. package/context/metadata/components/mobile/list.ts +179 -0
  76. package/context/metadata/metadata.d.ts +61 -0
  77. package/context/metadata/metadata.js +103 -0
  78. package/context/metadata/metadata.ts +178 -0
  79. package/context/metadata/objects/fields.d.ts +130 -0
  80. package/context/metadata/objects/fields.js +334 -0
  81. package/context/metadata/objects/fields.ts +457 -0
  82. package/context/metadata/objects/fields.util.d.ts +40 -0
  83. package/context/metadata/objects/fields.util.js +319 -0
  84. package/context/metadata/objects/fields.util.ts +360 -0
  85. package/context/metadata/objects/objects.d.ts +11 -0
  86. package/context/metadata/objects/objects.js +20 -0
  87. package/context/metadata/objects/objects.ts +30 -0
  88. package/context/metadata/types/common.d.ts +6 -0
  89. package/context/metadata/types/common.js +9 -0
  90. package/context/metadata/types/common.ts +13 -0
  91. package/context/metadata/types/components.d.ts +90 -0
  92. package/context/metadata/types/components.js +4 -0
  93. package/context/metadata/types/components.ts +108 -0
  94. package/context/metadata/types/objects.d.ts +226 -0
  95. package/context/metadata/types/objects.js +4 -0
  96. package/context/metadata/types/objects.ts +254 -0
  97. package/context/msg/msg.d.ts +38 -0
  98. package/context/msg/msg.js +35 -0
  99. package/context/msg/msg.ts +52 -0
  100. package/context/resources/IResources.d.ts +66 -0
  101. package/context/resources/IResources.js +2 -0
  102. package/context/resources/IResources.ts +73 -0
  103. package/context/resources/impl/resources.d.ts +42 -0
  104. package/context/resources/impl/resources.js +150 -0
  105. package/context/resources/impl/resources.ts +173 -0
  106. package/context/tasks/tasks.d.ts +33 -0
  107. package/context/tasks/tasks.js +139 -0
  108. package/context/tasks/tasks.ts +159 -0
  109. package/data/index.d.ts +27 -0
  110. package/data/index.js +4 -0
  111. package/data/index.ts +30 -0
  112. package/hooks/api.d.ts +61 -0
  113. package/hooks/api.js +327 -0
  114. package/hooks/api.ts +341 -0
  115. package/hooks/hooks.d.ts +1 -0
  116. package/hooks/hooks.js +88 -0
  117. package/hooks/hooks.ts +100 -0
  118. package/kunlun/kunlun.d.ts +55 -0
  119. package/kunlun/kunlun.js +58 -0
  120. package/kunlun/kunlun.ts +72 -0
  121. package/kunlun/operator/IOperator.d.ts +158 -0
  122. package/kunlun/operator/IOperator.js +4 -0
  123. package/kunlun/operator/IOperator.ts +166 -0
  124. package/kunlun/operator/impl/expression.d.ts +47 -0
  125. package/kunlun/operator/impl/expression.js +56 -0
  126. package/kunlun/operator/impl/expression.ts +74 -0
  127. package/kunlun/operator/impl/logic.d.ts +50 -0
  128. package/kunlun/operator/impl/logic.js +509 -0
  129. package/kunlun/operator/impl/logic.ts +598 -0
  130. package/kunlun/operator/impl/logicV2.d.ts +58 -0
  131. package/kunlun/operator/impl/logicV2.js +200 -0
  132. package/kunlun/operator/impl/logicV2.ts +254 -0
  133. package/kunlun/operator/impl/operator.d.ts +179 -0
  134. package/kunlun/operator/impl/operator.js +222 -0
  135. package/kunlun/operator/impl/operator.ts +258 -0
  136. package/kunlun/operator/impl/operatorV2.d.ts +180 -0
  137. package/kunlun/operator/impl/operatorV2.js +216 -0
  138. package/kunlun/operator/impl/operatorV2.ts +249 -0
  139. package/lib/core.d.ts +4 -0
  140. package/lib/core.js +19 -0
  141. package/lib/core.ts +7 -0
  142. package/package.json +50 -0
  143. package/request/common.d.ts +13 -0
  144. package/request/common.js +87 -0
  145. package/request/common.ts +91 -0
  146. package/request/constants.d.ts +10 -0
  147. package/request/constants.js +25 -0
  148. package/request/constants.ts +25 -0
  149. package/request/faasinfra.d.ts +8 -0
  150. package/request/faasinfra.js +358 -0
  151. package/request/faasinfra.ts +393 -0
  152. package/request/innerapi.d.ts +67 -0
  153. package/request/innerapi.js +1412 -0
  154. package/request/innerapi.ts +1648 -0
  155. package/request/interface.d.ts +95 -0
  156. package/request/interface.js +41 -0
  157. package/request/interface.ts +169 -0
  158. package/request/openapi.d.ts +69 -0
  159. package/request/openapi.js +1014 -0
  160. package/request/openapi.ts +1223 -0
  161. package/types/types.d.ts +79 -0
  162. package/types/types.js +4 -0
  163. package/types/types.ts +108 -0
@@ -0,0 +1,1223 @@
1
+ // Copyright 2022 ByteDance Ltd. and/or its affiliates
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import FormData = require("form-data");
5
+ import {Stream} from "stream";
6
+ import {notifyModel} from "../constants/constants";
7
+ import * as common from "@byted-apaas/server-common-node";
8
+ import {Order} from "../context/db/impl/order"
9
+ import {Criterion} from "../kunlun/operator/impl/logic";
10
+ import {openapiHttpPath as coreOapiHttpPath} from "./constants"
11
+ import {IInnerAPIRequest, OpenSDKGetRecordsReq, UploadFileResp} from "./interface";
12
+ import {headers} from "@byted-apaas/server-common-node/constants/constants";
13
+ import {
14
+ ExecutionInfo,
15
+ ExecutionResult,
16
+ FlowTaskInfo,
17
+ RevokeExecutionOptions
18
+ } from "@byted-apaas/server-common-node/context/workflow/workflow";
19
+ import {AppCtx} from "../application/application";
20
+ import {
21
+ getDebugExtraInfo,
22
+ getNamespaceForOpenAndFaaSSDK,
23
+ transFlowVariableToMap,
24
+ transMapToFlowVariable
25
+ } from "./common";
26
+
27
+ const nodeCls = require("node-cls");
28
+
29
+ const fs = require('fs');
30
+ const path = require('path');
31
+ const commonHttp = common.http;
32
+ const openapi = common.http.openapi;
33
+ const openapiHttpPath = common.http.openapiHttpPath;
34
+ const replaceKeys = common.http.replaceKeys;
35
+ const exceptions = common.exceptions;
36
+ const fieldType = common.fieldType;
37
+ const utils = common.utils;
38
+ const checkUtils = common.checkUtils;
39
+
40
+ export async function updateWorkflowVariables(ctx: any, instanceId: number, variables: object, variableTypes: object) {
41
+ let options = commonHttp.getOptions(null, openapiHttpPath.workflowUpdateVariablesV2);
42
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
43
+
44
+ // 2.请求
45
+ options.json = {
46
+ "operator": -1,
47
+ "workflowInstanceID": instanceId,
48
+ "variables": JSON.parse(variables as any),
49
+ "variableTypes": JSON.parse(variableTypes as any),
50
+ };
51
+
52
+ await openapi.doRequest(null, urlPath, options);
53
+ }
54
+
55
+ async function createRecordBySync(objectApiName: string, record: object) {
56
+ // 1.check
57
+ if (!objectApiName) {
58
+ throw new exceptions.InvalidParamError("objectApiName is empty");
59
+ }
60
+
61
+ // 2.获取 options
62
+ let options = commonHttp.getOptions(null, openapiHttpPath.createRecordBySyncV2);
63
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
64
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
65
+
66
+ // 3.请求
67
+ options.json = {
68
+ "operator": -1,
69
+ "data": record,
70
+ }
71
+
72
+ let task_id = utils.getTriggerTaskID();
73
+ if (task_id) {
74
+ options.json["task_id"] = task_id;
75
+ }
76
+
77
+ let data = await openapi.doRequest(null, urlPath, options)
78
+
79
+ if (data && data.record_id) {
80
+ return {_id: data.record_id};
81
+ }
82
+
83
+ if (data && data.recordID) {
84
+ return {_id: data.recordID};
85
+ }
86
+
87
+ return data;
88
+ }
89
+
90
+ async function updateRecordBySync(objectApiName: string, recordID: number, record: object) {
91
+ // 1.check
92
+ if (!objectApiName) {
93
+ throw new exceptions.InvalidParamError("objectApiName is empty");
94
+ }
95
+
96
+ // 2.获取 options
97
+ let options = commonHttp.getOptions(null, openapiHttpPath.updateRecordBySyncV2);
98
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
99
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
100
+
101
+ // 3.请求
102
+ options.json = {
103
+ "operator": -1,
104
+ "record_id": recordID,
105
+ "data": record,
106
+ }
107
+
108
+ let task_id = utils.getTriggerTaskID();
109
+ if (task_id) {
110
+ options.json["task_id"] = task_id;
111
+ }
112
+
113
+ return openapi.doRequest(null, urlPath, options);
114
+ }
115
+
116
+ async function deleteRecordBySync(objectApiName: string, recordID: number) {
117
+ // 1.check
118
+ if (!objectApiName) {
119
+ throw new exceptions.InvalidParamError("objectApiName is empty");
120
+ }
121
+
122
+ // 2.获取 options
123
+ let options = commonHttp.getOptions(null, openapiHttpPath.deleteRecordBySyncV2);
124
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
125
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
126
+
127
+ // 3.请求
128
+ options.json = {
129
+ "operator": -1,
130
+ "record_id": recordID,
131
+ }
132
+
133
+ let task_id = utils.getTriggerTaskID();
134
+ if (task_id) {
135
+ options.json["task_id"] = task_id;
136
+ }
137
+
138
+ return openapi.doRequest(null, urlPath, options);
139
+ }
140
+
141
+ async function createRecordsByAsync(objectApiName: string, records: object[]) {
142
+ // 1.获取 options
143
+ let options = commonHttp.getOptions(null, openapiHttpPath.createRecordsByAsyncV2);
144
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
145
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
146
+
147
+ // 2.请求
148
+ options.json = {
149
+ "operator": -1,
150
+ "data": records,
151
+ };
152
+
153
+ let task_id = utils.getTriggerTaskID();
154
+ if (task_id) {
155
+ options.json["automation_task_id"] = task_id;
156
+ }
157
+ return openapi.doRequest(null, urlPath, options);
158
+ }
159
+
160
+ async function updateRecordsByAsync(objectApiName: string, recordMap: Record<number, object>) {
161
+ // 1.获取 options
162
+ let options = commonHttp.getOptions(null, openapiHttpPath.updateRecordsByAsyncV2);
163
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
164
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
165
+
166
+ // 2.请求
167
+ options.json = {
168
+ "operator": -1,
169
+ "data": recordMap,
170
+ };
171
+
172
+ let task_id = utils.getTriggerTaskID();
173
+ if (task_id) {
174
+ options.json["automation_task_id"] = task_id;
175
+ }
176
+ return openapi.doRequest(null, urlPath, options);
177
+ }
178
+
179
+ async function deleteRecordsByAsync(objectApiName: string, recordIDs: number[]) {
180
+ // 1.获取 options
181
+ let options = commonHttp.getOptions(null, openapiHttpPath.deleteRecordsByAsyncV2);
182
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
183
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
184
+
185
+ // 2.请求
186
+ options.json = {
187
+ "operator": -1,
188
+ "record_id_list": recordIDs,
189
+ };
190
+
191
+ let task_id = utils.getTriggerTaskID();
192
+ if (task_id) {
193
+ options.json["automation_task_id"] = task_id;
194
+ }
195
+ return openapi.doRequest(null, urlPath, options);
196
+ }
197
+
198
+ async function createRecordsBySync(objectApiName: string, records: object[]) {
199
+ // 1.获取 options
200
+ let options = commonHttp.getOptions(null, openapiHttpPath.mCreateRecordsBySyncV2);
201
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
202
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
203
+
204
+ // 2.请求
205
+ options.json = {
206
+ "operator": -1,
207
+ "data": records,
208
+ };
209
+
210
+ let task_id = utils.getTriggerTaskID();
211
+ if (task_id) {
212
+ options.json["automation_task_id"] = task_id;
213
+ }
214
+ return openapi.doRequest(null, urlPath, options);
215
+ }
216
+
217
+ async function updateRecordsBySync(objectApiName: string, recordMap: Record<number, object>) {
218
+ // 1.获取 options
219
+ let options = commonHttp.getOptions(null, openapiHttpPath.mUpdateRecordsBySyncV2);
220
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
221
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
222
+
223
+ // 2.请求
224
+ options.json = {
225
+ "operator": -1,
226
+ "data": recordMap,
227
+ };
228
+
229
+ let task_id = utils.getTriggerTaskID();
230
+ if (task_id) {
231
+ options.json["automation_task_id"] = task_id;
232
+ }
233
+ return openapi.doRequest(null, urlPath, options);
234
+ }
235
+
236
+ async function deleteRecordsBySync(objectApiName: string, recordIDs: number[]) {
237
+ // 1.获取 options
238
+ let options = commonHttp.getOptions(null, openapiHttpPath.mDeleteRecordsBySyncV2);
239
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
240
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
241
+
242
+ // 2.请求
243
+ options.json = {
244
+ "operator": -1,
245
+ "record_id_list": recordIDs,
246
+ };
247
+
248
+ let task_id = utils.getTriggerTaskID();
249
+ if (task_id) {
250
+ options.json["automation_task_id"] = task_id;
251
+ }
252
+ return openapi.doRequest(null, urlPath, options);
253
+ }
254
+
255
+ function handleResponse(data: Record<string, unknown>, needCount: boolean) {
256
+ // 返回数据的特殊处理
257
+ if (needCount) {
258
+ if (checkUtils.isObject(data) && data["total"]) {
259
+ return data["total"]
260
+ }
261
+ return 0;
262
+ } else {
263
+ if (checkUtils.isObject(data) && data["data_list"]) {
264
+ return data["data_list"];
265
+ }
266
+ return [];
267
+ }
268
+ }
269
+
270
+ async function getRecordsOrCountByCriterion(objectApiName: string, criterion: string | Criterion, order: Order[], ignoreBackLookupField: boolean, fieldApiNames: string[], offset: number, limit: number, needCount: boolean) {
271
+ if (limit > 1000) {
272
+ limit = 1000;
273
+ }
274
+
275
+ // 1.check
276
+ if (!objectApiName) {
277
+ throw new exceptions.InternalError("objectApiName is empty");
278
+ }
279
+
280
+ // 2.获取 options
281
+ let options = commonHttp.getOptions(null, needCount ? openapiHttpPath.mGetCountByCriterionV2 : openapiHttpPath.mGetRecordByCriterionV2);
282
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
283
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
284
+
285
+ // 3.请求
286
+ options.json = {
287
+ "criterion": JSON.parse(criterion as any),
288
+ "order": order,
289
+ "ignore_back_lookup_field": ignoreBackLookupField,
290
+ "field_api_names": fieldApiNames,
291
+ "offset": offset,
292
+ "limit": limit,
293
+ "need_filter_user_permission": false,
294
+ "need_total_count": needCount,
295
+ };
296
+
297
+ let data = await openapi.doRequest(null, urlPath, options);
298
+ return handleResponse(data, needCount)
299
+ }
300
+
301
+ /**
302
+ * 上传文件
303
+ *
304
+ * @param ctx
305
+ * @param datas:filePath;Stream
306
+ * @param expire(seconds)
307
+ * @returns {Promise<*|string>}
308
+ */
309
+ async function uploadFile(datas: Stream, expire: number = 0): Promise<UploadFileResp> {
310
+ let formData = new FormData();
311
+ formData.append('file', datas);
312
+ formData.append('expireSeconds', expire.toString());
313
+ formData.append('ignoreUserId', 'true');
314
+ let formHeaders = formData.getHeaders();
315
+
316
+ // 1.获取config
317
+ let options = commonHttp.getOptions(null, openapiHttpPath.uploadAttachment);
318
+ let urlPath: string = options._reqPath;
319
+
320
+ options.responseType = 'buffer';
321
+ for (let key of Object.keys(formHeaders)) {
322
+ options.headers[key] = formHeaders[key];
323
+ }
324
+ options.body = formData;
325
+
326
+ // 2.请求
327
+ return await openapi.doRequest(null, urlPath, options);
328
+ }
329
+
330
+ async function downloadFileByID(id: string, filePath?: string): Promise<Buffer | void> {
331
+ // 1.获取 options
332
+ let options = commonHttp.getOptions(null, openapiHttpPath.downloadFile);
333
+ let urlPath = options._reqPath.replace(replaceKeys.fileID, id)
334
+ options.responseType = "buffer";
335
+
336
+ // 2.请求
337
+ let data = await openapi.doRequest(null, urlPath, options);
338
+
339
+ // 3.写入文件
340
+ data = Buffer.from(data, 'binary');
341
+ if (filePath) {
342
+ checkUtils.checkFile(filePath);
343
+ utils.mkdirs(path.dirname(filePath));
344
+ fs.writeFileSync(filePath, data);
345
+ return;
346
+ }
347
+
348
+ return data;
349
+ }
350
+
351
+ async function downloadFileByToken(fileToken: string, filePath?: string): Promise<Buffer | void> {
352
+ // 1.获取 options
353
+ let options = commonHttp.getOptions(null, openapiHttpPath.downloadAttachment);
354
+ let urlPath = options._reqPath + `?token=${fileToken}`;
355
+ options.responseType = "buffer";
356
+
357
+ // 2.请求
358
+ let data = await openapi.doRequest(null, urlPath, options);
359
+
360
+ // 3.写入文件
361
+ data = Buffer.from(data, 'binary');
362
+ if (filePath) {
363
+ checkUtils.checkFile(filePath);
364
+ utils.mkdirs(path.dirname(filePath));
365
+ fs.writeFileSync(filePath, data);
366
+ return;
367
+ }
368
+
369
+ return data;
370
+ }
371
+
372
+ async function mGetFileToken(ids: string[]): Promise<Record<string, string>> {
373
+ // 1.获取 options
374
+ let options = commonHttp.getOptions(null, coreOapiHttpPath.getBatchAttachmentToken);
375
+ let urlPath = options._reqPath;
376
+
377
+ // 2.请求
378
+ options.json = {
379
+ attachmentIds: ids
380
+ }
381
+ return await openapi.doRequest(null, urlPath, options);
382
+ }
383
+
384
+ async function createMessage(msg: any): Promise<number> {
385
+ // 1.解析参数 icon/receiverIds/percent
386
+ if (!msg.icon) {
387
+ throw new exceptions.InvalidParamError(`parameter icon is required!`);
388
+ }
389
+
390
+ if (typeof (msg.icon) != 'string') {
391
+ throw new exceptions.InvalidParamError(`Invalid format of parameter icon, icon should be string.`);
392
+ }
393
+ let params: any = {};
394
+ let modelKeySearchData: Record<string, any> = {"icon_type": msg.icon};
395
+
396
+ let receiverIds = msg["target_users"] || [];
397
+ if (!(receiverIds instanceof Array)) {
398
+ throw new exceptions.InvalidParamError(`Invalid format of parameter target_users, target_users should be array.`);
399
+ }
400
+ if (receiverIds.length === 0) {
401
+ throw new exceptions.InvalidParamError(`parameter target_users is required!`);
402
+ }
403
+
404
+ let percent = msg["percent"];
405
+ if (percent !== undefined && percent !== null) {
406
+ if (typeof (percent) != 'number') {
407
+ throw new exceptions.InvalidParamError(`Invalid format of parameter percent, percent should be number.`);
408
+ }
409
+ if (percent < 0 || percent > 100) {
410
+ throw new exceptions.InvalidParamError(`Invalid value of parameter percent, the value should be in range [0, 100].`);
411
+ } else {
412
+ modelKeySearchData["percent"] = JSON.stringify(percent);
413
+ }
414
+ }
415
+
416
+ // kunlun类型
417
+ if (checkUtils.isObject(msg)) {
418
+ for (let key of Object.keys(msg)) {
419
+ if (fieldType.isKFieldType(msg[key])) {
420
+ // instanceof 改动6
421
+ // if (msg[key] instanceof fieldType.Multilingual) {
422
+ if (fieldType.isKFieldTypeMultilingual((msg[key]))) {
423
+ let value = JSON.stringify(msg[key].toJSON());
424
+ params[key] = JSON.stringify({"value": value});
425
+ } else {
426
+ params[key] = JSON.stringify(msg[key].toJSON());
427
+ }
428
+ } else {
429
+ params[key] = JSON.stringify(msg[key]);
430
+ }
431
+ }
432
+ }
433
+
434
+ // 2.获取 options
435
+ let options = commonHttp.getOptions(null, openapiHttpPath.createMessage);
436
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK())
437
+
438
+ // 3.请求
439
+ options.json = {
440
+ "NotifyModelKey": notifyModel.key,
441
+ "ReceiverIDs": receiverIds,
442
+ "ParamsRawData": params,
443
+ "Percent": percent,
444
+ "ModelKeySearchData": modelKeySearchData
445
+ }
446
+
447
+ let res = await openapi.doRequest(null, urlPath, options);
448
+ return Number((res as any).taskID);
449
+ }
450
+
451
+ async function updateMessage(msgId: number, msg: any): Promise<void> {
452
+ // 1.解析参数 msgId/icon/percent
453
+ if (!msgId) {
454
+ throw new exceptions.InvalidParamError(`parameter msgId is required!`);
455
+ }
456
+
457
+ if (typeof (msgId) != 'number') {
458
+ throw new exceptions.InvalidParamError(`Invalid format of parameter msgId, msgId should be number.`);
459
+ }
460
+
461
+ if (!msg.icon) {
462
+ throw new exceptions.InvalidParamError(`parameter icon is required!`);
463
+ }
464
+ if (typeof (msg.icon) != 'string') {
465
+ throw new exceptions.InvalidParamError(`Invalid format of parameter icon, icon should be string.`);
466
+ }
467
+ let params: Record<string, any> = {};
468
+ let modelKeySearchData: Record<string, any> = {"icon_type": msg.icon};
469
+
470
+ let percent = msg["percent"];
471
+ if (percent !== undefined && percent !== null) {
472
+ if (typeof (percent) != 'number') {
473
+ throw new exceptions.InvalidParamError(`Invalid format of parameter percent, percent should be number.`);
474
+ }
475
+ if (percent < 0 || percent > 100) {
476
+ throw new exceptions.InvalidParamError(`Invalid value of parameter percent in message ${msgId}, the value should be in range [0, 100].`);
477
+ } else {
478
+ modelKeySearchData["percent"] = JSON.stringify(percent);
479
+ }
480
+ }
481
+
482
+ // kunlun类型
483
+ if (checkUtils.isObject(msg)) {
484
+ for (let key of Object.keys(msg)) {
485
+ if (fieldType.isKFieldType(msg[key])) {
486
+ // instanceof 改动7
487
+ // if (msg[key] instanceof fieldType.Multilingual) {
488
+ if (fieldType.isKFieldTypeMultilingual(msg[key])) {
489
+ let value = JSON.stringify(msg[key].toJSON());
490
+ params[key] = JSON.stringify({"value": value});
491
+ } else {
492
+ params[key] = JSON.stringify(msg[key].toJSON());
493
+ }
494
+ } else {
495
+ params[key] = JSON.stringify(msg[key])
496
+ }
497
+ }
498
+ }
499
+
500
+ // 2.获取 options
501
+ let options = commonHttp.getOptions(null, openapiHttpPath.updateMessage);
502
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK())
503
+
504
+ // 3.请求
505
+ options.json = {
506
+ "TaskID": msgId,
507
+ "Percent": percent,
508
+ "NotifyModelKey": notifyModel.key,
509
+ "ParamsRawData": params,
510
+ "IsBackground": true,
511
+ "ModelKeySearchData": modelKeySearchData
512
+ }
513
+
514
+ return await openapi.doRequest(null, urlPath, options);
515
+ }
516
+
517
+ async function getFields(objectApiName: string) {
518
+ // 1.check
519
+ if (!objectApiName) {
520
+ throw new exceptions.InvalidParamError("objectApiName is empty");
521
+ }
522
+
523
+ // 2.获取config
524
+ let options = commonHttp.getOptions(null, openapiHttpPath.getFieldsV5);
525
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK())
526
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
527
+
528
+ let obj = await openapi.doRequest(null, urlPath, options)
529
+ if (!obj || !obj.fields) {
530
+ return [];
531
+ }
532
+
533
+ return obj.fields;
534
+ }
535
+
536
+ async function getField(objectApiName: string, fieldApiName: string) {
537
+ // 1.check
538
+ if (!objectApiName) {
539
+ throw new exceptions.InternalError("objectApiName is empty");
540
+ }
541
+ if (!fieldApiName) {
542
+ throw new exceptions.InternalError("fieldApiName is empty");
543
+ }
544
+
545
+ // 2.获取 options
546
+ let options = commonHttp.getOptions(null, openapiHttpPath.getFieldV5);
547
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
548
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
549
+ urlPath = urlPath.replace(replaceKeys.fieldApiName, fieldApiName);
550
+
551
+ // 3.请求
552
+ return await openapi.doRequest(null, urlPath, options)
553
+ }
554
+
555
+ async function terminateWorkflowInstance(workflowInstanceId: number, operator: number, reason: string) {
556
+ // 1.check
557
+ if (typeof (workflowInstanceId) !== "number") {
558
+ throw new exceptions.InvalidParamError(`The type of workflowInstanceId should be number, but ${typeof (workflowInstanceId)}.`);
559
+ }
560
+
561
+ // 2.获取 options
562
+ let options = commonHttp.getOptions(null, openapiHttpPath.terminateWorkflowInstance);
563
+ let urlPath = options._reqPath;
564
+
565
+ // 3.请求
566
+ options.json = {
567
+ "workflowInstanceID": workflowInstanceId,
568
+ "operator": operator,
569
+ "reason": reason,
570
+ }
571
+
572
+ await openapi.doRequest(null, urlPath, options);
573
+ }
574
+
575
+ async function modifyRecordsWithTransaction(placeholders: Record<string, number>, operations: object[]): Promise<Map<string, number>> {
576
+ // 1.获取 options
577
+ let options = commonHttp.getOptions(null, openapiHttpPath.modifyRecordsWithTransaction);
578
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
579
+
580
+ // 2.请求
581
+ options.json = {
582
+ "placeholders": placeholders,
583
+ "operations": operations,
584
+ "operatorId": -1,
585
+ }
586
+
587
+ let taskId = utils.getTriggerTaskID();
588
+ if (taskId) {
589
+ options.json["taskId"] = taskId;
590
+ }
591
+
592
+ let result = await openapi.doRequest(null, urlPath, options);
593
+ let retPlaceholders = new Map<string, number>();
594
+ if (result && result.placeholders) {
595
+ for (let key of Object.keys(result.placeholders)) {
596
+ retPlaceholders.set(key, result.placeholders[key]);
597
+ }
598
+ }
599
+ return retPlaceholders
600
+ };
601
+
602
+ async function getGlobalConfigByKey<valueT>(key: string): Promise<valueT> {
603
+ // 1.获取 options
604
+ let options = commonHttp.getOptions(null, openapiHttpPath.getAllGlobalConfigs);
605
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
606
+
607
+ // 2.请求
608
+ let pageSize = 100, offset = 0;
609
+ for (let i = 0; ; i++) {
610
+ options.json = {
611
+ "biz_type": "GlobalVariables",
612
+ "used_by": "UsedBySystem",
613
+ "filter": {
614
+ "offset": offset * i,
615
+ "limit": pageSize
616
+ },
617
+ }
618
+
619
+ let data = await openapi.doRequest(null, urlPath, options);
620
+ for (let config of data.configs) {
621
+ if (config.key === key) {
622
+ return config.value;
623
+ }
624
+ }
625
+
626
+ if (data.configs.length < pageSize) {
627
+ break;
628
+ }
629
+ }
630
+
631
+ throw new exceptions.InvalidParamError(`undefined global variable (${key})`);
632
+ }
633
+
634
+
635
+ async function oql(oql: string, args: any[], namedArgs: Record<string, any>) {
636
+ // 1.获取 options
637
+ let options = commonHttp.getOptions(null, coreOapiHttpPath.oql);
638
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
639
+
640
+ // 2.请求
641
+ options.json = {
642
+ "query": oql,
643
+ "args": args,
644
+ "namedArgs": namedArgs,
645
+ "compat": true,
646
+ }
647
+
648
+ let result = await openapi.doRequest(null, urlPath, options);
649
+ if (result && result.rows) {
650
+ return result.rows;
651
+ }
652
+
653
+ return [];
654
+ }
655
+
656
+ async function openSDKCreateRecordBySync(objectApiName: string, record: object): Promise<any> {
657
+ // 1.获取 options
658
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKCreateRecord);
659
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
660
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
661
+
662
+ // 2.请求
663
+ options.json = record;
664
+
665
+ return await openapi.doRequest(null, urlPath, options);
666
+ };
667
+
668
+ async function openSDKUpdateRecordBySync(objectApiName: string, recordID: number, record: object): Promise<any> {
669
+ // 1.获取 options
670
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKUpdateRecord);
671
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
672
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
673
+ urlPath = urlPath.replace(replaceKeys.recordID, recordID.toString());
674
+
675
+ // 2.请求
676
+ options.json = record;
677
+
678
+ return await openapi.doRequest(null, urlPath, options);
679
+ };
680
+
681
+ async function openSDKDeleteRecordBySync(objectApiName: string, recordID: number): Promise<any> {
682
+ // 1.获取 options
683
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKDeleteRecord);
684
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
685
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
686
+ urlPath = urlPath.replace(replaceKeys.recordID, recordID.toString());
687
+
688
+ // 2.请求
689
+ return await openapi.doRequest(null, urlPath, options);
690
+ };
691
+
692
+ async function openSDKCreateRecordsBySync(objectApiName: string, records: object[]): Promise<any> {
693
+ // 1.获取 options
694
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKBatchCreateRecord);
695
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
696
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
697
+
698
+ // 2.请求
699
+ options.json = records;
700
+
701
+ return await openapi.doRequest(null, urlPath, options);
702
+ };
703
+
704
+ async function openSDKUpdateRecordsBySync(objectApiName: string, records: object[]): Promise<any> {
705
+ // 1.获取 options
706
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKBatchUpdateRecord);
707
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
708
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
709
+
710
+ // 2.请求
711
+ options.json = records;
712
+
713
+ return await openapi.doRequest(null, urlPath, options);
714
+ };
715
+
716
+ async function openSDKDeleteRecordsBySync(objectApiName: string, recordIDs: number[]): Promise<any> {
717
+ // 1.获取 options
718
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKBatchDeleteRecord);
719
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
720
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
721
+
722
+ // 2.请求
723
+ options.json = recordIDs;
724
+
725
+ return await openapi.doRequest(null, urlPath, options);
726
+ };
727
+
728
+ async function openSDKGetRecords(objectApiName: string, param: OpenSDKGetRecordsReq): Promise<any | number> {
729
+ // 1.获取 options
730
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKGetRecords);
731
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
732
+ urlPath = urlPath.replace(replaceKeys.objectApiName, objectApiName);
733
+
734
+ if (param.limit <= 0 || param.limit > 200) {
735
+ param.limit = 200;
736
+ }
737
+
738
+ // 2.请求
739
+ options.json = param;
740
+
741
+ const data = await openapi.doRequest(null, urlPath, options);
742
+ if (param.count) {
743
+ return JSON.parse(data.records);
744
+ } else {
745
+ return data.total;
746
+ }
747
+ }
748
+
749
+ async function openSDKUploadFile(fileName: string, data: Buffer): Promise<any> {
750
+ // 1.获取 options
751
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKUploadFile);
752
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
753
+
754
+ let formData = new FormData();
755
+ formData.append('file', data);
756
+ formData.append('ignoreUserId', 'true');
757
+ let formHeaders = formData.getHeaders();
758
+
759
+ options.responseType = 'buffer';
760
+ for (let key of Object.keys(formHeaders)) {
761
+ options.headers[key] = formHeaders[key];
762
+ }
763
+ options.body = formData;
764
+
765
+ // 2.请求
766
+ return await openapi.doRequest(null, urlPath, options);
767
+ }
768
+
769
+ async function openSDKUploadAvatar(fileName: string, data: Buffer): Promise<any> {
770
+ // 1.获取 options
771
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKUploadAvatar);
772
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
773
+
774
+ let formData = new FormData();
775
+ formData.append('file', data);
776
+ formData.append('ignoreUserId', 'true');
777
+ let formHeaders = formData.getHeaders();
778
+
779
+ options.responseType = 'buffer';
780
+ for (let key of Object.keys(formHeaders)) {
781
+ options.headers[key] = formHeaders[key];
782
+ }
783
+ options.body = formData;
784
+
785
+ // 2.请求
786
+ return await openapi.doRequest(null, urlPath, options);
787
+
788
+ }
789
+
790
+ async function openSDKDownloadAvatar(imageID: string): Promise<Buffer> {
791
+ // 1.获取 options
792
+ let options = commonHttp.getOptions(null, openapiHttpPath.openSDKDownloadAvatar);
793
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
794
+ urlPath = urlPath.replace(replaceKeys.fileID, imageID);
795
+
796
+ // 2.请求
797
+ let data = await openapi.doRequest(null, urlPath, options);
798
+
799
+ // 3.写入文件
800
+ data = Buffer.from(data, 'binary');
801
+
802
+ return data;
803
+ }
804
+
805
+ export class RequestHttp implements IInnerAPIRequest {
806
+
807
+ constructor() {
808
+ this.updateWorkflowVariables = updateWorkflowVariables;
809
+ this.createRecordBySync = createRecordBySync;
810
+ this.updateRecordBySync = updateRecordBySync;
811
+ this.deleteRecordBySync = deleteRecordBySync;
812
+ this.createRecordsByAsync = createRecordsByAsync;
813
+ this.updateRecordsByAsync = updateRecordsByAsync;
814
+ this.deleteRecordsByAsync = deleteRecordsByAsync;
815
+ this.createRecordsBySync = createRecordsBySync;
816
+ this.updateRecordsBySync = updateRecordsBySync;
817
+ this.deleteRecordsBySync = deleteRecordsBySync;
818
+ this.getRecordsOrCountByCriterion = getRecordsOrCountByCriterion;
819
+ this.uploadFile = uploadFile;
820
+ this.downloadFileByID = downloadFileByID;
821
+ this.downloadFileByToken = downloadFileByToken;
822
+ this.createMessage = createMessage;
823
+ this.updateMessage = updateMessage;
824
+ this.getFields = getFields;
825
+ this.getField = getField;
826
+ this.terminateWorkflowInstance = terminateWorkflowInstance;
827
+ this.modifyRecordsWithTransaction = modifyRecordsWithTransaction;
828
+ this.getGlobalConfigByKey = getGlobalConfigByKey;
829
+ this.oql = oql;
830
+ // TODO: open sdk api test
831
+ this.openSDKCreateRecordBySync = openSDKCreateRecordBySync;
832
+ this.openSDKUpdateRecordBySync = openSDKUpdateRecordBySync;
833
+ this.openSDKDeleteRecordBySync = openSDKDeleteRecordBySync;
834
+ this.openSDKCreateRecordsBySync = openSDKCreateRecordsBySync;
835
+ this.openSDKUpdateRecordsBySync = openSDKUpdateRecordsBySync;
836
+ this.openSDKDeleteRecordsBySync = openSDKDeleteRecordsBySync;
837
+ this.openSDKGetRecords = openSDKGetRecords;
838
+ this.openSDKUploadFile = openSDKUploadFile;
839
+ this.openSDKUploadAvatar = openSDKUploadAvatar;
840
+ this.openSDKDownloadAvatar = openSDKDownloadAvatar;
841
+ }
842
+
843
+ updateWorkflowVariables(ctx: any, instanceId: number, variables: object, variableTypes: object): any {
844
+ }
845
+
846
+ createRecordBySync(objectApiName: string, record: object): any {
847
+ }
848
+
849
+ updateRecordBySync(objectApiName: string, recordID: number, record: object): any {
850
+ }
851
+
852
+ deleteRecordBySync(objectApiName: string, recordID: number): any {
853
+ }
854
+
855
+ createRecordsByAsync(objectApiName: string, records: object[]): any {
856
+ }
857
+
858
+ updateRecordsByAsync(objectApiName: string, recordMap: Record<number, object>): any {
859
+ }
860
+
861
+ deleteRecordsByAsync(objectApiName: string, recordIDs: number[]): any {
862
+ }
863
+
864
+ createRecordsBySync(objectApiName: string, records: object[]): any {
865
+ }
866
+
867
+ updateRecordsBySync(objectApiName: string, recordMap: Record<number, object>): any {
868
+ }
869
+
870
+ deleteRecordsBySync(objectApiName: string, recordIDs: number[]): any {
871
+ }
872
+
873
+ getRecordsOrCountByCriterion(objectApiName: string, criterion: string | Criterion, order: Order[], ignoreBackLookupField: boolean, fieldApiNames: string[], offset: number, limit: number, needCount: boolean): any {
874
+ }
875
+
876
+ uploadFile(data: Stream, expire: number): Promise<UploadFileResp> {
877
+ return null;
878
+ }
879
+
880
+ downloadFileByID(fileID: string, filePath?: string): Promise<void | Buffer> {
881
+ return null;
882
+ }
883
+
884
+ downloadFileByToken(fileToken: string, filePath?: string): Promise<Buffer | void> {
885
+ return null;
886
+ }
887
+
888
+ createMessage(msg: any): Promise<number> {
889
+ return null;
890
+ }
891
+
892
+ updateMessage(msgId: number, msg: any): Promise<void> {
893
+ return null;
894
+ }
895
+
896
+ getFields(objectApiName: string): any {
897
+ }
898
+
899
+ getField(objectApiName: string, fieldApiName: string): any {
900
+ }
901
+
902
+ terminateWorkflowInstance(workflowInstanceId: number, operator: number, reason: string): any {
903
+ }
904
+
905
+ modifyRecordsWithTransaction(placeholders: Record<string, number>, operations: object[]): Promise<Map<string, number>> {
906
+ return null;
907
+ }
908
+
909
+ getGlobalConfigByKey<valueT>(key: string): Promise<valueT> {
910
+ return null;
911
+ }
912
+
913
+ oql(oql: string, args: any[], namedArgs: Record<string, any>): any {
914
+ }
915
+
916
+ async invokeFuncSync(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
917
+ return await invokeFuncSync(idOrName, params);
918
+ }
919
+
920
+ async invokeFuncWithAuth(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
921
+ return await invokeFuncWithAuth(idOrName, params);
922
+ }
923
+
924
+ async createAsyncTaskV1(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
925
+ return await createAsyncTaskV1(idOrName, params);
926
+ }
927
+
928
+ async createAsyncTaskV2(name: string, params: any): Promise<any> {
929
+ return await createAsyncTaskV2(name, params);
930
+ }
931
+
932
+ async getExecutionUserTaskInfo(executionId: bigint): Promise<any> {
933
+ return await getExecutionUserTaskInfo(executionId);
934
+ }
935
+
936
+ async executeFlow(APIName: string, options: any): Promise<ExecutionResult> {
937
+ return await executeFlow(APIName, options);
938
+ }
939
+
940
+ async revokeExecution(executionId: number, revokeOptions: RevokeExecutionOptions): Promise<void> {
941
+ return await revokeExecution(executionId, revokeOptions);
942
+ }
943
+
944
+ async getExecutionInfo(executionId: number): Promise<ExecutionInfo> {
945
+ return await getExecutionInfo(executionId);
946
+ }
947
+
948
+ async getTenantInfo(appCtx: AppCtx): Promise<any> {
949
+ return await appCtx.credential.getTenantInfo();
950
+ }
951
+
952
+ // open sdk empty implement
953
+ openSDKCreateRecordBySync(objectApiName: string, record: object): any {
954
+ };
955
+
956
+ openSDKUpdateRecordBySync(objectApiName: string, recordID: number, record: object): any {
957
+ };
958
+
959
+ openSDKDeleteRecordBySync(objectApiName: string, recordID: number): any {
960
+ };
961
+
962
+ openSDKCreateRecordsBySync(objectApiName: string, records: object[]): any {
963
+ };
964
+
965
+ openSDKUpdateRecordsBySync(objectApiName: string, recordMap: Record<number, object>): any {
966
+ };
967
+
968
+ openSDKDeleteRecordsBySync(objectApiName: string, recordIDs: number[]): any {
969
+ };
970
+
971
+ openSDKGetRecords(objectApiName: string, param: OpenSDKGetRecordsReq): Promise<any | number> {
972
+ return null;
973
+ };
974
+
975
+ openSDKUploadFile(fileName: string, data: Buffer): any {
976
+ };
977
+
978
+ openSDKUploadAvatar(fileName: string, data: Buffer): any {
979
+ };
980
+
981
+ openSDKDownloadAvatar(imageID: string): Promise<Buffer> {
982
+ return null
983
+ };
984
+ }
985
+
986
+ async function invokeFuncWithAuth(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
987
+ let options = commonHttp.getOptions(null, openapiHttpPath.invokeFuncWithAuthInnerAPI);
988
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
989
+ urlPath = urlPath.replace(replaceKeys.apiName, idOrName.APIName);
990
+
991
+ options.headers["User"] = utils.getUserIDFromCtx();
992
+
993
+ options.json = {
994
+ "params": params,
995
+ "context": getTriggerCtx(),
996
+ }
997
+
998
+ if (!idOrName.isInvokeByAPIName) {
999
+ options.json.apiID = idOrName.APIId;
1000
+ }
1001
+
1002
+ let res = await openapi.doRequest(null, urlPath, options);
1003
+
1004
+ if (!res || !res.result) {
1005
+ throw new exceptions.InternalError(`result is empty`)
1006
+ }
1007
+
1008
+ let result: { code: string, msg: string, data: any; };
1009
+ try {
1010
+ result = JSON.parse(res.result);
1011
+ } catch (e) {
1012
+ throw new exceptions.InternalError(`JSON parse failed for ${res.result}`);
1013
+ }
1014
+ if (!result || result.code !== "0") {
1015
+ throw new exceptions.InvalidParamError(`result is invalid for code: ${result ? result.code : ""} msg: ${result ? result.msg : ""}`)
1016
+ }
1017
+ return result.data;
1018
+ }
1019
+
1020
+ async function invokeFuncSync(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1021
+
1022
+ let options = commonHttp.getOptions(null, openapiHttpPath.invokeFuncSyncInnerAPI);
1023
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
1024
+
1025
+ options.headers["User"] = utils.getUserIDFromCtx();
1026
+
1027
+ options.json = {
1028
+ "params": JSON.stringify(params),
1029
+ "context": JSON.stringify(getTriggerCtx()),
1030
+ "triggerType": utils.getTriggerType(),
1031
+ }
1032
+
1033
+ // 相等时,表示是新版本函数的场景
1034
+ if (idOrName.isInvokeByAPIName) {
1035
+ options.json.apiAlias = idOrName.APIName;
1036
+ options.json.function_name = "";
1037
+ } else {
1038
+ options.json.function_name = idOrName.APIId;
1039
+ options.json.apiAlias = "";
1040
+ }
1041
+
1042
+ let res = await openapi.doRequest(null, urlPath, options);
1043
+ if (res && res.code === "0") {
1044
+ return res.data;
1045
+ }
1046
+ throw new exceptions.InternalError(`invokeFuncSync result.code(${res.code}) is invalid`)
1047
+ }
1048
+
1049
+ async function createAsyncTaskV1(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1050
+
1051
+ let options = commonHttp.getOptions(null, openapiHttpPath.createAsyncTaskInnerAPIV1);
1052
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
1053
+
1054
+ options.headers["User"] = utils.getUserIDFromCtx();
1055
+
1056
+ options.json = {
1057
+ "params": JSON.stringify(params),
1058
+ "context": JSON.stringify(getTriggerCtx()),
1059
+ "triggerType": utils.getTriggerType(),
1060
+ }
1061
+
1062
+ // 相等时,表示是新版本函数的场景
1063
+ if (idOrName.isInvokeByAPIName) {
1064
+ options.json.apiAlias = idOrName.APIName;
1065
+ options.json.function_name = ""; // required
1066
+ } else {
1067
+ options.json.function_name = idOrName.APIId;
1068
+ }
1069
+
1070
+ let res = await openapi.doRequest(null, urlPath, options);
1071
+ if (res && res.task_id) {
1072
+ return res.task_id;
1073
+ }
1074
+ throw new exceptions.InternalError(`createAsyncTaskV1 result is empty`)
1075
+ }
1076
+
1077
+ async function createAsyncTaskV2(APIName: string, params: any): Promise<any> {
1078
+
1079
+ let options = commonHttp.getOptions(null, openapiHttpPath.createAsyncTaskInnerAPIV2);
1080
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK());
1081
+
1082
+ let triggerCtx: Record<string, any> = {}, taskID = utils.getTriggerTaskID();
1083
+ if (taskID) {
1084
+ triggerCtx[headers.triggerTaskId] = taskID;
1085
+ }
1086
+
1087
+ let temp = utils.getDistributedHandleMask();
1088
+ if (temp) {
1089
+ triggerCtx[headers.distributedHandleMask] = temp;
1090
+ }
1091
+
1092
+ let reqCtx = nodeCls.get(headers.loopCtxKey);
1093
+ if (checkLoopCtx(reqCtx)) {
1094
+ triggerCtx[headers.loopMasks] = reqCtx.loopMasks
1095
+ }
1096
+
1097
+ options.headers["User"] = utils.getUserIDFromCtx();
1098
+
1099
+ options.json = {
1100
+ "apiAlias": APIName,
1101
+ "params": JSON.stringify(params),
1102
+ "context": JSON.stringify(getTriggerCtx()),
1103
+ "triggerType": utils.getTriggerType(),
1104
+ "extra": getDebugExtraInfo()
1105
+ }
1106
+
1107
+ let res = await openapi.doRequest(null, urlPath, options);
1108
+ if (res && res.task_id) {
1109
+ return res.task_id;
1110
+ }
1111
+ throw new exceptions.InternalError(`createAsyncTaskV2 result is empty`)
1112
+ }
1113
+
1114
+ function checkLoopCtx(reqCtx: any): any {
1115
+ return reqCtx && reqCtx.loopMasks && Array.isArray(reqCtx.loopMasks)
1116
+ }
1117
+
1118
+
1119
+ function getTriggerCtx(): any {
1120
+ let triggerCtx: Record<string, any> = {}, taskID = utils.getTriggerTaskID();
1121
+ if (taskID) {
1122
+ triggerCtx[headers.triggerTaskId] = taskID;
1123
+ }
1124
+
1125
+ let temp = utils.getDistributedHandleMask();
1126
+ if (temp) {
1127
+ triggerCtx[headers.distributedHandleMask] = temp;
1128
+ }
1129
+
1130
+ let reqCtx = nodeCls.get(headers.loopCtxKey);
1131
+ if (checkLoopCtx(reqCtx)) {
1132
+ triggerCtx[headers.loopMasks] = reqCtx.loopMasks
1133
+ }
1134
+ return triggerCtx;
1135
+ }
1136
+
1137
+ export async function getExecutionUserTaskInfo(executionId: bigint): Promise<any> {
1138
+ if (!executionId) {
1139
+ throw new exceptions.InvalidParamError(`executionId is empty`);
1140
+ }
1141
+
1142
+ let options = commonHttp.getOptions(null, coreOapiHttpPath.getExecutionUserTaskInfo);
1143
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK()).replace(replaceKeys.id, `${executionId}`);
1144
+ urlPath += `?operator=${utils.getUserIDFromCtx()}`
1145
+
1146
+ let res = await openapi.doRequest(null, urlPath, options);
1147
+
1148
+ if (!res) {
1149
+ throw new exceptions.InternalError(`getExecutionUserTaskInfo result is empty`);
1150
+ }
1151
+
1152
+ return res.taskList;
1153
+ }
1154
+
1155
+ export async function executeFlow(APIName: string, option: any): Promise<ExecutionResult> {
1156
+ if (!APIName) {
1157
+ throw new exceptions.InvalidParamError(`flow apiName is empty`);
1158
+ }
1159
+
1160
+ let options = commonHttp.getOptions(null, coreOapiHttpPath.executeFlow);
1161
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK()).replace(replaceKeys.apiName, APIName);
1162
+
1163
+ options.json = {
1164
+ "operator": utils.getUserIDFromCtx(),
1165
+ "variables": transMapToFlowVariable(option.params),
1166
+ "loopMasks": utils.getLoopMasks(),
1167
+ }
1168
+ let res = await openapi.doRequest(null, urlPath, options);
1169
+ return {
1170
+ executionId: res.executionId,
1171
+ status: res.status,
1172
+ data: transFlowVariableToMap(res.outParams),
1173
+ errCode: res.errCode ? res.errCode : undefined,
1174
+ errMsg: res.errMsg ? res.errMsg : undefined,
1175
+ };
1176
+ }
1177
+
1178
+ export async function revokeExecution(executionId: number, revokeOptions: RevokeExecutionOptions): Promise<void> {
1179
+ if (!executionId) {
1180
+ throw new exceptions.InvalidParamError(`executionId is empty`);
1181
+ }
1182
+ let options = commonHttp.getOptions(null, coreOapiHttpPath.revokeExecution);
1183
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK()).replace(replaceKeys.id, `${executionId}`);
1184
+
1185
+ let reason: { en_US?: string, zh_CN?: string } = undefined;
1186
+
1187
+ if (revokeOptions && revokeOptions.reason) {
1188
+ reason = {
1189
+ en_US: revokeOptions.reason.en_US,
1190
+ zh_CN: revokeOptions.reason.zh_CN
1191
+ }
1192
+ }
1193
+
1194
+ options.json = {
1195
+ "operator": utils.getUserIDFromCtx(),
1196
+ "reason": reason,
1197
+ };
1198
+
1199
+ return await openapi.doRequest(null, urlPath, options);
1200
+ }
1201
+
1202
+ export async function getExecutionInfo(executionId: number): Promise<ExecutionInfo> {
1203
+ if (!executionId) {
1204
+ throw new exceptions.InvalidParamError(`executionId is empty`);
1205
+ }
1206
+
1207
+ let options = commonHttp.getOptions(null, coreOapiHttpPath.getExecutionInfo);
1208
+ let urlPath = options._reqPath.replace(replaceKeys.namespace, await getNamespaceForOpenAndFaaSSDK()).replace(replaceKeys.id, `${executionId}`);
1209
+ urlPath += `?operator=${utils.getUserIDFromCtx()}`
1210
+
1211
+ let res = await openapi.doRequest(null, urlPath, options);
1212
+
1213
+ if (!res || !res.executionInfo) {
1214
+ throw new exceptions.InternalError(`getExecutionInfo result is empty`);
1215
+ }
1216
+
1217
+ return {
1218
+ status: res.executionInfo.status,
1219
+ data: transFlowVariableToMap(res.executionInfo.outParams),
1220
+ errCode: res.executionInfo.errCode ? res.executionInfo.errCode : undefined,
1221
+ errMsg: res.executionInfo.errMsg ? res.executionInfo.errMsg : undefined,
1222
+ };
1223
+ }