@or-sdk/flows 0.28.0-beta.631.0 → 0.28.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/src/Flows.ts CHANGED
@@ -1,31 +1,30 @@
1
1
  import { List, makeList } from '@or-sdk/base';
2
- import { DataHub, GraphqlResponseCheckExecution, GraphqlResponse, GraphqlResponseDelete, OperationNames } from '@or-sdk/data-hub';
2
+ import { DataHubSvc, setDiff } from '@or-sdk/data-hub-svc';
3
3
  import { Deployer, Flow, PollingResultActivateSuccess, PollingResultDeactivateSuccess, StepTemplateRaw } from '@or-sdk/deployer';
4
- import { FlowsConfig, FlowListResponse, ListFlowsParams, StepTemplateToDelete } from './types';
5
- import { QUERY_DELETE, ENTITY_NAME } from './constants';
4
+ import {
5
+ FlowsConfig,
6
+ ListFlowsParams,
7
+ StepTemplateToDelete,
8
+ PaginationOptions,
9
+ DownloadTemplateResult,
10
+ ListDataOutsParams, DataOut, GetFlowParams,
11
+ } from './types';
6
12
  import { Tags, Taggable, filterTagIds, addTagsIds, removeTagIds } from '@or-sdk/tags';
7
13
  import { listUnusedStepTemplates, deleteUnusedStepTemplates } from './utils';
8
14
 
9
15
  export class Flows implements Taggable<Flow> {
10
- private readonly dataHub: DataHub;
16
+ private readonly dataHubSvc: DataHubSvc;
11
17
  private readonly deployer: Deployer;
12
18
  private readonly tags: Tags;
13
19
 
14
- /**
15
- * ```typescript
16
- * import { Flows } from '@or-sdk/flows'
17
- * const flows = new Flows({token: 'my-account-token-string', baseUrl: 'http://example.flows/endpoint'});
18
- * await flows.init();
19
- * ```
20
- */
21
20
  constructor(params: FlowsConfig) {
22
- const { discoveryUrl, token, accountId, dataHubUrl, deployerUrl } = params;
21
+ const { discoveryUrl, token, accountId, dataHubSvcUrl, deployerUrl } = params;
23
22
 
24
- this.dataHub = new DataHub({
23
+ this.dataHubSvc = new DataHubSvc({
25
24
  token,
26
25
  discoveryUrl,
27
26
  accountId,
28
- dataHubUrl,
27
+ dataHubSvcUrl,
29
28
  });
30
29
  this.deployer = new Deployer({
31
30
  token,
@@ -37,72 +36,46 @@ export class Flows implements Taggable<Flow> {
37
36
  token,
38
37
  discoveryUrl,
39
38
  accountId,
40
- dataHubUrl,
39
+ dataHubSvcUrl,
41
40
  });
42
41
  }
43
42
 
44
- async init() {
45
- await Promise.all([
46
- this.dataHub.init(),
47
- this.deployer.init(),
48
- this.tags.init(),
49
- ]);
50
- }
51
-
52
43
  /**
53
- * List identifiers
54
- *
55
- * If botId is passed - lists flows from the specified bot
44
+ * List flows
56
45
  * ```typescript
57
- * const flowsList = await flows.listFlows('bot-id');
46
+ * const flowsList = await flows.listFlows();
58
47
  * ```
59
48
  */
60
- public async listFlows(botId?: string, params?: ListFlowsParams): Promise<List<Flow>> {
61
- const requestParams = {
62
- // defaults
63
- includeDeleted: false,
64
- includeExisting: true,
65
- projection: [
66
- 'id',
67
- 'version',
68
- 'botId',
69
- 'data.label',
70
- 'data.color',
71
- 'data.description',
72
- 'data.isHidden',
73
- 'tags',
74
- 'dateModified',
75
- ],
49
+ public async listFlows(botId?: string, params: ListFlowsParams = {}, paginationOptions: PaginationOptions = {}): Promise<List<Flow>> {
50
+ let includeQuery = {};
51
+
52
+ if (params.includeDeleted === false || (params.includeDeleted === undefined && params.includeExisting !== false)) {
53
+ includeQuery = { isDeleted: false };
54
+ }
55
+ if (params.includeExisting === false) {
56
+ includeQuery = { isDeleted: true };
57
+ }
76
58
 
77
- // overrides
59
+ const paramsToSend = {
78
60
  ...params,
61
+ query: {
62
+ ...params.query,
63
+ ...includeQuery,
64
+ },
65
+ ...paginationOptions,
66
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
79
67
  };
80
68
 
81
- const route = `/v2/${this.dataHub.currentAccountId ? this.dataHub.currentAccountId : 'current'}/flows`;
82
-
83
- let result: FlowListResponse;
84
- let flowList: Flow[] = [];
85
- const query: { botId?: string; last?: string; } = { botId: botId ? botId : undefined };
86
- const projection = JSON.stringify(requestParams.projection);
69
+ delete paramsToSend.includeDeleted;
70
+ delete paramsToSend.includeExisting;
87
71
 
88
- do {
89
- result = await this.dataHub.makeRequest({
90
- method: 'GET',
91
- route,
92
- params: {
93
- includeDeleted: requestParams.includeDeleted,
94
- includeExisting: requestParams.includeExisting,
95
- projection,
96
- query: JSON.stringify(query),
97
- },
98
- });
99
-
100
- flowList = [...flowList, ...result.items];
101
- query.last = result.last;
102
-
103
- } while (result.last);
72
+ const result = await this.dataHubSvc.makeRequest<Flow[]>({
73
+ method: 'GET',
74
+ route: 'flows',
75
+ params: paramsToSend,
76
+ });
104
77
 
105
- return makeList<Flow>(flowList);
78
+ return makeList<Flow>(result);
106
79
  }
107
80
 
108
81
  /**
@@ -111,19 +84,14 @@ export class Flows implements Taggable<Flow> {
111
84
  * const flow = await flows.getFlow('flow-id');
112
85
  * ```
113
86
  */
114
- public async getFlow(id: string): Promise<Flow> {
115
- const route = `/v2/${this.dataHub.currentAccountId ? this.dataHub.currentAccountId : 'current'}/flow/${id}`;
116
-
117
- const params = {
118
- includeDeleted: false,
119
- includeExisting: true,
120
- sandbox: false,
121
- };
122
-
123
- return await this.dataHub.makeRequest<Flow>({
87
+ public async getFlow(id: string, params: GetFlowParams = {}): Promise<Flow> {
88
+ return this.dataHubSvc.makeRequest<Flow>({
124
89
  method: 'GET',
125
- route,
126
- params,
90
+ route: `flows/${id}`,
91
+ params: {
92
+ ...params,
93
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
94
+ },
127
95
  });
128
96
  }
129
97
 
@@ -136,56 +104,59 @@ export class Flows implements Taggable<Flow> {
136
104
  * ```
137
105
  */
138
106
  public async saveFlow(source: Flow): Promise<Flow> {
139
- const route = `/v2/${this.dataHub.currentAccountId ? this.dataHub.currentAccountId : 'current'}/flow/${source.id ? source.id : 'new'}`;
140
-
141
- const data = {
142
- flow: source,
143
- previousVersion: source.id ? source.version : undefined,
144
- };
145
-
146
- return await this.dataHub.makeRequest<Flow>({
107
+ const result = await this.dataHubSvc.makeRequest<{ id: string; version: string; }>({
147
108
  method: 'POST',
148
- route,
149
- data,
109
+ route: `flows/${(source.id && source.id !== 'new') ? source.id : 'new'}`,
110
+ data: {
111
+ flow: (source.id && source.id !== 'new') ? source : {
112
+ ...source,
113
+ id: 'new', //TODO: remove later
114
+ },
115
+ previousVersion: (source.id && source.id !== 'new') ? source.version : undefined,
116
+ },
117
+ params: {
118
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
119
+ },
150
120
  });
121
+
122
+ return setDiff<Flow>(source, result);
151
123
  }
152
124
 
153
125
  /**
154
126
  * Delete flow
155
127
  * ```typescript
156
- * const result = await flows.deleteFlow(flowSource, flowAlias);
128
+ * await flows.deleteFlow('flow-id');
157
129
  * ```
158
130
  */
159
- public async deleteFlow(source: Flow, flowAlias = 'dev'): Promise<GraphqlResponseCheckExecution> {
160
- if (this.dataHub.isCrossAccount) {
161
- throw Error('Cross-account deleting is not implemented.');
162
- }
131
+ public async deleteFlow(flowId: string | Flow, temporarily = true): Promise<void> {
132
+ const flowSourceId = typeof flowId === 'string' ? flowId : flowId.id;
163
133
 
164
- const variables = {
165
- entity: ENTITY_NAME,
134
+ return this.dataHubSvc.makeRequest<void>({
135
+ method: 'DELETE',
136
+ route: `flows/${flowId}`,
166
137
  data: {
167
- id: source.id,
168
- flowAlias,
169
- subscribe: true,
170
- role: source.data.deploy.role,
138
+ temporarily,
139
+ },
140
+ params: {
141
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
171
142
  },
172
- };
173
-
174
- const operationName = this.dataHub.getOperationName(OperationNames.DELETE_TEMPORARILY);
175
-
176
- const data = {
177
- operationName,
178
- query: QUERY_DELETE,
179
- variables,
180
- };
181
-
182
- const result = await this.dataHub.makeRequest<GraphqlResponse<void>>({
183
- method: 'POST',
184
- route: '/graphql',
185
- data,
186
143
  });
144
+ }
187
145
 
188
- return this.dataHub.subscribe((result.data[operationName] as GraphqlResponseDelete).requestId);
146
+ /**
147
+ * Recover flow
148
+ * ```typescript
149
+ * await flows.recoverFlow('flow-id');
150
+ * ```
151
+ */
152
+ public async recoverFlow(flowId: string): Promise<void> {
153
+ return this.dataHubSvc.makeRequest<void>({
154
+ method: 'PATCH',
155
+ route: `flows/${flowId}`,
156
+ params: {
157
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
158
+ },
159
+ });
189
160
  }
190
161
 
191
162
  /**
@@ -208,6 +179,26 @@ export class Flows implements Taggable<Flow> {
208
179
  return this.deployer.deactivateFlow(source, flowAlias, deleteLambda);
209
180
  }
210
181
 
182
+ /**
183
+ * List data outs
184
+ * ```typescript
185
+ * const result = await flows.downloadTemplate('flow-template-id', 'bot-id');
186
+ * ```
187
+ */
188
+ public async listDataOuts(paginationOptions: PaginationOptions = {}, params: ListDataOutsParams = {}): Promise<List<DataOut>> {
189
+ const result = await this.dataHubSvc.makeRequest<DataOut[]>({
190
+ method: 'GET',
191
+ route: '/flows/data-outs',
192
+ params: {
193
+ ...params,
194
+ ...paginationOptions,
195
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
196
+ },
197
+ });
198
+
199
+ return makeList<DataOut>(result);
200
+ }
201
+
211
202
  /**
212
203
  * Add tags
213
204
  * ```typescript
@@ -252,6 +243,26 @@ export class Flows implements Taggable<Flow> {
252
243
  });
253
244
  }
254
245
 
246
+ /**
247
+ * Download flow template to account
248
+ * ```typescript
249
+ * const result = await flows.downloadTemplate('flow-template-id', 'bot-id');
250
+ * ```
251
+ */
252
+ public async downloadTemplate(flowTemplateId: string, botId: string, shouldUpdateSteps = false): Promise<DownloadTemplateResult> {
253
+ return this.dataHubSvc.makeRequest<DownloadTemplateResult>({
254
+ method: 'PUT',
255
+ route: `flows/${botId}`,
256
+ data: {
257
+ flowId: flowTemplateId,
258
+ shouldUpdateSteps,
259
+ },
260
+ params: {
261
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
262
+ },
263
+ });
264
+ }
265
+
255
266
  /**
256
267
  * List unused step templates from flow source
257
268
  * ```typescript
package/src/constants.ts CHANGED
@@ -1,12 +1,2 @@
1
- export { DATA_HUB_SERVICE_KEY } from '@or-sdk/data-hub';
1
+ export { DATA_HUB_SVC_SERVICE_KEY } from '@or-sdk/data-hub-svc';
2
2
  export { DEPLOYER_SERVICE_KEY } from '@or-sdk/deployer';
3
-
4
- export const QUERY_DELETE = `mutation deleteTemporarily($entity: EntityType!, $data: DeleteInput!) {
5
- deleteTemporarily(entity: $entity, data: $data) {
6
- ... on AsyncRequest {
7
- requestId
8
- }
9
- }
10
- }`;
11
-
12
- export const ENTITY_NAME = 'FLOW';
package/src/index.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export { Flows } from './Flows';
2
2
  export * from './types';
3
+ export * from './constants';
4
+
package/src/types.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Token } from '@or-sdk/base';
2
- import { Flow } from '@or-sdk/deployer';
3
2
 
4
3
  export type FlowsConfig = {
5
4
  /**
@@ -18,9 +17,9 @@ export type FlowsConfig = {
18
17
  accountId?: string;
19
18
 
20
19
  /**
21
- * Url of OneReach DataHub api
20
+ * Url of OneReach DataHubSvc api
22
21
  */
23
- dataHubUrl?: string;
22
+ dataHubSvcUrl?: string;
24
23
 
25
24
  /**
26
25
  * Url of OneReach Deployer api
@@ -28,19 +27,59 @@ export type FlowsConfig = {
28
27
  deployerUrl?: string;
29
28
  };
30
29
 
31
- export type FlowListResponse = {
32
- count: number;
33
- items: Flow[];
34
- last?: string;
30
+ export type ListFlowsParams = {
31
+ includeDeleted?: boolean;
32
+ includeExisting?: boolean;
33
+ query?: {
34
+ [key: string]: unknown;
35
+ };
36
+ projection?: string[];
37
+ group?: string[];
38
+ sandbox?: boolean;
35
39
  };
36
40
 
37
- export type ListFlowsParams = {
38
- includeDeleted: boolean;
39
- includeExisting: boolean;
40
- projection: string[];
41
+ export type ListDataOutsParams = {
42
+ query?: {
43
+ [key: string]: unknown;
44
+ };
45
+ projection?: string[];
46
+ group?: string[];
47
+ sandbox?: boolean;
48
+ };
49
+
50
+ export type PaginationOptions = {
51
+ limit?: number;
52
+ offset?: number;
53
+ };
54
+
55
+ export type GetFlowParams = {
56
+ query?: {
57
+ [key: string]: unknown;
58
+ };
59
+ projection?: string[];
60
+ sandbox?: boolean;
61
+ };
62
+
63
+ export type DataOut = {
64
+ flowId: string;
65
+ botId: string;
66
+ name: string;
67
+ type: string;
68
+ ttl: number;
69
+ meta: string;
70
+ outputExample: string;
71
+ stepLabel: string;
72
+ stepId: string;
41
73
  };
42
74
 
43
75
  export type StepTemplateToDelete = {
44
76
  id: string;
45
77
  [key: string]: unknown;
46
78
  };
79
+
80
+ export type DownloadTemplateResult = {
81
+ id: string;
82
+ data: {
83
+ label: string;
84
+ };
85
+ };
package/tsconfig.esm.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "outDir": "./dist/esm",
5
5
  "declarationDir": "./dist/types",
6
6
  "module": "ES6",
7
- "target": "es2022",
7
+ "target": "es6",
8
8
  "rootDir": "./src",
9
9
  "declaration": true,
10
10
  "declarationMap": true