@prisme.ai/sdk 1.0.1 → 2.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.
Files changed (54) hide show
  1. package/README.md +250 -0
  2. package/dist/index.d.mts +1129 -0
  3. package/dist/index.d.ts +1129 -7
  4. package/dist/index.js +1326 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/index.mjs +1287 -0
  7. package/dist/index.mjs.map +1 -0
  8. package/package.json +50 -11
  9. package/Readme.md +0 -28
  10. package/dist/_virtual/_tslib.js +0 -107
  11. package/dist/lib/ApiError.d.ts +0 -8
  12. package/dist/lib/ApiError.test.d.ts +0 -1
  13. package/dist/lib/HTTPError.d.ts +0 -6
  14. package/dist/lib/HTTPError.test.d.ts +0 -1
  15. package/dist/lib/api.d.ts +0 -140
  16. package/dist/lib/api.test.d.ts +0 -1
  17. package/dist/lib/endpoints/users.d.ts +0 -9
  18. package/dist/lib/endpoints/workspaces.d.ts +0 -9
  19. package/dist/lib/endpoints/workspacesVersions.d.ts +0 -10
  20. package/dist/lib/events.d.ts +0 -38
  21. package/dist/lib/events.test.d.ts +0 -1
  22. package/dist/lib/fetcher.d.ts +0 -23
  23. package/dist/lib/fetcher.test.d.ts +0 -1
  24. package/dist/lib/types.d.ts +0 -17
  25. package/dist/lib/utils.d.ts +0 -1
  26. package/dist/lib/utils.test.d.ts +0 -1
  27. package/dist/sdk/index.js +0 -19
  28. package/dist/sdk/lib/ApiError.js +0 -24
  29. package/dist/sdk/lib/HTTPError.js +0 -21
  30. package/dist/sdk/lib/api.js +0 -956
  31. package/dist/sdk/lib/endpoints/users.js +0 -43
  32. package/dist/sdk/lib/endpoints/workspaces.js +0 -23
  33. package/dist/sdk/lib/endpoints/workspacesVersions.js +0 -40
  34. package/dist/sdk/lib/events.js +0 -142
  35. package/dist/sdk/lib/fetcher.js +0 -175
  36. package/dist/sdk/lib/utils.js +0 -18
  37. package/index.ts +0 -7
  38. package/lib/ApiError.test.ts +0 -13
  39. package/lib/ApiError.ts +0 -21
  40. package/lib/HTTPError.test.ts +0 -8
  41. package/lib/HTTPError.ts +0 -13
  42. package/lib/api.test.ts +0 -772
  43. package/lib/api.ts +0 -871
  44. package/lib/endpoints/users.ts +0 -22
  45. package/lib/endpoints/workspaces.ts +0 -18
  46. package/lib/endpoints/workspacesVersions.ts +0 -34
  47. package/lib/events.test.ts +0 -89
  48. package/lib/events.ts +0 -180
  49. package/lib/fetcher.test.ts +0 -246
  50. package/lib/fetcher.ts +0 -163
  51. package/lib/types.ts +0 -21
  52. package/lib/utils.test.ts +0 -38
  53. package/lib/utils.ts +0 -12
  54. package/tsconfig.json +0 -21
package/lib/api.test.ts DELETED
@@ -1,772 +0,0 @@
1
- import api, { Api } from './api';
2
- import ApiError from './ApiError';
3
- import UsersEndpoint from './endpoints/users';
4
- import WorkspacesEndpoint from './endpoints/workspaces';
5
- import WorkspacesVersionsEndpoint from './endpoints/workspacesVersions';
6
-
7
- jest.mock('./events', () => {
8
- class Events {
9
- static mockedConstructor = jest.fn();
10
- static mockedClose = jest.fn();
11
- constructor({ api, ...args }: any) {
12
- // @ts-ignore
13
- this.api = api;
14
- Events.mockedConstructor({ api, ...args });
15
- }
16
-
17
- once = jest.fn((event: string, cb: Function) => {
18
- // @ts-ignore
19
- const shouldFail = this.api._shouldFail;
20
- if (!shouldFail && event === 'connect') cb();
21
- if (shouldFail && event !== 'connect') cb();
22
- return () => null;
23
- });
24
- close = Events.mockedClose;
25
- }
26
- return { Events };
27
- });
28
-
29
- it('should export an instance', () => {
30
- expect(api).toBeInstanceOf(Api);
31
- });
32
-
33
- it('should call /me', async () => {
34
- const api = new Api({ host: '/fake/' });
35
- api.get = jest.fn(
36
- async () =>
37
- ({
38
- sessionId: 'session',
39
- } as any)
40
- );
41
- const me = await api.me();
42
- expect(api.get).toHaveBeenCalledWith('/me');
43
- expect(api.user).toBe(me);
44
- });
45
-
46
- it('should call /login/anonymous', async () => {
47
- const api = new Api({ host: '/fake/' });
48
- api.post = jest.fn();
49
- const user = await api.createAnonymousSession();
50
- expect(api.post).toHaveBeenCalledWith(
51
- '/login/anonymous',
52
- {},
53
- { credentials: 'omit' }
54
- );
55
- expect(api.user).toBe(user);
56
- });
57
-
58
- it('should call /signup', () => {
59
- const api = new Api({ host: '/fake/' });
60
- api.post = jest.fn();
61
- api.signup('user@fake.com', 'password', 'firstname', 'lastname', 'fr');
62
- expect(api.post).toHaveBeenCalledWith('/signup', {
63
- email: 'user@fake.com',
64
- password: 'password',
65
- firstName: 'firstname',
66
- lastName: 'lastname',
67
- language: 'fr',
68
- });
69
- });
70
-
71
- it('should call get /workspaces', () => {
72
- const api = new Api({ host: '/fake/' });
73
- api.get = jest.fn();
74
- api.getWorkspaces();
75
- expect(api.get).toHaveBeenCalledWith('/workspaces?limit=600');
76
- });
77
-
78
- it('should call get /workspaces/42', () => {
79
- const api = new Api({ host: '/fake/' });
80
- api.get = jest.fn();
81
- api.getWorkspace('42');
82
- expect(api.get).toHaveBeenCalledWith('/workspaces/42');
83
- });
84
-
85
- it('should call post /workspaces', () => {
86
- const api = new Api({ host: '/fake/' });
87
- api.post = jest.fn();
88
- api.createWorkspace('foo');
89
- expect(api.post).toHaveBeenCalledWith('/workspaces', {
90
- name: 'foo',
91
- });
92
- });
93
-
94
- it('should call patch /workspaces/42', async () => {
95
- const api = new Api({ host: '/fake/' });
96
- api.patch = jest.fn();
97
- await api.updateWorkspace({
98
- id: '42',
99
- name: 'foo',
100
- automations: {},
101
- createdAt: '',
102
- updatedAt: '',
103
- });
104
- expect(api.patch).toHaveBeenCalledWith('/workspaces/42', {
105
- id: '42',
106
- name: 'foo',
107
- automations: {},
108
- createdAt: '',
109
- updatedAt: '',
110
- });
111
- });
112
-
113
- it('should call delete /workspaces/42', async () => {
114
- const api = new Api({ host: '/fake/' });
115
- api.delete = jest.fn();
116
- await api.deleteWorkspace('42');
117
- expect(api.delete).toHaveBeenCalledWith('/workspaces/42');
118
- });
119
-
120
- it('should call post /workspaces/42/automations', () => {
121
- const api = new Api({ host: '/fake/' });
122
- api.post = jest.fn();
123
- api.createAutomation('42', {
124
- name: 'foo',
125
- do: [],
126
- });
127
- expect(api.post).toHaveBeenCalledWith('/workspaces/42/automations', {
128
- name: 'foo',
129
- do: [],
130
- });
131
- });
132
-
133
- it('should call patch /workspaces/42/automations', async () => {
134
- const api = new Api({ host: '/fake/' });
135
- api.patch = jest.fn();
136
- await api.updateAutomation('42', '42-1', {
137
- name: 'foo',
138
- do: [],
139
- });
140
- expect(api.patch).toHaveBeenCalledWith('/workspaces/42/automations/42-1', {
141
- name: 'foo',
142
- do: [],
143
- });
144
- });
145
-
146
- it('should call delete /workspaces/42/automations/42-1', () => {
147
- const api = new Api({ host: '/fake/' });
148
- api.delete = jest.fn();
149
- api.deleteAutomation('42', '42-1');
150
- expect(api.delete).toHaveBeenCalledWith('/workspaces/42/automations/42-1');
151
- });
152
-
153
- it('should call get /workspaces/42/events', async () => {
154
- const api = new Api({ host: '/fake/' });
155
- api.get = jest.fn(
156
- async (): Promise<any> => ({
157
- result: {
158
- events: [
159
- {
160
- id: '1',
161
- createdAt: '2021-01-01',
162
- },
163
- ],
164
- },
165
- })
166
- );
167
- expect(await api.getEvents('42')).toEqual([
168
- {
169
- id: '1',
170
- createdAt: new Date('2021-01-01'),
171
- },
172
- ]);
173
- });
174
-
175
- it('should replace all images data', async () => {
176
- const api = new Api({ host: '/fake/' });
177
- api.uploadFiles = jest.fn(async () => [
178
- { url: 'http://image1.jpg' } as any,
179
- { url: 'http://image2.jpg' } as any,
180
- { url: 'http://image3.jpg' } as any,
181
- { url: 'http://image4.jpg' } as any,
182
- { url: 'http://image5.jpg' } as any,
183
- ]);
184
- const original = {
185
- foo: 'data:image/jpeg;base64…',
186
- bar: {
187
- pic: 'data:image/jpeg;base64…',
188
- pics: [
189
- 'data:image/jpeg;base64…',
190
- 'data:image/jpeg;base64…',
191
- 'data:image/jpeg;base64…',
192
- ],
193
- nopics: 'http://alreadyUpImage.jpg',
194
- },
195
- anythingElse: 42,
196
- };
197
- expect(await api.replaceAllImagesData(original, '42')).toEqual({
198
- foo: 'http://image1.jpg',
199
- bar: {
200
- pic: 'http://image2.jpg',
201
- pics: ['http://image3.jpg', 'http://image4.jpg', 'http://image5.jpg'],
202
- nopics: 'http://alreadyUpImage.jpg',
203
- },
204
- anythingElse: 42,
205
- });
206
- expect(api.uploadFiles).toHaveBeenCalledWith(
207
- [
208
- 'data:image/jpeg;base64…',
209
- 'data:image/jpeg;base64…',
210
- 'data:image/jpeg;base64…',
211
- 'data:image/jpeg;base64…',
212
- 'data:image/jpeg;base64…',
213
- ],
214
- '42'
215
- );
216
- expect(api.uploadFiles).toHaveBeenCalledTimes(1);
217
- });
218
-
219
- it('should upload file', async () => {
220
- const api = new Api({ host: '/fake/' });
221
- // @ts-ignore
222
- api._fetch = jest.fn(() => [{}]);
223
- await api.uploadFiles(
224
- 'data:image/jpeg;filename:foo.jpg;base64,abcdefg',
225
- '42'
226
- );
227
- // @ts-ignore
228
- expect(api._fetch).toHaveBeenCalledWith('/workspaces/42/files', {
229
- method: 'POST',
230
- body: expect.any(FormData),
231
- });
232
- const { body }: { body: FormData } =
233
- // @ts-ignore
234
- api._fetch.mock.calls[0][1];
235
- expect(body.getAll('file').length).toBe(1);
236
- expect((body.getAll('file')[0] as File).name).toBe('foo.jpg');
237
- });
238
-
239
- it('should generate api key', async () => {
240
- const api = new Api({ host: '/fake/' });
241
- api.post = jest.fn((): any => ({
242
- apiKey: 'api-key',
243
- }));
244
- const apiKey = await api.generateApiKey('42', ['event1', 'event2']);
245
- expect(apiKey).toBe('api-key');
246
- expect(api.post).toHaveBeenCalledWith('/workspaces/42/apiKeys', {
247
- rules: {
248
- events: {
249
- types: ['event1', 'event2'],
250
- filters: {
251
- 'source.sessionId': '${user.sessionId}',
252
- },
253
- },
254
- uploads: undefined,
255
- },
256
- });
257
- (api.post as jest.Mock).mockClear();
258
-
259
- await api.generateApiKey('42', ['event1', 'event2'], ['images/*']);
260
- expect(api.post).toHaveBeenCalledWith('/workspaces/42/apiKeys', {
261
- rules: {
262
- events: {
263
- types: ['event1', 'event2'],
264
- filters: {
265
- 'source.sessionId': '${user.sessionId}',
266
- },
267
- },
268
- uploads: {
269
- mimetypes: ['images/*'],
270
- },
271
- },
272
- });
273
- });
274
-
275
- it('should update api key', async () => {
276
- const api = new Api({ host: '/fake/' });
277
- api.put = jest.fn((): any => ({
278
- apiKey: 'api-key',
279
- }));
280
- const apiKey = await api.updateApiKey('42', 'api-key', [
281
- 'event1',
282
- 'event2',
283
- 'event3',
284
- ]);
285
- expect(apiKey).toBe('api-key');
286
- expect(api.put).toHaveBeenCalledWith('/workspaces/42/apiKeys/api-key', {
287
- rules: {
288
- events: {
289
- types: ['event1', 'event2', 'event3'],
290
- filters: {
291
- 'source.sessionId': '${user.sessionId}',
292
- },
293
- uploads: undefined,
294
- },
295
- },
296
- });
297
-
298
- (api.put as jest.Mock).mockClear();
299
-
300
- await api.updateApiKey('42', 'api-key', ['event1', 'event2'], ['images/*']);
301
- expect(api.put).toHaveBeenCalledWith('/workspaces/42/apiKeys/api-key', {
302
- rules: {
303
- events: {
304
- types: ['event1', 'event2'],
305
- filters: {
306
- 'source.sessionId': '${user.sessionId}',
307
- },
308
- },
309
- uploads: {
310
- mimetypes: ['images/*'],
311
- },
312
- },
313
- });
314
- });
315
-
316
- it('should send validation mail', () => {
317
- const api = new Api({ host: '/fake/' });
318
- api.post = jest.fn();
319
- api.sendValidationMail('email', 'fr');
320
- expect(api.post).toHaveBeenCalledWith('/user/validate', {
321
- email: 'email',
322
- language: 'fr',
323
- });
324
- });
325
-
326
- it('should validate mail', () => {
327
- const api = new Api({ host: '/fake/' });
328
- api.post = jest.fn();
329
- api.validateMail('token');
330
- expect(api.post).toHaveBeenCalledWith('/user/validate', {
331
- token: 'token',
332
- });
333
- });
334
-
335
- it('should send password reset mail', () => {
336
- const api = new Api({ host: '/fake/' });
337
- api.post = jest.fn();
338
- api.sendPasswordResetMail('email', 'fr');
339
- expect(api.post).toHaveBeenCalledWith('/user/password', {
340
- email: 'email',
341
- language: 'fr',
342
- });
343
- });
344
-
345
- it('should reset password', () => {
346
- const api = new Api({ host: '/fake/' });
347
- api.post = jest.fn();
348
- api.passwordReset('token', 'azerty');
349
- expect(api.post).toHaveBeenCalledWith('/user/password', {
350
- token: 'token',
351
- password: 'azerty',
352
- });
353
- });
354
-
355
- it('should get pages', async () => {
356
- const api = new Api({ host: '/fake/' });
357
- api.get = jest.fn((): any => [
358
- {
359
- id: '123',
360
- createdAt: 'must be removed',
361
- createdBy: 'must be removed',
362
- updatedAt: 'must be removed',
363
- updatedBy: 'must be removed',
364
- },
365
- ]);
366
- const pages = await api.getPages('42');
367
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/pages');
368
- expect(pages).toEqual([{ id: '123' }]);
369
-
370
- api.get = jest.fn((): any => {
371
- throw new Error();
372
- });
373
- const empty = await api.getPages('42');
374
- expect(empty).toEqual([]);
375
- });
376
-
377
- it('should get page', () => {
378
- const api = new Api({ host: '/fake/' });
379
- api.get = jest.fn((): any => ({}));
380
- api.getPage('42', '123');
381
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/pages/123');
382
- });
383
-
384
- it('should get page by slug', () => {
385
- const api = new Api({ host: '/fake/' });
386
- api.get = jest.fn((): any => ({}));
387
- api.getPageBySlug('42', '123');
388
- expect(api.get).toHaveBeenCalledWith('/pages/42/123');
389
- });
390
-
391
- it('should create page', () => {
392
- const api = new Api({ host: '/fake/' });
393
- api.post = jest.fn((): any => ({}));
394
- api.createPage('42', {} as Prismeai.Page);
395
- expect(api.post).toHaveBeenCalledWith(`/workspaces/42/pages`, {});
396
- });
397
-
398
- it('should update page', async () => {
399
- const api = new Api({ host: '/fake/' });
400
- api.patch = jest.fn((): any => ({ slug: 'my-page' }));
401
- await api.updatePage('42', { id: '123', slug: 'my-page' } as Prismeai.Page);
402
- expect(api.patch).toHaveBeenCalledWith(`/workspaces/42/pages/my-page`, {
403
- id: '123',
404
- slug: 'my-page',
405
- });
406
- });
407
-
408
- it('should delete page', () => {
409
- const api = new Api({ host: '/fake/' });
410
- api.delete = jest.fn((): any => ({ id: '123' }));
411
- api.deletePage('42', '123');
412
- expect(api.delete).toHaveBeenCalledWith('/workspaces/42/pages/123');
413
- });
414
-
415
- it('should stream events', async () => {
416
- const api = new Api({ host: '/fake/' });
417
-
418
- const events = await api.streamEvents('42');
419
- expect((events.constructor as any).mockedConstructor).toHaveBeenCalledWith({
420
- workspaceId: '42',
421
- token: '',
422
- apiKey: undefined,
423
- apiHost: '/fake/',
424
- filters: undefined,
425
- api,
426
- legacyToken: '',
427
- });
428
- expect(events.once).toHaveBeenCalled();
429
- (events.constructor as any).mockedConstructor.mockClear();
430
-
431
- const events2 = await api.streamEvents('42', { 'source.sessionId': true });
432
- expect((events2.constructor as any).mockedConstructor).toHaveBeenCalledWith({
433
- workspaceId: '42',
434
- token: '',
435
- apiKey: undefined,
436
- apiHost: '/fake/',
437
- filters: {},
438
- api,
439
- legacyToken: '',
440
- });
441
- expect(events2.once).toHaveBeenCalled();
442
-
443
- // @ts-ignore
444
- api.sessionId = 'session id';
445
- // @ts-ignore
446
- api._apiKey = 'api key';
447
- const events3 = await api.streamEvents('42', { 'source.sessionId': true });
448
- expect((events2.constructor as any).mockedConstructor).toHaveBeenCalledWith({
449
- workspaceId: '42',
450
- token: '',
451
- apiKey: 'api key',
452
- apiHost: '/fake/',
453
- filters: {
454
- 'source.sessionId': 'session id',
455
- },
456
- api,
457
- legacyToken: '',
458
- });
459
- expect(events3.once).toHaveBeenCalled();
460
-
461
- // @ts-ignore
462
- api._shouldFail = true;
463
- try {
464
- await api.streamEvents('42');
465
- } catch (e) {}
466
- expect((events3.constructor as any).mockedConstructor).toHaveBeenCalledWith({
467
- workspaceId: '42',
468
- token: '',
469
- apiKey: 'api key',
470
- apiHost: '/fake/',
471
- filters: undefined,
472
- api,
473
- legacyToken: '',
474
- });
475
- expect(events3.close).toHaveBeenCalled();
476
- });
477
-
478
- it('should get events', async () => {
479
- const api = new Api({ host: '/fake/' });
480
- api.get = jest.fn((): any => ({
481
- result: { events: [{ createdAt: '2022-01-01', id: '123' }] },
482
- }));
483
- api.getEvents('42');
484
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/events');
485
-
486
- const events = await api.getEvents('42', { foo: 'bar' });
487
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/events?foo=bar');
488
- expect(events).toEqual([{ id: '123', createdAt: new Date('2022-01-01') }]);
489
-
490
- api.get = jest.fn((): any => {
491
- throw new Error();
492
- });
493
- const empty = await api.getEvents('42', { foo: 'bar' });
494
- expect(empty).toEqual([]);
495
- });
496
-
497
- it('should post events', async () => {
498
- const api = new Api({ host: '/fake/' });
499
- api.post = jest.fn((): any => {});
500
- const res = await api.postEvents('42', [
501
- {
502
- type: 'foo',
503
- payload: {},
504
- },
505
- ]);
506
- expect(api.post).toHaveBeenCalledWith('/workspaces/42/events', {
507
- events: [
508
- {
509
- type: 'foo',
510
- payload: {},
511
- },
512
- ],
513
- });
514
- expect(res).toBe(true);
515
-
516
- api.post = jest.fn((): any => {
517
- throw new Error();
518
- });
519
- const res2 = await api.postEvents('42', [
520
- {
521
- type: 'foo',
522
- payload: {},
523
- },
524
- ]);
525
- expect(res2).toBe(false);
526
- });
527
-
528
- describe('permissions', () => {
529
- const api = new Api({ host: '/fake/' });
530
- api.get = jest.fn((path: string): any => {
531
- if (path === '/pages/id/permissions') {
532
- return {
533
- result: [],
534
- };
535
- }
536
- });
537
- api.post = jest.fn((path: string, body: any): any => {
538
- if (path === '/pages/id/permissions') {
539
- return body;
540
- }
541
- });
542
- api.findContacts = jest.fn(({ email }): any => {
543
- if (email === 'foo@bar.com')
544
- return {
545
- contacts: [
546
- {
547
- id: '1234',
548
- },
549
- ],
550
- };
551
- return { contacts: [] };
552
- });
553
- api.getPermissions('pages', 'id');
554
-
555
- it('should get permissions', async () => {
556
- expect(api.get).toHaveBeenCalledWith('/pages/id/permissions');
557
- });
558
-
559
- it('should add permissions', async () => {
560
- await api.addPermissions('pages', 'id', {
561
- permissions: {},
562
- target: { email: 'foo@bar.com' },
563
- });
564
- expect(api.post).toHaveBeenCalledWith('/pages/id/permissions', {
565
- permissions: {},
566
- target: {
567
- id: '1234',
568
- },
569
- });
570
- });
571
-
572
- it('should fail to add permissions', async () => {
573
- let expected: ApiError = {} as ApiError;
574
- try {
575
- await api.addPermissions('pages', 'id', {
576
- permissions: {},
577
- target: { email: 'foofoo@bar.com' },
578
- });
579
- } catch (e) {
580
- console.log(e);
581
- expected = e as ApiError;
582
- }
583
- expect(expected.message).toBe('This user does not exist');
584
- });
585
-
586
- it('should delete permissions', async () => {
587
- api.delete = jest.fn((): any => {});
588
- api.deletePermissions('pages', 'id', 'foo');
589
- expect(api.delete).toHaveBeenCalledWith('/pages/id/permissions/foo');
590
- });
591
- });
592
-
593
- it('should get apps', async () => {
594
- const api = new Api({ host: '/fake/' });
595
- const get: jest.Mock = (api.get = jest.fn((): any => {}));
596
- api.getApps({});
597
- expect(api.get).toHaveBeenCalledWith('/apps?');
598
- get.mockClear();
599
-
600
- api.getApps({ query: 'foo' });
601
- expect(get).toHaveBeenCalledWith('/apps?text=foo');
602
- get.mockClear();
603
-
604
- api.getApps({ query: 'foo', page: 1, limit: 10, workspaceId: '42' });
605
- expect(get).toHaveBeenCalledWith(
606
- '/apps?text=foo&page=1&limit=10&workspaceId=42'
607
- );
608
- });
609
-
610
- it('should install app', async () => {
611
- const api = new Api({ host: '/fake/' });
612
- api.post = jest.fn((): any => {});
613
- api.installApp('42', { appSlug: 'app' });
614
- expect(api.post).toHaveBeenCalledWith('/workspaces/42/apps', {
615
- appSlug: 'app',
616
- });
617
- });
618
-
619
- it('should update app', async () => {
620
- const api = new Api({ host: '/fake/' });
621
- api.patch = jest.fn((): any => {});
622
- api.updateApp('42', 'app', { appSlug: 'app' });
623
- expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app', {
624
- appSlug: 'app',
625
- });
626
- });
627
-
628
- it('should uninstall app', async () => {
629
- const api = new Api({ host: '/fake/' });
630
- api.delete = jest.fn((): any => {});
631
- api.uninstallApp('42', 'app');
632
- expect(api.delete).toHaveBeenCalledWith('/workspaces/42/apps/app');
633
- });
634
-
635
- it('should publish app', async () => {
636
- const api = new Api({ host: '/fake/' });
637
- api.post = jest.fn((): any => {});
638
- api.publishApp({ workspaceId: '42' });
639
- expect(api.post).toHaveBeenCalledWith('/apps', { workspaceId: '42' });
640
- });
641
-
642
- it('should list app instances', async () => {
643
- const api = new Api({ host: '/fake/' });
644
- api.get = jest.fn((): any => {});
645
- api.listAppInstances('42');
646
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/apps');
647
- });
648
-
649
- it('should get app config', async () => {
650
- const api = new Api({ host: '/fake/' });
651
- api.get = jest.fn((): any => {});
652
- api.getAppConfig('42', 'app');
653
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/apps/app/config');
654
- });
655
-
656
- it('should update app config', async () => {
657
- const api = new Api({ host: '/fake/' });
658
- api.patch = jest.fn((): any => {});
659
- api.updateAppConfig('42', 'app', {});
660
- expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app/config', {});
661
- });
662
-
663
- it('should save app instance', async () => {
664
- const api = new Api({ host: '/fake/' });
665
- api.patch = jest.fn((): any => {});
666
- api.saveAppInstance('42', 'app', {});
667
- expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app', {});
668
- });
669
-
670
- it('should upload files', async () => {
671
- const api = new Api({ host: '/fake/' });
672
- // @ts-ignore
673
- let fetch: jest.Mock = (api._fetch = jest.fn((): any => {}));
674
- api.uploadFiles('data:text/plain;base64,SGVsbG8gV29ybGQ', '42');
675
- // @ts-ignore
676
- expect(fetch).toHaveBeenCalledWith('/workspaces/42/files', {
677
- method: 'POST',
678
- body: expect.any(FormData),
679
- });
680
-
681
- expect(fetch.mock.calls[0][1].body.get('file')).toBeInstanceOf(Blob);
682
- fetch.mockClear();
683
-
684
- api.uploadFiles(['data:text/plain;text,Hello World'], '42');
685
- // @ts-ignore
686
- expect(fetch).toHaveBeenCalledWith('/workspaces/42/files', {
687
- method: 'POST',
688
- body: expect.any(FormData),
689
- });
690
-
691
- // @ts-ignore
692
- fetch = api._fetch = jest.fn((): any => {
693
- throw new Error();
694
- });
695
- const empty = await api.uploadFiles('data:text/plain;text,Hello World', '42');
696
- expect(empty).toEqual([]);
697
- });
698
-
699
- it('should call automation', () => {
700
- const api = new Api({ host: '/fake/' });
701
- api.post = jest.fn((): any => {});
702
- api.callAutomation('42', 'automation');
703
- expect(api.post).toHaveBeenCalledWith(
704
- '/workspaces/42/webhooks/automation',
705
- undefined
706
- );
707
- });
708
-
709
- it('should get workspace usage', () => {
710
- const api = new Api({ host: '/fake/' });
711
- const get: jest.Mock = (api.get = jest.fn((): any => {}));
712
- api.getWorkspaceUsage('42');
713
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/usage?');
714
- get.mockClear();
715
-
716
- api.getWorkspaceUsage('42', {
717
- afterDate: '2022',
718
- beforeDate: undefined,
719
- });
720
- expect(api.get).toHaveBeenCalledWith('/workspaces/42/usage?afterDate=2022');
721
- });
722
-
723
- describe('Workspaces', () => {
724
- it('should access workspaces methods', () => {
725
- expect(api.workspaces('42')).toBeInstanceOf(WorkspacesEndpoint);
726
- });
727
- describe('Versions', () => {
728
- it('should access versions methods', () => {
729
- expect(api.workspaces('42').versions).toBeInstanceOf(
730
- WorkspacesVersionsEndpoint
731
- );
732
- });
733
- it('create', () => {
734
- api.post = jest.fn();
735
- api.workspaces('42').versions.create({ description: 'foo' });
736
- expect(api.post).toHaveBeenCalledWith('/workspaces/42/versions', {
737
- description: 'foo',
738
- });
739
- });
740
- it('rollback', () => {
741
- api.post = jest.fn();
742
- api.workspaces('42').versions.rollback('v1.0.0');
743
- expect(api.post).toHaveBeenCalledWith(
744
- '/workspaces/42/versions/v1.0.0/rollback'
745
- );
746
- });
747
- });
748
- });
749
-
750
- describe('User', () => {
751
- it('should access user methods', () => {
752
- expect(api.users('42')).toBeInstanceOf(UsersEndpoint);
753
- });
754
- it('should access user methods with current user', () => {
755
- // @ts-ignore
756
- api._user = { id: '42' } as any;
757
- expect(api.users()).toBeInstanceOf(UsersEndpoint);
758
- });
759
-
760
- it('should set meta data', () => {
761
- api.post = jest.fn();
762
- api.users().setMeta('foo', 'bar');
763
- expect(api.post).toHaveBeenCalledWith('/user/meta', {
764
- foo: 'bar',
765
- });
766
- });
767
- it('should delete meta data', () => {
768
- api.delete = jest.fn();
769
- api.users().deleteMeta('foo');
770
- expect(api.delete).toHaveBeenCalledWith('/user/meta/foo');
771
- });
772
- });