@prisme.ai/sdk 1.0.2 → 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.
- package/README.md +250 -0
- package/dist/index.d.mts +1129 -0
- package/dist/index.d.ts +1129 -7
- package/dist/index.js +1326 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1287 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +50 -12
- package/Readme.md +0 -28
- package/dist/_virtual/_tslib.js +0 -116
- package/dist/lib/ApiError.d.ts +0 -8
- package/dist/lib/ApiError.test.d.ts +0 -1
- package/dist/lib/HTTPError.d.ts +0 -6
- package/dist/lib/HTTPError.test.d.ts +0 -1
- package/dist/lib/ImportProcessing.d.ts +0 -19
- package/dist/lib/WorkspacesEndpoint.d.ts +0 -10
- package/dist/lib/api.d.ts +0 -155
- package/dist/lib/api.test.d.ts +0 -1
- package/dist/lib/endpoints/users.d.ts +0 -13
- package/dist/lib/endpoints/workspaces.d.ts +0 -25
- package/dist/lib/endpoints/workspacesVersions.d.ts +0 -10
- package/dist/lib/events.d.ts +0 -40
- package/dist/lib/events.test.d.ts +0 -1
- package/dist/lib/fetch.d.ts +0 -2
- package/dist/lib/fetcher.d.ts +0 -24
- package/dist/lib/fetcher.test.d.ts +0 -1
- package/dist/lib/interpolate.d.ts +0 -2
- package/dist/lib/interpolate.test.d.ts +0 -1
- package/dist/lib/interpolateVars.d.ts +0 -2
- package/dist/lib/types.d.ts +0 -17
- package/dist/lib/utils.d.ts +0 -4
- package/dist/lib/utils.test.d.ts +0 -1
- package/dist/sdk/index.js +0 -19
- package/dist/sdk/lib/ApiError.js +0 -24
- package/dist/sdk/lib/HTTPError.js +0 -21
- package/dist/sdk/lib/ImportProcessing.js +0 -17
- package/dist/sdk/lib/WorkspacesEndpoint.js +0 -19
- package/dist/sdk/lib/api.js +0 -1018
- package/dist/sdk/lib/endpoints/users.js +0 -94
- package/dist/sdk/lib/endpoints/workspaces.js +0 -144
- package/dist/sdk/lib/endpoints/workspacesVersions.js +0 -40
- package/dist/sdk/lib/events.js +0 -169
- package/dist/sdk/lib/fetch.js +0 -12
- package/dist/sdk/lib/fetcher.js +0 -213
- package/dist/sdk/lib/interpolate.js +0 -26
- package/dist/sdk/lib/interpolateVars.js +0 -26
- package/dist/sdk/lib/utils.js +0 -65
- package/index.ts +0 -7
- package/lib/ApiError.test.ts +0 -13
- package/lib/ApiError.ts +0 -21
- package/lib/HTTPError.test.ts +0 -8
- package/lib/HTTPError.ts +0 -13
- package/lib/ImportProcessing.ts +0 -22
- package/lib/api.test.ts +0 -787
- package/lib/api.ts +0 -949
- package/lib/endpoints/users.ts +0 -58
- package/lib/endpoints/workspaces.ts +0 -121
- package/lib/endpoints/workspacesVersions.ts +0 -38
- package/lib/events.test.ts +0 -89
- package/lib/events.ts +0 -222
- package/lib/fetcher.test.ts +0 -246
- package/lib/fetcher.ts +0 -198
- package/lib/types.ts +0 -21
- package/lib/utils.test.ts +0 -38
- package/lib/utils.ts +0 -51
- package/tsconfig.json +0 -21
package/lib/endpoints/users.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import { Api } from '../api';
|
|
2
|
-
import { dataURItoBlob, isDataURL } from '../utils';
|
|
3
|
-
|
|
4
|
-
export class UsersEndpoint {
|
|
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
|
-
async update(
|
|
14
|
-
data: Partial<Prismeai.User>
|
|
15
|
-
): Promise<PrismeaiAPI.PatchUser.Responses.$200> {
|
|
16
|
-
if (isDataURL(data.photo)) {
|
|
17
|
-
await this.updatePhoto(data.photo);
|
|
18
|
-
delete data.photo;
|
|
19
|
-
}
|
|
20
|
-
return await this.api.patch('/user', data);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async updatePhoto(
|
|
24
|
-
photo: string
|
|
25
|
-
): Promise<PrismeaiAPI.PostUserPhoto.Responses.$200> {
|
|
26
|
-
if (!isDataURL(photo)) {
|
|
27
|
-
throw new Error('Photo must be a dataurl file');
|
|
28
|
-
}
|
|
29
|
-
const formData = new FormData();
|
|
30
|
-
formData.append('photo', ...dataURItoBlob(photo));
|
|
31
|
-
|
|
32
|
-
return await this.api.post<PrismeaiAPI.PostUserPhoto.Responses.$200>(
|
|
33
|
-
`/user/photo`,
|
|
34
|
-
formData
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
async setMeta(
|
|
39
|
-
k: string,
|
|
40
|
-
v: any
|
|
41
|
-
): Promise<PrismeaiAPI.SetMeta.Responses.$200> {
|
|
42
|
-
return await this.api.post(`/user/meta`, {
|
|
43
|
-
[k]: v,
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
async deleteMeta(k: string): Promise<PrismeaiAPI.DeleteMeta.Responses.$200> {
|
|
47
|
-
return await this.api.delete(`/user/meta/${k}`);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
async sendDeleteValidation() {
|
|
51
|
-
return await this.api.delete(`/user`);
|
|
52
|
-
}
|
|
53
|
-
async delete(token?: string) {
|
|
54
|
-
return await this.api.delete(`/users/${this.id}?token=${token}`);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export default UsersEndpoint;
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { Api } from '../api';
|
|
2
|
-
import { Fetched } from '../fetcher';
|
|
3
|
-
import {
|
|
4
|
-
ImportProcessing,
|
|
5
|
-
ImportProcessingError,
|
|
6
|
-
ImportSuccess,
|
|
7
|
-
} from '../ImportProcessing';
|
|
8
|
-
import { dataURItoBlob, removedUndefinedProperties } from '../utils';
|
|
9
|
-
import WorkspacesVersionsEndpoint from './workspacesVersions';
|
|
10
|
-
|
|
11
|
-
export class WorkspacesEndpoint {
|
|
12
|
-
private id: string;
|
|
13
|
-
private api: Api;
|
|
14
|
-
|
|
15
|
-
constructor(id: string, api: Api) {
|
|
16
|
-
this.id = id;
|
|
17
|
-
this.api = api;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
get versions() {
|
|
21
|
-
return new WorkspacesVersionsEndpoint(this.id, this.api);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
async update(
|
|
25
|
-
workspace: Prismeai.DSULPatch
|
|
26
|
-
): Promise<Fetched<PrismeaiAPI.UpdateWorkspace.Responses.$200> | null> {
|
|
27
|
-
return await this.api.patch(
|
|
28
|
-
`/workspaces/${this.id}`,
|
|
29
|
-
await this.api.replaceAllImagesData(workspace, this.id)
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
async delete(): Promise<PrismeaiAPI.DeleteWorkspace.Responses.$200> {
|
|
34
|
-
return await this.api.delete(`/workspaces/${this.id}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
async uploadFiles(
|
|
38
|
-
files: string | string[],
|
|
39
|
-
opts?: {
|
|
40
|
-
expiresAfter?: number;
|
|
41
|
-
public?: boolean;
|
|
42
|
-
shareToken?: boolean;
|
|
43
|
-
}
|
|
44
|
-
) {
|
|
45
|
-
const formData = new FormData();
|
|
46
|
-
(Array.isArray(files) ? files : [files]).forEach((file) => {
|
|
47
|
-
try {
|
|
48
|
-
formData.append('file', ...dataURItoBlob(file));
|
|
49
|
-
} catch {}
|
|
50
|
-
});
|
|
51
|
-
if (opts?.expiresAfter) {
|
|
52
|
-
formData.append('expiresAfter', `${opts?.expiresAfter}`);
|
|
53
|
-
}
|
|
54
|
-
if (typeof opts?.public === 'boolean') {
|
|
55
|
-
formData.append('public', `${opts?.public}`);
|
|
56
|
-
}
|
|
57
|
-
if (typeof opts?.shareToken === 'boolean') {
|
|
58
|
-
formData.append('shareToken', `${opts?.shareToken}`);
|
|
59
|
-
}
|
|
60
|
-
try {
|
|
61
|
-
return await this.api.post<PrismeaiAPI.UploadFile.Responses.$200>(
|
|
62
|
-
`/workspaces/${this.id}/files`,
|
|
63
|
-
formData
|
|
64
|
-
);
|
|
65
|
-
} catch (e) {
|
|
66
|
-
console.error(e);
|
|
67
|
-
}
|
|
68
|
-
return [];
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async listAppInstances(): Promise<
|
|
72
|
-
Fetched<PrismeaiAPI.ListAppInstances.Responses.$200>
|
|
73
|
-
> {
|
|
74
|
-
return await this.api.get(`/workspaces/${this.id}/apps`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async getUsage({
|
|
78
|
-
afterDate,
|
|
79
|
-
beforeDate,
|
|
80
|
-
details,
|
|
81
|
-
}: {
|
|
82
|
-
afterDate?: PrismeaiAPI.WorkspaceUsage.Parameters.AfterDate;
|
|
83
|
-
beforeDate?: PrismeaiAPI.WorkspaceUsage.Parameters.BeforeDate;
|
|
84
|
-
details?: PrismeaiAPI.WorkspaceUsage.Parameters.Details;
|
|
85
|
-
} = {}): Promise<Fetched<PrismeaiAPI.WorkspaceUsage.Responses.$200>> {
|
|
86
|
-
const params = new URLSearchParams(
|
|
87
|
-
removedUndefinedProperties(
|
|
88
|
-
{
|
|
89
|
-
afterDate: `${afterDate || ''}`,
|
|
90
|
-
beforeDate: `${beforeDate || ''}`,
|
|
91
|
-
details: `${details || ''}`,
|
|
92
|
-
},
|
|
93
|
-
true
|
|
94
|
-
)
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
return this.api.get(`/workspaces/${this.id}/usage?${params.toString()}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async importArchive(archive: File): Promise<ImportSuccess> {
|
|
101
|
-
return new Promise((resolve) => {
|
|
102
|
-
const fileReader = new FileReader();
|
|
103
|
-
fileReader.addEventListener('load', async ({ target }) => {
|
|
104
|
-
const file = target?.result as string;
|
|
105
|
-
const formData = new FormData();
|
|
106
|
-
formData.append('archive', ...dataURItoBlob(file));
|
|
107
|
-
const result = await this.api.post<ImportProcessing | ImportSuccess>(
|
|
108
|
-
`/workspaces/${this.id}/import`,
|
|
109
|
-
formData
|
|
110
|
-
);
|
|
111
|
-
if ((result as ImportProcessing).processing) {
|
|
112
|
-
throw new ImportProcessingError(result as ImportProcessing);
|
|
113
|
-
}
|
|
114
|
-
resolve(result as ImportSuccess);
|
|
115
|
-
});
|
|
116
|
-
fileReader.readAsDataURL(archive);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export default WorkspacesEndpoint;
|
|
@@ -1,38 +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
|
-
return this.api.post(`/workspaces/${this.workspaceId}/versions`, version);
|
|
14
|
-
}
|
|
15
|
-
rollback(
|
|
16
|
-
versionId: PrismeaiAPI.PullWorkspaceVersion.PathParameters['versionId'],
|
|
17
|
-
opts?: PrismeaiAPI.PullWorkspaceVersion.RequestBody
|
|
18
|
-
) {
|
|
19
|
-
return this.api.post(
|
|
20
|
-
`/workspaces/${this.workspaceId}/versions/${versionId}/pull`,
|
|
21
|
-
opts
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async export(
|
|
26
|
-
version: PrismeaiAPI.ExportWorkspaceVersion.Parameters.VersionId = 'current'
|
|
27
|
-
) {
|
|
28
|
-
const res = await this.api.prepareRequest(
|
|
29
|
-
`/workspaces/${this.workspaceId}/versions/${version}/export`,
|
|
30
|
-
{
|
|
31
|
-
method: 'post',
|
|
32
|
-
}
|
|
33
|
-
);
|
|
34
|
-
return new Blob([await res.arrayBuffer()], { type: 'application/zip' });
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export default WorkspacesVersionsEndpoint;
|
package/lib/events.test.ts
DELETED
|
@@ -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,222 +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
|
-
private socketId?: string;
|
|
24
|
-
|
|
25
|
-
constructor({
|
|
26
|
-
workspaceId,
|
|
27
|
-
token,
|
|
28
|
-
legacyToken,
|
|
29
|
-
apiKey,
|
|
30
|
-
apiHost = 'https://api.eda.prisme.ai',
|
|
31
|
-
filters,
|
|
32
|
-
api,
|
|
33
|
-
transports,
|
|
34
|
-
}: {
|
|
35
|
-
workspaceId: string;
|
|
36
|
-
token: string;
|
|
37
|
-
legacyToken?: string;
|
|
38
|
-
apiKey?: string;
|
|
39
|
-
apiHost?: string;
|
|
40
|
-
filters?: Record<string, any>;
|
|
41
|
-
api: Api;
|
|
42
|
-
transports?: string[];
|
|
43
|
-
}) {
|
|
44
|
-
this.workspaceId = workspaceId;
|
|
45
|
-
const queryString = new URLSearchParams(filters || {}).toString();
|
|
46
|
-
const fullQueryString = queryString ? `?${queryString}` : '';
|
|
47
|
-
const extraHeaders: any = token
|
|
48
|
-
? {
|
|
49
|
-
authorization: `Bearer ${token}`,
|
|
50
|
-
}
|
|
51
|
-
: { 'x-prismeai-token': legacyToken };
|
|
52
|
-
this.lastReceivedEventDate = new Date();
|
|
53
|
-
if (apiKey) {
|
|
54
|
-
extraHeaders['x-prismeai-api-key'] = apiKey;
|
|
55
|
-
}
|
|
56
|
-
this.filters = [filters || {}];
|
|
57
|
-
this.listenedUserTopics = new Map();
|
|
58
|
-
|
|
59
|
-
this.client = io(
|
|
60
|
-
`${apiHost}/workspaces/${workspaceId}/events${fullQueryString}`,
|
|
61
|
-
{
|
|
62
|
-
extraHeaders,
|
|
63
|
-
withCredentials: !extraHeaders.authorization,
|
|
64
|
-
transports: transports || ['polling', 'websocket'],
|
|
65
|
-
auth: (cb) => {
|
|
66
|
-
cb({
|
|
67
|
-
// Browser websockets cannot send extraHeaders, so we use socketio-client auth instead
|
|
68
|
-
extraHeaders:
|
|
69
|
-
transports && transports[0] === 'websocket' ? extraHeaders : {},
|
|
70
|
-
|
|
71
|
-
filters: {
|
|
72
|
-
payloadQuery: this.filters,
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
reuseSocketId: this.socketId,
|
|
76
|
-
});
|
|
77
|
-
},
|
|
78
|
-
}
|
|
79
|
-
);
|
|
80
|
-
|
|
81
|
-
this.client.on('connect_error', (err) => {
|
|
82
|
-
console.error(`Failed websocket connection : `, err);
|
|
83
|
-
// revert to classic upgrade
|
|
84
|
-
this.client.io.opts.transports = ['polling', 'websocket'];
|
|
85
|
-
});
|
|
86
|
-
this.client.on('error', (err) => {
|
|
87
|
-
this.client.io.opts.transports = ['polling', 'websocket'];
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
const onConnect = () => {
|
|
91
|
-
// First connection
|
|
92
|
-
if (!this.socketId) {
|
|
93
|
-
this.socketId = this.client.id;
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// Retrieve lost history on reconnection
|
|
98
|
-
setTimeout(async () => {
|
|
99
|
-
const events = await api.getEvents(workspaceId, {
|
|
100
|
-
...this.filters[this.filters.length - 1],
|
|
101
|
-
afterDate: this.lastReceivedEventDate.toISOString(),
|
|
102
|
-
});
|
|
103
|
-
events.reverse().forEach((event) => {
|
|
104
|
-
(this.listeners.get(event.type) || []).forEach((listener) =>
|
|
105
|
-
listener(event)
|
|
106
|
-
);
|
|
107
|
-
});
|
|
108
|
-
}, 2000);
|
|
109
|
-
};
|
|
110
|
-
this.client.on('connect', onConnect);
|
|
111
|
-
|
|
112
|
-
this.client.on('disconnect', () => {
|
|
113
|
-
if (!this.lastReceivedEventDate) {
|
|
114
|
-
this.lastReceivedEventDate = new Date();
|
|
115
|
-
}
|
|
116
|
-
// Make sure we reconnect with current filters & socketId
|
|
117
|
-
this.client.auth = {
|
|
118
|
-
...this.client.auth,
|
|
119
|
-
filters: {
|
|
120
|
-
payloadQuery: this.filters,
|
|
121
|
-
},
|
|
122
|
-
reuseSocketId: this.socketId,
|
|
123
|
-
};
|
|
124
|
-
this.client.off('connect', onConnect);
|
|
125
|
-
this.client.on('connect', onConnect);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
get socket() {
|
|
130
|
-
return this.client;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
destroy() {
|
|
134
|
-
this.workspaceId = '';
|
|
135
|
-
|
|
136
|
-
if (this.client.connected) {
|
|
137
|
-
this.client.disconnect();
|
|
138
|
-
return;
|
|
139
|
-
}
|
|
140
|
-
this.client.once('connect', () => {
|
|
141
|
-
this.client.disconnect();
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
all(listener: (eventName: string, eventData: Prismeai.PrismeEvent) => void) {
|
|
146
|
-
const anyListener = (
|
|
147
|
-
eventName: string,
|
|
148
|
-
eventData: Prismeai.PrismeEvent
|
|
149
|
-
) => {
|
|
150
|
-
this.lastReceivedEventDate = new Date(eventData?.createdAt);
|
|
151
|
-
return listener(eventName, eventData);
|
|
152
|
-
};
|
|
153
|
-
this.client.onAny(anyListener);
|
|
154
|
-
|
|
155
|
-
return () => this.client.offAny(anyListener);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
on(ev: string, listener: (eventData: Prismeai.PrismeEvent) => void) {
|
|
159
|
-
this.listeners.set(ev, [...(this.listeners.get(ev) || []), listener]);
|
|
160
|
-
|
|
161
|
-
this.client.on(ev, listener);
|
|
162
|
-
return () => {
|
|
163
|
-
this.listeners.set(
|
|
164
|
-
ev,
|
|
165
|
-
(this.listeners.get(ev) || []).filter((l) => l !== listener)
|
|
166
|
-
);
|
|
167
|
-
this.client.off(ev, listener);
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
emit(event: string, payload?: any, options?: any) {
|
|
172
|
-
this.client.emit('event', {
|
|
173
|
-
type: event,
|
|
174
|
-
payload,
|
|
175
|
-
options,
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
listenTopics({
|
|
180
|
-
event,
|
|
181
|
-
topics,
|
|
182
|
-
}: {
|
|
183
|
-
event: string;
|
|
184
|
-
topics: string | string[];
|
|
185
|
-
}) {
|
|
186
|
-
topics = Array.isArray(topics) ? topics : [topics];
|
|
187
|
-
|
|
188
|
-
this.listenedUserTopics.set(event, topics);
|
|
189
|
-
|
|
190
|
-
this.filters = [
|
|
191
|
-
{ ...this.filters[0] },
|
|
192
|
-
{
|
|
193
|
-
'target.userTopic': Array.from(this.listenedUserTopics).flatMap(
|
|
194
|
-
([_event, topics]) => topics
|
|
195
|
-
),
|
|
196
|
-
},
|
|
197
|
-
];
|
|
198
|
-
this.updateFilters({
|
|
199
|
-
payloadQuery: this.filters,
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
updateFilters(filters: SearchOptions) {
|
|
204
|
-
this.client.emit('filters', filters);
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
once(
|
|
208
|
-
ev: string,
|
|
209
|
-
listener: (eventName: string, eventData: Prismeai.PrismeEvent) => void
|
|
210
|
-
) {
|
|
211
|
-
this.client.once(ev, listener);
|
|
212
|
-
return () => {
|
|
213
|
-
this.client.off(ev, listener);
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
close() {
|
|
218
|
-
this.client.close();
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
export default Events;
|