@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
@@ -1,956 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _tslib = require('../../_virtual/_tslib.js');
6
- var QueryString = require('qs');
7
- var pkceChallenge = require('pkce-challenge');
8
- var fetcher = require('./fetcher.js');
9
- var events = require('./events.js');
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');
14
-
15
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
16
-
17
- var QueryString__default = /*#__PURE__*/_interopDefaultLegacy(QueryString);
18
- var pkceChallenge__default = /*#__PURE__*/_interopDefaultLegacy(pkceChallenge);
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
- }
48
- var Api = /** @class */ (function (_super) {
49
- _tslib.__extends(Api, _super);
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;
56
- }
57
- Object.defineProperty(Api.prototype, "user", {
58
- get: function () {
59
- return this._user;
60
- },
61
- enumerable: false,
62
- configurable: true
63
- });
64
- Api.prototype.me = function () {
65
- return _tslib.__awaiter(this, void 0, void 0, function () {
66
- var me;
67
- return _tslib.__generator(this, function (_a) {
68
- switch (_a.label) {
69
- case 0: return [4 /*yield*/, this.get('/me')];
70
- case 1:
71
- me = _a.sent();
72
- this.sessionId = me.sessionId;
73
- this._user = me;
74
- return [2 /*return*/, me];
75
- }
76
- });
77
- });
78
- };
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; }
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;
124
- return _tslib.__generator(this, function (_a) {
125
- switch (_a.label) {
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',
170
- })];
171
- case 1:
172
- user = _a.sent();
173
- this._user = user;
174
- return [2 /*return*/, user];
175
- }
176
- });
177
- });
178
- };
179
- Api.prototype.signup = function (email, password, firstName, lastName, language) {
180
- return _tslib.__awaiter(this, void 0, void 0, function () {
181
- return _tslib.__generator(this, function (_a) {
182
- switch (_a.label) {
183
- case 0: return [4 /*yield*/, this.post('/signup', {
184
- email: email,
185
- password: password,
186
- firstName: firstName,
187
- lastName: lastName,
188
- language: language,
189
- })];
190
- case 1: return [2 /*return*/, _a.sent()];
191
- }
192
- });
193
- });
194
- };
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) {
206
- return _tslib.__awaiter(this, void 0, void 0, function () {
207
- return _tslib.__generator(this, function (_a) {
208
- switch (_a.label) {
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()];
242
- }
243
- });
244
- });
245
- };
246
- // Workspaces
247
- Api.prototype.getWorkspaces = function () {
248
- return _tslib.__awaiter(this, void 0, void 0, function () {
249
- return _tslib.__generator(this, function (_a) {
250
- switch (_a.label) {
251
- case 0: return [4 /*yield*/, this.get('/workspaces?limit=600')];
252
- case 1: return [2 /*return*/, _a.sent()];
253
- }
254
- });
255
- });
256
- };
257
- Api.prototype.getWorkspace = function (id) {
258
- return _tslib.__awaiter(this, void 0, void 0, function () {
259
- return _tslib.__generator(this, function (_a) {
260
- switch (_a.label) {
261
- case 0: return [4 /*yield*/, this.get("/workspaces/".concat(id))];
262
- case 1: return [2 /*return*/, _a.sent()];
263
- }
264
- });
265
- });
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
- };
297
- Api.prototype.createWorkspace = function (name) {
298
- return _tslib.__awaiter(this, void 0, void 0, function () {
299
- return _tslib.__generator(this, function (_a) {
300
- switch (_a.label) {
301
- case 0: return [4 /*yield*/, this.post('/workspaces', { name: name })];
302
- case 1: return [2 /*return*/, _a.sent()];
303
- }
304
- });
305
- });
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
- };
318
- Api.prototype.updateWorkspace = function (workspace) {
319
- return _tslib.__awaiter(this, void 0, void 0, function () {
320
- var _a, _b;
321
- return _tslib.__generator(this, function (_c) {
322
- switch (_c.label) {
323
- case 0:
324
- if (!workspace.id)
325
- return [2 /*return*/, null];
326
- _a = this.patch;
327
- _b = ["/workspaces/".concat(workspace.id)];
328
- return [4 /*yield*/, this.replaceAllImagesData(workspace, workspace.id)];
329
- case 1: return [4 /*yield*/, _a.apply(this, _b.concat([_c.sent()]))];
330
- case 2: return [2 /*return*/, _c.sent()];
331
- }
332
- });
333
- });
334
- };
335
- Api.prototype.deleteWorkspace = function (workspaceId) {
336
- return _tslib.__awaiter(this, void 0, void 0, function () {
337
- return _tslib.__generator(this, function (_a) {
338
- switch (_a.label) {
339
- case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspaceId))];
340
- case 1: return [2 /*return*/, _a.sent()];
341
- }
342
- });
343
- });
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
- };
398
- // Automations
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) {
410
- return _tslib.__awaiter(this, void 0, void 0, function () {
411
- return _tslib.__generator(this, function (_a) {
412
- switch (_a.label) {
413
- case 0: return [4 /*yield*/, this.post("/workspaces/".concat(workspaceId, "/automations"), _tslib.__assign({}, automation))];
414
- case 1: return [2 /*return*/, _a.sent()];
415
- }
416
- });
417
- });
418
- };
419
- Api.prototype.updateAutomation = function (workspaceId, slug, automation) {
420
- return _tslib.__awaiter(this, void 0, void 0, function () {
421
- var _a, _b;
422
- return _tslib.__generator(this, function (_c) {
423
- switch (_c.label) {
424
- case 0:
425
- _a = this.patch;
426
- _b = ["/workspaces/".concat(workspaceId, "/automations/").concat(slug)];
427
- return [4 /*yield*/, this.replaceAllImagesData(automation, workspaceId)];
428
- case 1: return [4 /*yield*/, _a.apply(this, _b.concat([_c.sent()]))];
429
- case 2: return [2 /*return*/, _c.sent()];
430
- }
431
- });
432
- });
433
- };
434
- Api.prototype.deleteAutomation = function (workspaceId, slug) {
435
- return _tslib.__awaiter(this, void 0, void 0, function () {
436
- return _tslib.__generator(this, function (_a) {
437
- switch (_a.label) {
438
- case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspaceId, "/automations/").concat(slug))];
439
- case 1: return [2 /*return*/, _a.sent()];
440
- }
441
- });
442
- });
443
- };
444
- // Pages
445
- Api.prototype.getPages = function (workspaceId) {
446
- return _tslib.__awaiter(this, void 0, void 0, function () {
447
- var pages;
448
- return _tslib.__generator(this, function (_a) {
449
- switch (_a.label) {
450
- case 0:
451
- _a.trys.push([0, 2, , 3]);
452
- return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/pages"))];
453
- case 1:
454
- pages = _a.sent();
455
- return [2 /*return*/, pages.map(function (_a) {
456
- _a.createdAt; _a.createdBy; _a.updatedAt; _a.updatedBy; var page = _tslib.__rest(_a, ["createdAt", "createdBy", "updatedAt", "updatedBy"]);
457
- return page;
458
- })];
459
- case 2:
460
- _a.sent();
461
- return [2 /*return*/, []];
462
- case 3: return [2 /*return*/];
463
- }
464
- });
465
- });
466
- };
467
- Api.prototype.getPage = function (workspaceId, pageSlug) {
468
- return _tslib.__awaiter(this, void 0, void 0, function () {
469
- return _tslib.__generator(this, function (_a) {
470
- switch (_a.label) {
471
- case 0: return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/pages/").concat(encodeURIComponent(pageSlug)))];
472
- case 1: return [2 /*return*/, _a.sent()];
473
- }
474
- });
475
- });
476
- };
477
- Api.prototype.getPageBySlug = function (workspaceSlug, pageSlug) {
478
- return _tslib.__awaiter(this, void 0, void 0, function () {
479
- return _tslib.__generator(this, function (_a) {
480
- switch (_a.label) {
481
- case 0: return [4 /*yield*/, this.get("/pages/".concat(workspaceSlug, "/").concat(encodeURIComponent(pageSlug)))];
482
- case 1: return [2 /*return*/, _a.sent()];
483
- }
484
- });
485
- });
486
- };
487
- Api.prototype.createPage = function (workspaceId, page) {
488
- return _tslib.__awaiter(this, void 0, void 0, function () {
489
- var _a, newPage;
490
- return _tslib.__generator(this, function (_b) {
491
- switch (_b.label) {
492
- case 0: return [4 /*yield*/, this.post("/workspaces/".concat(workspaceId, "/pages"), page)];
493
- case 1:
494
- _a = _b.sent(), newPage = _tslib.__rest(_a, ["createdAt", "createdBy", "updatedAt", "updatedBy"]);
495
- return [2 /*return*/, newPage];
496
- }
497
- });
498
- });
499
- };
500
- Api.prototype.updatePage = function (workspaceId, page, prevSlug) {
501
- if (prevSlug === void 0) { prevSlug = page.slug || ''; }
502
- return _tslib.__awaiter(this, void 0, void 0, function () {
503
- var _a, updatedPage, _b, _c;
504
- return _tslib.__generator(this, function (_d) {
505
- switch (_d.label) {
506
- case 0:
507
- _b = this.patch;
508
- _c = ["/workspaces/".concat(workspaceId, "/pages/").concat(encodeURIComponent(prevSlug))];
509
- // Replace images as dataurl to uploaded url in any type of data
510
- return [4 /*yield*/, this.replaceAllImagesData(page, workspaceId)];
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()]))];
514
- case 2:
515
- _a = _d.sent(), updatedPage = _tslib.__rest(_a, ["createdAt", "createdBy", "updatedAt", "updatedBy"]);
516
- return [2 /*return*/, updatedPage];
517
- }
518
- });
519
- });
520
- };
521
- Api.prototype.deletePage = function (workspaceId, pageSlug) {
522
- return _tslib.__awaiter(this, void 0, void 0, function () {
523
- return _tslib.__generator(this, function (_a) {
524
- switch (_a.label) {
525
- case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspaceId, "/pages/").concat(encodeURIComponent(pageSlug)))];
526
- case 1: return [2 /*return*/, _a.sent()];
527
- }
528
- });
529
- });
530
- };
531
- // Events
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
- });
550
- return new Promise(function (resolve, reject) {
551
- var off = events$1.once('connect_error', function () {
552
- reject();
553
- events$1.close();
554
- });
555
- events$1.once('connect', function () {
556
- off();
557
- resolve(events$1);
558
- });
559
- });
560
- };
561
- Api.prototype.getEvents = function (workspaceId, options) {
562
- if (options === void 0) { options = {}; }
563
- return _tslib.__awaiter(this, void 0, void 0, function () {
564
- var query, events;
565
- return _tslib.__generator(this, function (_a) {
566
- switch (_a.label) {
567
- case 0:
568
- _a.trys.push([0, 2, , 3]);
569
- query = QueryString__default["default"].stringify(options);
570
- return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/events").concat(query && "?".concat(query)))];
571
- case 1:
572
- events = (_a.sent()).result.events;
573
- return [2 /*return*/, events.map(function (_a) {
574
- var createdAt = _a.createdAt, event = _tslib.__rest(_a, ["createdAt"]);
575
- return (_tslib.__assign(_tslib.__assign({}, event), { createdAt: new Date(createdAt) }));
576
- })];
577
- case 2:
578
- _a.sent();
579
- return [2 /*return*/, []];
580
- case 3: return [2 /*return*/];
581
- }
582
- });
583
- });
584
- };
585
- Api.prototype.postEvents = function (workspaceId, events) {
586
- return _tslib.__awaiter(this, void 0, void 0, function () {
587
- return _tslib.__generator(this, function (_a) {
588
- switch (_a.label) {
589
- case 0:
590
- _a.trys.push([0, 2, , 3]);
591
- return [4 /*yield*/, this.post("/workspaces/".concat(workspaceId, "/events"), {
592
- events: events,
593
- })];
594
- case 1:
595
- _a.sent();
596
- return [2 /*return*/, true];
597
- case 2:
598
- _a.sent();
599
- return [2 /*return*/, false];
600
- case 3: return [2 /*return*/];
601
- }
602
- });
603
- });
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
- };
615
- Api.prototype.getPermissions = function (subjectType, subjectId) {
616
- return _tslib.__awaiter(this, void 0, void 0, function () {
617
- var permissions, contacts;
618
- return _tslib.__generator(this, function (_a) {
619
- switch (_a.label) {
620
- case 0: return [4 /*yield*/, this.get("/".concat(subjectType, "/").concat(subjectId, "/permissions"))];
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
- }];
639
- }
640
- });
641
- });
642
- };
643
- Api.prototype.addPermissions = function (subjectType, subjectId, permissions) {
644
- return _tslib.__awaiter(this, void 0, void 0, function () {
645
- var body, email, contacts, result;
646
- return _tslib.__generator(this, function (_a) {
647
- switch (_a.label) {
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 }) })];
670
- }
671
- });
672
- });
673
- };
674
- Api.prototype.deletePermissions = function (subjectType, subjectId, id) {
675
- return _tslib.__awaiter(this, void 0, void 0, function () {
676
- return _tslib.__generator(this, function (_a) {
677
- switch (_a.label) {
678
- case 0: return [4 /*yield*/, this.delete("/".concat(subjectType, "/").concat(subjectId, "/permissions/").concat(id))];
679
- case 1: return [2 /*return*/, _a.sent()];
680
- }
681
- });
682
- });
683
- };
684
- Api.prototype.getApps = function (_a) {
685
- var query = _a.query, page = _a.page, limit = _a.limit, workspaceId = _a.workspaceId;
686
- return _tslib.__awaiter(this, void 0, void 0, function () {
687
- var params;
688
- return _tslib.__generator(this, function (_b) {
689
- switch (_b.label) {
690
- case 0:
691
- params = new URLSearchParams(utils.removedUndefinedProperties({
692
- text: "".concat(encodeURIComponent(query || '')),
693
- page: "".concat(page || ''),
694
- limit: "".concat(limit || ''),
695
- workspaceId: "".concat(workspaceId || ''),
696
- }, true));
697
- return [4 /*yield*/, this.get("/apps?".concat(params.toString()))];
698
- case 1: return [2 /*return*/, _b.sent()];
699
- }
700
- });
701
- });
702
- };
703
- Api.prototype.installApp = function (workspaceId, body) {
704
- return _tslib.__awaiter(this, void 0, void 0, function () {
705
- return _tslib.__generator(this, function (_a) {
706
- switch (_a.label) {
707
- case 0: return [4 /*yield*/, this.post("/workspaces/".concat(workspaceId, "/apps"), body)];
708
- case 1: return [2 /*return*/, _a.sent()];
709
- }
710
- });
711
- });
712
- };
713
- Api.prototype.updateApp = function (workspaceId, slug, body) {
714
- return _tslib.__awaiter(this, void 0, void 0, function () {
715
- return _tslib.__generator(this, function (_a) {
716
- switch (_a.label) {
717
- case 0: return [4 /*yield*/, this.patch("/workspaces/".concat(workspaceId, "/apps/").concat(slug), body)];
718
- case 1: return [2 /*return*/, _a.sent()];
719
- }
720
- });
721
- });
722
- };
723
- Api.prototype.uninstallApp = function (workspaceId, slug) {
724
- return _tslib.__awaiter(this, void 0, void 0, function () {
725
- return _tslib.__generator(this, function (_a) {
726
- switch (_a.label) {
727
- case 0: return [4 /*yield*/, this.delete("/workspaces/".concat(workspaceId, "/apps/").concat(slug))];
728
- case 1: return [2 /*return*/, _a.sent()];
729
- }
730
- });
731
- });
732
- };
733
- Api.prototype.publishApp = function (body) {
734
- return _tslib.__awaiter(this, void 0, void 0, function () {
735
- return _tslib.__generator(this, function (_a) {
736
- switch (_a.label) {
737
- case 0: return [4 /*yield*/, this.post("/apps", body)];
738
- case 1: return [2 /*return*/, _a.sent()];
739
- }
740
- });
741
- });
742
- };
743
- Api.prototype.listAppInstances = function (workspaceId) {
744
- return _tslib.__awaiter(this, void 0, void 0, function () {
745
- return _tslib.__generator(this, function (_a) {
746
- switch (_a.label) {
747
- case 0: return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/apps"))];
748
- case 1: return [2 /*return*/, _a.sent()];
749
- }
750
- });
751
- });
752
- };
753
- Api.prototype.getAppConfig = function (workspaceId, slug) {
754
- return _tslib.__awaiter(this, void 0, void 0, function () {
755
- var config;
756
- return _tslib.__generator(this, function (_a) {
757
- switch (_a.label) {
758
- case 0: return [4 /*yield*/, this.get("/workspaces/".concat(workspaceId, "/apps/").concat(slug, "/config"))];
759
- case 1:
760
- config = _a.sent();
761
- return [2 /*return*/, config];
762
- }
763
- });
764
- });
765
- };
766
- Api.prototype.updateAppConfig = function (workspaceId, slug, config) {
767
- return _tslib.__awaiter(this, void 0, void 0, function () {
768
- return _tslib.__generator(this, function (_a) {
769
- switch (_a.label) {
770
- case 0: return [4 /*yield*/, this.patch("/workspaces/".concat(workspaceId, "/apps/").concat(slug, "/config"), _tslib.__assign({}, config))];
771
- case 1:
772
- _a.sent();
773
- return [2 /*return*/, config];
774
- }
775
- });
776
- });
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
- };
798
- Api.prototype.uploadFiles = function (files, workspaceId) {
799
- return _tslib.__awaiter(this, void 0, void 0, function () {
800
- var formData;
801
- return _tslib.__generator(this, function (_a) {
802
- switch (_a.label) {
803
- case 0:
804
- formData = new FormData();
805
- (Array.isArray(files) ? files : [files]).forEach(function (file) {
806
- try {
807
- formData.append.apply(formData, _tslib.__spreadArray(['file'], dataURItoBlob(file), false));
808
- }
809
- catch (_a) { }
810
- });
811
- _a.label = 1;
812
- case 1:
813
- _a.trys.push([1, 3, , 4]);
814
- return [4 /*yield*/, this._fetch("/workspaces/".concat(workspaceId, "/files"), {
815
- method: 'POST',
816
- body: formData,
817
- })];
818
- case 2: return [2 /*return*/, _a.sent()];
819
- case 3:
820
- _a.sent();
821
- return [3 /*break*/, 4];
822
- case 4: return [2 /*return*/, []];
823
- }
824
- });
825
- });
826
- };
827
- Api.prototype.replaceAllImagesData = function (original, workspaceId) {
828
- return _tslib.__awaiter(this, void 0, void 0, function () {
829
- var key, toUpload, searchImages, searching, uploaded, replaced;
830
- return _tslib.__generator(this, function (_a) {
831
- switch (_a.label) {
832
- case 0:
833
- key = '…uploading-';
834
- toUpload = [];
835
- searchImages = function (mayHaveImage, uploaded) {
836
- switch (typeof mayHaveImage) {
837
- case 'string':
838
- if (uploaded && mayHaveImage.match(key)) {
839
- // Replace with url
840
- var _a = mayHaveImage.split(key), index = _a[1];
841
- return uploaded[+index];
842
- }
843
- if (mayHaveImage.match(/^data:/)) {
844
- toUpload.push(mayHaveImage);
845
- return "".concat(key).concat(toUpload.length - 1);
846
- }
847
- return mayHaveImage;
848
- case 'object':
849
- var isArray = Array.isArray(mayHaveImage);
850
- var withImagesUrl = isArray
851
- ? _tslib.__spreadArray([], mayHaveImage) : _tslib.__assign({}, mayHaveImage);
852
- for (var _i = 0, _b = Object.keys(withImagesUrl); _i < _b.length; _i++) {
853
- var key_1 = _b[_i];
854
- withImagesUrl[key_1] = searchImages(withImagesUrl[key_1], uploaded);
855
- }
856
- return withImagesUrl;
857
- default:
858
- return mayHaveImage;
859
- }
860
- };
861
- searching = searchImages(original);
862
- if (toUpload.length === 0)
863
- return [2 /*return*/, original];
864
- return [4 /*yield*/, this.uploadFiles(toUpload, workspaceId)];
865
- case 1:
866
- uploaded = _a.sent();
867
- replaced = searchImages(searching, uploaded.map(function (_a) {
868
- var url = _a.url;
869
- return url;
870
- }));
871
- return [2 /*return*/, replaced];
872
- }
873
- });
874
- });
875
- };
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) {
908
- return _tslib.__awaiter(this, void 0, void 0, function () {
909
- var _this = this;
910
- return _tslib.__generator(this, function (_a) {
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
- })];
937
- });
938
- });
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
- };
951
- return Api;
952
- }(fetcher.Fetcher));
953
- var api = new Api({ host: 'https://api.eda.prisme.ai' });
954
-
955
- exports.Api = Api;
956
- exports["default"] = api;