@or-sdk/cards 0.27.0 → 1.0.1

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/Cards.ts CHANGED
@@ -1,47 +1,27 @@
1
- import { List } from '@or-sdk/base';
2
- import { DataHub, GraphqlResponse, GraphqlResponseCheckExecution, GraphqlResponseDelete, OperationNames } from '@or-sdk/data-hub';
3
- import { CardsConfig, Card } from './types';
4
- import {
5
- ENTITY_NAME,
6
- QUERY_LIST,
7
- QUERY_GET,
8
- QUERY_CREATE,
9
- QUERY_UPDATE,
10
- QUERY_DELETE,
11
- QUERY_LIST_CROSSACCOUNT,
12
- QUERY_GET_CROSSACCOUNT,
13
- QUERY_CREATE_CROSSACCOUNT,
14
- QUERY_UPDATE_CROSSACCOUNT,
15
- } from './constants';
1
+ import { List, makeList } from '@or-sdk/base';
2
+ import { DataHubSvc, setDiff } from '@or-sdk/data-hub-svc';
3
+ import { CardsConfig, Card, ListCardsParams, PaginationOptions } from './types';
16
4
  import { Tags, Taggable, filterTagIds, addTagsIds, removeTagIds } from '@or-sdk/tags';
17
5
 
18
6
  export class Cards implements Taggable<Card> {
19
- private readonly dataHub: DataHub;
7
+ private readonly dataHubSvc: DataHubSvc;
20
8
  private readonly tags: Tags;
21
9
 
22
10
  constructor(params: CardsConfig) {
23
- const { token, discoveryUrl, accountId, dataHubUrl } = params;
11
+ const { token, discoveryUrl, accountId, dataHubSvcUrl } = params;
24
12
 
25
- this.dataHub = new DataHub({
13
+ this.dataHubSvc = new DataHubSvc({
26
14
  token,
27
15
  discoveryUrl,
28
16
  accountId,
29
- dataHubUrl,
17
+ dataHubSvcUrl,
30
18
  });
31
19
  this.tags = new Tags({
32
20
  token,
33
21
  discoveryUrl,
34
22
  accountId,
35
- dataHubUrl,
23
+ dataHubSvcUrl,
36
24
  });
37
-
38
- }
39
-
40
- async init() {
41
- await Promise.all([
42
- this.dataHub.init(),
43
- this.tags.init(),
44
- ]);
45
25
  }
46
26
 
47
27
  /**
@@ -50,27 +30,18 @@ export class Cards implements Taggable<Card> {
50
30
  * const cardList = await cards.listCards();
51
31
  * ```
52
32
  */
53
- public async listCards(): Promise<List<Card>> {
54
- const variables = {
55
- entity: ENTITY_NAME,
33
+ public async listCards(params: ListCardsParams = {}, paginationOptions: PaginationOptions = {}): Promise<List<Card>> {
34
+ const result = await this.dataHubSvc.makeRequest<Card[]>({
35
+ method: 'POST',
36
+ route: 'get-cards',
56
37
  params: {
57
- queryParams: {},
58
- includeDeleted: false,
59
- includeExisting: true,
60
- limit: 30,
38
+ ...params,
39
+ ...paginationOptions,
40
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
61
41
  },
62
- ... this.dataHub.isCrossAccount ? { accountId: this.dataHub.currentAccountId } : { sandbox: false },
63
- };
64
-
65
- const operationName = this.dataHub.getOperationName(OperationNames.LIST);
66
-
67
- const data = {
68
- operationName,
69
- query: this.dataHub.isCrossAccount ? QUERY_LIST_CROSSACCOUNT : QUERY_LIST,
70
- variables,
71
- };
42
+ });
72
43
 
73
- return this.dataHub.getFullList<Card>('POST', '/graphql', data);
44
+ return makeList<Card>(result);
74
45
  }
75
46
 
76
47
  /**
@@ -80,31 +51,13 @@ export class Cards implements Taggable<Card> {
80
51
  * ```
81
52
  */
82
53
  public async getCard(id: string): Promise<Card> {
83
- const variables = {
84
- entity: ENTITY_NAME,
54
+ return this.dataHubSvc.makeRequest<Card>({
55
+ method: 'GET',
56
+ route: `cards/${id}`,
85
57
  params: {
86
- id,
87
- includeDeleted: false,
88
- includeExisting: true,
58
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
89
59
  },
90
- ... this.dataHub.isCrossAccount ? { accountId: this.dataHub.currentAccountId } : {},
91
- };
92
-
93
- const operationName = this.dataHub.getOperationName(OperationNames.GET);
94
-
95
- const data = {
96
- operationName,
97
- query: this.dataHub.isCrossAccount ? QUERY_GET_CROSSACCOUNT : QUERY_GET,
98
- variables,
99
- };
100
-
101
- const result = await this.dataHub.makeRequest<GraphqlResponse<Card>>({
102
- method: 'POST',
103
- route: '/graphql',
104
- data,
105
60
  });
106
-
107
- return result.data[operationName] as Card;
108
61
  }
109
62
 
110
63
  /**
@@ -116,7 +69,7 @@ export class Cards implements Taggable<Card> {
116
69
  * ```
117
70
  */
118
71
  public async saveCard(source: Card): Promise<Card> {
119
- return source.id ? this.updateCard(source) : this.createCard(source);
72
+ return (source.id && source.id !== 'new') ? this.updateCard(source) : this.createCard(source);
120
73
  }
121
74
 
122
75
  /**
@@ -126,29 +79,21 @@ export class Cards implements Taggable<Card> {
126
79
  * ```
127
80
  */
128
81
  public async createCard(source: Card): Promise<Card> {
129
- const variables = {
130
- entity: ENTITY_NAME,
82
+ const result = await this.dataHubSvc.makeRequest<{ id: string; }>({
83
+ method: 'POST',
84
+ route: 'cards/new',
131
85
  data: {
132
- body: source,
86
+ card: {
87
+ ...source,
88
+ id: 'new', //TODO: remove later
89
+ },
90
+ },
91
+ params: {
92
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
133
93
  },
134
- ... this.dataHub.isCrossAccount ? { accountId: this.dataHub.currentAccountId } : {},
135
- };
136
-
137
- const operationName = this.dataHub.getOperationName(OperationNames.CREATE);
138
-
139
- const data = {
140
- operationName,
141
- query: this.dataHub.isCrossAccount ? QUERY_CREATE_CROSSACCOUNT : QUERY_CREATE,
142
- variables,
143
- };
144
-
145
- const result = await this.dataHub.makeRequest<GraphqlResponse<Card>>({
146
- method: 'POST',
147
- route: '/graphql',
148
- data,
149
94
  });
150
95
 
151
- return result.data[operationName] as Card;
96
+ return setDiff<Card>(source, result);
152
97
  }
153
98
 
154
99
  /**
@@ -158,30 +103,18 @@ export class Cards implements Taggable<Card> {
158
103
  * ```
159
104
  */
160
105
  public async updateCard(source: Card): Promise<Card> {
161
- const variables = {
162
- entity: ENTITY_NAME,
106
+ const result = await this.dataHubSvc.makeRequest<{ id: string; }>({
107
+ method: 'POST',
108
+ route: `cards/${source.id}`,
163
109
  data: {
164
- id: source.id,
165
- body: source,
110
+ card: source,
111
+ },
112
+ params: {
113
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
166
114
  },
167
- ... this.dataHub.isCrossAccount ? { accountId: this.dataHub.currentAccountId } : {},
168
- };
169
-
170
- const operationName = this.dataHub.getOperationName(OperationNames.UPDATE);
171
-
172
- const data = {
173
- operationName,
174
- query: this.dataHub.isCrossAccount ? QUERY_UPDATE_CROSSACCOUNT : QUERY_UPDATE,
175
- variables,
176
- };
177
-
178
- const result = await this.dataHub.makeRequest<GraphqlResponse<Card>>({
179
- method: 'POST',
180
- route: '/graphql',
181
- data,
182
115
  });
183
116
 
184
- return result.data[operationName] as Card;
117
+ return setDiff<Card>(source, result);
185
118
  }
186
119
 
187
120
  /**
@@ -190,34 +123,52 @@ export class Cards implements Taggable<Card> {
190
123
  * await cards.deleteCard('card-id');
191
124
  * ```
192
125
  */
193
- public async deleteCard(cardId: string): Promise<GraphqlResponseCheckExecution> {
194
- if (this.dataHub.isCrossAccount) {
195
- throw Error('Cross-account deleting is not implemented.');
196
- }
197
-
198
- const variables = {
199
- entity: ENTITY_NAME,
126
+ public async deleteCard(cardId: string, temporarily = true): Promise<void> {
127
+ return this.dataHubSvc.makeRequest<void>({
128
+ method: 'DELETE',
129
+ route: `cards/${cardId}`,
200
130
  data: {
201
- id: cardId,
202
- subscribe: true,
131
+ temporarily,
203
132
  },
204
- };
205
-
206
- const operationName = this.dataHub.getOperationName(OperationNames.DELETE_TEMPORARILY);
207
-
208
- const data = {
209
- operationName,
210
- query: QUERY_DELETE,
211
- variables,
212
- };
133
+ params: {
134
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
135
+ },
136
+ });
137
+ }
213
138
 
214
- const result = await this.dataHub.makeRequest<GraphqlResponse<void>>({
215
- method: 'POST',
216
- route: '/graphql',
217
- data,
139
+ /**
140
+ * Recover card
141
+ * ```typescript
142
+ * await cards.recoverCard('card-id');
143
+ * ```
144
+ */
145
+ public async recoverCard(cardId: string): Promise<void> {
146
+ return this.dataHubSvc.makeRequest<void>({
147
+ method: 'PATCH',
148
+ route: `cards/${cardId}`,
149
+ params: {
150
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
151
+ },
218
152
  });
153
+ }
219
154
 
220
- return this.dataHub.subscribe((result.data[operationName] as GraphqlResponseDelete).requestId);
155
+ /**
156
+ * Clone card
157
+ * ```typescript
158
+ * const result = await cards.cloneCard('card-id');
159
+ * ```
160
+ */
161
+ public async cloneCard(cardId: string, keepTemplateDetails = true): Promise<Card> {
162
+ return this.dataHubSvc.makeRequest<Card>({
163
+ method: 'PUT',
164
+ route: `cards/${cardId}`,
165
+ data: {
166
+ keepTemplateDetails,
167
+ },
168
+ params: {
169
+ ... this.dataHubSvc.isCrossAccount ? { accountId: this.dataHubSvc.currentAccountId } : {},
170
+ },
171
+ });
221
172
  }
222
173
 
223
174
  /**
@@ -269,4 +220,5 @@ export class Cards implements Taggable<Card> {
269
220
  },
270
221
  });
271
222
  }
223
+
272
224
  }
package/src/constants.ts CHANGED
@@ -1,376 +1 @@
1
- export { DATA_HUB_SERVICE_KEY } from '@or-sdk/data-hub';
2
-
3
- export const QUERY_LIST = `query list($entity: EntityType!, $params: ListInput!, $sandbox: Boolean) {
4
- list(entity: $entity, params: $params, sandbox: $sandbox) {
5
- records {
6
- ... on Card {
7
- id
8
- schemaVersion
9
- dateCreated
10
- dateModified
11
- data {
12
- data
13
- form {
14
- code
15
- data
16
- style
17
- template
18
- }
19
- presentation {
20
- code
21
- data
22
- style
23
- template
24
- }
25
- tagIds
26
- }
27
- reference {
28
- id
29
- type
30
- }
31
- template {
32
- category
33
- description
34
- help
35
- icon
36
- iconUrl
37
- implicitly
38
- label
39
- publishedBy
40
- tags
41
- type
42
- version
43
- }
44
- }
45
- }
46
- last
47
- }
48
- }
49
- `;
50
-
51
- export const QUERY_LIST_CROSSACCOUNT = `query listCrossAccount($entity: EntityType!, $params: ListInput!, $accountId: String!) {
52
- listCrossAccount(entity: $entity, params: $params, accountId: $accountId) {
53
- records {
54
- ... on Card {
55
- id
56
- schemaVersion
57
- dateCreated
58
- dateModified
59
- data {
60
- data
61
- form {
62
- code
63
- data
64
- style
65
- template
66
- }
67
- presentation {
68
- code
69
- data
70
- style
71
- template
72
- }
73
- tagIds
74
- }
75
- reference {
76
- id
77
- type
78
- }
79
- template {
80
- category
81
- description
82
- help
83
- icon
84
- iconUrl
85
- implicitly
86
- label
87
- publishedBy
88
- tags
89
- type
90
- version
91
- }
92
- }
93
- }
94
- last
95
- }
96
- }
97
- `;
98
-
99
- export const QUERY_GET = `query get($entity: EntityType!, $params: GetInput!) {
100
- get(entity: $entity, params: $params) {
101
- ... on Card {
102
- id
103
- schemaVersion
104
- dateCreated
105
- dateModified
106
- data {
107
- data
108
- form {
109
- code
110
- data
111
- style
112
- template
113
- }
114
- presentation {
115
- code
116
- data
117
- style
118
- template
119
- }
120
- tagIds
121
- }
122
- reference {
123
- id
124
- type
125
- }
126
- template {
127
- category
128
- description
129
- help
130
- icon
131
- iconUrl
132
- implicitly
133
- label
134
- publishedBy
135
- tags
136
- type
137
- version
138
- }
139
- }
140
- }
141
- }
142
- `;
143
-
144
- export const QUERY_GET_CROSSACCOUNT = `query getCrossAccount($entity: EntityType!, $params: GetInput!, $accountId: String!) {
145
- getCrossAccount(entity: $entity, params: $params, accountId: $accountId) {
146
- ... on Card {
147
- id
148
- schemaVersion
149
- dateCreated
150
- dateModified
151
- data {
152
- data
153
- form {
154
- code
155
- data
156
- style
157
- template
158
- }
159
- presentation {
160
- code
161
- data
162
- style
163
- template
164
- }
165
- tagIds
166
- }
167
- reference {
168
- id
169
- type
170
- }
171
- template {
172
- category
173
- description
174
- help
175
- icon
176
- iconUrl
177
- implicitly
178
- label
179
- publishedBy
180
- tags
181
- type
182
- version
183
- }
184
- }
185
- }
186
- }
187
- `;
188
-
189
- export const QUERY_CREATE = `mutation create($entity: EntityType!, $data: CreateInput!) {
190
- create(entity: $entity, data: $data) {
191
- ... on Card {
192
- id
193
- schemaVersion
194
- dateCreated
195
- dateModified
196
- data {
197
- data
198
- form {
199
- code
200
- data
201
- style
202
- template
203
- }
204
- presentation {
205
- code
206
- data
207
- style
208
- template
209
- }
210
- tagIds
211
- }
212
- reference {
213
- id
214
- type
215
- }
216
- template {
217
- category
218
- description
219
- help
220
- icon
221
- iconUrl
222
- implicitly
223
- label
224
- publishedBy
225
- tags
226
- type
227
- version
228
- }
229
- }
230
- }
231
- }
232
- `;
233
-
234
- export const QUERY_CREATE_CROSSACCOUNT = `mutation createCrossAccount($entity: EntityType!, $data: CreateInput!, $accountId: String!) {
235
- createCrossAccount(entity: $entity, data: $data, accountId: $accountId) {
236
- ... on Card {
237
- id
238
- schemaVersion
239
- dateCreated
240
- dateModified
241
- data {
242
- data
243
- form {
244
- code
245
- data
246
- style
247
- template
248
- }
249
- presentation {
250
- code
251
- data
252
- style
253
- template
254
- }
255
- tagIds
256
- }
257
- reference {
258
- id
259
- type
260
- }
261
- template {
262
- category
263
- description
264
- help
265
- icon
266
- iconUrl
267
- implicitly
268
- label
269
- publishedBy
270
- tags
271
- type
272
- version
273
- }
274
- }
275
- }
276
- }
277
- `;
278
-
279
- export const QUERY_UPDATE = `mutation update($entity: EntityType!, $data: UpdateInput!) {
280
- update(entity: $entity, data: $data) {
281
- ... on Card {
282
- id
283
- schemaVersion
284
- dateCreated
285
- dateModified
286
- data {
287
- data
288
- form {
289
- code
290
- data
291
- style
292
- template
293
- }
294
- presentation {
295
- code
296
- data
297
- style
298
- template
299
- }
300
- tagIds
301
- }
302
- reference {
303
- id
304
- type
305
- }
306
- template {
307
- category
308
- description
309
- help
310
- icon
311
- iconUrl
312
- implicitly
313
- label
314
- publishedBy
315
- tags
316
- type
317
- version
318
- }
319
- }
320
- }
321
- }
322
- `;
323
- export const QUERY_UPDATE_CROSSACCOUNT = `mutation updateCrossAccount($entity: EntityType!, $data: UpdateInput!, $accountId: String!) {
324
- updateCrossAccount(entity: $entity, data: $data, accountId: $accountId) {
325
- ... on Card {
326
- id
327
- schemaVersion
328
- dateCreated
329
- dateModified
330
- data {
331
- data
332
- form {
333
- code
334
- data
335
- style
336
- template
337
- }
338
- presentation {
339
- code
340
- data
341
- style
342
- template
343
- }
344
- tagIds
345
- }
346
- reference {
347
- id
348
- type
349
- }
350
- template {
351
- category
352
- description
353
- help
354
- icon
355
- iconUrl
356
- implicitly
357
- label
358
- publishedBy
359
- tags
360
- type
361
- version
362
- }
363
- }
364
- }
365
- }
366
- `;
367
-
368
- export const QUERY_DELETE = `mutation deleteTemporarily($entity: EntityType!, $data: DeleteInput!) {
369
- deleteTemporarily(entity: $entity, data: $data) {
370
- ... on AsyncRequest {
371
- requestId
372
- }
373
- }
374
- }`;
375
-
376
- export const ENTITY_NAME = 'CARD';
1
+ export { DATA_HUB_SVC_SERVICE_KEY } from '@or-sdk/data-hub-svc';
package/src/types.ts CHANGED
@@ -17,9 +17,9 @@ export type CardsConfig = {
17
17
  accountId?: string;
18
18
 
19
19
  /**
20
- * Url of OneReach DataHub api
20
+ * Url of OneReach DataHubSvc api
21
21
  */
22
- dataHubUrl?: string;
22
+ dataHubSvcUrl?: string;
23
23
  };
24
24
 
25
25
  export type Card = {
@@ -58,3 +58,17 @@ export type Card = {
58
58
  reference?: string | null;
59
59
  template?: string | null;
60
60
  };
61
+
62
+ export type ListCardsParams = {
63
+ query?: {
64
+ [key: string]: unknown;
65
+ };
66
+ projection?: string[];
67
+ group?: string[];
68
+ sandbox?: boolean;
69
+ };
70
+
71
+ export type PaginationOptions = {
72
+ limit?: number;
73
+ offset?: number;
74
+ };