-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.api.ts
More file actions
58 lines (52 loc) · 1.2 KB
/
sync.api.ts
File metadata and controls
58 lines (52 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { AdminApi } from './admin.api';
import { Exception } from '../exception';
export type SyncOperatorPayload = {
entity: string;
action: SYNC_OPERATOR;
payload: Record<string, any>;
};
export enum SYNC_OPERATOR {
UPSERT = 'upsert',
DELETE = 'delete',
}
export class SyncPayload extends Map<string, SyncOperatorPayload> {
setOperator(
entity: string,
action: SYNC_OPERATOR,
payload: Record<string, any>,
): this {
if (!Object.values(SYNC_OPERATOR).includes(action)) {
throw new Exception(
`Action ${action} is not allowed, allowed types: upsert, delete`,
);
}
return this.set(`${entity}-${action}`, {
action,
entity,
payload,
});
}
parse(): {
[k: string]: SyncOperatorPayload;
} {
return Object.fromEntries(this);
}
}
export class SyncApi extends AdminApi {
sync(
payload: SyncPayload,
additionalPayload: Record<string, any> = {},
additionalHeaders: Record<string, any> = {},
): Promise<any> {
return this.post(
'/_action/sync',
{
...payload.parse(),
...additionalPayload,
},
{
headers: this.getBasicHeaders(additionalHeaders),
},
);
}
}