@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.
@@ -0,0 +1,43 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('../../../_virtual/_tslib.js');
6
+
7
+ var UsersEndpoint = /** @class */ (function () {
8
+ function UsersEndpoint(id, api) {
9
+ this.id = id;
10
+ this.api = api;
11
+ }
12
+ UsersEndpoint.prototype.setMeta = function (k, v) {
13
+ return _tslib.__awaiter(this, void 0, void 0, function () {
14
+ var _a;
15
+ return _tslib.__generator(this, function (_b) {
16
+ switch (_b.label) {
17
+ case 0: return [4 /*yield*/, this.api.post("/user/meta", (_a = {},
18
+ _a[k] = v,
19
+ _a))];
20
+ case 1:
21
+ _b.sent();
22
+ return [2 /*return*/];
23
+ }
24
+ });
25
+ });
26
+ };
27
+ UsersEndpoint.prototype.deleteMeta = function (k) {
28
+ return _tslib.__awaiter(this, void 0, void 0, function () {
29
+ return _tslib.__generator(this, function (_a) {
30
+ switch (_a.label) {
31
+ case 0: return [4 /*yield*/, this.api.delete("/user/meta/".concat(k))];
32
+ case 1:
33
+ _a.sent();
34
+ return [2 /*return*/];
35
+ }
36
+ });
37
+ });
38
+ };
39
+ return UsersEndpoint;
40
+ }());
41
+
42
+ exports.UsersEndpoint = UsersEndpoint;
43
+ exports["default"] = UsersEndpoint;
@@ -0,0 +1,23 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var workspacesVersions = require('./workspacesVersions.js');
6
+
7
+ var WorkspacesEndpoint = /** @class */ (function () {
8
+ function WorkspacesEndpoint(id, api) {
9
+ this.id = id;
10
+ this.api = api;
11
+ }
12
+ Object.defineProperty(WorkspacesEndpoint.prototype, "versions", {
13
+ get: function () {
14
+ return new workspacesVersions.WorkspacesVersionsEndpoint(this.id, this.api);
15
+ },
16
+ enumerable: false,
17
+ configurable: true
18
+ });
19
+ return WorkspacesEndpoint;
20
+ }());
21
+
22
+ exports.WorkspacesEndpoint = WorkspacesEndpoint;
23
+ exports["default"] = WorkspacesEndpoint;
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('../../../_virtual/_tslib.js');
6
+
7
+ var WorkspacesVersionsEndpoint = /** @class */ (function () {
8
+ function WorkspacesVersionsEndpoint(workspaceId, api) {
9
+ this.workspaceId = workspaceId;
10
+ this.api = api;
11
+ }
12
+ WorkspacesVersionsEndpoint.prototype.create = function (version) {
13
+ this.api.post("/workspaces/".concat(this.workspaceId, "/versions"), version);
14
+ };
15
+ WorkspacesVersionsEndpoint.prototype.rollback = function (versionId) {
16
+ this.api.post("/workspaces/".concat(this.workspaceId, "/versions/").concat(versionId, "/rollback"));
17
+ };
18
+ WorkspacesVersionsEndpoint.prototype.export = function (version) {
19
+ if (version === void 0) { version = 'current'; }
20
+ return _tslib.__awaiter(this, void 0, void 0, function () {
21
+ var res, _a;
22
+ return _tslib.__generator(this, function (_b) {
23
+ switch (_b.label) {
24
+ case 0: return [4 /*yield*/, this.api.prepareRequest("/workspaces/".concat(this.workspaceId, "/versions/").concat(version, "/export"), {
25
+ method: 'post'
26
+ })];
27
+ case 1:
28
+ res = _b.sent();
29
+ _a = Blob.bind;
30
+ return [4 /*yield*/, res.arrayBuffer()];
31
+ case 2: return [2 /*return*/, new (_a.apply(Blob, [void 0, [_b.sent()], { type: 'application/zip' }]))()];
32
+ }
33
+ });
34
+ });
35
+ };
36
+ return WorkspacesVersionsEndpoint;
37
+ }());
38
+
39
+ exports.WorkspacesVersionsEndpoint = WorkspacesVersionsEndpoint;
40
+ exports["default"] = WorkspacesVersionsEndpoint;
@@ -2,6 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var _tslib = require('../../_virtual/_tslib.js');
5
6
  var io = require('socket.io-client');
6
7
 
7
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -9,14 +10,58 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
9
10
  var io__default = /*#__PURE__*/_interopDefaultLegacy(io);
10
11
 
11
12
  var Events = /** @class */ (function () {
12
- function Events(workspaceId, token, apiHost) {
13
- if (apiHost === void 0) { apiHost = 'https://api.eda.prisme.ai'; }
13
+ function Events(_a) {
14
+ var workspaceId = _a.workspaceId, token = _a.token, legacyToken = _a.legacyToken, apiKey = _a.apiKey, _b = _a.apiHost, apiHost = _b === void 0 ? 'https://api.eda.prisme.ai' : _b, filters = _a.filters, api = _a.api;
15
+ var _this = this;
16
+ this.listeners = new Map();
14
17
  this.workspaceId = workspaceId;
15
- this.client = io__default["default"]("".concat(apiHost, "/workspaces/").concat(workspaceId, "/events"), {
16
- extraHeaders: {
17
- 'x-prismeai-session-token': token,
18
- },
18
+ var queryString = new URLSearchParams(filters || {}).toString();
19
+ var fullQueryString = queryString ? "?".concat(queryString) : '';
20
+ var extraHeaders = token
21
+ ? {
22
+ Authorization: "Bearer ".concat(token),
23
+ }
24
+ : { 'x-prismeai-token': legacyToken };
25
+ this.lastReceivedEventDate = new Date();
26
+ if (apiKey) {
27
+ extraHeaders['x-prismeai-api-key'] = apiKey;
28
+ }
29
+ this.client = io__default["default"]("".concat(apiHost, "/workspaces/").concat(workspaceId, "/events").concat(fullQueryString), {
30
+ extraHeaders: extraHeaders,
31
+ withCredentials: true,
19
32
  });
33
+ var onConnect = function () {
34
+ // Reset last filters
35
+ _this.updateFilters({
36
+ payloadQuery: _this.filters,
37
+ });
38
+ setTimeout(function () { return _tslib.__awaiter(_this, void 0, void 0, function () {
39
+ var events;
40
+ var _this = this;
41
+ return _tslib.__generator(this, function (_a) {
42
+ switch (_a.label) {
43
+ case 0: return [4 /*yield*/, api.getEvents(workspaceId, _tslib.__assign(_tslib.__assign({}, this.filters[this.filters.length - 1]), { afterDate: this.lastReceivedEventDate.toISOString() }))];
44
+ case 1:
45
+ events = _a.sent();
46
+ events.reverse().forEach(function (event) {
47
+ (_this.listeners.get(event.type) || []).forEach(function (listener) {
48
+ return listener(event);
49
+ });
50
+ });
51
+ return [2 /*return*/];
52
+ }
53
+ });
54
+ }); }, 2000);
55
+ };
56
+ this.client.on('disconnect', function () {
57
+ if (!_this.lastReceivedEventDate) {
58
+ _this.lastReceivedEventDate = new Date();
59
+ }
60
+ _this.client.off('connect', onConnect);
61
+ _this.client.on('connect', onConnect);
62
+ });
63
+ this.filters = [filters || {}];
64
+ this.listenedUserTopics = new Map();
20
65
  }
21
66
  Object.defineProperty(Events.prototype, "socket", {
22
67
  get: function () {
@@ -38,22 +83,54 @@ var Events = /** @class */ (function () {
38
83
  };
39
84
  Events.prototype.all = function (listener) {
40
85
  var _this = this;
41
- this.client.onAny(listener);
86
+ this.client.onAny(function (eventName, eventData) {
87
+ _this.lastReceivedEventDate = new Date(eventData === null || eventData === void 0 ? void 0 : eventData.createdAt);
88
+ return listener(eventName, eventData);
89
+ });
42
90
  return function () { return _this.client.offAny(listener); };
43
91
  };
44
92
  Events.prototype.on = function (ev, listener) {
45
93
  var _this = this;
94
+ this.listeners.set(ev, _tslib.__spreadArray(_tslib.__spreadArray([], (this.listeners.get(ev) || []), true), [listener]));
46
95
  this.client.on(ev, listener);
47
- return function () { return _this.client.off(ev, listener); };
96
+ return function () {
97
+ _this.listeners.set(ev, (_this.listeners.get(ev) || []).filter(function (l) { return l !== listener; }));
98
+ _this.client.off(ev, listener);
99
+ };
48
100
  };
49
- Events.prototype.emit = function (event, payload) {
101
+ Events.prototype.emit = function (event, payload, options) {
50
102
  this.client.emit('event', {
51
103
  type: event,
52
104
  payload: payload,
105
+ options: options,
53
106
  });
54
107
  };
108
+ Events.prototype.listenTopics = function (_a) {
109
+ var event = _a.event, topics = _a.topics;
110
+ topics = Array.isArray(topics) ? topics : [topics];
111
+ this.listenedUserTopics.set(event, topics);
112
+ this.filters = [
113
+ _tslib.__assign({}, this.filters[0]),
114
+ {
115
+ 'target.userTopic': Array.from(this.listenedUserTopics).flatMap(function (_a) {
116
+ _a[0]; var topics = _a[1];
117
+ return topics;
118
+ }),
119
+ },
120
+ ];
121
+ this.updateFilters({
122
+ payloadQuery: this.filters,
123
+ });
124
+ };
125
+ Events.prototype.updateFilters = function (filters) {
126
+ this.client.emit('filters', filters);
127
+ };
55
128
  Events.prototype.once = function (ev, listener) {
129
+ var _this = this;
56
130
  this.client.once(ev, listener);
131
+ return function () {
132
+ _this.client.off(ev, listener);
133
+ };
57
134
  };
58
135
  Events.prototype.close = function () {
59
136
  this.client.close();
@@ -14,29 +14,63 @@ 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;
20
+ this._apiKey = null;
19
21
  this.host = host;
22
+ this.clientIdHeader = clientIdHeader;
20
23
  }
24
+ Object.defineProperty(Fetcher.prototype, "apiKey", {
25
+ set: function (apiKey) {
26
+ this._apiKey = apiKey;
27
+ },
28
+ enumerable: false,
29
+ configurable: true
30
+ });
31
+ Fetcher.prototype.prepareRequest = function (url, options) {
32
+ if (options === void 0) { options = {}; }
33
+ var headers = new Headers(options.headers || {});
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);
39
+ }
40
+ if (this._apiKey && !headers.has('x-prismeai-apikey')) {
41
+ headers.append('x-prismeai-api-key', this._apiKey);
42
+ }
43
+ if (this.language) {
44
+ headers.append('accept-language', this.language);
45
+ }
46
+ if (options.body instanceof URLSearchParams) {
47
+ headers.set('Content-Type', 'application/x-www-form-urlencoded');
48
+ }
49
+ if ((!options.body || !(options.body instanceof FormData)) &&
50
+ !headers.has('Content-Type')) {
51
+ headers.append('Content-Type', 'application/json');
52
+ }
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 }));
57
+ };
21
58
  Fetcher.prototype._fetch = function (url, options) {
22
59
  if (options === void 0) { options = {}; }
23
60
  return _tslib.__awaiter(this, void 0, void 0, function () {
24
- var headers, res, error, _a, clone, response;
61
+ var res, error, _a, contentType, response, text;
25
62
  return _tslib.__generator(this, function (_b) {
26
63
  switch (_b.label) {
27
- case 0:
28
- headers = new Headers(options.headers || {});
29
- if (this.token && !headers.has('x-prismeai-session-token')) {
30
- headers.append('x-prismeai-session-token', this.token);
31
- }
32
- if ((!options.body || !(options.body instanceof FormData)) &&
33
- !headers.has('Content-Type')) {
34
- headers.append('Content-Type', 'application/json');
35
- }
36
- headers.append('Access-Control-Allow-Origin', '*');
37
- return [4 /*yield*/, global.fetch("".concat(this.host).concat(url), _tslib.__assign(_tslib.__assign({}, options), { headers: headers }))];
64
+ case 0: return [4 /*yield*/, this.prepareRequest(url, options)];
38
65
  case 1:
39
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
+ }
40
74
  if (!!res.ok) return [3 /*break*/, 6];
41
75
  error = void 0;
42
76
  _b.label = 2;
@@ -53,10 +87,11 @@ var Fetcher = /** @class */ (function () {
53
87
  return [3 /*break*/, 5];
54
88
  case 5: throw error;
55
89
  case 6:
56
- clone = res.clone();
90
+ contentType = res.headers.get('content-type');
91
+ if (!(contentType && contentType.includes('application/json'))) return [3 /*break*/, 10];
57
92
  _b.label = 7;
58
93
  case 7:
59
- _b.trys.push([7, 9, , 11]);
94
+ _b.trys.push([7, 9, , 10]);
60
95
  return [4 /*yield*/, res.json()];
61
96
  case 8:
62
97
  response = (_b.sent()) || {};
@@ -69,9 +104,17 @@ var Fetcher = /** @class */ (function () {
69
104
  return [2 /*return*/, response];
70
105
  case 9:
71
106
  _b.sent();
72
- return [4 /*yield*/, clone.text()];
73
- case 10: return [2 /*return*/, (_b.sent())];
74
- case 11: return [2 /*return*/];
107
+ return [2 /*return*/, {}];
108
+ case 10: return [4 /*yield*/, res.text()];
109
+ case 11:
110
+ text = _b.sent();
111
+ try {
112
+ return [2 /*return*/, JSON.parse(text)];
113
+ }
114
+ catch (_c) {
115
+ return [2 /*return*/, text];
116
+ }
117
+ return [2 /*return*/];
75
118
  }
76
119
  });
77
120
  });
@@ -85,12 +128,23 @@ var Fetcher = /** @class */ (function () {
85
128
  });
86
129
  });
87
130
  };
88
- Fetcher.prototype.post = function (url, body) {
131
+ Fetcher.prototype.post = function (url, body, opts) {
132
+ return _tslib.__awaiter(this, void 0, void 0, function () {
133
+ return _tslib.__generator(this, function (_a) {
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))];
139
+ });
140
+ });
141
+ };
142
+ Fetcher.prototype.put = function (url, body) {
89
143
  return _tslib.__awaiter(this, void 0, void 0, function () {
90
144
  return _tslib.__generator(this, function (_a) {
91
145
  return [2 /*return*/, this._fetch(url, {
92
- method: 'POST',
93
- body: body && !(body instanceof FormData) ? JSON.stringify(body) : body,
146
+ method: 'PUT',
147
+ body: JSON.stringify(body),
94
148
  })];
95
149
  });
96
150
  });