@forge/storage 2.0.2 → 2.0.3-experimental-04cc2b9

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 (51) hide show
  1. package/LICENSE.txt +1 -1
  2. package/package.json +11 -2
  3. package/README.md +0 -50
  4. package/out/__test__/errors.test.d.ts +0 -2
  5. package/out/__test__/errors.test.d.ts.map +0 -1
  6. package/out/__test__/errors.test.js +0 -34
  7. package/out/__test__/global-storage.test.d.ts +0 -2
  8. package/out/__test__/global-storage.test.d.ts.map +0 -1
  9. package/out/__test__/global-storage.test.js +0 -833
  10. package/out/__test__/list-api.test.d.ts +0 -2
  11. package/out/__test__/list-api.test.d.ts.map +0 -1
  12. package/out/__test__/list-api.test.js +0 -650
  13. package/out/conditions.d.ts +0 -3
  14. package/out/conditions.d.ts.map +0 -1
  15. package/out/conditions.js +0 -10
  16. package/out/eap/conditions.d.ts +0 -40
  17. package/out/eap/conditions.d.ts.map +0 -1
  18. package/out/eap/conditions.js +0 -112
  19. package/out/eap/index.d.ts +0 -2
  20. package/out/eap/index.d.ts.map +0 -1
  21. package/out/eap/index.js +0 -4
  22. package/out/entity-storage/index.d.ts +0 -2
  23. package/out/entity-storage/index.d.ts.map +0 -1
  24. package/out/entity-storage/index.js +0 -5
  25. package/out/entity-storage/query-api.d.ts +0 -48
  26. package/out/entity-storage/query-api.d.ts.map +0 -1
  27. package/out/entity-storage/query-api.js +0 -154
  28. package/out/entity-storage/storage-builder.d.ts +0 -20
  29. package/out/entity-storage/storage-builder.d.ts.map +0 -1
  30. package/out/entity-storage/storage-builder.js +0 -25
  31. package/out/errors.d.ts +0 -9
  32. package/out/errors.d.ts.map +0 -1
  33. package/out/errors.js +0 -41
  34. package/out/global-storage.d.ts +0 -50
  35. package/out/global-storage.d.ts.map +0 -1
  36. package/out/global-storage.js +0 -156
  37. package/out/gql-queries.d.ts +0 -81
  38. package/out/gql-queries.d.ts.map +0 -1
  39. package/out/gql-queries.js +0 -200
  40. package/out/index.d.ts +0 -49
  41. package/out/index.d.ts.map +0 -1
  42. package/out/index.js +0 -33
  43. package/out/query-api.d.ts +0 -14
  44. package/out/query-api.d.ts.map +0 -1
  45. package/out/query-api.js +0 -44
  46. package/out/query-interfaces.d.ts +0 -113
  47. package/out/query-interfaces.d.ts.map +0 -1
  48. package/out/query-interfaces.js +0 -8
  49. package/out/storage-adapter.d.ts +0 -42
  50. package/out/storage-adapter.d.ts.map +0 -1
  51. package/out/storage-adapter.js +0 -2
@@ -1,833 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const errors_1 = require("../errors");
4
- const global_storage_1 = require("../global-storage");
5
- const gql_queries_1 = require("../gql-queries");
6
- const mocks_1 = require("@atlassian/metrics-interface/dist/mocks");
7
- const getStorage = (apiClientMock, metrics) => new global_storage_1.GlobalStorage(apiClientMock, () => metrics);
8
- const getApiClientMock = (response, statusCode = 200) => {
9
- return jest.fn().mockReturnValue({
10
- ok: statusCode === 200,
11
- status: statusCode,
12
- text: jest.fn().mockResolvedValue(JSON.stringify(response))
13
- });
14
- };
15
- const getMetricMock = () => {
16
- const mockMetrics = new mocks_1.MockMetrics();
17
- const mockCounter = new mocks_1.MockCounter('');
18
- const mockTiming = new mocks_1.MockTiming('');
19
- const mockStopTiming = jest.fn();
20
- const mockMeasure = {
21
- stop: mockStopTiming
22
- };
23
- mockMetrics.counter.mockReturnValue(mockCounter);
24
- mockMetrics.timing.mockReturnValue(mockTiming);
25
- mockTiming.measure.mockReturnValue(mockMeasure);
26
- return {
27
- mockMetrics,
28
- mockCounter,
29
- mockStopTiming
30
- };
31
- };
32
- const checkMetricsFired = ({ mockMetrics, mockCounter, mockStopTiming }, { encrypted, ...restTags }, success) => {
33
- expect(mockMetrics.counter).toHaveBeenCalledWith('forge.runtime.storage.operation', {
34
- ...restTags,
35
- encrypted: String(encrypted),
36
- success: String(success)
37
- });
38
- expect(mockCounter.incr).toHaveBeenCalled();
39
- expect(mockMetrics.timing).toHaveBeenCalledWith('forge.runtime.storage.operation.latency', {
40
- ...restTags,
41
- encrypted: String(encrypted)
42
- });
43
- expect(mockStopTiming).toHaveBeenCalledWith({ success: String(success) });
44
- };
45
- const getApiClientMockInvalidJson = (response, statusCode = 200) => {
46
- return jest.fn().mockReturnValue({
47
- ok: statusCode === 200,
48
- status: statusCode,
49
- text: jest.fn().mockResolvedValue(response)
50
- });
51
- };
52
- const INVALID_CURSOR_ERROR = {
53
- message: 'error message',
54
- extensions: {
55
- errorType: 'INVALID_CURSOR'
56
- }
57
- };
58
- describe.each([
59
- ['with metrics', true],
60
- ['no metrics', false]
61
- ])('GlobalStorage - %s', (_, passMetrics) => {
62
- function verifyApiClientCalledWith(apiClientMock, variables, query) {
63
- expect(apiClientMock).toHaveBeenCalledWith('/forge/entities/graphql', expect.objectContaining({
64
- method: 'POST',
65
- body: expect.any(String),
66
- headers: {
67
- 'content-type': 'application/json'
68
- }
69
- }));
70
- const [, { body }] = apiClientMock.mock.calls[0];
71
- const expectedBody = query ? { query, variables } : { variables };
72
- expect(JSON.parse(body)).toEqual(expect.objectContaining(expectedBody));
73
- }
74
- describe('get', () => {
75
- it('should call the storage API, passing the provided key and returning the stored value', async () => {
76
- const apiClientMock = getApiClientMock({
77
- data: {
78
- appStoredEntity: {
79
- value: 'testValue'
80
- }
81
- }
82
- });
83
- const metricMocks = getMetricMock();
84
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
85
- const returnedValue = await globalStorage.get('testKey');
86
- verifyApiClientCalledWith(apiClientMock, {
87
- key: 'testKey',
88
- encrypted: false
89
- });
90
- expect(returnedValue).toEqual('testValue');
91
- passMetrics
92
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
93
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
94
- });
95
- it('should call the storage API, passing the provided key and returning undefined if the key doesnt exist', async () => {
96
- const apiClientMock = getApiClientMock({
97
- data: {
98
- appStoredEntity: {
99
- value: null
100
- }
101
- }
102
- });
103
- const metricMocks = getMetricMock();
104
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
105
- const returnedValue = await globalStorage.get('testKey');
106
- verifyApiClientCalledWith(apiClientMock, {
107
- key: 'testKey',
108
- encrypted: false
109
- });
110
- expect(returnedValue).toEqual(undefined);
111
- passMetrics
112
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
113
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
114
- });
115
- it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
116
- const apiClientMock = getApiClientMock({
117
- data: {
118
- appStoredEntity: {
119
- value: 0
120
- }
121
- }
122
- });
123
- const metricMocks = getMetricMock();
124
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
125
- const returnedValue = await globalStorage.get('testKey');
126
- verifyApiClientCalledWith(apiClientMock, {
127
- key: 'testKey',
128
- encrypted: false
129
- });
130
- expect(returnedValue).toEqual(0);
131
- passMetrics
132
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
133
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
134
- });
135
- it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
136
- const apiClientMock = getApiClientMock({
137
- data: {
138
- appStoredEntity: {
139
- value: ''
140
- }
141
- }
142
- });
143
- const metricMocks = getMetricMock();
144
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
145
- const returnedValue = await globalStorage.get('testKey');
146
- verifyApiClientCalledWith(apiClientMock, {
147
- key: 'testKey',
148
- encrypted: false
149
- });
150
- expect(returnedValue).toEqual('');
151
- passMetrics
152
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, true)
153
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
154
- });
155
- it('should throw an error with the returned status for non-200 status codes', async () => {
156
- const apiClientMock = getApiClientMock(undefined, 400);
157
- const metricMocks = getMetricMock();
158
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
159
- const response = globalStorage.get('testKey');
160
- expect(apiClientMock).toHaveBeenCalled();
161
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
162
- passMetrics
163
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, false)
164
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
165
- });
166
- it('should throw an error with the returned error message for failed responses', async () => {
167
- const apiClientMock = getApiClientMock({
168
- errors: [INVALID_CURSOR_ERROR]
169
- }, 200);
170
- const metricMocks = getMetricMock();
171
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
172
- const response = globalStorage.get('testKey');
173
- expect(apiClientMock).toHaveBeenCalled();
174
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
175
- passMetrics
176
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, false)
177
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
178
- });
179
- it('should throw an error if the response is not a valid JSON', async () => {
180
- const apiClientMock = getApiClientMockInvalidJson('test', 200);
181
- const metricMocks = getMetricMock();
182
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
183
- const response = globalStorage.get('testKey');
184
- expect(apiClientMock).toHaveBeenCalled();
185
- await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
186
- passMetrics
187
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: false }, false)
188
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
189
- });
190
- });
191
- describe('get secret', () => {
192
- it('should call the storage API, passing the provided key and returning the stored value', async () => {
193
- const apiClientMock = getApiClientMock({
194
- data: {
195
- appStoredEntity: {
196
- value: 'testValue'
197
- }
198
- }
199
- });
200
- const metricMocks = getMetricMock();
201
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
202
- const returnedValue = await globalStorage.getSecret('testKey');
203
- verifyApiClientCalledWith(apiClientMock, {
204
- key: 'testKey',
205
- encrypted: true
206
- });
207
- expect(returnedValue).toEqual('testValue');
208
- passMetrics
209
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'get', encrypted: true }, true)
210
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
211
- });
212
- });
213
- describe('set', () => {
214
- it('should call the storage API, passing the provided key and value', async () => {
215
- const apiClientMock = getApiClientMock({
216
- data: {
217
- appStorage: {
218
- setAppStoredEntity: {
219
- success: true
220
- }
221
- }
222
- }
223
- });
224
- const metricMocks = getMetricMock();
225
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
226
- await globalStorage.set('testKey', 'testValue');
227
- verifyApiClientCalledWith(apiClientMock, {
228
- input: {
229
- key: 'testKey',
230
- value: 'testValue',
231
- encrypted: false
232
- }
233
- });
234
- passMetrics
235
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, true)
236
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
237
- });
238
- it('should throw an error if the storage API returns successful = false', async () => {
239
- const apiClientMock = getApiClientMock({
240
- data: {
241
- appStorage: {
242
- setAppStoredEntity: {
243
- success: false,
244
- errors: [INVALID_CURSOR_ERROR]
245
- }
246
- }
247
- }
248
- });
249
- const metricMocks = getMetricMock();
250
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
251
- const response = globalStorage.set('testKey', 'testValue');
252
- expect(apiClientMock).toHaveBeenCalled();
253
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
254
- passMetrics
255
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, false)
256
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
257
- });
258
- it('should throw an error if the storage API returns a non 200 status code', async () => {
259
- const apiClientMock = getApiClientMockInvalidJson('', 400);
260
- const metricMocks = getMetricMock();
261
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
262
- const response = globalStorage.set('testKey', 'testValue');
263
- expect(apiClientMock).toHaveBeenCalled();
264
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
265
- passMetrics
266
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, false)
267
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
268
- });
269
- it('should throw a 500 error if success=false but no errors were returned', async () => {
270
- const apiClientMock = getApiClientMock({
271
- data: {
272
- appStorage: {
273
- setAppStoredEntity: {
274
- success: false
275
- }
276
- }
277
- }
278
- });
279
- const metricMocks = getMetricMock();
280
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
281
- await expect(globalStorage.set('testKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
282
- verifyApiClientCalledWith(apiClientMock, {
283
- input: {
284
- key: 'testKey',
285
- value: 'testValue',
286
- encrypted: false
287
- }
288
- });
289
- passMetrics
290
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: false }, false)
291
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
292
- });
293
- });
294
- describe('set secret', () => {
295
- it('should call the storage API, passing the provided key and value', async () => {
296
- const apiClientMock = getApiClientMock({
297
- data: {
298
- appStorage: {
299
- setAppStoredEntity: {
300
- success: true
301
- }
302
- }
303
- }
304
- });
305
- const metricMocks = getMetricMock();
306
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
307
- await globalStorage.setSecret('testKey', 'testValue');
308
- verifyApiClientCalledWith(apiClientMock, {
309
- input: {
310
- key: 'testKey',
311
- value: 'testValue',
312
- encrypted: true
313
- }
314
- });
315
- passMetrics
316
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'set', encrypted: true }, true)
317
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
318
- });
319
- });
320
- describe('delete', () => {
321
- it('should call the storage API, passing the provided key', async () => {
322
- const apiClientMock = getApiClientMock({
323
- data: {
324
- appStorage: {
325
- deleteAppStoredEntity: {
326
- success: true
327
- }
328
- }
329
- }
330
- });
331
- const metricMocks = getMetricMock();
332
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
333
- await globalStorage.delete('testKey');
334
- verifyApiClientCalledWith(apiClientMock, {
335
- input: {
336
- key: 'testKey',
337
- encrypted: false
338
- }
339
- });
340
- passMetrics
341
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: false }, true)
342
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
343
- });
344
- it('should throw an error if the storage API returns successful = false', async () => {
345
- const apiClientMock = getApiClientMock({
346
- data: {
347
- appStorage: {
348
- deleteAppStoredEntity: {
349
- success: false,
350
- errors: [INVALID_CURSOR_ERROR]
351
- }
352
- }
353
- }
354
- });
355
- const metricMocks = getMetricMock();
356
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
357
- const response = globalStorage.delete('testKey');
358
- expect(apiClientMock).toHaveBeenCalled();
359
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
360
- passMetrics
361
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: false }, false)
362
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
363
- });
364
- it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
365
- const apiClientMock = getApiClientMockInvalidJson('', 400);
366
- const metricMocks = getMetricMock();
367
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
368
- const response = globalStorage.delete('testKey');
369
- expect(apiClientMock).toHaveBeenCalled();
370
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
371
- passMetrics
372
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: false }, false)
373
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
374
- });
375
- });
376
- describe('delete secret', () => {
377
- it('should call the storage API, passing the provided key', async () => {
378
- const apiClientMock = getApiClientMock({
379
- data: {
380
- appStorage: {
381
- deleteAppStoredEntity: {
382
- success: true
383
- }
384
- }
385
- }
386
- });
387
- const metricMocks = getMetricMock();
388
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
389
- await globalStorage.deleteSecret('testKey');
390
- verifyApiClientCalledWith(apiClientMock, {
391
- input: {
392
- key: 'testKey',
393
- encrypted: true
394
- }
395
- });
396
- passMetrics
397
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'delete', encrypted: true }, true)
398
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
399
- });
400
- });
401
- describe('getEntity', () => {
402
- it('should call the storage API, passing the provided entity name and entity key and returning the stored value', async () => {
403
- const apiClientMock = getApiClientMock({
404
- data: {
405
- appStoredCustomEntity: {
406
- value: 'testValue'
407
- }
408
- }
409
- });
410
- const metricMocks = getMetricMock();
411
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
412
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
413
- verifyApiClientCalledWith(apiClientMock, {
414
- entityName: 'testEntityName',
415
- key: 'testEntityKey'
416
- });
417
- expect(returnedValue).toEqual('testValue');
418
- passMetrics
419
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
420
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
421
- });
422
- it('should call the storage API, passing the provided entity key and returning undefined if the key doesnt exist', async () => {
423
- const apiClientMock = getApiClientMock({
424
- data: {
425
- appStoredCustomEntity: {
426
- value: null
427
- }
428
- }
429
- });
430
- const metricMocks = getMetricMock();
431
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
432
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
433
- verifyApiClientCalledWith(apiClientMock, {
434
- entityName: 'testEntityName',
435
- key: 'testEntityKey'
436
- });
437
- expect(returnedValue).toEqual(undefined);
438
- passMetrics
439
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
440
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
441
- });
442
- it('should call the storage API, passing the provided key and returning the stored falsey value 0', async () => {
443
- const apiClientMock = getApiClientMock({
444
- data: {
445
- appStoredCustomEntity: {
446
- value: 0
447
- }
448
- }
449
- });
450
- const metricMocks = getMetricMock();
451
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
452
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
453
- verifyApiClientCalledWith(apiClientMock, {
454
- entityName: 'testEntityName',
455
- key: 'testEntityKey'
456
- });
457
- expect(returnedValue).toEqual(0);
458
- passMetrics
459
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
460
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
461
- });
462
- it('should call the storage API, passing the provided key and returning the stored empty string', async () => {
463
- const apiClientMock = getApiClientMock({
464
- data: {
465
- appStoredCustomEntity: {
466
- value: ''
467
- }
468
- }
469
- });
470
- const metricMocks = getMetricMock();
471
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
472
- const returnedValue = await globalStorage.getEntity('testEntityName', 'testEntityKey');
473
- verifyApiClientCalledWith(apiClientMock, {
474
- entityName: 'testEntityName',
475
- key: 'testEntityKey'
476
- });
477
- expect(returnedValue).toEqual('');
478
- passMetrics
479
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, true)
480
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
481
- });
482
- it('should throw an error with the returned status for non-200 status codes', async () => {
483
- const apiClientMock = getApiClientMock(undefined, 400);
484
- const metricMocks = getMetricMock();
485
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
486
- const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
487
- expect(apiClientMock).toHaveBeenCalled();
488
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
489
- passMetrics
490
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, false)
491
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
492
- });
493
- it('should throw an error with the returned error message for failed responses', async () => {
494
- const apiClientMock = getApiClientMock({
495
- errors: [INVALID_CURSOR_ERROR]
496
- }, 200);
497
- const metricMocks = getMetricMock();
498
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
499
- const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
500
- expect(apiClientMock).toHaveBeenCalled();
501
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
502
- passMetrics
503
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, false)
504
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
505
- });
506
- it('should throw an error if the response is not a valid JSON', async () => {
507
- const apiClientMock = getApiClientMockInvalidJson('test', 200);
508
- const metricMocks = getMetricMock();
509
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
510
- const response = globalStorage.getEntity('testEntityName', 'testEntityKey');
511
- expect(apiClientMock).toHaveBeenCalled();
512
- await expect(response).rejects.toThrow(errors_1.APIError.forUnexpected('Response text was not a valid JSON: test'));
513
- passMetrics
514
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'get', encrypted: false }, false)
515
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
516
- });
517
- });
518
- describe('setEntity', () => {
519
- it('should call the storage API, passing the provided entity name, entity key and value', async () => {
520
- const apiClientMock = getApiClientMock({
521
- data: {
522
- appStorageCustomEntity: {
523
- setAppStoredCustomEntity: {
524
- success: true
525
- }
526
- }
527
- }
528
- });
529
- const metricMocks = getMetricMock();
530
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
531
- await globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
532
- verifyApiClientCalledWith(apiClientMock, {
533
- input: {
534
- entityName: 'testEntityName',
535
- key: 'testEntityKey',
536
- value: 'testValue'
537
- }
538
- });
539
- passMetrics
540
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, true)
541
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
542
- });
543
- it('should throw an error if the storage API returns successful = false', async () => {
544
- const apiClientMock = getApiClientMock({
545
- data: {
546
- appStorageCustomEntity: {
547
- setAppStoredCustomEntity: {
548
- success: false,
549
- errors: [INVALID_CURSOR_ERROR]
550
- }
551
- }
552
- }
553
- });
554
- const metricMocks = getMetricMock();
555
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
556
- const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
557
- expect(apiClientMock).toHaveBeenCalled();
558
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('INVALID_CURSOR', 'error message'));
559
- passMetrics
560
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, false)
561
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
562
- });
563
- it('should throw an error if the storage API returns a non 200 status code', async () => {
564
- const apiClientMock = getApiClientMockInvalidJson('', 400);
565
- const metricMocks = getMetricMock();
566
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
567
- const response = globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue');
568
- expect(apiClientMock).toHaveBeenCalled();
569
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
570
- passMetrics
571
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, false)
572
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
573
- });
574
- it('should throw a 500 error if success=false but no errors were returned', async () => {
575
- const apiClientMock = getApiClientMock({
576
- data: {
577
- appStorageCustomEntity: {
578
- setAppStoredCustomEntity: {
579
- success: false
580
- }
581
- }
582
- }
583
- });
584
- const metricMocks = getMetricMock();
585
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
586
- await expect(globalStorage.setEntity('testEntityName', 'testEntityKey', 'testValue')).rejects.toThrow(errors_1.APIError.forStatus(500));
587
- verifyApiClientCalledWith(apiClientMock, {
588
- input: {
589
- entityName: 'testEntityName',
590
- key: 'testEntityKey',
591
- value: 'testValue'
592
- }
593
- });
594
- passMetrics
595
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'set', encrypted: false }, false)
596
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
597
- });
598
- });
599
- describe('deleteEntity', () => {
600
- it('should call the storage API, passing the provided entity name and key', async () => {
601
- const apiClientMock = getApiClientMock({
602
- data: {
603
- appStorageCustomEntity: {
604
- deleteAppStoredCustomEntity: {
605
- success: true
606
- }
607
- }
608
- }
609
- });
610
- const metricMocks = getMetricMock();
611
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
612
- await globalStorage.deleteEntity('testEntityName', 'testEntityKey');
613
- verifyApiClientCalledWith(apiClientMock, {
614
- input: {
615
- entityName: 'testEntityName',
616
- key: 'testEntityKey'
617
- }
618
- });
619
- passMetrics
620
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'delete', encrypted: false }, true)
621
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
622
- });
623
- it('should throw an error if the storage API returns successful = false', async () => {
624
- const apiClientMock = getApiClientMock({
625
- data: {
626
- appStorageCustomEntity: {
627
- deleteAppStoredCustomEntity: {
628
- success: false,
629
- errors: [INVALID_CURSOR_ERROR]
630
- }
631
- }
632
- }
633
- });
634
- const metricMocks = getMetricMock();
635
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
636
- const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
637
- expect(apiClientMock).toHaveBeenCalled();
638
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
639
- passMetrics
640
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'delete', encrypted: false }, false)
641
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
642
- });
643
- it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
644
- const apiClientMock = getApiClientMockInvalidJson('', 400);
645
- const metricMocks = getMetricMock();
646
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
647
- const response = globalStorage.deleteEntity('testEntityKey', 'testEntityKey');
648
- expect(apiClientMock).toHaveBeenCalled();
649
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
650
- passMetrics
651
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'delete', encrypted: false }, false)
652
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
653
- });
654
- });
655
- describe('list', () => {
656
- it('should call the storage API with the provided parameters', async () => {
657
- const apiClientMock = getApiClientMock({
658
- data: {
659
- appStoredEntities: {
660
- edges: [
661
- { node: { key: 'key1', value: 'testValue' }, cursor: 'cursor1' },
662
- { node: { key: 'key2', value: 'testValue' }, cursor: 'cursor2' }
663
- ]
664
- }
665
- }
666
- });
667
- const metricMocks = getMetricMock();
668
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
669
- const where = [
670
- {
671
- field: 'key',
672
- condition: 'STARTS_WITH',
673
- value: 'test'
674
- }
675
- ];
676
- const cursor = 'cursor';
677
- const limit = 10;
678
- const response = await globalStorage.list({ where, cursor, limit });
679
- verifyApiClientCalledWith(apiClientMock, {
680
- where,
681
- cursor,
682
- limit
683
- }, gql_queries_1.UntypedQueries.listQuery({}).query);
684
- expect(response).toEqual(expect.objectContaining({
685
- results: [
686
- { key: 'key1', value: 'testValue' },
687
- { key: 'key2', value: 'testValue' }
688
- ],
689
- nextCursor: 'cursor2'
690
- }));
691
- passMetrics
692
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, true)
693
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
694
- });
695
- it('should use default values', async () => {
696
- const apiClientMock = getApiClientMock({
697
- data: {
698
- appStoredEntities: {
699
- edges: []
700
- }
701
- }
702
- });
703
- const metricMocks = getMetricMock();
704
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
705
- await globalStorage.list({});
706
- verifyApiClientCalledWith(apiClientMock, {
707
- where: null,
708
- cursor: null,
709
- limit: null
710
- }, gql_queries_1.UntypedQueries.listQuery({}).query);
711
- passMetrics
712
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, true)
713
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
714
- });
715
- it('should handle an empty result set', async () => {
716
- const apiClientMock = getApiClientMock({
717
- data: {
718
- appStoredEntities: {
719
- edges: []
720
- }
721
- }
722
- });
723
- const metricMocks = getMetricMock();
724
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
725
- const where = [
726
- {
727
- field: 'key',
728
- condition: 'STARTS_WITH',
729
- value: 'test'
730
- }
731
- ];
732
- const response = await globalStorage.list({ where });
733
- expect(response).toEqual(expect.objectContaining({
734
- results: [],
735
- nextCursor: undefined
736
- }));
737
- passMetrics
738
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, true)
739
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
740
- });
741
- it('should throw an error if the storage API returns an error', async () => {
742
- const apiClientMock = getApiClientMock({
743
- errors: [INVALID_CURSOR_ERROR]
744
- });
745
- const metricMocks = getMetricMock();
746
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
747
- const response = globalStorage.list({});
748
- expect(apiClientMock).toHaveBeenCalled();
749
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
750
- passMetrics
751
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, false)
752
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
753
- });
754
- it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
755
- const apiClientMock = getApiClientMockInvalidJson('', 400);
756
- const metricMocks = getMetricMock();
757
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
758
- const response = globalStorage.list({});
759
- expect(apiClientMock).toHaveBeenCalled();
760
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
761
- passMetrics
762
- ? checkMetricsFired(metricMocks, { store: 'untyped', operation: 'query', encrypted: false }, false)
763
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
764
- });
765
- });
766
- describe('listCustomEntities', () => {
767
- it('should use default values', async () => {
768
- const apiClientMock = getApiClientMock({
769
- data: {
770
- appStoredCustomEntities: {
771
- edges: []
772
- }
773
- }
774
- });
775
- const metricMocks = getMetricMock();
776
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
777
- const response = await globalStorage.listCustomEntities({});
778
- expect(response).toMatchObject({
779
- results: [],
780
- nextCursor: null
781
- });
782
- verifyApiClientCalledWith(apiClientMock, {}, gql_queries_1.CustomEntityQueries.listQuery({}).query);
783
- passMetrics
784
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, true)
785
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
786
- });
787
- it('should return cursor when results are not present', async () => {
788
- const apiClientMock = getApiClientMock({
789
- data: {
790
- appStoredCustomEntities: {
791
- edges: [],
792
- cursor: 'DUMMY_CURSOR'
793
- }
794
- }
795
- });
796
- const metricMocks = getMetricMock();
797
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
798
- const response = await globalStorage.listCustomEntities({});
799
- expect(response).toMatchObject({
800
- results: [],
801
- nextCursor: 'DUMMY_CURSOR'
802
- });
803
- verifyApiClientCalledWith(apiClientMock, {}, gql_queries_1.CustomEntityQueries.listQuery({}).query);
804
- passMetrics
805
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, true)
806
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
807
- });
808
- it('should throw an error if the storage API returns an error', async () => {
809
- const apiClientMock = getApiClientMock({
810
- errors: [INVALID_CURSOR_ERROR]
811
- });
812
- const metricMocks = getMetricMock();
813
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
814
- const response = globalStorage.listCustomEntities({});
815
- expect(apiClientMock).toHaveBeenCalled();
816
- await expect(response).rejects.toThrow(errors_1.APIError.forErrorCode('CURSOR_INVALID', 'error message'));
817
- passMetrics
818
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, false)
819
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
820
- });
821
- it('should throw an error if the storage API returns a non 200 status code and has no body', async () => {
822
- const apiClientMock = getApiClientMockInvalidJson('', 400);
823
- const metricMocks = getMetricMock();
824
- const globalStorage = getStorage(apiClientMock, passMetrics ? metricMocks.mockMetrics : undefined);
825
- const response = globalStorage.listCustomEntities({});
826
- expect(apiClientMock).toHaveBeenCalled();
827
- await expect(response).rejects.toThrow(errors_1.APIError.forStatus(400));
828
- passMetrics
829
- ? checkMetricsFired(metricMocks, { store: 'typed', operation: 'query', encrypted: false }, false)
830
- : expect(metricMocks.mockMetrics.counter).not.toHaveBeenCalled();
831
- });
832
- });
833
- });