@prisme.ai/sdk 0.0.2

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,121 @@
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) {
18
+ this.token = null;
19
+ this.host = host;
20
+ }
21
+ Fetcher.prototype._fetch = function (url, options) {
22
+ if (options === void 0) { options = {}; }
23
+ return _tslib.__awaiter(this, void 0, void 0, function () {
24
+ var headers, res, error, _a, clone, response;
25
+ return _tslib.__generator(this, function (_b) {
26
+ 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 }))];
38
+ case 1:
39
+ res = _b.sent();
40
+ if (!!res.ok) return [3 /*break*/, 6];
41
+ error = void 0;
42
+ _b.label = 2;
43
+ case 2:
44
+ _b.trys.push([2, 4, , 5]);
45
+ _a = ApiError.ApiError.bind;
46
+ return [4 /*yield*/, res.json()];
47
+ case 3:
48
+ error = new (_a.apply(ApiError.ApiError, [void 0, _b.sent(), res.status]))();
49
+ return [3 /*break*/, 5];
50
+ case 4:
51
+ _b.sent();
52
+ error = new HTTPError.HTTPError(res.statusText, res.status);
53
+ return [3 /*break*/, 5];
54
+ case 5: throw error;
55
+ case 6:
56
+ clone = res.clone();
57
+ _b.label = 7;
58
+ case 7:
59
+ _b.trys.push([7, 9, , 11]);
60
+ return [4 /*yield*/, res.json()];
61
+ case 8:
62
+ response = (_b.sent()) || {};
63
+ Object.defineProperty(response, 'headers', {
64
+ value: headersAsObject(res.headers),
65
+ configurable: false,
66
+ enumerable: false,
67
+ writable: false,
68
+ });
69
+ return [2 /*return*/, response];
70
+ case 9:
71
+ _b.sent();
72
+ return [4 /*yield*/, clone.text()];
73
+ case 10: return [2 /*return*/, (_b.sent())];
74
+ case 11: return [2 /*return*/];
75
+ }
76
+ });
77
+ });
78
+ };
79
+ Fetcher.prototype.get = function (url) {
80
+ return _tslib.__awaiter(this, void 0, void 0, function () {
81
+ return _tslib.__generator(this, function (_a) {
82
+ return [2 /*return*/, this._fetch(url, {
83
+ method: 'GET',
84
+ })];
85
+ });
86
+ });
87
+ };
88
+ Fetcher.prototype.post = function (url, body) {
89
+ return _tslib.__awaiter(this, void 0, void 0, function () {
90
+ return _tslib.__generator(this, function (_a) {
91
+ return [2 /*return*/, this._fetch(url, {
92
+ method: 'POST',
93
+ body: body && !(body instanceof FormData) ? JSON.stringify(body) : body,
94
+ })];
95
+ });
96
+ });
97
+ };
98
+ Fetcher.prototype.patch = function (url, body) {
99
+ return _tslib.__awaiter(this, void 0, void 0, function () {
100
+ return _tslib.__generator(this, function (_a) {
101
+ return [2 /*return*/, this._fetch(url, {
102
+ method: 'PATCH',
103
+ body: JSON.stringify(body),
104
+ })];
105
+ });
106
+ });
107
+ };
108
+ Fetcher.prototype.delete = function (url) {
109
+ return _tslib.__awaiter(this, void 0, void 0, function () {
110
+ return _tslib.__generator(this, function (_a) {
111
+ return [2 /*return*/, this._fetch(url, {
112
+ method: 'DELETE',
113
+ })];
114
+ });
115
+ });
116
+ };
117
+ return Fetcher;
118
+ }());
119
+
120
+ exports.Fetcher = Fetcher;
121
+ exports["default"] = Fetcher;
@@ -0,0 +1,18 @@
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 ADDED
@@ -0,0 +1,7 @@
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';
@@ -0,0 +1,13 @@
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
+ });
@@ -0,0 +1,21 @@
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;
@@ -0,0 +1,8 @@
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
+ });
@@ -0,0 +1,13 @@
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;
@@ -0,0 +1,231 @@
1
+ import api, { Api } from './api';
2
+
3
+ it('should export an instance', () => {
4
+ expect(api).toBeInstanceOf(Api);
5
+ });
6
+
7
+ it('should call /me', () => {
8
+ const api = new Api('/fake/');
9
+ api.get = jest.fn();
10
+ api.me();
11
+ expect(api.get).toHaveBeenCalledWith('/me');
12
+ });
13
+
14
+ it('should call /signin', () => {
15
+ const api = new Api('/fake/');
16
+ api.post = jest.fn();
17
+ api.signin('user@fake.com', 'password');
18
+ expect(api.post).toHaveBeenCalledWith('/login', {
19
+ email: 'user@fake.com',
20
+ password: 'password',
21
+ });
22
+ });
23
+
24
+ it('should call /signup', () => {
25
+ const api = new Api('/fake/');
26
+ api.post = jest.fn();
27
+ api.signup('user@fake.com', 'password', 'firstname', 'lastname');
28
+ expect(api.post).toHaveBeenCalledWith('/signup', {
29
+ email: 'user@fake.com',
30
+ password: 'password',
31
+ firstName: 'firstname',
32
+ lastName: 'lastname',
33
+ });
34
+ });
35
+
36
+ it('should call /signout', () => {
37
+ const api = new Api('/fake/');
38
+ api.post = jest.fn();
39
+ api.signout();
40
+ expect(api.post).toHaveBeenCalledWith('/logout');
41
+ expect(api.token).toBeNull();
42
+ });
43
+
44
+ it('should call get /workspaces', () => {
45
+ const api = new Api('/fake/');
46
+ api.get = jest.fn();
47
+ api.getWorkspaces();
48
+ expect(api.get).toHaveBeenCalledWith('/workspaces?limit=300');
49
+ });
50
+
51
+ it('should call get /workspaces/42', () => {
52
+ const api = new Api('/fake/');
53
+ api.get = jest.fn();
54
+ api.getWorkspace('42');
55
+ expect(api.get).toHaveBeenCalledWith('/workspaces/42');
56
+ });
57
+
58
+ it('should call post /workspaces', () => {
59
+ const api = new Api('/fake/');
60
+ api.post = jest.fn();
61
+ api.createWorkspace('foo');
62
+ expect(api.post).toHaveBeenCalledWith('/workspaces', {
63
+ name: 'foo',
64
+ });
65
+ });
66
+
67
+ it('should call patch /workspaces/42', async () => {
68
+ const api = new Api('/fake/');
69
+ api.patch = jest.fn();
70
+ await api.updateWorkspace({
71
+ id: '42',
72
+ name: 'foo',
73
+ automations: {},
74
+ createdAt: '',
75
+ updatedAt: '',
76
+ });
77
+ expect(api.patch).toHaveBeenCalledWith('/workspaces/42', {
78
+ id: '42',
79
+ name: 'foo',
80
+ automations: {},
81
+ createdAt: '',
82
+ updatedAt: '',
83
+ });
84
+ });
85
+
86
+ it('should call post /workspaces/42/automations', () => {
87
+ const api = new Api('/fake/');
88
+ api.post = jest.fn();
89
+ api.createAutomation(
90
+ {
91
+ id: '42',
92
+ name: 'foo',
93
+ automations: {},
94
+ createdAt: '',
95
+ updatedAt: '',
96
+ },
97
+ {
98
+ name: 'foo',
99
+ do: [],
100
+ }
101
+ );
102
+ expect(api.post).toHaveBeenCalledWith('/workspaces/42/automations', {
103
+ name: 'foo',
104
+ do: [],
105
+ });
106
+ });
107
+
108
+ it('should call patch /workspaces/42/automations', async () => {
109
+ const api = new Api('/fake/');
110
+ api.patch = jest.fn();
111
+ await api.updateAutomation(
112
+ {
113
+ id: '42',
114
+ name: 'foo',
115
+ automations: {},
116
+ createdAt: '',
117
+ updatedAt: '',
118
+ },
119
+ '42-1',
120
+ {
121
+ name: 'foo',
122
+ do: [],
123
+ }
124
+ );
125
+ expect(api.patch).toHaveBeenCalledWith('/workspaces/42/automations/42-1', {
126
+ name: 'foo',
127
+ do: [],
128
+ });
129
+ });
130
+
131
+ it('should call delete /workspaces/42/automations/42-1', () => {
132
+ const api = new Api('/fake/');
133
+ api.delete = jest.fn();
134
+ api.deleteAutomation(
135
+ {
136
+ id: '42',
137
+ name: 'foo',
138
+ automations: {},
139
+ createdAt: '',
140
+ updatedAt: '',
141
+ },
142
+ '42-1'
143
+ );
144
+ expect(api.delete).toHaveBeenCalledWith('/workspaces/42/automations/42-1');
145
+ });
146
+
147
+ it('should call get /workspaces/42/events', async () => {
148
+ const api = new Api('/fake/');
149
+ api.get = jest.fn(
150
+ async (): Promise<any> => ({
151
+ result: {
152
+ events: [
153
+ {
154
+ id: '1',
155
+ createdAt: '2021-01-01',
156
+ },
157
+ ],
158
+ },
159
+ })
160
+ );
161
+ expect(await api.getEvents('42')).toEqual([
162
+ {
163
+ id: '1',
164
+ createdAt: new Date('2021-01-01'),
165
+ },
166
+ ]);
167
+ });
168
+
169
+ it('should replace all images data', async () => {
170
+ const api = new Api('/fake/');
171
+ api.uploadFiles = jest.fn(async () => [
172
+ { url: 'http://image1.jpg' } as any,
173
+ { url: 'http://image2.jpg' } as any,
174
+ { url: 'http://image3.jpg' } as any,
175
+ { url: 'http://image4.jpg' } as any,
176
+ { url: 'http://image5.jpg' } as any,
177
+ ]);
178
+ const original = {
179
+ foo: 'data:image/jpeg;base64…',
180
+ bar: {
181
+ pic: 'data:image/jpeg;base64…',
182
+ pics: [
183
+ 'data:image/jpeg;base64…',
184
+ 'data:image/jpeg;base64…',
185
+ 'data:image/jpeg;base64…',
186
+ ],
187
+ nopics: 'http://alreadyUpImage.jpg',
188
+ },
189
+ anythingElse: 42,
190
+ };
191
+ expect(await api.replaceAllImagesData(original, '42')).toEqual({
192
+ foo: 'http://image1.jpg',
193
+ bar: {
194
+ pic: 'http://image2.jpg',
195
+ pics: ['http://image3.jpg', 'http://image4.jpg', 'http://image5.jpg'],
196
+ nopics: 'http://alreadyUpImage.jpg',
197
+ },
198
+ anythingElse: 42,
199
+ });
200
+ expect(api.uploadFiles).toHaveBeenCalledWith(
201
+ [
202
+ 'data:image/jpeg;base64…',
203
+ 'data:image/jpeg;base64…',
204
+ 'data:image/jpeg;base64…',
205
+ 'data:image/jpeg;base64…',
206
+ 'data:image/jpeg;base64…',
207
+ ],
208
+ '42'
209
+ );
210
+ expect(api.uploadFiles).toHaveBeenCalledTimes(1);
211
+ });
212
+
213
+ it('should upload file', async () => {
214
+ const api = new Api('/fake/');
215
+ // @ts-ignore
216
+ api._fetch = jest.fn(() => [{}]);
217
+ await api.uploadFiles(
218
+ 'data:image/jpeg;filename:foo.jpg;base64,abcdefg',
219
+ '42'
220
+ );
221
+ // @ts-ignore
222
+ expect(api._fetch).toHaveBeenCalledWith('/workspaces/42/files', {
223
+ method: 'POST',
224
+ body: expect.any(FormData),
225
+ });
226
+ const { body }: { body: FormData } =
227
+ // @ts-ignore
228
+ api._fetch.mock.calls[0][1];
229
+ expect(body.getAll('file').length).toBe(1);
230
+ expect((body.getAll('file')[0] as File).name).toBe('foo.jpg');
231
+ });