@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,22 +0,0 @@
1
- import { Api } from '../api';
2
-
3
- export class UsersEndpoint {
4
- private id: string;
5
- private api: Api;
6
-
7
- constructor(id: string, api: Api) {
8
- this.id = id;
9
- this.api = api;
10
- }
11
-
12
- async setMeta(k: string, v: any) {
13
- await this.api.post(`/user/meta`, {
14
- [k]: v,
15
- });
16
- }
17
- async deleteMeta(k: string) {
18
- await this.api.delete(`/user/meta/${k}`);
19
- }
20
- }
21
-
22
- export default UsersEndpoint;
@@ -1,18 +0,0 @@
1
- import { Api } from '../api';
2
- import WorkspacesVersionsEndpoint from './workspacesVersions';
3
-
4
- export class WorkspacesEndpoint {
5
- private id: string;
6
- private api: Api;
7
-
8
- constructor(id: string, api: Api) {
9
- this.id = id;
10
- this.api = api;
11
- }
12
-
13
- get versions() {
14
- return new WorkspacesVersionsEndpoint(this.id, this.api);
15
- }
16
- }
17
-
18
- export default WorkspacesEndpoint;
@@ -1,34 +0,0 @@
1
- import { Api } from '../api';
2
-
3
- export class WorkspacesVersionsEndpoint {
4
- private workspaceId: string;
5
- private api: Api;
6
-
7
- constructor(workspaceId: string, api: Api) {
8
- this.workspaceId = workspaceId;
9
- this.api = api;
10
- }
11
-
12
- create(version?: PrismeaiAPI.PublishWorkspaceVersion.RequestBody) {
13
- this.api.post(`/workspaces/${this.workspaceId}/versions`, version);
14
- }
15
- rollback(
16
- versionId: PrismeaiAPI.RollbackWorkspaceVersion.PathParameters['versionId']
17
- ) {
18
- this.api.post(
19
- `/workspaces/${this.workspaceId}/versions/${versionId}/rollback`
20
- );
21
- }
22
-
23
- async export(version: PrismeaiAPI.ExportWorkspaceVersion.Parameters.VersionId = 'current') {
24
- const res = await this.api.prepareRequest(
25
- `/workspaces/${this.workspaceId}/versions/${version}/export`, {
26
- method: 'post'
27
- }
28
- )
29
- return new Blob([await res.arrayBuffer()], { type: 'application/zip' });
30
-
31
- }
32
- }
33
-
34
- export default WorkspacesVersionsEndpoint;
@@ -1,89 +0,0 @@
1
- import getConfig from 'next/config';
2
- import Events from './events';
3
- import io from 'socket.io-client';
4
- import { Api } from './api';
5
-
6
- jest.mock('socket.io-client', () => {
7
- const mock = {
8
- disconnect: jest.fn(),
9
- onAny: jest.fn(),
10
- offAny: jest.fn(),
11
- on: jest.fn(),
12
- once: jest.fn(),
13
- };
14
- const io = jest.fn(() => mock);
15
- return io;
16
- });
17
-
18
- it('should connect to Websocket', () => {
19
- new Events({ workspaceId: '1', token: 'abcde', api: {} as Api });
20
- expect(io).toHaveBeenCalledWith(
21
- `https://api.eda.prisme.ai/workspaces/1/events`,
22
- {
23
- extraHeaders: {
24
- Authorization: 'Bearer abcde',
25
- },
26
- withCredentials: true,
27
- }
28
- );
29
- });
30
-
31
- it('should connect to Websocket with apiKey', () => {
32
- new Events({
33
- workspaceId: '1',
34
- token: 'abcde',
35
- apiKey: 'fghij',
36
- api: {} as Api,
37
- });
38
- expect(io).toHaveBeenCalledWith(
39
- `https://api.eda.prisme.ai/workspaces/1/events`,
40
- {
41
- extraHeaders: {
42
- Authorization: 'Bearer abcde',
43
- 'x-prismeai-api-key': 'fghij',
44
- },
45
- withCredentials: true,
46
- }
47
- );
48
- });
49
-
50
- it('should disconnect to Websocket', () => {
51
- const client = new Events({
52
- workspaceId: '1',
53
- token: 'abcde',
54
- api: {} as Api,
55
- });
56
- (client as any).client.connected = true;
57
- client.destroy();
58
- expect(io().disconnect).toHaveBeenCalled();
59
- });
60
-
61
- it('should wait before disconnecting Websocket', () => {
62
- const client = new Events({
63
- workspaceId: '1',
64
- token: 'abcde',
65
- api: {} as Api,
66
- });
67
- const ioInstance = io();
68
- (client as any).client.connected = false;
69
- ((client as any).client.once as jest.Mock).mockClear();
70
- ioInstance.disconnect = jest.fn();
71
- client.destroy();
72
- expect(ioInstance.disconnect).not.toHaveBeenCalled();
73
- expect((client as any).client.once).toHaveBeenCalled();
74
- ((client as any).client.once as jest.Mock).mock.calls[0][1]();
75
- expect(ioInstance.disconnect).toHaveBeenCalled();
76
- });
77
-
78
- it('should listen to all events', () => {
79
- const client = new Events({
80
- workspaceId: '1',
81
- token: 'abcde',
82
- api: {} as Api,
83
- });
84
- const listener = () => null;
85
- const off = client.all(listener);
86
- expect(io().onAny).toHaveBeenCalledWith(listener);
87
- off();
88
- expect(io().offAny).toHaveBeenCalledWith(listener);
89
- });
package/lib/events.ts DELETED
@@ -1,180 +0,0 @@
1
- import io, { Socket } from 'socket.io-client';
2
-
3
- import { Api } from './api';
4
-
5
- export type PayloadQuery = Record<string, string | string[]>;
6
- export type OrQuery = PayloadQuery[];
7
-
8
- export type SearchOptions = Omit<
9
- PrismeaiAPI.EventsLongpolling.QueryParameters,
10
- 'query' | 'types'
11
- > & {
12
- payloadQuery?: PayloadQuery | OrQuery;
13
- types?: string[];
14
- };
15
-
16
- export class Events {
17
- protected client: Socket;
18
- public workspaceId: string;
19
- private filters: Record<string, any>[];
20
- private listenedUserTopics: Map<string, string[]>;
21
- private listeners: Map<string, Function[]> = new Map();
22
- private lastReceivedEventDate: Date;
23
-
24
- constructor({
25
- workspaceId,
26
- token,
27
- legacyToken,
28
- apiKey,
29
- apiHost = 'https://api.eda.prisme.ai',
30
- filters,
31
- api,
32
- }: {
33
- workspaceId: string;
34
- token: string;
35
- legacyToken?: string;
36
- apiKey?: string;
37
- apiHost?: string;
38
- filters?: Record<string, any>;
39
- api: Api;
40
- }) {
41
- this.workspaceId = workspaceId;
42
- const queryString = new URLSearchParams(filters || {}).toString();
43
- const fullQueryString = queryString ? `?${queryString}` : '';
44
- const extraHeaders: any = token
45
- ? {
46
- Authorization: `Bearer ${token}`,
47
- }
48
- : { 'x-prismeai-token': legacyToken };
49
- this.lastReceivedEventDate = new Date();
50
- if (apiKey) {
51
- extraHeaders['x-prismeai-api-key'] = apiKey;
52
- }
53
-
54
- this.client = io(
55
- `${apiHost}/workspaces/${workspaceId}/events${fullQueryString}`,
56
- {
57
- extraHeaders,
58
- withCredentials: true,
59
- }
60
- );
61
-
62
- const onConnect = () => {
63
- // Reset last filters
64
- this.updateFilters({
65
- payloadQuery: this.filters,
66
- });
67
- setTimeout(async () => {
68
- const events = await api.getEvents(workspaceId, {
69
- ...this.filters[this.filters.length - 1],
70
- afterDate: this.lastReceivedEventDate.toISOString(),
71
- });
72
- events.reverse().forEach((event) => {
73
- (this.listeners.get(event.type) || []).forEach((listener) =>
74
- listener(event)
75
- );
76
- });
77
- }, 2000);
78
- };
79
- this.client.on('disconnect', () => {
80
- if (!this.lastReceivedEventDate) {
81
- this.lastReceivedEventDate = new Date();
82
- }
83
- this.client.off('connect', onConnect);
84
- this.client.on('connect', onConnect);
85
- });
86
-
87
- this.filters = [filters || {}];
88
- this.listenedUserTopics = new Map();
89
- }
90
-
91
- get socket() {
92
- return this.client;
93
- }
94
-
95
- destroy() {
96
- this.workspaceId = '';
97
-
98
- if (this.client.connected) {
99
- this.client.disconnect();
100
- return;
101
- }
102
- this.client.once('connect', () => {
103
- this.client.disconnect();
104
- });
105
- }
106
-
107
- all(listener: (eventName: string, eventData: Prismeai.PrismeEvent) => void) {
108
- this.client.onAny((eventName: string, eventData: Prismeai.PrismeEvent) => {
109
- this.lastReceivedEventDate = new Date(eventData?.createdAt);
110
- return listener(eventName, eventData);
111
- });
112
-
113
- return () => this.client.offAny(listener);
114
- }
115
-
116
- on(ev: string, listener: (eventData: Prismeai.PrismeEvent) => void) {
117
- this.listeners.set(ev, [...(this.listeners.get(ev) || []), listener]);
118
-
119
- this.client.on(ev, listener);
120
- return () => {
121
- this.listeners.set(
122
- ev,
123
- (this.listeners.get(ev) || []).filter((l) => l !== listener)
124
- );
125
- this.client.off(ev, listener);
126
- };
127
- }
128
-
129
- emit(event: string, payload?: any, options?: any) {
130
- this.client.emit('event', {
131
- type: event,
132
- payload,
133
- options,
134
- });
135
- }
136
-
137
- listenTopics({
138
- event,
139
- topics,
140
- }: {
141
- event: string;
142
- topics: string | string[];
143
- }) {
144
- topics = Array.isArray(topics) ? topics : [topics];
145
-
146
- this.listenedUserTopics.set(event, topics);
147
-
148
- this.filters = [
149
- { ...this.filters[0] },
150
- {
151
- 'target.userTopic': Array.from(this.listenedUserTopics).flatMap(
152
- ([_event, topics]) => topics
153
- ),
154
- },
155
- ];
156
- this.updateFilters({
157
- payloadQuery: this.filters,
158
- });
159
- }
160
-
161
- updateFilters(filters: SearchOptions) {
162
- this.client.emit('filters', filters);
163
- }
164
-
165
- once(
166
- ev: string,
167
- listener: (eventName: string, eventData: Prismeai.PrismeEvent) => void
168
- ) {
169
- this.client.once(ev, listener);
170
- return () => {
171
- this.client.off(ev, listener);
172
- };
173
- }
174
-
175
- close() {
176
- this.client.close();
177
- }
178
- }
179
-
180
- export default Events;
@@ -1,246 +0,0 @@
1
- import ApiError from './ApiError';
2
- import Fetcher from './fetcher';
3
- import HTTPError from './HTTPError';
4
-
5
- it('should fetch', async () => {
6
- const fetcher = new Fetcher('http/');
7
- // @ts-ignore
8
- global.fetch = jest.fn(() => ({
9
- ok: true,
10
- headers: new Headers([['foo', 'bar']]),
11
- text() {
12
- return '';
13
- },
14
- clone() {
15
- return { ...this };
16
- },
17
- }));
18
- const o = await fetcher.get('url');
19
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
20
- headers: expect.any(Headers),
21
- method: 'GET',
22
- });
23
- const headers = (global.fetch as jest.Mock).mock.calls[0][1].headers;
24
- expect(headers.get('Access-Control-Allow-Origin')).toBe('*');
25
- expect(headers.get('Content-Type')).toBe('application/json');
26
- });
27
-
28
- it('should fetch with auth', async () => {
29
- const fetcher = new Fetcher('http/');
30
- // @ts-ignore
31
- global.fetch = jest.fn(() => ({
32
- ok: true,
33
- headers: new Headers(),
34
- text() {
35
- return '';
36
- },
37
- clone() {
38
- return { ...this };
39
- },
40
- }));
41
- fetcher.token = 'token';
42
- await fetcher.get('url');
43
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
44
- headers: expect.any(Headers),
45
- method: 'GET',
46
- });
47
- const headers = (global.fetch as jest.Mock).mock.calls[0][1].headers;
48
- expect(headers.get('Access-Control-Allow-Origin')).toBe('*');
49
- expect(headers.get('x-prismeai-token')).toBe('token');
50
- });
51
-
52
- it('should fetch with api key', async () => {
53
- const fetcher = new Fetcher('http/');
54
- // @ts-ignore
55
- global.fetch = jest.fn(() => ({
56
- ok: true,
57
- headers: new Headers(),
58
- text() {
59
- return '';
60
- },
61
- clone() {
62
- return { ...this };
63
- },
64
- }));
65
- fetcher.token = 'token';
66
- fetcher.apiKey = 'api-key';
67
- await fetcher.get('url');
68
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
69
- headers: expect.any(Headers),
70
- method: 'GET',
71
- });
72
- const headers = (global.fetch as jest.Mock).mock.calls[0][1].headers;
73
- expect(headers.get('Access-Control-Allow-Origin')).toBe('*');
74
- expect(headers.get('x-prismeai-token')).toBe('token');
75
- expect(headers.get('x-prismeai-api-key')).toBe('api-key');
76
- });
77
-
78
- it('should fail to fetch', async () => {
79
- const fetcher = new Fetcher('http/');
80
- // @ts-ignore
81
- global.fetch = jest.fn(() => ({
82
- ok: false,
83
- json: () => ({
84
- error: 'error',
85
- message: 'message',
86
- }),
87
- status: 400,
88
- }));
89
- try {
90
- await fetcher.get('url');
91
- } catch (e) {
92
- expect(e).toBeInstanceOf(ApiError);
93
- if (e instanceof ApiError) {
94
- expect(e.error).toBe('error');
95
- expect(e.message).toBe('message');
96
- }
97
- }
98
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
99
- headers: expect.any(Headers),
100
- method: 'GET',
101
- });
102
- });
103
-
104
- it('should fail to fetch with unformatted error', async () => {
105
- const fetcher = new Fetcher('http/');
106
- // @ts-ignore
107
- global.fetch = jest.fn(() => ({
108
- ok: false,
109
- statusText: 'error',
110
- status: 400,
111
- }));
112
- try {
113
- await fetcher.get('url');
114
- } catch (e) {
115
- expect(e).toBeInstanceOf(HTTPError);
116
- if (e instanceof ApiError) {
117
- expect(e.message).toBe('error');
118
- }
119
- }
120
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
121
- headers: expect.any(Headers),
122
- method: 'GET',
123
- });
124
- });
125
-
126
- it('should post', async () => {
127
- const fetcher = new Fetcher('http/');
128
- // @ts-ignore
129
- global.fetch = jest.fn(() => ({
130
- ok: true,
131
- headers: new Headers([['content-type', 'application/json']]),
132
- json() {
133
- return {};
134
- },
135
- clone() {
136
- return { ...this };
137
- },
138
- }));
139
- await fetcher.post('url');
140
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
141
- headers: expect.any(Headers),
142
- method: 'POST',
143
- });
144
- });
145
-
146
- it('should post with body', async () => {
147
- const fetcher = new Fetcher('http/');
148
- // @ts-ignore
149
- global.fetch = jest.fn(() => ({
150
- ok: true,
151
- headers: new Headers([['content-type', 'application/json']]),
152
- json() {
153
- return {};
154
- },
155
- clone() {
156
- return { ...this };
157
- },
158
- }));
159
- await fetcher.post('url', {});
160
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
161
- headers: expect.any(Headers),
162
- method: 'POST',
163
- body: '{}',
164
- });
165
- });
166
-
167
- it('should put', async () => {
168
- const fetcher = new Fetcher('http/');
169
- // @ts-ignore
170
- global.fetch = jest.fn(() => ({
171
- ok: true,
172
- headers: new Headers([['content-type', 'application/json']]),
173
- json() {
174
- return {};
175
- },
176
- clone() {
177
- return { ...this };
178
- },
179
- }));
180
- await fetcher.put('url', {});
181
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
182
- headers: expect.any(Headers),
183
- method: 'PUT',
184
- body: '{}',
185
- });
186
- });
187
-
188
- it('should patch', async () => {
189
- const fetcher = new Fetcher('http/');
190
- // @ts-ignore
191
- global.fetch = jest.fn(() => ({
192
- ok: true,
193
- headers: new Headers([['content-type', 'application/json']]),
194
- json() {
195
- return {};
196
- },
197
- clone() {
198
- return { ...this };
199
- },
200
- }));
201
- await fetcher.patch('url', {});
202
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
203
- headers: expect.any(Headers),
204
- method: 'PATCH',
205
- body: '{}',
206
- });
207
- });
208
-
209
- it('should delete', async () => {
210
- const fetcher = new Fetcher('http/');
211
- // @ts-ignore
212
- global.fetch = jest.fn(() => ({
213
- ok: true,
214
- headers: new Headers([['content-type', 'application/json']]),
215
- json() {
216
- return {};
217
- },
218
- clone() {
219
- return { ...this };
220
- },
221
- }));
222
- await fetcher.delete('url');
223
- expect(global.fetch).toHaveBeenCalledWith('http/url', {
224
- headers: expect.any(Headers),
225
- method: 'DELETE',
226
- });
227
- });
228
-
229
- it('should use formData', async () => {
230
- const fetcher = new Fetcher('http/');
231
- // @ts-ignore
232
- global.fetch = jest.fn(() => ({
233
- ok: true,
234
- headers: new Headers([['content-type', 'application/json']]),
235
- json() {
236
- return {};
237
- },
238
- clone() {
239
- return { ...this };
240
- },
241
- }));
242
- const formData = new FormData();
243
- await fetcher.post('url', formData);
244
- const headers = (global.fetch as jest.Mock).mock.calls[0][1].headers;
245
- expect(headers.has('Content-Type')).toBe(false);
246
- });