@or-sdk/flows 0.27.0-beta.427.0 → 0.27.1-beta.531.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/Flows.js CHANGED
@@ -1,19 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  var __assign = (this && this.__assign) || function () {
18
3
  __assign = Object.assign || function(t) {
19
4
  for (var s, i = 1, n = arguments.length; i < n; i++) {
@@ -61,422 +46,234 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
61
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
62
47
  }
63
48
  };
64
- var __importDefault = (this && this.__importDefault) || function (mod) {
65
- return (mod && mod.__esModule) ? mod : { "default": mod };
49
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
50
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
51
+ if (ar || !(i in from)) {
52
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
53
+ ar[i] = from[i];
54
+ }
55
+ }
56
+ return to.concat(ar || Array.prototype.slice.call(from));
66
57
  };
67
58
  Object.defineProperty(exports, "__esModule", { value: true });
68
59
  exports.Flows = void 0;
69
60
  var base_1 = require("@or-sdk/base");
70
- var lodash_1 = __importDefault(require("lodash"));
71
- var uuid_1 = require("uuid");
61
+ var data_hub_1 = require("@or-sdk/data-hub");
62
+ var deployer_1 = require("@or-sdk/deployer");
72
63
  var constants_1 = require("./constants");
64
+ var tags_1 = require("@or-sdk/tags");
73
65
  var utils_1 = require("./utils");
74
- var Flows = (function (_super) {
75
- __extends(Flows, _super);
66
+ var Flows = (function () {
76
67
  function Flows(params) {
77
- var _this = this;
78
- var discoveryUrl = params.discoveryUrl, token = params.token, accountId = params.accountId, dataHub2Url = params.dataHub2Url, deployerUrl = params.deployerUrl, sdkApiUrl = params.sdkApiUrl;
79
- _this = _super.call(this, {
68
+ var discoveryUrl = params.discoveryUrl, token = params.token, accountId = params.accountId, dataHubUrl = params.dataHubUrl, deployerUrl = params.deployerUrl;
69
+ this.dataHub = new data_hub_1.DataHub({
80
70
  token: token,
81
71
  discoveryUrl: discoveryUrl,
82
- serviceKey: constants_1.SERVICE_KEY,
83
72
  accountId: accountId,
84
- serviceUrl: dataHub2Url,
85
- }) || this;
86
- _this.deployerUrl = deployerUrl;
87
- _this.sdkApiUrl = sdkApiUrl;
88
- return _this;
89
- }
90
- Flows.prototype.init = function () {
91
- return __awaiter(this, void 0, void 0, function () {
92
- return __generator(this, function (_a) {
93
- return [2];
94
- });
95
- });
96
- };
97
- Flows.prototype.deleteFlow = function (flow, flowAlias, temporarily) {
98
- if (flowAlias === void 0) { flowAlias = 'dev'; }
99
- if (temporarily === void 0) { temporarily = true; }
100
- return __awaiter(this, void 0, void 0, function () {
101
- return __generator(this, function (_a) {
102
- return [2, this.callApiV2({
103
- route: "/flows/".concat(flow.id),
104
- data: {
105
- flowAlias: flowAlias,
106
- temporarily: temporarily,
107
- },
108
- method: 'delete',
109
- })];
110
- });
111
- });
112
- };
113
- Flows.prototype.recoverFlow = function (flow) {
114
- return __awaiter(this, void 0, void 0, function () {
115
- return __generator(this, function (_a) {
116
- return [2, this.callApiV2({
117
- route: "/flows/".concat(flow.id),
118
- method: 'patch',
119
- })];
120
- });
121
- });
122
- };
123
- Flows.prototype.getFlowById = function (id, projection, sandbox) {
124
- if (sandbox === void 0) { sandbox = false; }
125
- return __awaiter(this, void 0, void 0, function () {
126
- return __generator(this, function (_a) {
127
- return [2, this.callApiV2({
128
- route: "/flows/".concat(id),
129
- params: {
130
- projection: projection,
131
- sandbox: sandbox,
132
- },
133
- })];
134
- });
135
- });
136
- };
137
- Flows.prototype.getFlows = function (_a) {
138
- var _b = _a === void 0 ? {} : _a, _c = _b.query, query = _c === void 0 ? {} : _c, projection = _b.projection, _d = _b.includeDeleted, includeDeleted = _d === void 0 ? false : _d, _e = _b.includeExisting, includeExisting = _e === void 0 ? true : _e, _f = _b.sandbox, sandbox = _f === void 0 ? false : _f, limit = _b.limit, offset = _b.offset;
139
- return __awaiter(this, void 0, void 0, function () {
140
- return __generator(this, function (_g) {
141
- if (typeof query.id === 'string')
142
- query.id = query.id.split('.');
143
- if (includeDeleted && !includeExisting)
144
- query.isDeleted = true;
145
- if (!includeDeleted && includeExisting)
146
- query.isDeleted = false;
147
- return [2, this.callApiV2({
148
- route: '/flows',
149
- params: {
150
- projection: projection,
151
- query: query,
152
- sandbox: sandbox,
153
- limit: limit,
154
- offset: offset,
155
- },
156
- })];
157
- });
158
- });
159
- };
160
- Flows.prototype.getFlowEvents = function (_a) {
161
- var flowId = _a.flowId, _b = _a.alias, alias = _b === void 0 ? 'prod' : _b;
162
- return __awaiter(this, void 0, void 0, function () {
163
- return __generator(this, function (_c) {
164
- return [2, this.callApiV2({
165
- url: this.sdkApiUrl,
166
- route: "".concat(this.sdkApiUrl, "/event-manager/get-flow-events/").concat(flowId, "/").concat(alias),
167
- })];
168
- });
73
+ dataHubUrl: dataHubUrl,
169
74
  });
170
- };
171
- Flows.prototype.getTriggerFlows = function (triggers) {
172
- if (triggers === void 0) { triggers = []; }
173
- return __awaiter(this, void 0, void 0, function () {
174
- return __generator(this, function (_a) {
175
- return [2, this.callApiV2({
176
- url: this.sdkApiUrl,
177
- route: "".concat(this.sdkApiUrl, "event-manager/get-trigger-flows"),
178
- data: { triggers: triggers },
179
- method: 'post',
180
- })];
181
- });
75
+ this.deployer = new deployer_1.Deployer({
76
+ token: token,
77
+ discoveryUrl: discoveryUrl,
78
+ accountId: accountId,
79
+ deployerUrl: deployerUrl,
182
80
  });
183
- };
184
- Flows.prototype.listVersions = function (query) {
185
- return __awaiter(this, void 0, void 0, function () {
186
- return __generator(this, function (_a) {
187
- return [2, this.callApiV2({
188
- route: '/flows/versions',
189
- params: { query: query },
190
- })];
191
- });
81
+ this.tags = new tags_1.Tags({
82
+ token: token,
83
+ discoveryUrl: discoveryUrl,
84
+ accountId: accountId,
85
+ dataHubUrl: dataHubUrl,
192
86
  });
193
- };
194
- Flows.prototype.getVersion = function (params) {
87
+ }
88
+ Flows.prototype.init = function () {
195
89
  return __awaiter(this, void 0, void 0, function () {
196
- var data;
197
90
  return __generator(this, function (_a) {
198
91
  switch (_a.label) {
199
- case 0: return [4, this.listVersions(params)];
92
+ case 0: return [4, Promise.all([
93
+ this.dataHub.init(),
94
+ this.deployer.init(),
95
+ this.tags.init(),
96
+ ])];
200
97
  case 1:
201
- data = _a.sent();
202
- return [2, data[0]];
98
+ _a.sent();
99
+ return [2];
203
100
  }
204
101
  });
205
102
  });
206
103
  };
207
- Flows.prototype.listFlowVersionsByBotId = function (params) {
104
+ Flows.prototype.listFlows = function (botId, params) {
208
105
  return __awaiter(this, void 0, void 0, function () {
106
+ var requestParams, route, result, flowList, query, projection;
209
107
  return __generator(this, function (_a) {
210
- return [2, this.listVersions(params)];
108
+ switch (_a.label) {
109
+ case 0:
110
+ requestParams = __assign({ includeDeleted: false, includeExisting: true, projection: [
111
+ 'id',
112
+ 'version',
113
+ 'botId',
114
+ 'data.label',
115
+ 'data.color',
116
+ 'data.description',
117
+ 'data.isHidden',
118
+ 'tags',
119
+ 'dateModified',
120
+ ] }, params);
121
+ route = "/v2/".concat(this.dataHub.currentAccountId ? this.dataHub.currentAccountId : 'current', "/flows");
122
+ flowList = [];
123
+ query = { botId: botId ? botId : undefined };
124
+ projection = JSON.stringify(requestParams.projection);
125
+ _a.label = 1;
126
+ case 1: return [4, this.dataHub.makeRequest({
127
+ method: 'GET',
128
+ route: route,
129
+ params: {
130
+ includeDeleted: requestParams.includeDeleted,
131
+ includeExisting: requestParams.includeExisting,
132
+ projection: projection,
133
+ query: JSON.stringify(query),
134
+ },
135
+ })];
136
+ case 2:
137
+ result = _a.sent();
138
+ flowList = __spreadArray(__spreadArray([], flowList, true), result.items, true);
139
+ query.last = result.last;
140
+ _a.label = 3;
141
+ case 3:
142
+ if (result.last) return [3, 1];
143
+ _a.label = 4;
144
+ case 4: return [2, (0, base_1.makeList)(flowList)];
145
+ }
211
146
  });
212
147
  });
213
148
  };
214
- Flows.prototype.loadLogs = function (params) {
149
+ Flows.prototype.getFlow = function (id) {
215
150
  return __awaiter(this, void 0, void 0, function () {
216
- var flowId, next, startTime, endTime, limit, filter, relativeTime, queryParams;
151
+ var route, params;
217
152
  return __generator(this, function (_a) {
218
- flowId = params.flowId, next = params.next, startTime = params.startTime, endTime = params.endTime, limit = params.limit, filter = params.filter, relativeTime = params.relativeTime;
219
- queryParams = { skipOriginal: true };
220
- if (next) {
221
- queryParams.next = next;
222
- }
223
- if (startTime) {
224
- queryParams.start = startTime;
225
- }
226
- if (endTime) {
227
- queryParams.end = endTime;
228
- }
229
- if (limit) {
230
- queryParams.limit = limit;
231
- }
232
- if (filter) {
233
- queryParams.filter = filter;
234
- }
235
- if (relativeTime) {
236
- queryParams.relativeTime = relativeTime;
153
+ switch (_a.label) {
154
+ case 0:
155
+ route = "/v2/".concat(this.dataHub.currentAccountId ? this.dataHub.currentAccountId : 'current', "/flow/").concat(id);
156
+ params = {
157
+ includeDeleted: false,
158
+ includeExisting: true,
159
+ sandbox: false,
160
+ };
161
+ return [4, this.dataHub.makeRequest({
162
+ method: 'GET',
163
+ route: route,
164
+ params: params,
165
+ })];
166
+ case 1: return [2, _a.sent()];
237
167
  }
238
- return [2, this.callApiV2({
239
- url: this.deployerUrl,
240
- route: "/flows/".concat(flowId, "/logs"),
241
- params: queryParams,
242
- })];
243
168
  });
244
169
  });
245
170
  };
246
- Flows.prototype.saveFlow = function (flow, _a) {
247
- var _b = _a === void 0 ? {} : _a, previousVersion = _b.previousVersion;
171
+ Flows.prototype.saveFlow = function (source) {
248
172
  return __awaiter(this, void 0, void 0, function () {
249
- var _c, id, version;
250
- return __generator(this, function (_d) {
251
- switch (_d.label) {
173
+ var route, data;
174
+ return __generator(this, function (_a) {
175
+ switch (_a.label) {
252
176
  case 0:
253
- flow.id = flow.id || 'new';
254
- return [4, this.callApiV2({
255
- route: "/flows/".concat(flow.id),
256
- data: {
257
- flow: flow,
258
- previousVersion: previousVersion,
259
- },
260
- method: 'post',
177
+ route = "/v2/".concat(this.dataHub.currentAccountId ? this.dataHub.currentAccountId : 'current', "/flow/").concat(source.id ? source.id : 'new');
178
+ data = {
179
+ flow: source,
180
+ previousVersion: source.id ? source.version : undefined,
181
+ };
182
+ return [4, this.dataHub.makeRequest({
183
+ method: 'POST',
184
+ route: route,
185
+ data: data,
261
186
  })];
262
- case 1:
263
- _c = _d.sent(), id = _c.id, version = _c.version;
264
- return [2, __assign(__assign({}, flow), { id: id, version: version })];
187
+ case 1: return [2, _a.sent()];
265
188
  }
266
189
  });
267
190
  });
268
191
  };
269
- Flows.prototype.getFlowsByBotId = function (botId, projection) {
270
- return this.getFlows({
271
- query: { botId: botId },
272
- projection: projection,
273
- });
274
- };
275
- Flows.prototype.getRemoteDataOutsByBotId = function (id) {
192
+ Flows.prototype.deleteFlow = function (source, flowAlias) {
193
+ if (flowAlias === void 0) { flowAlias = 'dev'; }
276
194
  return __awaiter(this, void 0, void 0, function () {
277
- var flows, dataOuts;
195
+ var variables, operationName, data, result;
278
196
  return __generator(this, function (_a) {
279
197
  switch (_a.label) {
280
- case 0: return [4, this.getFlows({
281
- query: {
282
- botId: id,
283
- isDeleted: false,
198
+ case 0:
199
+ if (this.dataHub.isCrossAccount) {
200
+ throw Error('Cross-account deleting is not implemented.');
201
+ }
202
+ variables = {
203
+ entity: constants_1.ENTITY_NAME,
204
+ data: {
205
+ id: source.id,
206
+ flowAlias: flowAlias,
207
+ subscribe: true,
208
+ role: source.data.deploy.role,
284
209
  },
285
- projection: ['id', 'data.label'],
286
- })];
287
- case 1:
288
- flows = _a.sent();
289
- return [4, this.callApiV2({
290
- route: 'flows/data-outs',
291
- params: {
292
- query: {
293
- type: {
294
- not: 'session',
295
- ne: null,
296
- },
297
- name: { ne: null },
298
- botId: id,
299
- flowId: flows.map(function (flow) { return flow.id; }),
300
- },
301
- },
210
+ };
211
+ operationName = this.dataHub.getOperationName(data_hub_1.OperationNames.DELETE_TEMPORARILY);
212
+ data = {
213
+ operationName: operationName,
214
+ query: constants_1.QUERY_DELETE,
215
+ variables: variables,
216
+ };
217
+ return [4, this.dataHub.makeRequest({
218
+ method: 'POST',
219
+ route: '/graphql',
220
+ data: data,
302
221
  })];
303
- case 2:
304
- dataOuts = _a.sent();
305
- return [2, lodash_1.default.reduce(flows, function (acc, flow) {
306
- var flowLabel = lodash_1.default.get(flow, 'data.label', 'No name flow');
307
- acc[flow.id] = lodash_1.default.map(dataOuts, function (dataOut) { return (__assign(__assign({}, dataOut), { flowLabel: flowLabel })); });
308
- return acc;
309
- }, {})];
222
+ case 1:
223
+ result = _a.sent();
224
+ return [2, this.dataHub.subscribe(result.data[operationName].requestId)];
310
225
  }
311
226
  });
312
227
  });
313
228
  };
314
- Flows.getEmptyStep = function (stepId) {
315
- return {
316
- id: stepId || (0, uuid_1.v4)(),
317
- icon: 'add',
318
- iconType: 'default',
319
- iconUrl: '',
320
- shape: 'empty',
321
- type: 'empty',
322
- pinLabel: true,
323
- stepInputData: {},
324
- };
325
- };
326
- Flows.getEmptyFlow = function (botId) {
327
- var defaultLambdaMemorySize = 1024;
328
- var defaultLambdaTimeout = 30;
329
- return {
330
- botId: botId,
331
- data: {
332
- label: '',
333
- description: '',
334
- deploy: {
335
- memory: defaultLambdaMemorySize,
336
- timeout: defaultLambdaTimeout,
337
- role: 'USER',
338
- },
339
- meta: {},
340
- color: null,
341
- categories: null,
342
- annotations: null,
343
- stepTemplates: [],
344
- trees: {
345
- main: {
346
- position: {
347
- x: 0,
348
- y: 0,
349
- },
350
- steps: [this.getEmptyStep()],
351
- },
352
- },
353
- },
354
- };
355
- };
356
- Flows.getNewFlow = function (botId, flowTemplate) {
229
+ Flows.prototype.activateFlow = function (source, interactiveDebug) {
230
+ if (interactiveDebug === void 0) { interactiveDebug = false; }
357
231
  return __awaiter(this, void 0, void 0, function () {
358
- var flow, template;
359
232
  return __generator(this, function (_a) {
360
- flow = this.getEmptyFlow(botId);
361
- template = lodash_1.default.omit(flowTemplate, ['id', 'botId', 'dateCreated', 'dateModified', 'version']);
362
- flow.data.steps = lodash_1.default.get(template, 'data.steps.length') > 0 ? [] : flow.data.steps;
363
- return [2, lodash_1.default.merge(flow, template)];
233
+ return [2, this.deployer.activateFlow(source, interactiveDebug)];
364
234
  });
365
235
  });
366
236
  };
367
- Flows.prototype.listFlows = function (botId, params) {
368
- if (params === void 0) { params = {}; }
369
- return __awaiter(this, void 0, void 0, function () {
370
- return __generator(this, function (_a) {
371
- return [2, this.getFlows(__assign(__assign({}, params), { query: { botId: botId } }))];
372
- });
373
- });
374
- };
375
- Flows.prototype.getFlow = function (id) {
237
+ Flows.prototype.deactivateFlow = function (source, flowAlias, deleteLambda) {
238
+ if (deleteLambda === void 0) { deleteLambda = false; }
376
239
  return __awaiter(this, void 0, void 0, function () {
377
240
  return __generator(this, function (_a) {
378
- return [2, this.getFlowById(id)];
241
+ return [2, this.deployer.deactivateFlow(source, flowAlias, deleteLambda)];
379
242
  });
380
243
  });
381
244
  };
382
- Object.defineProperty(Flows.prototype, "deployUrl", {
383
- get: function () {
384
- return this.targetAccountId ? "".concat(this.deployerUrl, "/accounts/").concat(this.targetAccountId, "/flows/deploy") : "".concat(this.deployerUrl, "/flows/deploy");
385
- },
386
- enumerable: false,
387
- configurable: true
388
- });
389
- Object.defineProperty(Flows.prototype, "checkUrl", {
390
- get: function () {
391
- return this.targetAccountId ? "".concat(this.deployerUrl, "/accounts/").concat(this.targetAccountId, "/flows/check") : "".concat(this.deployerUrl, "/flows/check");
392
- },
393
- enumerable: false,
394
- configurable: true
395
- });
396
- Flows.prototype._pollResult = function (flowId, requestId, headers, progresCallback) {
245
+ Flows.prototype.addTags = function (source, tagNames) {
397
246
  return __awaiter(this, void 0, void 0, function () {
398
- var counter, result;
247
+ var tags, tagIds, newIds;
399
248
  return __generator(this, function (_a) {
400
249
  switch (_a.label) {
401
- case 0:
402
- counter = 0;
403
- _a.label = 1;
250
+ case 0: return [4, this.tags.getMultipleTagsByName(tagNames, true)];
404
251
  case 1:
405
- counter++;
406
- return [4, new Promise(function (r) { return setTimeout(r, 1000); })];
407
- case 2:
408
- _a.sent();
409
- return [4, this.callApiV2({
410
- url: "".concat(this.checkUrl),
411
- route: "/".concat(flowId, "/").concat(requestId),
412
- })];
413
- case 3:
414
- result = _a.sent();
415
- if (lodash_1.default.isFunction(progresCallback)) {
416
- progresCallback(result);
252
+ tags = _a.sent();
253
+ tagIds = tags.map(function (tag) { return tag.id; });
254
+ newIds = (0, tags_1.filterTagIds)(source.tags, tagIds).newIds;
255
+ if (!newIds.length) {
256
+ throw Error('No tags to add.');
417
257
  }
418
- if (result.status !== 'pending') {
419
- if (result.errorData) {
420
- throw result;
421
- }
422
- return [2, result];
423
- }
424
- _a.label = 4;
425
- case 4:
426
- if (counter < 100) return [3, 1];
427
- _a.label = 5;
428
- case 5: throw new Error('Activation duration is too long');
429
- }
430
- });
431
- });
432
- };
433
- Flows.prototype.activateFlow = function (flow, progresCallback) {
434
- return __awaiter(this, void 0, void 0, function () {
435
- var flowAlias, requestId;
436
- return __generator(this, function (_a) {
437
- switch (_a.label) {
438
- case 0:
439
- flowAlias = "v-".concat(Date.now());
440
- return [4, this.callApiV2({
441
- url: this.deployUrl,
442
- route: '',
443
- method: 'post',
444
- data: {
445
- flowId: flow.id,
446
- flowAlias: flowAlias,
447
- interactiveDebug: flow.interactiveDebug,
448
- role: lodash_1.default.get(flow, 'data.deploy.role'),
449
- },
450
- })];
451
- case 1:
452
- requestId = (_a.sent()).requestId;
453
- return [2, this._pollResult(flow.id, requestId, null, progresCallback)];
258
+ return [2, this.saveFlow(__assign(__assign({}, source), { tags: (0, tags_1.addTagsIds)(source.tags, newIds) }))];
454
259
  }
455
260
  });
456
261
  });
457
262
  };
458
- Flows.prototype.deactivateFlow = function (flow, flowAlias, deleteLambda) {
459
- if (deleteLambda === void 0) { deleteLambda = false; }
263
+ Flows.prototype.removeTags = function (source, tagNames) {
460
264
  return __awaiter(this, void 0, void 0, function () {
461
- var requestId;
265
+ var tags, tagIds, existingIds;
462
266
  return __generator(this, function (_a) {
463
267
  switch (_a.label) {
464
- case 0: return [4, this.callApiV2({
465
- url: this.deployUrl,
466
- route: '',
467
- method: 'delete',
468
- data: {
469
- flow: {
470
- id: flow.id,
471
- },
472
- flowAlias: flowAlias,
473
- role: lodash_1.default.get(flow, 'data.deploy.role'),
474
- deleteLambda: deleteLambda,
475
- },
476
- })];
268
+ case 0: return [4, this.tags.getMultipleTagsByName(tagNames)];
477
269
  case 1:
478
- requestId = (_a.sent()).requestId;
479
- return [2, this._pollResult(flow.id, requestId)];
270
+ tags = _a.sent();
271
+ tagIds = tags.map(function (tag) { return tag.id; });
272
+ existingIds = (0, tags_1.filterTagIds)(source.tags, tagIds).existingIds;
273
+ if (!existingIds.length) {
274
+ throw Error('No tags to remove.');
275
+ }
276
+ return [2, this.saveFlow(__assign(__assign({}, source), { tags: (0, tags_1.removeTagIds)(source.tags, existingIds) }))];
480
277
  }
481
278
  });
482
279
  });
@@ -489,6 +286,6 @@ var Flows = (function (_super) {
489
286
  return (0, utils_1.deleteUnusedStepTemplates)(source, stepTemplates);
490
287
  };
491
288
  return Flows;
492
- }(base_1.Base));
289
+ }());
493
290
  exports.Flows = Flows;
494
291
  //# sourceMappingURL=Flows.js.map