@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,1648 @@
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 { Order } from "../context/db/impl/order"
7
+ import { Criterion } from "../kunlun/operator/impl/logic";
8
+ import { IInnerAPIRequest, OpenSDKGetRecordsReq, UploadFileResp } from "./interface";
9
+ import * as common from "@byted-apaas/server-common-node";
10
+
11
+ import { streamToBuffer } from '@jorgeferrero/stream-to-buffer';
12
+ import { notifyModel } from "../constants/constants";
13
+
14
+ import {
15
+ getDebugExtraInfo,
16
+ getNamespaceForOpenAndFaaSSDK,
17
+ getTriggerCtx,
18
+ transFlowVariableToMap,
19
+ transMapToFlowVariable
20
+ } from './common';
21
+ import {
22
+ ExecutionInfo,
23
+ ExecutionResult,
24
+ RevokeExecutionOptions
25
+ } from "@byted-apaas/server-common-node/context/workflow/workflow";
26
+ import { AppCtx } from "../application/application";
27
+ import { InternalError, InvalidParamError } from "@byted-apaas/server-common-node/utils/exceptions";
28
+
29
+ const rpc = (common as any).rpc;
30
+ const exceptions = common.exceptions;
31
+ const utils = common.utils;
32
+ const fs = require("fs");
33
+ const checkUtils = common.checkUtils;
34
+ const path = require('path');
35
+
36
+ async function pre(isTransUser?: boolean) {
37
+ let ctx;
38
+ try {
39
+ ctx = await rpc.rebuildRpcCtx(isTransUser);
40
+ } catch (err) {
41
+ throw new exceptions.InternalError(`RebuildRpcCtx failed: ${err.message}`)
42
+ }
43
+ return ctx;
44
+ }
45
+
46
+ function post(baseResp: any) {
47
+ if (!baseResp) {
48
+ throw new exceptions.InternalError(`Call InnerAPI resp is empty`)
49
+ }
50
+
51
+ if (baseResp.KStatusCode !== "") {
52
+ let msg = baseResp.KStatusMessage;
53
+ if (baseResp.StatusMessage !== "") {
54
+ msg = baseResp.StatusMessage
55
+ }
56
+ if ((baseResp.extra as any)["is_system_error"] === "true") {
57
+ throw new exceptions.InternalError(`${msg} [${baseResp.KStatusCode}]`)
58
+ }
59
+ throw new exceptions.InvalidParamError(`${msg} [${baseResp.KStatusCode}]`)
60
+ }
61
+ }
62
+
63
+ async function createRecordBySync(objectApiName: string, record: object) {
64
+ // 1.check
65
+ if (!objectApiName) {
66
+ throw new exceptions.InvalidParamError("objectApiName is empty");
67
+ }
68
+
69
+ // 2.前置处理
70
+ let ctx = await pre();
71
+
72
+ // 3.构造 params
73
+ let param: any = {};
74
+
75
+ let task_id = utils.getTriggerTaskID();
76
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
77
+ param.ObjectAPIAlias = objectApiName;
78
+ param.Data = utils.stringify(record);
79
+ param.Operator = BigInt(-1);
80
+ if (task_id) {
81
+ param.TaskID = BigInt(task_id);
82
+ }
83
+
84
+ // 4.发起请求
85
+ let resp;
86
+ try {
87
+ resp = await rpc.getInnerAPICli().CreateRecordBySync(ctx, param);
88
+ } catch (err) {
89
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
90
+ }
91
+
92
+ // 5.后置处理
93
+ post(resp.BaseResp);
94
+
95
+ return { _id: Number(resp.RecordID) };
96
+ }
97
+
98
+ async function updateRecordBySync(objectApiName: string, recordID: number, record: object) {
99
+ // 1.check
100
+ if (!objectApiName) {
101
+ throw new exceptions.InvalidParamError("objectApiName is empty");
102
+ }
103
+
104
+ // 2.前置处理
105
+ let ctx = await pre();
106
+
107
+ // 3.构造 params
108
+ let param: any = {};
109
+ let task_id = utils.getTriggerTaskID();
110
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
111
+ param.ObjectAPIAlias = objectApiName;
112
+ param.RecordID = BigInt(recordID);
113
+ param.Data = utils.stringify(record);
114
+ param.Operator = BigInt(-1);
115
+ if (task_id) {
116
+ param.TaskID = BigInt(task_id);
117
+ }
118
+
119
+ // 4.发起请求
120
+ let resp;
121
+ try {
122
+ resp = await rpc.getInnerAPICli().UpdateRecordBySync(ctx, param);
123
+ } catch (err) {
124
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
125
+ }
126
+
127
+ // 5.后置处理
128
+ post(resp.BaseResp);
129
+ return
130
+ }
131
+
132
+ async function deleteRecordBySync(objectApiName: string, recordID: number) {
133
+ // 1.check
134
+ if (!objectApiName) {
135
+ throw new exceptions.InvalidParamError("objectApiName is empty");
136
+ }
137
+
138
+ // 2.前置处理
139
+ let ctx = await pre();
140
+
141
+ // 3.构造 params
142
+ let param: any = {};
143
+ let task_id = utils.getTriggerTaskID();
144
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
145
+ param.ObjectAPIAlias = objectApiName;
146
+ param.RecordID = BigInt(recordID);
147
+ param.Operator = BigInt(-1);
148
+ if (task_id) {
149
+ param.TaskID = BigInt(task_id);
150
+ }
151
+
152
+ // 4.发起请求
153
+ let resp;
154
+ try {
155
+ resp = await rpc.getInnerAPICli().DeleteRecordBySync(ctx, param);
156
+ } catch (err) {
157
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
158
+ }
159
+
160
+ // 5.后置处理
161
+ post(resp.BaseResp);
162
+ return
163
+ }
164
+
165
+ async function createRecordsByAsync(objectApiName: string, records: object[]) {
166
+ // 1.check
167
+ if (!objectApiName) {
168
+ throw new exceptions.InvalidParamError("objectApiName is empty");
169
+ }
170
+
171
+ // 2.前置处理
172
+ let ctx = await pre();
173
+
174
+ // 3.构造 params
175
+ let param: any = {};
176
+ let task_id = utils.getTriggerTaskID();
177
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
178
+ param.ObjectAPIAlias = objectApiName;
179
+ param.Data = utils.stringify(records);
180
+ param.OperatorID = BigInt(-1);
181
+ if (task_id) {
182
+ param.AutomationTaskID = BigInt(task_id);
183
+ }
184
+
185
+ // 4.发起请求
186
+ let resp;
187
+ try {
188
+ resp = await rpc.getInnerAPICli().BatchCreateRecordByAsync(ctx, param);
189
+ } catch (err) {
190
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
191
+ }
192
+
193
+ // 5.后置处理
194
+ post(resp.BaseResp);
195
+
196
+ return { taskID: Number(resp.TaskID) };
197
+ }
198
+
199
+ async function updateRecordsByAsync(objectApiName: string, recordMap: Record<number, object>) {
200
+ // 1.check
201
+ if (!objectApiName) {
202
+ throw new exceptions.InvalidParamError("objectApiName is empty");
203
+ }
204
+
205
+ // 2.前置处理
206
+ let ctx = await pre();
207
+
208
+ // 3.构造 params
209
+ let param: any = {};
210
+ let task_id = utils.getTriggerTaskID();
211
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
212
+ param.ObjectAPIAlias = objectApiName;
213
+ param.Data = utils.stringify(recordMap);
214
+ param.OperatorID = BigInt(-1);
215
+ if (task_id) {
216
+ param.AutomationTaskID = BigInt(task_id);
217
+ }
218
+
219
+ // 4.发起请求
220
+ let resp;
221
+ try {
222
+ resp = await rpc.getInnerAPICli().BatchUpdateRecordByAsync(ctx, param);
223
+ } catch (err) {
224
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
225
+ }
226
+
227
+ // 5.后置处理
228
+ post(resp.BaseResp);
229
+
230
+ return { taskID: Number(resp.TaskID) };
231
+ }
232
+
233
+ async function deleteRecordsByAsync(objectApiName: string, recordIDs: number[]) {
234
+ // 1.check
235
+ if (!objectApiName) {
236
+ throw new exceptions.InvalidParamError("objectApiName is empty");
237
+ }
238
+
239
+ // 2.前置处理
240
+ let ctx = await pre();
241
+
242
+ // 3.构造 params
243
+ let param: any = {};
244
+ let task_id = utils.getTriggerTaskID();
245
+ let recordIDList: Array<bigint> = new Array();
246
+ recordIDs.forEach(
247
+ (v: number, idx: number) => {
248
+ recordIDList.push(BigInt(v));
249
+ }
250
+ );
251
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
252
+ param.ObjectAPIAlias = objectApiName;
253
+ param.RecordIDList = recordIDList;
254
+ param.OperatorID = BigInt(-1);
255
+ if (task_id) {
256
+ param.AutomationTaskID = BigInt(task_id);
257
+ }
258
+
259
+ // 4.发起请求
260
+ let resp;
261
+ try {
262
+ resp = await rpc.getInnerAPICli().BatchDeleteRecordByAsync(ctx, param);
263
+ } catch (err) {
264
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
265
+ }
266
+
267
+ // 5.后置处理
268
+ post(resp.BaseResp);
269
+
270
+ return { taskID: Number(resp.TaskID) };
271
+ }
272
+
273
+ async function createRecordsBySync(objectApiName: string, records: object[]) {
274
+ // 1.check
275
+ if (!objectApiName) {
276
+ throw new exceptions.InvalidParamError("objectApiName is empty");
277
+ }
278
+
279
+ // 2.前置处理
280
+ let ctx = await pre();
281
+
282
+ // 3.构造 params
283
+ let param: any = {};
284
+ let task_id = utils.getTriggerTaskID();
285
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
286
+ param.ObjectAPIAlias = objectApiName;
287
+ param.Data = utils.stringify(records);
288
+ param.OperatorID = BigInt(-1);
289
+ if (task_id) {
290
+ param.AutomationTaskID = BigInt(task_id);
291
+ }
292
+
293
+ // 4.发起请求
294
+ let resp;
295
+ try {
296
+ resp = await rpc.getInnerAPICli().BatchCreateRecordBySync(ctx, param);
297
+ } catch (err) {
298
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
299
+ }
300
+
301
+ // 5.后置处理
302
+ post(resp.BaseResp);
303
+
304
+ let recordIDList: Array<number> = new Array();
305
+ let errMap: Map<number, string> = new Map();
306
+ resp.RecordIDs.forEach(
307
+ (v: bigint, idx: number) => {
308
+ recordIDList.push(Number(v));
309
+ });
310
+ resp.ErrMap.forEach(
311
+ (v: string, k: bigint) => {
312
+ errMap.set(Number(k), v);
313
+ }
314
+ );
315
+ return {
316
+ record_ids: recordIDList,
317
+ err_map: errMap
318
+ };
319
+ }
320
+
321
+ async function updateRecordsBySync(objectApiName: string, recordMap: Record<number, object>) {
322
+ // 1.check
323
+ if (!objectApiName) {
324
+ throw new exceptions.InvalidParamError("objectApiName is empty");
325
+ }
326
+
327
+ // 2.前置处理
328
+ let ctx = await pre();
329
+
330
+ // 3.构造 params
331
+ let param: any = {};
332
+ let task_id = utils.getTriggerTaskID();
333
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
334
+ param.ObjectAPIAlias = objectApiName;
335
+ param.Data = utils.stringify(recordMap);
336
+ param.OperatorID = BigInt(-1);
337
+ if (task_id) {
338
+ param.AutomationTaskID = BigInt(task_id);
339
+ }
340
+
341
+ // 4.发起请求
342
+ let resp;
343
+ try {
344
+ resp = await rpc.getInnerAPICli().BatchUpdateRecordBySync(ctx, param);
345
+ } catch (err) {
346
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
347
+ }
348
+
349
+ // 5.后置处理
350
+ post(resp.BaseResp);
351
+
352
+ let errMap: Map<number, string> = new Map();
353
+ resp.ErrMap.forEach(
354
+ (v: string, k: bigint) => {
355
+ errMap.set(Number(k), v);
356
+ }
357
+ );
358
+ return { err_map: errMap };
359
+ }
360
+
361
+ async function deleteRecordsBySync(objectApiName: string, recordIDs: number[]) {
362
+ // 1.check
363
+ if (!objectApiName) {
364
+ throw new exceptions.InvalidParamError("objectApiName is empty");
365
+ }
366
+
367
+ // 2.前置处理
368
+ let ctx = await pre();
369
+
370
+ // 3.构造 params
371
+ let param: any = {};
372
+ let task_id = utils.getTriggerTaskID();
373
+ let recordIDList: Array<bigint> = new Array();
374
+ recordIDs.forEach(
375
+ (v: number, idx: number) => {
376
+ recordIDList.push(BigInt(v));
377
+ }
378
+ );
379
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
380
+ param.ObjectAPIAlias = objectApiName;
381
+ param.RecordIDs = recordIDList;
382
+ param.OperatorID = BigInt(-1);
383
+ if (task_id) {
384
+ param.AutomationTaskID = BigInt(task_id);
385
+ }
386
+
387
+ // 4.发起请求
388
+ let resp;
389
+ try {
390
+ resp = await rpc.getInnerAPICli().BatchDeleteRecordBySync(ctx, param);
391
+ } catch (err) {
392
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
393
+ }
394
+
395
+ // 5.后置处理
396
+ post(resp.BaseResp);
397
+
398
+ let errMap: Map<number, string> = new Map();
399
+ resp.ErrMap.forEach(
400
+ (v: string, k: bigint) => {
401
+ errMap.set(Number(k), v);
402
+ }
403
+ );
404
+ return { err_map: errMap };
405
+ }
406
+
407
+ async function getRecordsOrCountByCriterion(objectApiName: string, criterion: string, order: Order[], ignoreBackLookupField: boolean, fieldApiNames: string[], offset: number, limit: number, needCount: boolean) {
408
+ if (limit > 1000) {
409
+ limit = 1000;
410
+ }
411
+
412
+ // 1.check
413
+ if (!objectApiName) {
414
+ throw new exceptions.InvalidParamError("objectApiName is empty");
415
+ }
416
+
417
+ // 2.前置处理
418
+ let ctx = await pre();
419
+
420
+ // 3.构造 params
421
+ let param: any = {};
422
+ let orders = new Array();
423
+ order.forEach(
424
+ (v: Order, idx: number) => {
425
+ orders.push({
426
+ field: v.field,
427
+ type: v.type,
428
+ direction: v.direction,
429
+ } as any);
430
+
431
+ }
432
+ );
433
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
434
+ param.ObjectAPIAlias = objectApiName;
435
+ param.Criterion = criterion;
436
+ param.Order = orders;
437
+ param.IgnoreBackLookupField = ignoreBackLookupField;
438
+ param.Limit = BigInt(limit);
439
+ param.offset = BigInt(offset);
440
+ param.FieldAPIAPIAliases = fieldApiNames;
441
+ param.NeedFilterUserPermission = false;
442
+ param.NeedTotalCount = needCount;
443
+
444
+ // 4.发起请求
445
+ let resp;
446
+ try {
447
+ resp = await rpc.getInnerAPICli().BatchGetRecordWithCriterionV3(ctx, param);
448
+ } catch (err) {
449
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
450
+ }
451
+
452
+ // 5.后置处理
453
+ post(resp.BaseResp);
454
+
455
+ if (needCount) {
456
+ return Number(resp.Total);
457
+ } else {
458
+ return JSON.parse(resp.DataList);
459
+ }
460
+ }
461
+
462
+ async function updateWorkflowVariables(iCtx: any, instanceId: number, variables: object, variableTypes: object): Promise<void> {
463
+ // 1.前置处理
464
+ let ctx = await pre();
465
+
466
+ // 2.构造 params
467
+ let param: any = {};
468
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
469
+ param.WorkflowInstanceID = BigInt(instanceId);
470
+ param.Variables = variables;
471
+
472
+ // 3.发起请求
473
+ let resp;
474
+ try {
475
+ resp = await rpc.getInnerAPICli().UpdateVariablesByAPIAlias(ctx, param);
476
+ } catch (err) {
477
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
478
+ }
479
+
480
+ // 4.后置处理
481
+ post(resp.BaseResp);
482
+ return;
483
+ }
484
+
485
+ async function uploadFile(data: Stream, expire: number): Promise<UploadFileResp> {
486
+ // 1.前置处理
487
+ let ctx = await pre();
488
+
489
+ // 2.构造 params
490
+ let param: any = {};
491
+ param.Data = await streamToBuffer(data as any);
492
+ param.Name = "file"
493
+ param.EncryptType = 1
494
+ param.PermissionConf = {
495
+ PermissionType: "",
496
+ PermissionInfo: "",
497
+ IgnoreUserID: true
498
+ }
499
+ if (expire) {
500
+ param.ExpireSecond = expire
501
+ }
502
+
503
+ // 3.发起请求
504
+ let resp;
505
+ try {
506
+ resp = await rpc.getInnerAPICli().UploadAttachmentForRPC(ctx, param);
507
+ } catch (err) {
508
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
509
+ }
510
+
511
+ // 4.后置处理
512
+ post(resp.BaseResp);
513
+
514
+ return {
515
+ id: resp.ID,
516
+ token: resp.Token,
517
+ size: resp.Size,
518
+ mimeType: resp.MimeType,
519
+ name: resp.Filename,
520
+ };
521
+ }
522
+
523
+ async function downloadFileByID(fileID: string, filePath?: string): Promise<Buffer | void> {
524
+ // 1. build parameters
525
+ let param: any = {};
526
+ param.FileID = fileID;
527
+
528
+ // 2. prepare request
529
+ let ctx = await pre();
530
+
531
+ // 3. do request
532
+ let resp;
533
+ try {
534
+ resp = await rpc.getInnerAPICli().DownloadFile(ctx, param);
535
+ } catch (err) {
536
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
537
+ }
538
+
539
+ // 4. deal with response
540
+ post(resp.BaseResp);
541
+
542
+ if (filePath) {
543
+ checkUtils.checkFile(filePath);
544
+ utils.mkdirs(path.dirname(filePath));
545
+ fs.writeFileSync(filePath, resp.Data);
546
+ return;
547
+ } else {
548
+ return resp.Data;
549
+ }
550
+ }
551
+
552
+ async function downloadFileByToken(fileToken: string, filePath?: string): Promise<Buffer | void> {
553
+ // 1. build parameters
554
+ let param: any = {};
555
+ param.Token = fileToken;
556
+
557
+ // 2. prepare request
558
+ let ctx = await pre();
559
+
560
+ // 3. do request
561
+ let resp;
562
+ try {
563
+ resp = await rpc.getInnerAPICli().DownloadAttachment(ctx, param);
564
+ } catch (err) {
565
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
566
+ }
567
+
568
+ // 4. deal with response
569
+ post(resp.BaseResp);
570
+
571
+ if (filePath) {
572
+ checkUtils.checkFile(filePath);
573
+ utils.mkdirs(path.dirname(filePath));
574
+ fs.writeFileSync(filePath, resp.Data);
575
+ return;
576
+ } else {
577
+ return resp.Data;
578
+ }
579
+ }
580
+
581
+ async function createMessage(msg: any): Promise<number> {
582
+ // 1. prepare args
583
+ if (!msg.icon) {
584
+ throw new exceptions.InvalidParamError(`parameter icon is required!`);
585
+ }
586
+
587
+ if (typeof (msg.icon) != 'string') {
588
+ throw new exceptions.InvalidParamError(`Invalid format of parameter icon, icon should be string.`);
589
+ }
590
+ let params: any = {};
591
+ let modelKeySearchData = new Map<string, string>().set("icon_type", msg.icon as string);
592
+
593
+ let receiverIds = msg["target_users"] || [];
594
+ if (!(receiverIds instanceof Array)) {
595
+ throw new exceptions.InvalidParamError(`Invalid format of parameter target_users, target_users should be array.`);
596
+ }
597
+ if (receiverIds.length === 0) {
598
+ throw new exceptions.InvalidParamError(`parameter target_users is required!`);
599
+ }
600
+
601
+ let percent = msg["percent"];
602
+ if (percent !== undefined && percent !== null) {
603
+ if (typeof (percent) != 'number') {
604
+ throw new exceptions.InvalidParamError(`Invalid format of parameter percent, percent should be number.`);
605
+ }
606
+ if (percent < 0 || percent > 100) {
607
+ throw new exceptions.InvalidParamError(`Invalid value of parameter percent, the value should be in range [0, 100].`);
608
+ } else {
609
+ modelKeySearchData.set("percent", JSON.stringify(percent));
610
+ }
611
+ }
612
+
613
+ // kunlun类型
614
+ if (common.checkUtils.isObject(msg)) {
615
+ for (let key of Object.keys(msg)) {
616
+ if (common.fieldType.isKFieldType(msg[key])) {
617
+ // instanceof 改动6
618
+ // if (msg[key] instanceof fieldType.Multilingual) {
619
+ if (common.fieldType.isKFieldTypeMultilingual((msg[key]))) {
620
+ let value = JSON.stringify(msg[key].toJSON());
621
+ params[key] = JSON.stringify({ "value": value });
622
+ } else {
623
+ params[key] = JSON.stringify(msg[key].toJSON());
624
+ }
625
+ } else {
626
+ params[key] = JSON.stringify(msg[key]);
627
+ }
628
+ }
629
+ }
630
+
631
+ // 2. prepare for request
632
+ let ctx = await pre();
633
+
634
+ // 3. build parameters
635
+ let param: any = {};
636
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
637
+ param.NotifyModelKey = notifyModel.key;
638
+ param.ReceiverIDs = receiverIds;
639
+ param.ParamsRawData = params;
640
+ param.Percent = percent;
641
+ param.ModelKeySearchData = modelKeySearchData;
642
+
643
+ // 4. do request
644
+ let resp;
645
+ try {
646
+ resp = await rpc.getInnerAPICli().CreateNotifyTask(ctx, param);
647
+ } catch (err) {
648
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
649
+ }
650
+
651
+ // 5. deal with response
652
+ post(resp.BaseResp);
653
+
654
+ return Number(resp.TaskID);
655
+ }
656
+
657
+ async function updateMessage(msgId: number, msg: any): Promise<void> {
658
+ // 1. prepare args
659
+ if (!msgId) {
660
+ throw new exceptions.InvalidParamError(`parameter msgId is required!`);
661
+ }
662
+
663
+ if (typeof (msgId) != 'number') {
664
+ throw new exceptions.InvalidParamError(`Invalid format of parameter msgId, msgId should be number.`);
665
+ }
666
+
667
+ if (!msg.icon) {
668
+ throw new exceptions.InvalidParamError(`parameter icon is required!`);
669
+ }
670
+ if (typeof (msg.icon) != 'string') {
671
+ throw new exceptions.InvalidParamError(`Invalid format of parameter icon, icon should be string.`);
672
+ }
673
+ let params = new Map<string, string>();
674
+ let modelKeySearchData = new Map<string, string>().set("icon_type", msg.icon);
675
+
676
+ let percent = msg["percent"];
677
+ if (percent !== undefined && percent !== null) {
678
+ if (typeof (percent) != 'number') {
679
+ throw new exceptions.InvalidParamError(`Invalid format of parameter percent, percent should be number.`);
680
+ }
681
+ if (percent < 0 || percent > 100) {
682
+ throw new exceptions.InvalidParamError(`Invalid value of parameter percent in message ${msgId}, the value should be in range [0, 100].`);
683
+ } else {
684
+ modelKeySearchData.set("percent", JSON.stringify(percent));
685
+ }
686
+ }
687
+
688
+ // kunlun类型
689
+ if (common.checkUtils.isObject(msg)) {
690
+ for (let key of Object.keys(msg)) {
691
+ if (common.fieldType.isKFieldType(msg[key])) {
692
+ // instanceof 改动7
693
+ // if (msg[key] instanceof fieldType.Multilingual) {
694
+ if (common.fieldType.isKFieldTypeMultilingual(msg[key])) {
695
+ let value = JSON.stringify(msg[key].toJSON());
696
+ params.set(key, JSON.stringify({ "value": value }));
697
+ } else {
698
+ params.set(key, JSON.stringify(msg[key].toJSON()));
699
+ }
700
+ } else {
701
+ params.set(key, JSON.stringify(msg[key]));
702
+ }
703
+ }
704
+ }
705
+
706
+ // 2. prepare for request
707
+ let ctx = await pre();
708
+
709
+ // 3. build parameters
710
+ let param: any = {};
711
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
712
+ param.TaskID = BigInt(msgId);
713
+ param.NotifyModelKey = notifyModel.key;
714
+ param.ParamsRawData = params;
715
+ param.Percent = percent;
716
+ param.ModelKeySearchData = modelKeySearchData;
717
+ param.IsBackground = true;
718
+
719
+ // 4. do request
720
+ let resp;
721
+ try {
722
+ resp = await rpc.getInnerAPICli().UpdateNotifyTask(ctx, param);
723
+ } catch (err) {
724
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
725
+ }
726
+
727
+ // 5. deal with response
728
+ post(resp.BaseResp);
729
+ }
730
+
731
+ async function getFields(objectApiName: string): Promise<any> {
732
+ // 1. prepare request
733
+ let ctx = await pre();
734
+
735
+ // 2. build parameters
736
+ let param: any = {};
737
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
738
+ param.ObjectAPIName = objectApiName;
739
+
740
+ // 3. do request
741
+ let resp;
742
+ try {
743
+ resp = await rpc.getInnerAPICli().GetObjectFields(ctx, param);
744
+ } catch (err) {
745
+ throw new exceptions.InternalError(`Call InnerAPI getFields failed: ${err.message}`);
746
+ }
747
+
748
+ // 4.后置处理
749
+ post(resp.BaseResp);
750
+
751
+ try {
752
+ return JSON.parse(resp.Data)
753
+ } catch (e) {
754
+ throw new exceptions.InternalError(`JSON parse failed: ${e.message}`);
755
+ }
756
+ }
757
+
758
+ async function getField(objectApiName: string, fieldApiName: string): Promise<any> {
759
+ // 1. prepare request
760
+ let ctx = await pre();
761
+
762
+ // 2. build parameters
763
+ let param: any = {};
764
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
765
+ param.ObjectAPIName = objectApiName;
766
+ param.FieldAPIName = fieldApiName;
767
+
768
+ // 3. do request
769
+ let resp: any;
770
+ try {
771
+ resp = await rpc.getInnerAPICli().GetObjectField(ctx, param);
772
+ } catch (err) {
773
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
774
+ }
775
+
776
+ // 4. deal with response
777
+ post(resp.BaseResp);
778
+
779
+ try {
780
+ return JSON.parse(resp.Data)
781
+ } catch (e) {
782
+ throw new exceptions.InternalError(`JSON parse failed: ${e.message}`);
783
+ }
784
+ }
785
+
786
+ async function terminateWorkflowInstance(workflowInstanceId: number, operator: number, reason: string): Promise<void> {
787
+ // 1. prepare args
788
+ if (typeof (workflowInstanceId) !== "number") {
789
+ throw new exceptions.InvalidParamError(`The type of workflowInstanceId should be number, but ${typeof (workflowInstanceId)}.`);
790
+ }
791
+
792
+ // 2. prepare request
793
+ let ctx = await pre();
794
+
795
+ // 3. build parameters
796
+ let param: any = {};
797
+ param.WorkflowInstanceID = BigInt(workflowInstanceId);
798
+ param.Operator = BigInt(operator);
799
+ param.Reason = reason;
800
+
801
+ // 4. do request
802
+ let resp: any;
803
+ try {
804
+ resp = await rpc.getInnerAPICli().TerminateWorkflowInstance(ctx, param);
805
+ } catch (err) {
806
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
807
+ }
808
+
809
+ // 5. deal with response
810
+ post(resp.BaseResp);
811
+ }
812
+
813
+ async function modifyRecordsWithTransaction(placeholders: Record<string, number>, operations: object[]): Promise<Map<string, number>> {
814
+ // 1. prepare args
815
+ // TODO: change placeholders type
816
+ let ph: Map<string, bigint> = new Map();
817
+ for (let key of Object.keys(placeholders)) {
818
+ ph.set(key, BigInt(placeholders[key]));
819
+ }
820
+
821
+ // 2. build parameters
822
+ let param: any = {};
823
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
824
+ param.Placeholders = ph;
825
+ // TODO: change operations type
826
+ param.Operations = operations as any;
827
+ param.OperatorID = BigInt(-1);
828
+ const taskID = utils.getTriggerTaskID();
829
+ if (taskID) {
830
+ param.TaskID = BigInt(taskID);
831
+ }
832
+
833
+ // 3. prepare request
834
+ let ctx = await pre();
835
+
836
+ // 4. do request
837
+ let resp;
838
+ try {
839
+ resp = await rpc.getInnerAPICli().ModifyRecordsWithTX(ctx, param);
840
+ } catch (err) {
841
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
842
+ }
843
+
844
+ // 5. deal with response
845
+ post(resp.BaseResp);
846
+
847
+ return resp.Placeholders;
848
+ }
849
+
850
+ async function getGlobalConfigByKey<valueT>(key: string): Promise<valueT> {
851
+ // 1. prepare request
852
+ let ctx = await pre();
853
+
854
+ // 2. get all global configs by page
855
+ let pageSize = 100, offset = 0;
856
+ for (let i = 0; ; i++) {
857
+ let param: any = {};
858
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
859
+ param.BizType = "GlobalVariables"
860
+ param.UsedBy = "UsedBySystem"
861
+ param.Filter = {
862
+ Offset: BigInt(offset * i),
863
+ Limit: BigInt(pageSize),
864
+ }
865
+
866
+ // 3. do request
867
+ let resp: any;
868
+ try {
869
+ resp = await rpc.getInnerAPICli().GetAllConfig(ctx, param);
870
+ } catch (err) {
871
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
872
+ }
873
+
874
+ post(resp.BaseResp);
875
+
876
+ // 4. find the config
877
+ for (let config of resp.Configs) {
878
+ if (config.Key === key) {
879
+ return config.Value;
880
+ }
881
+ }
882
+
883
+ // 5. check if there is next page
884
+ if (resp.Configs.length < 100) {
885
+ break;
886
+ }
887
+ }
888
+
889
+ throw new exceptions.InvalidParamError(`undefined global variable (${key})`);
890
+ }
891
+
892
+ async function oql(oql: string, args: any[], namedArgs: Record<string, any>): Promise<string | any[]> {
893
+ // 1. build parameters
894
+ const queryData = JSON.stringify({
895
+ "query": oql,
896
+ "args": args,
897
+ "named_args": namedArgs,
898
+ "compat": true
899
+ });
900
+
901
+ // 2. prepare args
902
+ let param: any = {};
903
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
904
+ param.QueryData = Buffer.from(queryData);
905
+
906
+ // 3. prepare request
907
+ let ctx = await pre();
908
+
909
+ // 4. do request
910
+ let resp: any;
911
+ try {
912
+ resp = await rpc.getInnerAPICli().QueryRecordsByOQL(ctx, param);
913
+ } catch (err) {
914
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
915
+ }
916
+
917
+ // 5. deal with response
918
+ post(resp.BaseResp);
919
+
920
+ try {
921
+ return JSON.parse(resp.Rows ? resp.Rows : "[]")
922
+ } catch (e) {
923
+ throw new exceptions.InternalError(`oql result parse failed: ${e.message}`);
924
+ }
925
+ }
926
+
927
+ async function openSDKCreateRecordBySync(objectApiName: string, record: object): Promise<any> {
928
+ // 1. prepare args
929
+ if (typeof (objectApiName) !== "string") {
930
+ throw new exceptions.InvalidParamError(`The type of objectApiName should be string, but ${typeof (objectApiName)}.`);
931
+ }
932
+
933
+ // 2. prepare request
934
+ let ctx = await pre();
935
+
936
+ // 3. build parameters
937
+ let param: any = {};
938
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
939
+ param.ObjectAPIName = objectApiName;
940
+ param.Record = record;
941
+
942
+ // 4. do request
943
+ let resp: any;
944
+ try {
945
+ resp = await rpc.getInnerAPICli().CreateRecord(ctx, param);
946
+ } catch (err) {
947
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
948
+ }
949
+
950
+ // 5. deal with response
951
+ post(resp.BaseResp);
952
+ }
953
+
954
+ async function openSDKUpdateRecordBySync(objectApiName: string, recordID: number, record: object): Promise<any> {
955
+ // 1. prepare args
956
+ if (typeof (objectApiName) !== "string") {
957
+ throw new exceptions.InvalidParamError(`The type of objectApiName should be string, but ${typeof (objectApiName)}.`);
958
+ }
959
+
960
+ if (typeof (recordID) !== "number") {
961
+ throw new exceptions.InvalidParamError(`The type of recordID should be number, but ${typeof (recordID)}.`);
962
+ }
963
+
964
+ // 2. prepare request
965
+ let ctx = await pre();
966
+
967
+ // 3. build parameters
968
+ let param: any = {};
969
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
970
+ param.ObjectAPIName = objectApiName;
971
+ param.RecordID = BigInt(recordID);
972
+ param.Record = record;
973
+
974
+ // 4. do request
975
+ let resp: any;
976
+ try {
977
+ resp = await rpc.getInnerAPICli().UpdateRecord(ctx, param);
978
+ } catch (err) {
979
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
980
+ }
981
+
982
+ // 5. deal with response
983
+ post(resp.BaseResp);
984
+ }
985
+
986
+ async function openSDKDeleteRecordBySync(objectApiName: string, recordID: number): Promise<any> {
987
+ // 1. prepare args
988
+ if (typeof (objectApiName) !== "string") {
989
+ throw new exceptions.InvalidParamError(`The type of objectApiName should be string, but ${typeof (objectApiName)}.`);
990
+ }
991
+
992
+ if (typeof (recordID) !== "number") {
993
+ throw new exceptions.InvalidParamError(`The type of recordID should be number, but ${typeof (recordID)}.`);
994
+ }
995
+
996
+ // 2. prepare request
997
+ let ctx = await pre();
998
+
999
+ // 3. build parameters
1000
+ let param: any = {};
1001
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1002
+ param.ObjectAPIName = objectApiName;
1003
+ param.RecordID = BigInt(recordID);
1004
+
1005
+ // 4. do request
1006
+ let resp: any;
1007
+ try {
1008
+ resp = await rpc.getInnerAPICli().DeleteRecord(ctx, param);
1009
+ } catch (err) {
1010
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1011
+ }
1012
+
1013
+ // 5. deal with response
1014
+ post(resp.BaseResp);
1015
+ }
1016
+
1017
+ async function openSDKCreateRecordsBySync(objectApiName: string, records: object[]): Promise<any> {
1018
+ // 1. prepare args
1019
+ if (typeof (objectApiName) !== "string") {
1020
+ throw new exceptions.InvalidParamError(`The type of objectApiName should be string, but ${typeof (objectApiName)}.`);
1021
+ }
1022
+
1023
+ // 2. prepare request
1024
+ let ctx = await pre();
1025
+
1026
+ // 3. build parameters
1027
+ let param: any = {};
1028
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1029
+ param.ObjectAPIName = objectApiName;
1030
+ param.Records = records;
1031
+
1032
+ // 4. do request
1033
+ let resp: any;
1034
+ try {
1035
+ resp = await rpc.getInnerAPICli().BatchCreateRecords(ctx, param);
1036
+ } catch (err) {
1037
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1038
+ }
1039
+
1040
+ // 5. deal with response
1041
+ post(resp.BaseResp);
1042
+ }
1043
+
1044
+ async function openSDKUpdateRecordsBySync(objectApiName: string, recordMap: Record<number, object>): Promise<any> {
1045
+ // 1. prepare args
1046
+ if (typeof (objectApiName) !== "string") {
1047
+ throw new exceptions.InvalidParamError(`The type of objectApiName should be string, but ${typeof (objectApiName)}.`);
1048
+ }
1049
+
1050
+ // 2. prepare request
1051
+ let ctx = await pre();
1052
+
1053
+ // 3. build parameters
1054
+ let param: any = {};
1055
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1056
+ param.ObjectAPIName = objectApiName;
1057
+ param.RecordMap = recordMap;
1058
+
1059
+ // 4. do request
1060
+ let resp: any;
1061
+ try {
1062
+ resp = await rpc.getInnerAPICli().BatchUpdateRecords(ctx, param);
1063
+ } catch (err) {
1064
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1065
+ }
1066
+
1067
+ // 5. deal with response
1068
+ post(resp.BaseResp);
1069
+ }
1070
+
1071
+ async function openSDKDeleteRecordsBySync(objectApiName: string, recordIDs: number[]): Promise<any> {
1072
+ // 1. prepare args
1073
+ if (typeof (objectApiName) !== "string") {
1074
+ throw new exceptions.InvalidParamError(`The type of objectApiName should be string, but ${typeof (objectApiName)}.`);
1075
+ }
1076
+
1077
+ // 2. prepare request
1078
+ let ctx = await pre();
1079
+
1080
+ // 3. build parameters
1081
+ let param: any = {};
1082
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1083
+ param.ObjectAPIName = objectApiName;
1084
+ param.RecordIDs = recordIDs;
1085
+
1086
+ // 4. do request
1087
+ let resp: any;
1088
+ try {
1089
+ resp = await rpc.getInnerAPICli().BatchDeleteRecords(ctx, param);
1090
+ } catch (err) {
1091
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1092
+ }
1093
+
1094
+ // 5. deal with response
1095
+ post(resp.BaseResp);
1096
+ }
1097
+
1098
+ async function openSDKGetRecords(objectApiName: string, param: OpenSDKGetRecordsReq): Promise<any | number> {
1099
+ // 1. prepare args
1100
+ if (param.limit <= 0 || param.limit > 200) {
1101
+ param.limit = 200;
1102
+ }
1103
+ let req: any;
1104
+ req.ObjectAPIAlias = objectApiName;
1105
+ req.Filter = JSON.stringify(param.filter);
1106
+ req.Limit = BigInt(param.limit);
1107
+ req.Offset = BigInt(param.offset);
1108
+ req.FiledAPIAlias = param.fields;
1109
+ req.NeedTotalCount = param.count;
1110
+ let sortList: any[] = [];
1111
+ if (param.sort) {
1112
+ param.sort.forEach((sort) => {
1113
+ sortList.push({
1114
+ FieldApiAlias: sort.field,
1115
+ Type: sort.type,
1116
+ Direction: sort.direction,
1117
+ });
1118
+ })
1119
+ }
1120
+ req.Sort = sortList;
1121
+
1122
+ // 2. prepare request
1123
+ let ctx = await pre();
1124
+
1125
+ // 3. do request
1126
+ let resp: any;
1127
+ try {
1128
+ resp = await rpc.getInnerAPICli().GetRecordList(ctx, req);
1129
+ } catch (err) {
1130
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1131
+ }
1132
+
1133
+ // 4. deal with response
1134
+ post(resp.BaseResp);
1135
+
1136
+ // 5. return result
1137
+ if (param.count) {
1138
+ return resp.Total;
1139
+ } else {
1140
+ return resp.Data;
1141
+ }
1142
+ };
1143
+
1144
+ async function openSDKUploadFile(fileName: string, data: Buffer): Promise<any> {
1145
+ // 1. prepare args
1146
+ let param: any;
1147
+ param.Name = fileName;
1148
+ param.EncryptType = 1; // no encrypt
1149
+ param.Data = data;
1150
+
1151
+ // 2. prepare request
1152
+ let ctx = await pre();
1153
+
1154
+ // 3. do request
1155
+ let resp: any;
1156
+ try {
1157
+ resp = await rpc.getAttachmentCli().UploadFileForRPC(ctx, param);
1158
+ } catch (err) {
1159
+ throw new exceptions.InternalError(`Call Attachment failed: ${err.message}`);
1160
+ }
1161
+
1162
+ // 4. deal with response
1163
+ post(resp.BaseResp);
1164
+
1165
+ return resp;
1166
+ }
1167
+
1168
+ async function openSDKUploadAvatar(fileName: string, data: Buffer): Promise<any> {
1169
+ // 1. prepare args
1170
+ let param: any;
1171
+ param.EncryptType = 1; // no encrypt
1172
+ param.Data = data;
1173
+
1174
+ // 2. prepare request
1175
+ let ctx = await pre();
1176
+
1177
+ // 3. do request
1178
+ let resp: any;
1179
+ try {
1180
+ resp = await rpc.getAttachmentCli().UploadAvatarForRPC(ctx, param);
1181
+ } catch (err) {
1182
+ throw new exceptions.InternalError(`Call Attachment failed: ${err.message}`);
1183
+ }
1184
+
1185
+ // 4. deal with response
1186
+ post(resp.BaseResp);
1187
+
1188
+ return resp;
1189
+ }
1190
+
1191
+ async function openSDKDownloadAvatar(imageID: string): Promise<Buffer> {
1192
+ // 1. prepare args
1193
+ let param: any;
1194
+ param.ImageID = imageID;
1195
+
1196
+ // 2. prepare request
1197
+ let ctx = await pre();
1198
+
1199
+ // 3. do request
1200
+ let resp;
1201
+ try {
1202
+ resp = await rpc.getAttachmentCli().DownloadAvatarForRPC(ctx, param);
1203
+ } catch (err) {
1204
+ throw new exceptions.InternalError(`Call Attachment failed: ${err.message}`);
1205
+ }
1206
+
1207
+ // 4. deal with response
1208
+ post(resp.BaseResp);
1209
+
1210
+ return resp.Data;
1211
+ }
1212
+
1213
+ export class RequestRpc implements IInnerAPIRequest {
1214
+
1215
+ constructor() {
1216
+ this.updateWorkflowVariables = updateWorkflowVariables;
1217
+ this.createRecordBySync = createRecordBySync;
1218
+ this.updateRecordBySync = updateRecordBySync;
1219
+ this.deleteRecordBySync = deleteRecordBySync;
1220
+ this.createRecordsByAsync = createRecordsByAsync;
1221
+ this.updateRecordsByAsync = updateRecordsByAsync;
1222
+ this.deleteRecordsByAsync = deleteRecordsByAsync;
1223
+ this.createRecordsBySync = createRecordsBySync;
1224
+ this.updateRecordsBySync = updateRecordsBySync;
1225
+ this.deleteRecordsBySync = deleteRecordsBySync;
1226
+ this.getRecordsOrCountByCriterion = getRecordsOrCountByCriterion;
1227
+
1228
+ this.uploadFile = uploadFile;
1229
+ this.downloadFileByID = downloadFileByID
1230
+ this.downloadFileByToken = downloadFileByToken;
1231
+ this.createMessage = createMessage;
1232
+ this.updateMessage = updateMessage;
1233
+ this.getFields = getFields;
1234
+ this.getField = getField;
1235
+ this.terminateWorkflowInstance = terminateWorkflowInstance;
1236
+ this.modifyRecordsWithTransaction = modifyRecordsWithTransaction;
1237
+ this.getGlobalConfigByKey = getGlobalConfigByKey;
1238
+ this.oql = oql;
1239
+
1240
+ // open sdk api
1241
+ this.openSDKCreateRecordBySync = openSDKCreateRecordBySync;
1242
+ this.openSDKUpdateRecordBySync = openSDKUpdateRecordBySync;
1243
+ this.openSDKDeleteRecordBySync = openSDKDeleteRecordBySync;
1244
+ this.openSDKCreateRecordsBySync = openSDKCreateRecordsBySync;
1245
+ this.openSDKUpdateRecordsBySync = openSDKUpdateRecordsBySync;
1246
+ this.openSDKDeleteRecordsBySync = openSDKDeleteRecordsBySync;
1247
+ this.openSDKGetRecords = openSDKGetRecords;
1248
+ this.openSDKUploadFile = openSDKUploadFile;
1249
+ this.openSDKUploadAvatar = openSDKUploadAvatar;
1250
+ this.openSDKDownloadAvatar = openSDKDownloadAvatar;
1251
+ }
1252
+
1253
+ createRecordBySync(objectApiName: string, record: object): any {
1254
+ }
1255
+
1256
+ updateRecordBySync(objectApiName: string, recordID: number, record: object): any {
1257
+ }
1258
+
1259
+ deleteRecordBySync(objectApiName: string, recordID: number): any {
1260
+ }
1261
+
1262
+ createRecordsByAsync(objectApiName: string, records: object[]): any {
1263
+ }
1264
+
1265
+ updateRecordsByAsync(objectApiName: string, recordMap: Record<number, object>): any {
1266
+ }
1267
+
1268
+ deleteRecordsByAsync(objectApiName: string, recordIDs: number[]): any {
1269
+ }
1270
+
1271
+ createRecordsBySync(objectApiName: string, records: object[]): any {
1272
+ }
1273
+
1274
+ updateRecordsBySync(objectApiName: string, recordMap: Record<number, object>): any {
1275
+ }
1276
+
1277
+ deleteRecordsBySync(objectApiName: string, recordIDs: number[]): any {
1278
+ }
1279
+
1280
+ getRecordsOrCountByCriterion(objectApiName: string, criterion: string | Criterion, order: Order[], ignoreBackLookupField: boolean, fieldApiNames: string[], offset: number, limit: number, needCount: boolean): any {
1281
+ }
1282
+
1283
+ async updateWorkflowVariables(ctx: any, instanceId: number, variables: object, variableTypes: object): Promise<void> {
1284
+ }
1285
+
1286
+ async uploadFile(data: Stream, expire: number): Promise<UploadFileResp> {
1287
+ return null;
1288
+ }
1289
+
1290
+ async downloadFileByID(fileID: string, filePath?: string): Promise<Buffer | void> {
1291
+ return null;
1292
+ }
1293
+
1294
+ async downloadFileByToken(fileToken: string, filePath?: string): Promise<Buffer | void> {
1295
+ return null;
1296
+ }
1297
+
1298
+ async createMessage(msg: any): Promise<number> {
1299
+ return null;
1300
+ }
1301
+
1302
+ async updateMessage(msgId: number, msg: any): Promise<void> {
1303
+ return null;
1304
+ }
1305
+
1306
+ async getFields(objectApiName: string): Promise<any> {
1307
+ }
1308
+
1309
+ async getField(objectApiName: string, fieldApiName: string): Promise<any> {
1310
+ }
1311
+
1312
+ async terminateWorkflowInstance(workflowInstanceId: number, operator: number, reason: string): Promise<void> {
1313
+ }
1314
+
1315
+ async modifyRecordsWithTransaction(placeholders: Record<string, number>, operations: object[]): Promise<Map<string, number>> {
1316
+ return null;
1317
+ }
1318
+
1319
+ async getGlobalConfigByKey<valueT>(key: string): Promise<valueT> {
1320
+ return null;
1321
+ }
1322
+
1323
+ async oql(oql: string, args: any[], namedArgs: Record<string, any>): Promise<string | any[]> {
1324
+ return [];
1325
+ }
1326
+
1327
+ async invokeFuncSync(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1328
+ return await invokeFuncSync(idOrName, params);
1329
+ }
1330
+
1331
+ async invokeFuncWithAuth(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1332
+ return await invokeFuncWithAuth(idOrName, params);
1333
+ }
1334
+
1335
+ async createAsyncTaskV1(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1336
+ return await createAsyncTaskV1(idOrName, params);
1337
+ }
1338
+
1339
+ async createAsyncTaskV2(name: string, params: any): Promise<any> {
1340
+ return await createAsyncTaskV2(name, params);
1341
+ }
1342
+
1343
+ async getExecutionUserTaskInfo(executionId: bigint): Promise<any> {
1344
+ return await getExecutionUserTaskInfo(executionId);
1345
+ }
1346
+
1347
+ async executeFlow(APIName: string, options: any): Promise<ExecutionResult> {
1348
+ return await executeFlow(APIName, options);
1349
+ }
1350
+
1351
+ async revokeExecution(executionId: number, revokeOptions: RevokeExecutionOptions): Promise<void> {
1352
+ return await revokeExecution(executionId, revokeOptions);
1353
+ }
1354
+
1355
+ async getExecutionInfo(executionId: number): Promise<ExecutionInfo> {
1356
+ return await getExecutionInfo(executionId);
1357
+ }
1358
+
1359
+ async getTenantInfo(appCtx: AppCtx): Promise<any> {
1360
+ return await appCtx.credential.getTenantInfo();
1361
+ }
1362
+
1363
+ // open sdk empty implement
1364
+ openSDKCreateRecordBySync(objectApiName: string, record: object): any {
1365
+ };
1366
+
1367
+ openSDKUpdateRecordBySync(objectApiName: string, recordID: number, record: object): any {
1368
+ };
1369
+
1370
+ openSDKDeleteRecordBySync(objectApiName: string, recordID: number): any {
1371
+ };
1372
+
1373
+ openSDKCreateRecordsBySync(objectApiName: string, records: object[]): any {
1374
+ };
1375
+
1376
+ openSDKUpdateRecordsBySync(objectApiName: string, recordMap: Record<number, object>): any {
1377
+ };
1378
+
1379
+ openSDKDeleteRecordsBySync(objectApiName: string, recordIDs: number[]): any {
1380
+ };
1381
+
1382
+ openSDKGetRecords(objectApiName: string, param: OpenSDKGetRecordsReq) {
1383
+ };
1384
+
1385
+ openSDKUploadFile(fileName: string, data: Buffer): any {
1386
+ };
1387
+
1388
+ openSDKUploadAvatar(fileName: string, data: Buffer): any {
1389
+ };
1390
+
1391
+ openSDKDownloadAvatar(imageID: string): any {
1392
+ };
1393
+ }
1394
+
1395
+ async function invokeFuncWithAuth(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1396
+ let ctx = await pre(true);
1397
+
1398
+ let param: any = {};
1399
+
1400
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1401
+ if (idOrName.isInvokeByAPIName) {
1402
+ param.ApiName = idOrName.APIName;
1403
+ } else {
1404
+ param.ApiID = idOrName.APIId;
1405
+ }
1406
+ param.Params = JSON.stringify(params);
1407
+ param.Context = JSON.stringify(getTriggerCtx());
1408
+
1409
+ let resp: any = undefined;
1410
+ try {
1411
+ resp = await rpc.getInnerAPICli().InvokeFunctionWithAuth(ctx, param);
1412
+ } catch (err) {
1413
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1414
+ }
1415
+
1416
+ post(resp.BaseResp);
1417
+
1418
+ let result: { code: string, msg: string, data: any };
1419
+ try {
1420
+ result = JSON.parse(resp.result);
1421
+ } catch (e) {
1422
+ throw new exceptions.InternalError(`JSON parse failed for ${resp.result}`);
1423
+ }
1424
+ if (!result || result.code !== "0") {
1425
+ throw new exceptions.InvalidParamError(`result is invalid for code: ${result ? result.code : ""} msg: ${result ? result.msg : ""}`)
1426
+ }
1427
+ return result.data;
1428
+ }
1429
+
1430
+ async function executeFlow(APIName: string, options: any): Promise<ExecutionResult> {
1431
+ let ctx = await pre();
1432
+
1433
+ let param: any = {};
1434
+
1435
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
1436
+ param.Operator = BigInt(utils.getUserIDFromCtx());
1437
+ param.FlowAPIName = APIName;
1438
+ param.variables = JSON.stringify(transMapToFlowVariable(options.params));
1439
+ param.LoopMasks = utils.getLoopMasks();
1440
+
1441
+ let resp: any = undefined;
1442
+ try {
1443
+ resp = await rpc.getInnerAPICli().Execute(ctx, param);
1444
+ } catch (err) {
1445
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1446
+ }
1447
+
1448
+ // 5.后置处理
1449
+ post(resp.BaseResp);
1450
+
1451
+ let outParams: any = [];
1452
+ try {
1453
+ if (resp.OutParams) {
1454
+ outParams = JSON.parse(resp.OutParams);
1455
+ }
1456
+ } catch (e) {
1457
+ throw new InternalError(`Execute OutParams in Result is invalid (${resp.OutParams}), err: ${e.message}`);
1458
+ }
1459
+
1460
+ return {
1461
+ executionId: Number(resp.ExecutionID),
1462
+ status: resp.Status as any,
1463
+ data: transFlowVariableToMap(outParams),
1464
+ errCode: resp.errCode ? resp.errCode : undefined,
1465
+ errMsg: resp.errMsg ? resp.errMsg : undefined,
1466
+ };
1467
+ }
1468
+
1469
+ export async function revokeExecution(executionId: number, revokeOptions: RevokeExecutionOptions): Promise<void> {
1470
+ let ctx = await pre();
1471
+
1472
+ let param: any = {};
1473
+
1474
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
1475
+ param.Operator = BigInt(utils.getUserIDFromCtx());
1476
+ param.ExecutionID = BigInt(executionId);
1477
+ let reason: any = undefined;
1478
+ if (revokeOptions && revokeOptions.reason) {
1479
+ reason = {
1480
+ En: revokeOptions.reason.en_US,
1481
+ Zh: revokeOptions.reason.zh_CN
1482
+ }
1483
+ }
1484
+ param.Reason = reason;
1485
+
1486
+ let resp = undefined;
1487
+ try {
1488
+ resp = await rpc.getInnerAPICli().RevokeExecution(ctx, param);
1489
+ } catch (err) {
1490
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1491
+ }
1492
+
1493
+ // 5.后置处理
1494
+ post(resp.BaseResp);
1495
+ }
1496
+
1497
+ export async function getExecutionInfo(executionId: number): Promise<ExecutionInfo> {
1498
+ let ctx = await pre();
1499
+
1500
+ let param: any = {};
1501
+
1502
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
1503
+ param.ExecutionID = BigInt(executionId);
1504
+ param.Operator = BigInt(utils.getUserIDFromCtx());
1505
+
1506
+ let resp: any = undefined;
1507
+ try {
1508
+ resp = await rpc.getInnerAPICli().GetExecutionInfo(ctx, param);
1509
+ } catch (err) {
1510
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1511
+ }
1512
+
1513
+ // 5.后置处理
1514
+ post(resp.BaseResp);
1515
+
1516
+ if (!resp || !resp.ExecutionInfo) {
1517
+ throw new InternalError(`GetExecutionInfo Response is empty`)
1518
+ }
1519
+
1520
+ let outParams: any = [];
1521
+ try {
1522
+ if (resp.ExecutionInfo.outParams) {
1523
+ outParams = JSON.parse(resp.ExecutionInfo.outParams);
1524
+ }
1525
+ } catch (e) {
1526
+ throw new InternalError(`GetExecutionInfo OutParams in Result is invalid (${resp.ExecutionInfo.outParams}), err: ${e.message}`);
1527
+ }
1528
+
1529
+ return {
1530
+ status: resp.ExecutionInfo.status as any,
1531
+ data: transFlowVariableToMap(outParams),
1532
+ errCode: resp.ExecutionInfo.errCode ? resp.ExecutionInfo.errCode : undefined,
1533
+ errMsg: resp.ExecutionInfo.errMsg ? resp.ExecutionInfo.errMsg : undefined,
1534
+ };
1535
+ }
1536
+
1537
+ export async function getExecutionUserTaskInfo(executionId: bigint): Promise<any> {
1538
+ let ctx = await pre();
1539
+
1540
+ let param: any = {};
1541
+
1542
+ param.NameSpace = await getNamespaceForOpenAndFaaSSDK();
1543
+ param.ExecutionID = BigInt(executionId);
1544
+ param.Operator = BigInt(utils.getUserIDFromCtx());
1545
+
1546
+ let resp: any = undefined;
1547
+ try {
1548
+ resp = await rpc.getInnerAPICli().GetExecutionUserTaskInfo(ctx, param);
1549
+ } catch (err) {
1550
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1551
+ }
1552
+
1553
+ // 5.后置处理
1554
+ post(resp.BaseResp);
1555
+
1556
+ return resp.taskList;
1557
+ }
1558
+
1559
+ async function invokeFuncSync(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1560
+ let ctx = await pre(true);
1561
+
1562
+ let param: any = {};
1563
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1564
+ if (idOrName.isInvokeByAPIName) {
1565
+ param.FunctionName = "";
1566
+ param.APIAlias = idOrName.APIName;
1567
+ } else {
1568
+ param.FunctionName = idOrName.APIId;
1569
+ param.APIAlias = "";
1570
+ }
1571
+ param.Params = JSON.stringify(params);
1572
+ param.Context = JSON.stringify(getTriggerCtx());
1573
+ param.TriggerType = utils.getTriggerType();
1574
+
1575
+ let resp;
1576
+ try {
1577
+ resp = await rpc.getInnerAPICli().InvokeFuncSync(ctx, param);
1578
+ } catch (err) {
1579
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1580
+ }
1581
+
1582
+ // 5.后置处理
1583
+ post(resp.BaseResp);
1584
+
1585
+ let result: any = {};
1586
+ try {
1587
+ result = JSON.parse(resp.Result);
1588
+ } catch (e) {
1589
+ throw new exceptions.InternalError(`Parse Result failed: ${e.message}`);
1590
+ }
1591
+
1592
+ if (result && result.code === "0") {
1593
+ return result.data;
1594
+ }
1595
+ throw new exceptions.InternalError(`[${result.code}] ${result.msg}`);
1596
+ }
1597
+
1598
+ async function createAsyncTaskV1(idOrName: { APIId?: string; APIName?: string; isInvokeByAPIName: boolean }, params: any): Promise<any> {
1599
+ let ctx = await pre(true);
1600
+
1601
+ let param: any = {};
1602
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1603
+ if (idOrName.isInvokeByAPIName) {
1604
+ param.Alias = idOrName.APIName;
1605
+ param.Name = ""; // required
1606
+ } else {
1607
+ param.Name = idOrName.APIId;
1608
+ }
1609
+ param.Params = JSON.stringify(params);
1610
+ param.Context = JSON.stringify(getTriggerCtx());
1611
+ param.TriggerType = utils.getTriggerType();
1612
+
1613
+ let resp;
1614
+ try {
1615
+ resp = await rpc.getInnerAPICli().CreateAsyncTaskOld(ctx, param);
1616
+ } catch (err) {
1617
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1618
+ }
1619
+
1620
+ // 5.后置处理
1621
+ post(resp.BaseResp);
1622
+
1623
+ return Number(resp.TaskID);
1624
+ }
1625
+
1626
+ async function createAsyncTaskV2(APIName: string, params: any): Promise<any> {
1627
+ let ctx = await pre(true);
1628
+
1629
+ let param: any = {};
1630
+ param.Namespace = await getNamespaceForOpenAndFaaSSDK();
1631
+ param.APIAlias = APIName;
1632
+ param.Params = JSON.stringify(params);
1633
+ param.Context = JSON.stringify(getTriggerCtx());
1634
+ param.TriggerType = utils.getTriggerType();
1635
+ param.Extra = getDebugExtraInfo()
1636
+
1637
+ let resp;
1638
+ try {
1639
+ resp = await rpc.getInnerAPICli().CreateAsyncTask(ctx, param);
1640
+ } catch (err) {
1641
+ throw new exceptions.InternalError(`Call InnerAPI failed: ${err.message}`);
1642
+ }
1643
+
1644
+ // 5.后置处理
1645
+ post(resp.BaseResp);
1646
+
1647
+ return Number(resp.TaskID);
1648
+ }