@prisme.ai/sdk 0.0.2 → 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.
@@ -4,43 +4,179 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var _tslib = require('../../_virtual/_tslib.js');
6
6
  var QueryString = require('qs');
7
+ var pkceChallenge = require('pkce-challenge');
7
8
  var fetcher = require('./fetcher.js');
8
9
  var events = require('./events.js');
9
10
  var utils = require('./utils.js');
11
+ var workspaces = require('./endpoints/workspaces.js');
12
+ var ApiError = require('./ApiError.js');
13
+ var users = require('./endpoints/users.js');
10
14
 
11
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
12
16
 
13
17
  var QueryString__default = /*#__PURE__*/_interopDefaultLegacy(QueryString);
18
+ var pkceChallenge__default = /*#__PURE__*/_interopDefaultLegacy(pkceChallenge);
14
19
 
20
+ function dataURItoBlob(dataURI) {
21
+ // convert base64/URLEncoded data component to raw binary data held in a string
22
+ var byteString;
23
+ if (dataURI.split(',')[0].indexOf('base64') >= 0)
24
+ byteString = atob(dataURI.split(',')[1]);
25
+ else
26
+ byteString = unescape(dataURI.split(',')[1]);
27
+ // separate out the mime component
28
+ var metadata = dataURI
29
+ .split(';')
30
+ .filter(function (v, k, all) { return k < all.length - 1; })
31
+ .map(function (v) { return v.split(/:/); });
32
+ var _a = metadata.find(function (_a) {
33
+ var k = _a[0]; _a[1];
34
+ return k === 'data';
35
+ }) || [], _b = _a[1], mimeString = _b === void 0 ? '' : _b;
36
+ var _c = mimeString.split(/\//), ext = _c[1];
37
+ var _d = metadata.find(function (_a) {
38
+ var k = _a[0]; _a[1];
39
+ return k === 'filename';
40
+ }) || [], _e = _d[1], fileName = _e === void 0 ? "file.".concat(ext) : _e;
41
+ // write the bytes of the string to a typed array
42
+ var ia = new Uint8Array(byteString.length);
43
+ for (var i = 0; i < byteString.length; i++) {
44
+ ia[i] = byteString.charCodeAt(i);
45
+ }
46
+ return [new Blob([ia], { type: mimeString }), fileName];
47
+ }
15
48
  var Api = /** @class */ (function (_super) {
16
49
  _tslib.__extends(Api, _super);
17
- function Api() {
18
- return _super !== null && _super.apply(this, arguments) || this;
50
+ function Api(opts) {
51
+ var _this = this;
52
+ var _a;
53
+ _this = _super.call(this, opts.host, (_a = opts === null || opts === void 0 ? void 0 : opts.oidc) === null || _a === void 0 ? void 0 : _a.clientIdHeader) || this;
54
+ _this.opts = _tslib.__assign(_tslib.__assign({}, opts), { oidc: _tslib.__assign({ url: 'http://studio.local.prisme.ai:3001', clientId: 'local-client-id', redirectUri: 'http://studio.local.prisme.ai:3000/signin' }, opts.oidc) });
55
+ return _this;
19
56
  }
57
+ Object.defineProperty(Api.prototype, "user", {
58
+ get: function () {
59
+ return this._user;
60
+ },
61
+ enumerable: false,
62
+ configurable: true
63
+ });
20
64
  Api.prototype.me = function () {
21
65
  return _tslib.__awaiter(this, void 0, void 0, function () {
66
+ var me;
22
67
  return _tslib.__generator(this, function (_a) {
23
68
  switch (_a.label) {
24
69
  case 0: return [4 /*yield*/, this.get('/me')];
25
- case 1: return [2 /*return*/, _a.sent()];
70
+ case 1:
71
+ me = _a.sent();
72
+ this.sessionId = me.sessionId;
73
+ this._user = me;
74
+ return [2 /*return*/, me];
26
75
  }
27
76
  });
28
77
  });
29
78
  };
30
- Api.prototype.signin = function (email, password) {
79
+ Api.prototype.clientId = function () {
80
+ var _a, _b;
81
+ return this.overwriteClientId || ((_b = (_a = this.opts) === null || _a === void 0 ? void 0 : _a.oidc) === null || _b === void 0 ? void 0 : _b.clientId);
82
+ };
83
+ Api.prototype.getAuthorizationURL = function (overrideRedirectUri, authParams, locale) {
84
+ var _a, _b;
85
+ if (locale === void 0) { locale = ((_a = window === null || window === void 0 ? void 0 : window.navigator) === null || _a === void 0 ? void 0 : _a.language)
86
+ ? window.navigator.language.substring(0, 2)
87
+ : undefined; }
31
88
  return _tslib.__awaiter(this, void 0, void 0, function () {
89
+ var url, clientId, _c, codeVerifier, codeChallenge;
90
+ return _tslib.__generator(this, function (_d) {
91
+ switch (_d.label) {
92
+ case 0:
93
+ url = new URL('/oidc/auth', this.opts.oidc.url);
94
+ url.searchParams.set('response_type', 'code');
95
+ url.searchParams.set('response_mode', 'query'); // Send the final authorization code as a query param to the redirect uri
96
+ url.searchParams.set('redirect_uri', overrideRedirectUri || ((_b = this.opts.oidc) === null || _b === void 0 ? void 0 : _b.redirectUri) || '');
97
+ url.searchParams.set('scope', 'openid profile email settings offline_access events:write events:read webhooks pages:read files:write files:read');
98
+ clientId = this.clientId();
99
+ url.searchParams.set('client_id', clientId);
100
+ url.searchParams.set('code_challenge_method', 'S256');
101
+ return [4 /*yield*/, pkceChallenge__default["default"](64)];
102
+ case 1:
103
+ _c = _d.sent(), codeVerifier = _c.code_verifier, codeChallenge = _c.code_challenge;
104
+ url.searchParams.set('code_challenge', codeChallenge);
105
+ if (locale) {
106
+ url.searchParams.set('locale', locale);
107
+ }
108
+ Object.entries(authParams || {}).forEach(function (_a) {
109
+ var k = _a[0], v = _a[1];
110
+ url.searchParams.set(k, v);
111
+ });
112
+ return [2 /*return*/, {
113
+ url: url.toString(),
114
+ codeVerifier: codeVerifier,
115
+ clientId: clientId,
116
+ }];
117
+ }
118
+ });
119
+ });
120
+ };
121
+ Api.prototype.signin = function (body) {
122
+ return _tslib.__awaiter(this, void 0, void 0, function () {
123
+ var url, redirectTo;
32
124
  return _tslib.__generator(this, function (_a) {
33
125
  switch (_a.label) {
34
- case 0: return [4 /*yield*/, this.post('/login', {
35
- email: email,
36
- password: password,
126
+ case 0:
127
+ url = new URL("/oidc/interaction/".concat(body.interaction, "/login"), this.opts.oidc.url);
128
+ // Do not follow redirects as we need to get redirected from browser itself to save final token in local storage
129
+ return [4 /*yield*/, this.post(url.toString(), new URLSearchParams(body), {
130
+ redirect: 'manual',
131
+ })];
132
+ case 1:
133
+ // Do not follow redirects as we need to get redirected from browser itself to save final token in local storage
134
+ _a.sent();
135
+ redirectTo = new URL("/oidc/auth/".concat(body.interaction), this.opts.oidc.url);
136
+ return [2 /*return*/, { redirectTo: redirectTo.toString() }];
137
+ }
138
+ });
139
+ });
140
+ };
141
+ Api.prototype.getToken = function (authorizationCode, codeVerifier, overrideRedirectUri) {
142
+ return _tslib.__awaiter(this, void 0, void 0, function () {
143
+ var url, token;
144
+ return _tslib.__generator(this, function (_a) {
145
+ switch (_a.label) {
146
+ case 0:
147
+ this.token = null;
148
+ url = new URL('/oidc/token', this.opts.oidc.url);
149
+ return [4 /*yield*/, this.post(url.toString(), new URLSearchParams({
150
+ grant_type: 'authorization_code',
151
+ code: authorizationCode,
152
+ code_verifier: codeVerifier,
153
+ client_id: this.clientId(),
154
+ redirect_uri: overrideRedirectUri || this.opts.oidc.redirectUri,
155
+ }))];
156
+ case 1:
157
+ token = _a.sent();
158
+ return [2 /*return*/, token];
159
+ }
160
+ });
161
+ });
162
+ };
163
+ Api.prototype.createAnonymousSession = function () {
164
+ return _tslib.__awaiter(this, void 0, void 0, function () {
165
+ var user;
166
+ return _tslib.__generator(this, function (_a) {
167
+ switch (_a.label) {
168
+ case 0: return [4 /*yield*/, this.post('/login/anonymous', {}, {
169
+ credentials: 'omit',
37
170
  })];
38
- case 1: return [2 /*return*/, _a.sent()];
171
+ case 1:
172
+ user = _a.sent();
173
+ this._user = user;
174
+ return [2 /*return*/, user];
39
175
  }
40
176
  });
41
177
  });
42
178
  };
43
- Api.prototype.signup = function (email, password, firstName, lastName) {
179
+ Api.prototype.signup = function (email, password, firstName, lastName, language) {
44
180
  return _tslib.__awaiter(this, void 0, void 0, function () {
45
181
  return _tslib.__generator(this, function (_a) {
46
182
  switch (_a.label) {
@@ -49,21 +185,60 @@ var Api = /** @class */ (function (_super) {
49
185
  password: password,
50
186
  firstName: firstName,
51
187
  lastName: lastName,
188
+ language: language,
52
189
  })];
53
190
  case 1: return [2 /*return*/, _a.sent()];
54
191
  }
55
192
  });
56
193
  });
57
194
  };
58
- Api.prototype.signout = function () {
195
+ Api.prototype.getSignoutURL = function (redirectUri) {
196
+ var params = new URLSearchParams();
197
+ params.set('client_id', this.clientId());
198
+ if (redirectUri) {
199
+ params.set('post_logout_redirect_uri', redirectUri);
200
+ }
201
+ var url = new URL("/oidc/session/end?".concat(params.toString()), this.opts.oidc.url);
202
+ return url.toString();
203
+ };
204
+ // Mail validation
205
+ Api.prototype.sendValidationMail = function (email, language) {
59
206
  return _tslib.__awaiter(this, void 0, void 0, function () {
60
207
  return _tslib.__generator(this, function (_a) {
61
208
  switch (_a.label) {
62
- case 0: return [4 /*yield*/, this.post('/logout')];
63
- case 1:
64
- _a.sent();
65
- this.token = null;
66
- return [2 /*return*/];
209
+ case 0: return [4 /*yield*/, this.post('/user/validate', { email: email, language: language })];
210
+ case 1: return [2 /*return*/, _a.sent()];
211
+ }
212
+ });
213
+ });
214
+ };
215
+ Api.prototype.validateMail = function (token) {
216
+ return _tslib.__awaiter(this, void 0, void 0, function () {
217
+ return _tslib.__generator(this, function (_a) {
218
+ switch (_a.label) {
219
+ case 0: return [4 /*yield*/, this.post('/user/validate', { token: token })];
220
+ case 1: return [2 /*return*/, _a.sent()];
221
+ }
222
+ });
223
+ });
224
+ };
225
+ // Password reset
226
+ Api.prototype.sendPasswordResetMail = function (email, language) {
227
+ return _tslib.__awaiter(this, void 0, void 0, function () {
228
+ return _tslib.__generator(this, function (_a) {
229
+ switch (_a.label) {
230
+ case 0: return [4 /*yield*/, this.post('/user/password', { email: email, language: language })];
231
+ case 1: return [2 /*return*/, _a.sent()];
232
+ }
233
+ });
234
+ });
235
+ };
236
+ Api.prototype.passwordReset = function (token, password) {
237
+ return _tslib.__awaiter(this, void 0, void 0, function () {
238
+ return _tslib.__generator(this, function (_a) {
239
+ switch (_a.label) {
240
+ case 0: return [4 /*yield*/, this.post('/user/password', { token: token, password: password })];
241
+ case 1: return [2 /*return*/, _a.sent()];
67
242
  }
68
243
  });
69
244
  });
@@ -73,7 +248,7 @@ var Api = /** @class */ (function (_super) {
73
248
  return _tslib.__awaiter(this, void 0, void 0, function () {
74
249
  return _tslib.__generator(this, function (_a) {
75
250
  switch (_a.label) {
76
- case 0: return [4 /*yield*/, this.get('/workspaces?limit=300')];
251
+ case 0: return [4 /*yield*/, this.get('/workspaces?limit=600')];
77
252
  case 1: return [2 /*return*/, _a.sent()];
78
253
  }
79
254
  });
@@ -89,6 +264,36 @@ var Api = /** @class */ (function (_super) {
89
264
  });
90
265
  });
91
266
  };
267
+ Api.prototype.getWorkspaceSecurity = function (id) {
268
+ return _tslib.__awaiter(this, void 0, void 0, function () {
269
+ return _tslib.__generator(this, function (_a) {
270
+ switch (_a.label) {
271
+ case 0: return [4 /*yield*/, this.get("/workspaces/".concat(id, "/security"))];
272
+ case 1: return [2 /*return*/, _a.sent()];
273
+ }
274
+ });
275
+ });
276
+ };
277
+ Api.prototype.updateWorkspaceSecurity = function (workspaceId, security) {
278
+ return _tslib.__awaiter(this, void 0, void 0, function () {
279
+ return _tslib.__generator(this, function (_a) {
280
+ switch (_a.label) {
281
+ case 0: return [4 /*yield*/, this.put("/workspaces/".concat(workspaceId, "/security"), security)];
282
+ case 1: return [2 /*return*/, _a.sent()];
283
+ }
284
+ });
285
+ });
286
+ };
287
+ Api.prototype.getWorkspaceRoles = function (id) {
288
+ return _tslib.__awaiter(this, void 0, void 0, function () {
289
+ return _tslib.__generator(this, function (_a) {
290
+ switch (_a.label) {
291
+ case 0: return [4 /*yield*/, this.get("/workspaces/".concat(id, "/security/roles"))];
292
+ case 1: return [2 /*return*/, _a.sent()];
293
+ }
294
+ });
295
+ });
296
+ };
92
297
  Api.prototype.createWorkspace = function (name) {
93
298
  return _tslib.__awaiter(this, void 0, void 0, function () {
94
299
  return _tslib.__generator(this, function (_a) {
@@ -99,12 +304,25 @@ var Api = /** @class */ (function (_super) {
99
304
  });
100
305
  });
101
306
  };
307
+ Api.prototype.duplicateWorkspace = function (_a) {
308
+ var id = _a.id;
309
+ return _tslib.__awaiter(this, void 0, void 0, function () {
310
+ return _tslib.__generator(this, function (_b) {
311
+ switch (_b.label) {
312
+ case 0: return [4 /*yield*/, this.post("/workspaces/".concat(id, "/versions/current/duplicate"), {})];
313
+ case 1: return [2 /*return*/, _b.sent()];
314
+ }
315
+ });
316
+ });
317
+ };
102
318
  Api.prototype.updateWorkspace = function (workspace) {
103
319
  return _tslib.__awaiter(this, void 0, void 0, function () {
104
320
  var _a, _b;
105
321
  return _tslib.__generator(this, function (_c) {
106
322
  switch (_c.label) {
107
323
  case 0:
324
+ if (!workspace.id)
325
+ return [2 /*return*/, null];
108
326
  _a = this.patch;
109
327
  _b = ["/workspaces/".concat(workspace.id)];
110
328
  return [4 /*yield*/, this.replaceAllImagesData(workspace, workspace.id)];
@@ -124,37 +342,100 @@ var Api = /** @class */ (function (_super) {
124
342
  });
125
343
  });
126
344
  };
345
+ Api.prototype.generateApiKey = function (workspaceId, events, uploads) {
346
+ return _tslib.__awaiter(this, void 0, void 0, function () {
347
+ var apiKey;
348
+ return _tslib.__generator(this, function (_a) {
349
+ switch (_a.label) {
350
+ case 0: return [4 /*yield*/, this.post("/workspaces/".concat(workspaceId, "/apiKeys"), {
351
+ rules: {
352
+ events: {
353
+ types: events,
354
+ filters: {
355
+ 'source.sessionId': '${user.sessionId}',
356
+ },
357
+ },
358
+ uploads: uploads
359
+ ? {
360
+ mimetypes: uploads,
361
+ }
362
+ : undefined,
363
+ },
364
+ })];
365
+ case 1:
366
+ apiKey = (_a.sent()).apiKey;
367
+ return [2 /*return*/, apiKey];
368
+ }
369
+ });
370
+ });
371
+ };
372
+ Api.prototype.updateApiKey = function (workspaceId, apiKey, events, uploads) {
373
+ return _tslib.__awaiter(this, void 0, void 0, function () {
374
+ return _tslib.__generator(this, function (_a) {
375
+ switch (_a.label) {
376
+ case 0: return [4 /*yield*/, this.put("/workspaces/".concat(workspaceId, "/apiKeys/").concat(apiKey), {
377
+ rules: {
378
+ events: {
379
+ types: events,
380
+ filters: {
381
+ 'source.sessionId': '${user.sessionId}',
382
+ },
383
+ },
384
+ uploads: uploads
385
+ ? {
386
+ mimetypes: uploads,
387
+ }
388
+ : undefined,
389
+ },
390
+ })];
391
+ case 1:
392
+ _a.sent();
393
+ return [2 /*return*/, apiKey];
394
+ }
395
+ });
396
+ });
397
+ };
127
398
  // Automations
128
- Api.prototype.createAutomation = function (workspace, automation) {
399
+ Api.prototype.getAutomation = function (workspaceId, automationSlug) {
400
+ return _tslib.__awaiter(this, void 0, void 0, function () {
401
+ return _tslib.__generator(this, function (_a) {
402
+ switch (_a.label) {
403
+ case 0: return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/automations/").concat(automationSlug))];
404
+ case 1: return [2 /*return*/, _a.sent()];
405
+ }
406
+ });
407
+ });
408
+ };
409
+ Api.prototype.createAutomation = function (workspaceId, automation) {
129
410
  return _tslib.__awaiter(this, void 0, void 0, function () {
130
411
  return _tslib.__generator(this, function (_a) {
131
412
  switch (_a.label) {
132
- case 0: return [4 /*yield*/, this.post("/workspaces/".concat(workspace.id, "/automations"), _tslib.__assign({}, automation))];
413
+ case 0: return [4 /*yield*/, this.post("/workspaces/".concat(workspaceId, "/automations"), _tslib.__assign({}, automation))];
133
414
  case 1: return [2 /*return*/, _a.sent()];
134
415
  }
135
416
  });
136
417
  });
137
418
  };
138
- Api.prototype.updateAutomation = function (workspace, slug, automation) {
419
+ Api.prototype.updateAutomation = function (workspaceId, slug, automation) {
139
420
  return _tslib.__awaiter(this, void 0, void 0, function () {
140
421
  var _a, _b;
141
422
  return _tslib.__generator(this, function (_c) {
142
423
  switch (_c.label) {
143
424
  case 0:
144
425
  _a = this.patch;
145
- _b = ["/workspaces/".concat(workspace.id, "/automations/").concat(slug)];
146
- return [4 /*yield*/, this.replaceAllImagesData(automation, workspace.id)];
426
+ _b = ["/workspaces/".concat(workspaceId, "/automations/").concat(slug)];
427
+ return [4 /*yield*/, this.replaceAllImagesData(automation, workspaceId)];
147
428
  case 1: return [4 /*yield*/, _a.apply(this, _b.concat([_c.sent()]))];
148
429
  case 2: return [2 /*return*/, _c.sent()];
149
430
  }
150
431
  });
151
432
  });
152
433
  };
153
- Api.prototype.deleteAutomation = function (workspace, slug) {
434
+ Api.prototype.deleteAutomation = function (workspaceId, slug) {
154
435
  return _tslib.__awaiter(this, void 0, void 0, function () {
155
436
  return _tslib.__generator(this, function (_a) {
156
437
  switch (_a.label) {
157
- case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspace.id, "/automations/").concat(slug))];
438
+ case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspaceId, "/automations/").concat(slug))];
158
439
  case 1: return [2 /*return*/, _a.sent()];
159
440
  }
160
441
  });
@@ -183,21 +464,21 @@ var Api = /** @class */ (function (_super) {
183
464
  });
184
465
  });
185
466
  };
186
- Api.prototype.getPage = function (workspaceId, pageId) {
467
+ Api.prototype.getPage = function (workspaceId, pageSlug) {
187
468
  return _tslib.__awaiter(this, void 0, void 0, function () {
188
469
  return _tslib.__generator(this, function (_a) {
189
470
  switch (_a.label) {
190
- case 0: return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/pages/").concat(pageId))];
471
+ case 0: return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/pages/").concat(encodeURIComponent(pageSlug)))];
191
472
  case 1: return [2 /*return*/, _a.sent()];
192
473
  }
193
474
  });
194
475
  });
195
476
  };
196
- Api.prototype.getPageBySlug = function (pageSlug) {
477
+ Api.prototype.getPageBySlug = function (workspaceSlug, pageSlug) {
197
478
  return _tslib.__awaiter(this, void 0, void 0, function () {
198
479
  return _tslib.__generator(this, function (_a) {
199
480
  switch (_a.label) {
200
- case 0: return [4 /*yield*/, this.get("/pages/".concat(pageSlug))];
481
+ case 0: return [4 /*yield*/, this.get("/pages/".concat(workspaceSlug, "/").concat(encodeURIComponent(pageSlug)))];
201
482
  case 1: return [2 /*return*/, _a.sent()];
202
483
  }
203
484
  });
@@ -216,17 +497,20 @@ var Api = /** @class */ (function (_super) {
216
497
  });
217
498
  });
218
499
  };
219
- // Replace images as dataurl to uploaded url in any type of data
220
- Api.prototype.updatePage = function (workspaceId, page) {
500
+ Api.prototype.updatePage = function (workspaceId, page, prevSlug) {
501
+ if (prevSlug === void 0) { prevSlug = page.slug || ''; }
221
502
  return _tslib.__awaiter(this, void 0, void 0, function () {
222
503
  var _a, updatedPage, _b, _c;
223
504
  return _tslib.__generator(this, function (_d) {
224
505
  switch (_d.label) {
225
506
  case 0:
226
507
  _b = this.patch;
227
- _c = ["/workspaces/".concat(workspaceId, "/pages/").concat(page.id)];
508
+ _c = ["/workspaces/".concat(workspaceId, "/pages/").concat(encodeURIComponent(prevSlug))];
509
+ // Replace images as dataurl to uploaded url in any type of data
228
510
  return [4 /*yield*/, this.replaceAllImagesData(page, workspaceId)];
229
- case 1: return [4 /*yield*/, _b.apply(this, _c.concat([_d.sent()]))];
511
+ case 1: return [4 /*yield*/, _b.apply(this, _c.concat([
512
+ // Replace images as dataurl to uploaded url in any type of data
513
+ _d.sent()]))];
230
514
  case 2:
231
515
  _a = _d.sent(), updatedPage = _tslib.__rest(_a, ["createdAt", "createdBy", "updatedAt", "updatedBy"]);
232
516
  return [2 /*return*/, updatedPage];
@@ -234,27 +518,44 @@ var Api = /** @class */ (function (_super) {
234
518
  });
235
519
  });
236
520
  };
237
- Api.prototype.deletePage = function (workspaceId, pageId) {
521
+ Api.prototype.deletePage = function (workspaceId, pageSlug) {
238
522
  return _tslib.__awaiter(this, void 0, void 0, function () {
239
523
  return _tslib.__generator(this, function (_a) {
240
524
  switch (_a.label) {
241
- case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspaceId, "/pages/").concat(pageId))];
525
+ case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspaceId, "/pages/").concat(encodeURIComponent(pageSlug)))];
242
526
  case 1: return [2 /*return*/, _a.sent()];
243
527
  }
244
528
  });
245
529
  });
246
530
  };
247
531
  // Events
248
- Api.prototype.streamEvents = function (workspaceId) {
249
- var events$1 = new events.Events(workspaceId, this.token || '', this.host);
532
+ Api.prototype.streamEvents = function (workspaceId, filters) {
533
+ if (filters && filters['source.sessionId'] === true) {
534
+ if (this.sessionId) {
535
+ filters['source.sessionId'] = this.sessionId;
536
+ }
537
+ else {
538
+ delete filters['source.sessionId'];
539
+ }
540
+ }
541
+ var events$1 = new events.Events({
542
+ workspaceId: workspaceId,
543
+ token: this.token || '',
544
+ legacyToken: this.legacyToken || '',
545
+ apiKey: this._apiKey ? this._apiKey : undefined,
546
+ apiHost: this.host,
547
+ filters: filters,
548
+ api: this,
549
+ });
250
550
  return new Promise(function (resolve, reject) {
251
- events$1.once('connect', function () {
252
- resolve(events$1);
253
- });
254
- events$1.once('connect_error', function () {
551
+ var off = events$1.once('connect_error', function () {
255
552
  reject();
256
553
  events$1.close();
257
554
  });
555
+ events$1.once('connect', function () {
556
+ off();
557
+ resolve(events$1);
558
+ });
258
559
  });
259
560
  };
260
561
  Api.prototype.getEvents = function (workspaceId, options) {
@@ -301,31 +602,80 @@ var Api = /** @class */ (function (_super) {
301
602
  });
302
603
  });
303
604
  };
605
+ Api.prototype.findContacts = function (query) {
606
+ return _tslib.__awaiter(this, void 0, void 0, function () {
607
+ return _tslib.__generator(this, function (_a) {
608
+ switch (_a.label) {
609
+ case 0: return [4 /*yield*/, this.post("/contacts", query)];
610
+ case 1: return [2 /*return*/, _a.sent()];
611
+ }
612
+ });
613
+ });
614
+ };
304
615
  Api.prototype.getPermissions = function (subjectType, subjectId) {
305
616
  return _tslib.__awaiter(this, void 0, void 0, function () {
617
+ var permissions, contacts;
306
618
  return _tslib.__generator(this, function (_a) {
307
619
  switch (_a.label) {
308
620
  case 0: return [4 /*yield*/, this.get("/".concat(subjectType, "/").concat(subjectId, "/permissions"))];
309
- case 1: return [2 /*return*/, _a.sent()];
621
+ case 1:
622
+ permissions = _a.sent();
623
+ return [4 /*yield*/, this.findContacts({
624
+ ids: permissions.result
625
+ .filter(function (cur) { return cur.target.id && !cur.target.displayName; })
626
+ .map(function (cur) { return cur.target.id; }),
627
+ })];
628
+ case 2:
629
+ contacts = _a.sent();
630
+ return [2 /*return*/, {
631
+ result: permissions.result.map(function (perm) {
632
+ var user = perm.target.id && !perm.target.displayName
633
+ ? contacts.contacts.find(function (cur) { return cur.id === perm.target.id; })
634
+ : undefined;
635
+ var displayName = perm.target.displayName || "".concat(user === null || user === void 0 ? void 0 : user.firstName, " ").concat(user === null || user === void 0 ? void 0 : user.lastName);
636
+ return _tslib.__assign(_tslib.__assign({}, perm), { target: _tslib.__assign(_tslib.__assign({}, perm.target), { id: perm.target.id, displayName: displayName }) });
637
+ }),
638
+ }];
310
639
  }
311
640
  });
312
641
  });
313
642
  };
314
643
  Api.prototype.addPermissions = function (subjectType, subjectId, permissions) {
315
644
  return _tslib.__awaiter(this, void 0, void 0, function () {
645
+ var body, email, contacts, result;
316
646
  return _tslib.__generator(this, function (_a) {
317
647
  switch (_a.label) {
318
- case 0: return [4 /*yield*/, this.post("/".concat(subjectType, "/").concat(subjectId, "/permissions"), permissions)];
319
- case 1: return [2 /*return*/, _a.sent()];
648
+ case 0:
649
+ body = _tslib.__assign({}, permissions);
650
+ email = permissions.target.email;
651
+ if (!email) return [3 /*break*/, 2];
652
+ return [4 /*yield*/, this.findContacts({
653
+ email: email,
654
+ })];
655
+ case 1:
656
+ contacts = _a.sent();
657
+ if (!contacts.contacts.length) {
658
+ throw new ApiError.ApiError({
659
+ error: 'CollaboratorNotFound',
660
+ message: 'This user does not exist',
661
+ details: { email: email },
662
+ }, 404);
663
+ }
664
+ body.target = { id: contacts.contacts[0].id };
665
+ _a.label = 2;
666
+ case 2: return [4 /*yield*/, this.post("/".concat(subjectType, "/").concat(subjectId, "/permissions"), body)];
667
+ case 3:
668
+ result = _a.sent();
669
+ return [2 /*return*/, _tslib.__assign(_tslib.__assign({}, result), { target: _tslib.__assign(_tslib.__assign({}, result.target), { id: result.target.id, email: email }) })];
320
670
  }
321
671
  });
322
672
  });
323
673
  };
324
- Api.prototype.deletePermissions = function (subjectType, subjectId, userEmail) {
674
+ Api.prototype.deletePermissions = function (subjectType, subjectId, id) {
325
675
  return _tslib.__awaiter(this, void 0, void 0, function () {
326
676
  return _tslib.__generator(this, function (_a) {
327
677
  switch (_a.label) {
328
- case 0: return [4 /*yield*/, this.delete("/".concat(subjectType, "/").concat(subjectId, "/permissions/").concat(userEmail))];
678
+ case 0: return [4 /*yield*/, this.delete("/".concat(subjectType, "/").concat(subjectId, "/permissions/").concat(id))];
329
679
  case 1: return [2 /*return*/, _a.sent()];
330
680
  }
331
681
  });
@@ -339,7 +689,7 @@ var Api = /** @class */ (function (_super) {
339
689
  switch (_b.label) {
340
690
  case 0:
341
691
  params = new URLSearchParams(utils.removedUndefinedProperties({
342
- text: "".concat(query || ''),
692
+ text: "".concat(encodeURIComponent(query || '')),
343
693
  page: "".concat(page || ''),
344
694
  limit: "".concat(limit || ''),
345
695
  workspaceId: "".concat(workspaceId || ''),
@@ -400,16 +750,6 @@ var Api = /** @class */ (function (_super) {
400
750
  });
401
751
  });
402
752
  };
403
- Api.prototype.fetchImports = function (workspaceId) {
404
- return _tslib.__awaiter(this, void 0, void 0, function () {
405
- return _tslib.__generator(this, function (_a) {
406
- switch (_a.label) {
407
- case 0: return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/apps"))];
408
- case 1: return [2 /*return*/, _a.sent()];
409
- }
410
- });
411
- });
412
- };
413
753
  Api.prototype.getAppConfig = function (workspaceId, slug) {
414
754
  return _tslib.__awaiter(this, void 0, void 0, function () {
415
755
  var config;
@@ -435,43 +775,38 @@ var Api = /** @class */ (function (_super) {
435
775
  });
436
776
  });
437
777
  };
778
+ Api.prototype.getAppInstance = function (workspaceId, slug) {
779
+ return _tslib.__awaiter(this, void 0, void 0, function () {
780
+ return _tslib.__generator(this, function (_a) {
781
+ return [2 /*return*/, this.get("/workspaces/".concat(workspaceId, "/apps/").concat(slug))];
782
+ });
783
+ });
784
+ };
785
+ Api.prototype.saveAppInstance = function (workspaceId, slug, appInstance) {
786
+ return _tslib.__awaiter(this, void 0, void 0, function () {
787
+ var response;
788
+ return _tslib.__generator(this, function (_a) {
789
+ switch (_a.label) {
790
+ case 0: return [4 /*yield*/, this.patch("/workspaces/".concat(workspaceId, "/apps/").concat(slug), _tslib.__assign({}, appInstance))];
791
+ case 1:
792
+ response = _a.sent();
793
+ return [2 /*return*/, response];
794
+ }
795
+ });
796
+ });
797
+ };
438
798
  Api.prototype.uploadFiles = function (files, workspaceId) {
439
799
  return _tslib.__awaiter(this, void 0, void 0, function () {
440
- function dataURItoBlob(dataURI) {
441
- // convert base64/URLEncoded data component to raw binary data held in a string
442
- var byteString;
443
- if (dataURI.split(',')[0].indexOf('base64') >= 0)
444
- byteString = atob(dataURI.split(',')[1]);
445
- else
446
- byteString = unescape(dataURI.split(',')[1]);
447
- // separate out the mime component
448
- var metadata = dataURI
449
- .split(';')
450
- .filter(function (v, k, all) { return k < all.length - 1; })
451
- .map(function (v) { return v.split(/:/); });
452
- var _a = metadata.find(function (_a) {
453
- var k = _a[0]; _a[1];
454
- return k === 'data';
455
- }) || [], _b = _a[1], mimeString = _b === void 0 ? '' : _b;
456
- var _c = mimeString.split(/\//), ext = _c[1];
457
- var _d = metadata.find(function (_a) {
458
- var k = _a[0]; _a[1];
459
- return k === 'filename';
460
- }) || [], _e = _d[1], fileName = _e === void 0 ? "file.".concat(ext) : _e;
461
- // write the bytes of the string to a typed array
462
- var ia = new Uint8Array(byteString.length);
463
- for (var i = 0; i < byteString.length; i++) {
464
- ia[i] = byteString.charCodeAt(i);
465
- }
466
- return [new Blob([ia], { type: mimeString }), fileName];
467
- }
468
800
  var formData;
469
801
  return _tslib.__generator(this, function (_a) {
470
802
  switch (_a.label) {
471
803
  case 0:
472
804
  formData = new FormData();
473
805
  (Array.isArray(files) ? files : [files]).forEach(function (file) {
474
- formData.append.apply(formData, _tslib.__spreadArray(['file'], dataURItoBlob(file)));
806
+ try {
807
+ formData.append.apply(formData, _tslib.__spreadArray(['file'], dataURItoBlob(file), false));
808
+ }
809
+ catch (_a) { }
475
810
  });
476
811
  _a.label = 1;
477
812
  case 1:
@@ -538,16 +873,84 @@ var Api = /** @class */ (function (_super) {
538
873
  });
539
874
  });
540
875
  };
541
- Api.prototype.callAutomation = function (workspaceId, automation) {
876
+ Api.prototype.callAutomation = function (workspaceId, automation, params) {
877
+ return _tslib.__awaiter(this, void 0, void 0, function () {
878
+ return _tslib.__generator(this, function (_a) {
879
+ return [2 /*return*/, this.post("/workspaces/".concat(workspaceId, "/webhooks/").concat(automation), params)];
880
+ });
881
+ });
882
+ };
883
+ Api.prototype.testAutomation = function (_a) {
884
+ var workspaceId = _a.workspaceId, automation = _a.automation, payload = _a.payload;
885
+ return _tslib.__awaiter(this, void 0, void 0, function () {
886
+ return _tslib.__generator(this, function (_b) {
887
+ return [2 /*return*/, this.post("/workspaces/".concat(workspaceId, "/test/").concat(automation), {
888
+ payload: payload,
889
+ })];
890
+ });
891
+ });
892
+ };
893
+ Api.prototype.getWorkspaceUsage = function (workspaceId, _a) {
894
+ var _b = _a === void 0 ? {} : _a, afterDate = _b.afterDate, beforeDate = _b.beforeDate, details = _b.details;
895
+ return _tslib.__awaiter(this, void 0, void 0, function () {
896
+ var params;
897
+ return _tslib.__generator(this, function (_c) {
898
+ params = new URLSearchParams(utils.removedUndefinedProperties({
899
+ afterDate: "".concat(afterDate || ''),
900
+ beforeDate: "".concat(beforeDate || ''),
901
+ details: "".concat(details || ''),
902
+ }, true));
903
+ return [2 /*return*/, this.get("/workspaces/".concat(workspaceId, "/usage?").concat(params.toString()))];
904
+ });
905
+ });
906
+ };
907
+ Api.prototype.importArchive = function (archive) {
542
908
  return _tslib.__awaiter(this, void 0, void 0, function () {
909
+ var _this = this;
543
910
  return _tslib.__generator(this, function (_a) {
544
- return [2 /*return*/, this._fetch("/workspaces/".concat(workspaceId, "/webhooks/").concat(automation))];
911
+ return [2 /*return*/, new Promise(function (resolve) {
912
+ var fileReader = new FileReader();
913
+ fileReader.addEventListener('load', function (_a) {
914
+ var target = _a.target;
915
+ return _tslib.__awaiter(_this, void 0, void 0, function () {
916
+ var file, formData, _b;
917
+ return _tslib.__generator(this, function (_c) {
918
+ switch (_c.label) {
919
+ case 0:
920
+ file = target === null || target === void 0 ? void 0 : target.result;
921
+ formData = new FormData();
922
+ formData.append.apply(formData, _tslib.__spreadArray(['archive'], dataURItoBlob(file)));
923
+ _b = resolve;
924
+ return [4 /*yield*/, this._fetch("/workspaces/import", {
925
+ method: 'POST',
926
+ body: formData,
927
+ })];
928
+ case 1:
929
+ _b.apply(void 0, [_c.sent()]);
930
+ return [2 /*return*/];
931
+ }
932
+ });
933
+ });
934
+ });
935
+ fileReader.readAsDataURL(archive);
936
+ })];
545
937
  });
546
938
  });
547
939
  };
940
+ Api.prototype.users = function (id) {
941
+ var _a;
942
+ if (id === void 0) { id = ((_a = this.user) === null || _a === void 0 ? void 0 : _a.id) || ''; }
943
+ if (!id) {
944
+ throw new Error();
945
+ }
946
+ return new users.UsersEndpoint(id, this);
947
+ };
948
+ Api.prototype.workspaces = function (id) {
949
+ return new workspaces.WorkspacesEndpoint(id, this);
950
+ };
548
951
  return Api;
549
952
  }(fetcher.Fetcher));
550
- var api = new Api('https://api.eda.prisme.ai');
953
+ var api = new Api({ host: 'https://api.eda.prisme.ai' });
551
954
 
552
955
  exports.Api = Api;
553
956
  exports["default"] = api;