@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,43 +0,0 @@
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;
@@ -1,23 +0,0 @@
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;
@@ -1,40 +0,0 @@
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;
@@ -1,142 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _tslib = require('../../_virtual/_tslib.js');
6
- var io = require('socket.io-client');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var io__default = /*#__PURE__*/_interopDefaultLegacy(io);
11
-
12
- var Events = /** @class */ (function () {
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();
17
- this.workspaceId = workspaceId;
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,
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();
65
- }
66
- Object.defineProperty(Events.prototype, "socket", {
67
- get: function () {
68
- return this.client;
69
- },
70
- enumerable: false,
71
- configurable: true
72
- });
73
- Events.prototype.destroy = function () {
74
- var _this = this;
75
- this.workspaceId = '';
76
- if (this.client.connected) {
77
- this.client.disconnect();
78
- return;
79
- }
80
- this.client.once('connect', function () {
81
- _this.client.disconnect();
82
- });
83
- };
84
- Events.prototype.all = function (listener) {
85
- var _this = this;
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
- });
90
- return function () { return _this.client.offAny(listener); };
91
- };
92
- Events.prototype.on = function (ev, listener) {
93
- var _this = this;
94
- this.listeners.set(ev, _tslib.__spreadArray(_tslib.__spreadArray([], (this.listeners.get(ev) || []), true), [listener]));
95
- this.client.on(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
- };
100
- };
101
- Events.prototype.emit = function (event, payload, options) {
102
- this.client.emit('event', {
103
- type: event,
104
- payload: payload,
105
- options: options,
106
- });
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
- };
128
- Events.prototype.once = function (ev, listener) {
129
- var _this = this;
130
- this.client.once(ev, listener);
131
- return function () {
132
- _this.client.off(ev, listener);
133
- };
134
- };
135
- Events.prototype.close = function () {
136
- this.client.close();
137
- };
138
- return Events;
139
- }());
140
-
141
- exports.Events = Events;
142
- exports["default"] = Events;
@@ -1,175 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var _tslib = require('../../_virtual/_tslib.js');
6
- var ApiError = require('./ApiError.js');
7
- var HTTPError = require('./HTTPError.js');
8
-
9
- var headersAsObject = function (headers) {
10
- return Array.from(headers).reduce(function (prev, _a) {
11
- var _b;
12
- var k = _a[0], v = _a[1];
13
- return (_tslib.__assign(_tslib.__assign({}, prev), (_b = {}, _b[k] = v, _b)));
14
- }, {});
15
- };
16
- var Fetcher = /** @class */ (function () {
17
- function Fetcher(host, clientIdHeader) {
18
- this.token = null;
19
- this.legacyToken = null;
20
- this._apiKey = null;
21
- this.host = host;
22
- this.clientIdHeader = clientIdHeader;
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
- };
58
- Fetcher.prototype._fetch = function (url, options) {
59
- if (options === void 0) { options = {}; }
60
- return _tslib.__awaiter(this, void 0, void 0, function () {
61
- var res, error, _a, contentType, response, text;
62
- return _tslib.__generator(this, function (_b) {
63
- switch (_b.label) {
64
- case 0: return [4 /*yield*/, this.prepareRequest(url, options)];
65
- case 1:
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
- }
74
- if (!!res.ok) return [3 /*break*/, 6];
75
- error = void 0;
76
- _b.label = 2;
77
- case 2:
78
- _b.trys.push([2, 4, , 5]);
79
- _a = ApiError.ApiError.bind;
80
- return [4 /*yield*/, res.json()];
81
- case 3:
82
- error = new (_a.apply(ApiError.ApiError, [void 0, _b.sent(), res.status]))();
83
- return [3 /*break*/, 5];
84
- case 4:
85
- _b.sent();
86
- error = new HTTPError.HTTPError(res.statusText, res.status);
87
- return [3 /*break*/, 5];
88
- case 5: throw error;
89
- case 6:
90
- contentType = res.headers.get('content-type');
91
- if (!(contentType && contentType.includes('application/json'))) return [3 /*break*/, 10];
92
- _b.label = 7;
93
- case 7:
94
- _b.trys.push([7, 9, , 10]);
95
- return [4 /*yield*/, res.json()];
96
- case 8:
97
- response = (_b.sent()) || {};
98
- Object.defineProperty(response, 'headers', {
99
- value: headersAsObject(res.headers),
100
- configurable: false,
101
- enumerable: false,
102
- writable: false,
103
- });
104
- return [2 /*return*/, response];
105
- case 9:
106
- _b.sent();
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*/];
118
- }
119
- });
120
- });
121
- };
122
- Fetcher.prototype.get = function (url) {
123
- return _tslib.__awaiter(this, void 0, void 0, function () {
124
- return _tslib.__generator(this, function (_a) {
125
- return [2 /*return*/, this._fetch(url, {
126
- method: 'GET',
127
- })];
128
- });
129
- });
130
- };
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) {
143
- return _tslib.__awaiter(this, void 0, void 0, function () {
144
- return _tslib.__generator(this, function (_a) {
145
- return [2 /*return*/, this._fetch(url, {
146
- method: 'PUT',
147
- body: JSON.stringify(body),
148
- })];
149
- });
150
- });
151
- };
152
- Fetcher.prototype.patch = function (url, body) {
153
- return _tslib.__awaiter(this, void 0, void 0, function () {
154
- return _tslib.__generator(this, function (_a) {
155
- return [2 /*return*/, this._fetch(url, {
156
- method: 'PATCH',
157
- body: JSON.stringify(body),
158
- })];
159
- });
160
- });
161
- };
162
- Fetcher.prototype.delete = function (url) {
163
- return _tslib.__awaiter(this, void 0, void 0, function () {
164
- return _tslib.__generator(this, function (_a) {
165
- return [2 /*return*/, this._fetch(url, {
166
- method: 'DELETE',
167
- })];
168
- });
169
- });
170
- };
171
- return Fetcher;
172
- }());
173
-
174
- exports.Fetcher = Fetcher;
175
- exports["default"] = Fetcher;
@@ -1,18 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var removedUndefinedProperties = function (obj, removeEmptyStrings) {
6
- if (removeEmptyStrings === void 0) { removeEmptyStrings = false; }
7
- return Object.entries(obj).reduce(function (newObject, _a) {
8
- var key = _a[0], value = _a[1];
9
- if (value !== undefined) {
10
- if (!(removeEmptyStrings && value === '')) {
11
- newObject[key] = value;
12
- }
13
- }
14
- return newObject;
15
- }, {});
16
- };
17
-
18
- exports.removedUndefinedProperties = removedUndefinedProperties;
package/index.ts DELETED
@@ -1,7 +0,0 @@
1
- export * from './lib/api';
2
- export * from './lib/ApiError';
3
- export * from './lib/events';
4
- export * from './lib/fetcher';
5
- export * from './lib/HTTPError';
6
- export * from './lib/types';
7
- export { default } from './lib/api';
@@ -1,13 +0,0 @@
1
- import ApiError from './ApiError';
2
-
3
- it('should construct', () => {
4
- const apiError = new ApiError(
5
- { error: 'foo', message: 'bar', details: [{}] },
6
- 400
7
- );
8
- expect(apiError.code).toBe(400);
9
- expect(apiError.message).toBe('bar');
10
- expect(apiError.error).toBe('foo');
11
- expect(apiError.details).toEqual([{}]);
12
- expect(`${apiError}`).toBe('foo');
13
- });
package/lib/ApiError.ts DELETED
@@ -1,21 +0,0 @@
1
- export class ApiError extends Error implements Prismeai.GenericError {
2
- public code: number;
3
- public error: Prismeai.GenericError['error'];
4
- public details: Prismeai.GenericError['details'];
5
-
6
- constructor(
7
- { error, message, details }: Prismeai.GenericError,
8
- code: number
9
- ) {
10
- super(message);
11
- this.code = code;
12
- this.error = error;
13
- this.details = details;
14
- }
15
-
16
- toString() {
17
- return this.error;
18
- }
19
- }
20
-
21
- export default ApiError;
@@ -1,8 +0,0 @@
1
- import HTTPError from './HTTPError';
2
-
3
- it('should construct', () => {
4
- const error = new HTTPError('error', 400);
5
- expect(error.code).toBe(400);
6
- expect(error.message).toBe('error');
7
- expect(`${error}`).toBe('400 error');
8
- });
package/lib/HTTPError.ts DELETED
@@ -1,13 +0,0 @@
1
- export class HTTPError extends Error {
2
- public code: number;
3
- constructor(status: string, code: number) {
4
- super(status);
5
- this.code = code;
6
- }
7
-
8
- toString() {
9
- return `${this.code} ${this.message}`;
10
- }
11
- }
12
-
13
- export default HTTPError;