@prisme.ai/sdk 1.0.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.
@@ -14,10 +14,12 @@ var headersAsObject = function (headers) {
14
14
  }, {});
15
15
  };
16
16
  var Fetcher = /** @class */ (function () {
17
- function Fetcher(host) {
17
+ function Fetcher(host, clientIdHeader) {
18
18
  this.token = null;
19
+ this.legacyToken = null;
19
20
  this._apiKey = null;
20
21
  this.host = host;
22
+ this.clientIdHeader = clientIdHeader;
21
23
  }
22
24
  Object.defineProperty(Fetcher.prototype, "apiKey", {
23
25
  set: function (apiKey) {
@@ -29,8 +31,11 @@ var Fetcher = /** @class */ (function () {
29
31
  Fetcher.prototype.prepareRequest = function (url, options) {
30
32
  if (options === void 0) { options = {}; }
31
33
  var headers = new Headers(options.headers || {});
32
- if (this.token && !headers.has('x-prismeai-token')) {
33
- headers.append('x-prismeai-token', this.token);
34
+ if (this.token && !headers.has('Authorization')) {
35
+ headers.append('Authorization', "Bearer ".concat(this.token));
36
+ }
37
+ else if (this.legacyToken && !headers.has('Authorization')) {
38
+ headers.append('x-prismeai-token', this.legacyToken);
34
39
  }
35
40
  if (this._apiKey && !headers.has('x-prismeai-apikey')) {
36
41
  headers.append('x-prismeai-api-key', this._apiKey);
@@ -38,12 +43,17 @@ var Fetcher = /** @class */ (function () {
38
43
  if (this.language) {
39
44
  headers.append('accept-language', this.language);
40
45
  }
46
+ if (options.body instanceof URLSearchParams) {
47
+ headers.set('Content-Type', 'application/x-www-form-urlencoded');
48
+ }
41
49
  if ((!options.body || !(options.body instanceof FormData)) &&
42
50
  !headers.has('Content-Type')) {
43
51
  headers.append('Content-Type', 'application/json');
44
52
  }
45
- headers.append('Access-Control-Allow-Origin', '*');
46
- return global.fetch("".concat(this.host).concat(url), _tslib.__assign(_tslib.__assign({}, options), { headers: headers }));
53
+ var fullUrl = url.startsWith('http://') || url.startsWith('https://')
54
+ ? url
55
+ : "".concat(this.host).concat(url);
56
+ return global.fetch(fullUrl, _tslib.__assign(_tslib.__assign({ credentials: 'include' }, options), { headers: headers }));
47
57
  };
48
58
  Fetcher.prototype._fetch = function (url, options) {
49
59
  if (options === void 0) { options = {}; }
@@ -54,6 +64,13 @@ var Fetcher = /** @class */ (function () {
54
64
  case 0: return [4 /*yield*/, this.prepareRequest(url, options)];
55
65
  case 1:
56
66
  res = _b.sent();
67
+ if (options.redirect === 'manual' && res.status === 0) {
68
+ return [2 /*return*/, { redirected: true }];
69
+ }
70
+ this.lastReceivedHeaders = headersAsObject(res.headers);
71
+ if (this.clientIdHeader && this.lastReceivedHeaders[this.clientIdHeader]) {
72
+ this.overwriteClientId = this.lastReceivedHeaders[this.clientIdHeader];
73
+ }
57
74
  if (!!res.ok) return [3 /*break*/, 6];
58
75
  error = void 0;
59
76
  _b.label = 2;
@@ -111,13 +128,14 @@ var Fetcher = /** @class */ (function () {
111
128
  });
112
129
  });
113
130
  };
114
- Fetcher.prototype.post = function (url, body) {
131
+ Fetcher.prototype.post = function (url, body, opts) {
115
132
  return _tslib.__awaiter(this, void 0, void 0, function () {
116
133
  return _tslib.__generator(this, function (_a) {
117
- return [2 /*return*/, this._fetch(url, {
118
- method: 'POST',
119
- body: body && !(body instanceof FormData) ? JSON.stringify(body) : body,
120
- })];
134
+ return [2 /*return*/, this._fetch(url, _tslib.__assign({ method: 'POST', body: body &&
135
+ !(body instanceof FormData) &&
136
+ !(body instanceof URLSearchParams)
137
+ ? JSON.stringify(body)
138
+ : body }, opts))];
121
139
  });
122
140
  });
123
141
  };
package/lib/api.test.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import api, { Api } from './api';
2
2
  import ApiError from './ApiError';
3
+ import UsersEndpoint from './endpoints/users';
3
4
  import WorkspacesEndpoint from './endpoints/workspaces';
4
5
  import WorkspacesVersionsEndpoint from './endpoints/workspacesVersions';
5
6
 
@@ -30,7 +31,7 @@ it('should export an instance', () => {
30
31
  });
31
32
 
32
33
  it('should call /me', async () => {
33
- const api = new Api('/fake/');
34
+ const api = new Api({ host: '/fake/' });
34
35
  api.get = jest.fn(
35
36
  async () =>
36
37
  ({
@@ -42,26 +43,20 @@ it('should call /me', async () => {
42
43
  expect(api.user).toBe(me);
43
44
  });
44
45
 
45
- it('should call /signin', () => {
46
- const api = new Api('/fake/');
47
- api.post = jest.fn();
48
- api.signin('user@fake.com', 'password');
49
- expect(api.post).toHaveBeenCalledWith('/login', {
50
- email: 'user@fake.com',
51
- password: 'password',
52
- });
53
- });
54
-
55
46
  it('should call /login/anonymous', async () => {
56
- const api = new Api('/fake/');
47
+ const api = new Api({ host: '/fake/' });
57
48
  api.post = jest.fn();
58
49
  const user = await api.createAnonymousSession();
59
- expect(api.post).toHaveBeenCalledWith('/login/anonymous');
50
+ expect(api.post).toHaveBeenCalledWith(
51
+ '/login/anonymous',
52
+ {},
53
+ { credentials: 'omit' }
54
+ );
60
55
  expect(api.user).toBe(user);
61
56
  });
62
57
 
63
58
  it('should call /signup', () => {
64
- const api = new Api('/fake/');
59
+ const api = new Api({ host: '/fake/' });
65
60
  api.post = jest.fn();
66
61
  api.signup('user@fake.com', 'password', 'firstname', 'lastname', 'fr');
67
62
  expect(api.post).toHaveBeenCalledWith('/signup', {
@@ -73,30 +68,22 @@ it('should call /signup', () => {
73
68
  });
74
69
  });
75
70
 
76
- it('should call /signout', () => {
77
- const api = new Api('/fake/');
78
- api.post = jest.fn();
79
- api.signout();
80
- expect(api.post).toHaveBeenCalledWith('/logout');
81
- expect(api.token).toBeNull();
82
- });
83
-
84
71
  it('should call get /workspaces', () => {
85
- const api = new Api('/fake/');
72
+ const api = new Api({ host: '/fake/' });
86
73
  api.get = jest.fn();
87
74
  api.getWorkspaces();
88
75
  expect(api.get).toHaveBeenCalledWith('/workspaces?limit=600');
89
76
  });
90
77
 
91
78
  it('should call get /workspaces/42', () => {
92
- const api = new Api('/fake/');
79
+ const api = new Api({ host: '/fake/' });
93
80
  api.get = jest.fn();
94
81
  api.getWorkspace('42');
95
82
  expect(api.get).toHaveBeenCalledWith('/workspaces/42');
96
83
  });
97
84
 
98
85
  it('should call post /workspaces', () => {
99
- const api = new Api('/fake/');
86
+ const api = new Api({ host: '/fake/' });
100
87
  api.post = jest.fn();
101
88
  api.createWorkspace('foo');
102
89
  expect(api.post).toHaveBeenCalledWith('/workspaces', {
@@ -105,7 +92,7 @@ it('should call post /workspaces', () => {
105
92
  });
106
93
 
107
94
  it('should call patch /workspaces/42', async () => {
108
- const api = new Api('/fake/');
95
+ const api = new Api({ host: '/fake/' });
109
96
  api.patch = jest.fn();
110
97
  await api.updateWorkspace({
111
98
  id: '42',
@@ -124,14 +111,14 @@ it('should call patch /workspaces/42', async () => {
124
111
  });
125
112
 
126
113
  it('should call delete /workspaces/42', async () => {
127
- const api = new Api('/fake/');
114
+ const api = new Api({ host: '/fake/' });
128
115
  api.delete = jest.fn();
129
116
  await api.deleteWorkspace('42');
130
117
  expect(api.delete).toHaveBeenCalledWith('/workspaces/42');
131
118
  });
132
119
 
133
120
  it('should call post /workspaces/42/automations', () => {
134
- const api = new Api('/fake/');
121
+ const api = new Api({ host: '/fake/' });
135
122
  api.post = jest.fn();
136
123
  api.createAutomation('42', {
137
124
  name: 'foo',
@@ -144,7 +131,7 @@ it('should call post /workspaces/42/automations', () => {
144
131
  });
145
132
 
146
133
  it('should call patch /workspaces/42/automations', async () => {
147
- const api = new Api('/fake/');
134
+ const api = new Api({ host: '/fake/' });
148
135
  api.patch = jest.fn();
149
136
  await api.updateAutomation('42', '42-1', {
150
137
  name: 'foo',
@@ -157,14 +144,14 @@ it('should call patch /workspaces/42/automations', async () => {
157
144
  });
158
145
 
159
146
  it('should call delete /workspaces/42/automations/42-1', () => {
160
- const api = new Api('/fake/');
147
+ const api = new Api({ host: '/fake/' });
161
148
  api.delete = jest.fn();
162
149
  api.deleteAutomation('42', '42-1');
163
150
  expect(api.delete).toHaveBeenCalledWith('/workspaces/42/automations/42-1');
164
151
  });
165
152
 
166
153
  it('should call get /workspaces/42/events', async () => {
167
- const api = new Api('/fake/');
154
+ const api = new Api({ host: '/fake/' });
168
155
  api.get = jest.fn(
169
156
  async (): Promise<any> => ({
170
157
  result: {
@@ -186,7 +173,7 @@ it('should call get /workspaces/42/events', async () => {
186
173
  });
187
174
 
188
175
  it('should replace all images data', async () => {
189
- const api = new Api('/fake/');
176
+ const api = new Api({ host: '/fake/' });
190
177
  api.uploadFiles = jest.fn(async () => [
191
178
  { url: 'http://image1.jpg' } as any,
192
179
  { url: 'http://image2.jpg' } as any,
@@ -230,7 +217,7 @@ it('should replace all images data', async () => {
230
217
  });
231
218
 
232
219
  it('should upload file', async () => {
233
- const api = new Api('/fake/');
220
+ const api = new Api({ host: '/fake/' });
234
221
  // @ts-ignore
235
222
  api._fetch = jest.fn(() => [{}]);
236
223
  await api.uploadFiles(
@@ -250,7 +237,7 @@ it('should upload file', async () => {
250
237
  });
251
238
 
252
239
  it('should generate api key', async () => {
253
- const api = new Api('/fake/');
240
+ const api = new Api({ host: '/fake/' });
254
241
  api.post = jest.fn((): any => ({
255
242
  apiKey: 'api-key',
256
243
  }));
@@ -286,7 +273,7 @@ it('should generate api key', async () => {
286
273
  });
287
274
 
288
275
  it('should update api key', async () => {
289
- const api = new Api('/fake/');
276
+ const api = new Api({ host: '/fake/' });
290
277
  api.put = jest.fn((): any => ({
291
278
  apiKey: 'api-key',
292
279
  }));
@@ -327,7 +314,7 @@ it('should update api key', async () => {
327
314
  });
328
315
 
329
316
  it('should send validation mail', () => {
330
- const api = new Api('/fake/');
317
+ const api = new Api({ host: '/fake/' });
331
318
  api.post = jest.fn();
332
319
  api.sendValidationMail('email', 'fr');
333
320
  expect(api.post).toHaveBeenCalledWith('/user/validate', {
@@ -337,7 +324,7 @@ it('should send validation mail', () => {
337
324
  });
338
325
 
339
326
  it('should validate mail', () => {
340
- const api = new Api('/fake/');
327
+ const api = new Api({ host: '/fake/' });
341
328
  api.post = jest.fn();
342
329
  api.validateMail('token');
343
330
  expect(api.post).toHaveBeenCalledWith('/user/validate', {
@@ -346,7 +333,7 @@ it('should validate mail', () => {
346
333
  });
347
334
 
348
335
  it('should send password reset mail', () => {
349
- const api = new Api('/fake/');
336
+ const api = new Api({ host: '/fake/' });
350
337
  api.post = jest.fn();
351
338
  api.sendPasswordResetMail('email', 'fr');
352
339
  expect(api.post).toHaveBeenCalledWith('/user/password', {
@@ -356,7 +343,7 @@ it('should send password reset mail', () => {
356
343
  });
357
344
 
358
345
  it('should reset password', () => {
359
- const api = new Api('/fake/');
346
+ const api = new Api({ host: '/fake/' });
360
347
  api.post = jest.fn();
361
348
  api.passwordReset('token', 'azerty');
362
349
  expect(api.post).toHaveBeenCalledWith('/user/password', {
@@ -366,7 +353,7 @@ it('should reset password', () => {
366
353
  });
367
354
 
368
355
  it('should get pages', async () => {
369
- const api = new Api('/fake/');
356
+ const api = new Api({ host: '/fake/' });
370
357
  api.get = jest.fn((): any => [
371
358
  {
372
359
  id: '123',
@@ -388,28 +375,28 @@ it('should get pages', async () => {
388
375
  });
389
376
 
390
377
  it('should get page', () => {
391
- const api = new Api('/fake/');
378
+ const api = new Api({ host: '/fake/' });
392
379
  api.get = jest.fn((): any => ({}));
393
380
  api.getPage('42', '123');
394
381
  expect(api.get).toHaveBeenCalledWith('/workspaces/42/pages/123');
395
382
  });
396
383
 
397
384
  it('should get page by slug', () => {
398
- const api = new Api('/fake/');
385
+ const api = new Api({ host: '/fake/' });
399
386
  api.get = jest.fn((): any => ({}));
400
387
  api.getPageBySlug('42', '123');
401
388
  expect(api.get).toHaveBeenCalledWith('/pages/42/123');
402
389
  });
403
390
 
404
391
  it('should create page', () => {
405
- const api = new Api('/fake/');
392
+ const api = new Api({ host: '/fake/' });
406
393
  api.post = jest.fn((): any => ({}));
407
394
  api.createPage('42', {} as Prismeai.Page);
408
395
  expect(api.post).toHaveBeenCalledWith(`/workspaces/42/pages`, {});
409
396
  });
410
397
 
411
398
  it('should update page', async () => {
412
- const api = new Api('/fake/');
399
+ const api = new Api({ host: '/fake/' });
413
400
  api.patch = jest.fn((): any => ({ slug: 'my-page' }));
414
401
  await api.updatePage('42', { id: '123', slug: 'my-page' } as Prismeai.Page);
415
402
  expect(api.patch).toHaveBeenCalledWith(`/workspaces/42/pages/my-page`, {
@@ -419,14 +406,14 @@ it('should update page', async () => {
419
406
  });
420
407
 
421
408
  it('should delete page', () => {
422
- const api = new Api('/fake/');
409
+ const api = new Api({ host: '/fake/' });
423
410
  api.delete = jest.fn((): any => ({ id: '123' }));
424
411
  api.deletePage('42', '123');
425
412
  expect(api.delete).toHaveBeenCalledWith('/workspaces/42/pages/123');
426
413
  });
427
414
 
428
415
  it('should stream events', async () => {
429
- const api = new Api('/fake/');
416
+ const api = new Api({ host: '/fake/' });
430
417
 
431
418
  const events = await api.streamEvents('42');
432
419
  expect((events.constructor as any).mockedConstructor).toHaveBeenCalledWith({
@@ -436,6 +423,7 @@ it('should stream events', async () => {
436
423
  apiHost: '/fake/',
437
424
  filters: undefined,
438
425
  api,
426
+ legacyToken: '',
439
427
  });
440
428
  expect(events.once).toHaveBeenCalled();
441
429
  (events.constructor as any).mockedConstructor.mockClear();
@@ -448,6 +436,7 @@ it('should stream events', async () => {
448
436
  apiHost: '/fake/',
449
437
  filters: {},
450
438
  api,
439
+ legacyToken: '',
451
440
  });
452
441
  expect(events2.once).toHaveBeenCalled();
453
442
 
@@ -465,6 +454,7 @@ it('should stream events', async () => {
465
454
  'source.sessionId': 'session id',
466
455
  },
467
456
  api,
457
+ legacyToken: '',
468
458
  });
469
459
  expect(events3.once).toHaveBeenCalled();
470
460
 
@@ -480,12 +470,13 @@ it('should stream events', async () => {
480
470
  apiHost: '/fake/',
481
471
  filters: undefined,
482
472
  api,
473
+ legacyToken: '',
483
474
  });
484
475
  expect(events3.close).toHaveBeenCalled();
485
476
  });
486
477
 
487
478
  it('should get events', async () => {
488
- const api = new Api('/fake/');
479
+ const api = new Api({ host: '/fake/' });
489
480
  api.get = jest.fn((): any => ({
490
481
  result: { events: [{ createdAt: '2022-01-01', id: '123' }] },
491
482
  }));
@@ -504,7 +495,7 @@ it('should get events', async () => {
504
495
  });
505
496
 
506
497
  it('should post events', async () => {
507
- const api = new Api('/fake/');
498
+ const api = new Api({ host: '/fake/' });
508
499
  api.post = jest.fn((): any => {});
509
500
  const res = await api.postEvents('42', [
510
501
  {
@@ -535,7 +526,7 @@ it('should post events', async () => {
535
526
  });
536
527
 
537
528
  describe('permissions', () => {
538
- const api = new Api('/fake/');
529
+ const api = new Api({ host: '/fake/' });
539
530
  api.get = jest.fn((path: string): any => {
540
531
  if (path === '/pages/id/permissions') {
541
532
  return {
@@ -600,7 +591,7 @@ describe('permissions', () => {
600
591
  });
601
592
 
602
593
  it('should get apps', async () => {
603
- const api = new Api('/fake/');
594
+ const api = new Api({ host: '/fake/' });
604
595
  const get: jest.Mock = (api.get = jest.fn((): any => {}));
605
596
  api.getApps({});
606
597
  expect(api.get).toHaveBeenCalledWith('/apps?');
@@ -617,7 +608,7 @@ it('should get apps', async () => {
617
608
  });
618
609
 
619
610
  it('should install app', async () => {
620
- const api = new Api('/fake/');
611
+ const api = new Api({ host: '/fake/' });
621
612
  api.post = jest.fn((): any => {});
622
613
  api.installApp('42', { appSlug: 'app' });
623
614
  expect(api.post).toHaveBeenCalledWith('/workspaces/42/apps', {
@@ -626,7 +617,7 @@ it('should install app', async () => {
626
617
  });
627
618
 
628
619
  it('should update app', async () => {
629
- const api = new Api('/fake/');
620
+ const api = new Api({ host: '/fake/' });
630
621
  api.patch = jest.fn((): any => {});
631
622
  api.updateApp('42', 'app', { appSlug: 'app' });
632
623
  expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app', {
@@ -635,49 +626,49 @@ it('should update app', async () => {
635
626
  });
636
627
 
637
628
  it('should uninstall app', async () => {
638
- const api = new Api('/fake/');
629
+ const api = new Api({ host: '/fake/' });
639
630
  api.delete = jest.fn((): any => {});
640
631
  api.uninstallApp('42', 'app');
641
632
  expect(api.delete).toHaveBeenCalledWith('/workspaces/42/apps/app');
642
633
  });
643
634
 
644
635
  it('should publish app', async () => {
645
- const api = new Api('/fake/');
636
+ const api = new Api({ host: '/fake/' });
646
637
  api.post = jest.fn((): any => {});
647
638
  api.publishApp({ workspaceId: '42' });
648
639
  expect(api.post).toHaveBeenCalledWith('/apps', { workspaceId: '42' });
649
640
  });
650
641
 
651
642
  it('should list app instances', async () => {
652
- const api = new Api('/fake/');
643
+ const api = new Api({ host: '/fake/' });
653
644
  api.get = jest.fn((): any => {});
654
645
  api.listAppInstances('42');
655
646
  expect(api.get).toHaveBeenCalledWith('/workspaces/42/apps');
656
647
  });
657
648
 
658
649
  it('should get app config', async () => {
659
- const api = new Api('/fake/');
650
+ const api = new Api({ host: '/fake/' });
660
651
  api.get = jest.fn((): any => {});
661
652
  api.getAppConfig('42', 'app');
662
653
  expect(api.get).toHaveBeenCalledWith('/workspaces/42/apps/app/config');
663
654
  });
664
655
 
665
656
  it('should update app config', async () => {
666
- const api = new Api('/fake/');
657
+ const api = new Api({ host: '/fake/' });
667
658
  api.patch = jest.fn((): any => {});
668
659
  api.updateAppConfig('42', 'app', {});
669
660
  expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app/config', {});
670
661
  });
671
662
 
672
663
  it('should save app instance', async () => {
673
- const api = new Api('/fake/');
664
+ const api = new Api({ host: '/fake/' });
674
665
  api.patch = jest.fn((): any => {});
675
666
  api.saveAppInstance('42', 'app', {});
676
667
  expect(api.patch).toHaveBeenCalledWith('/workspaces/42/apps/app', {});
677
668
  });
678
669
 
679
670
  it('should upload files', async () => {
680
- const api = new Api('/fake/');
671
+ const api = new Api({ host: '/fake/' });
681
672
  // @ts-ignore
682
673
  let fetch: jest.Mock = (api._fetch = jest.fn((): any => {}));
683
674
  api.uploadFiles('data:text/plain;base64,SGVsbG8gV29ybGQ', '42');
@@ -706,7 +697,7 @@ it('should upload files', async () => {
706
697
  });
707
698
 
708
699
  it('should call automation', () => {
709
- const api = new Api('/fake/');
700
+ const api = new Api({ host: '/fake/' });
710
701
  api.post = jest.fn((): any => {});
711
702
  api.callAutomation('42', 'automation');
712
703
  expect(api.post).toHaveBeenCalledWith(
@@ -716,7 +707,7 @@ it('should call automation', () => {
716
707
  });
717
708
 
718
709
  it('should get workspace usage', () => {
719
- const api = new Api('/fake/');
710
+ const api = new Api({ host: '/fake/' });
720
711
  const get: jest.Mock = (api.get = jest.fn((): any => {}));
721
712
  api.getWorkspaceUsage('42');
722
713
  expect(api.get).toHaveBeenCalledWith('/workspaces/42/usage?');
@@ -755,3 +746,27 @@ describe('Workspaces', () => {
755
746
  });
756
747
  });
757
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
+ });