-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathjetstream-data-sync.desktop.controller.ts
More file actions
65 lines (59 loc) · 1.86 KB
/
jetstream-data-sync.desktop.controller.ts
File metadata and controls
65 lines (59 loc) · 1.86 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
59
60
61
62
63
64
65
import { HTTP } from '@jetstream/shared/constants';
import { app } from 'electron';
import { z } from 'zod';
import { ENV } from '../config/environment';
import { createRoute, getTokens, handleErrorResponse, RouteValidator } from '../utils/route.utils';
/**
* FIXME: this file needs to be worked on
*/
export const routeDefinition = {
pull: {
controllerFn: () => pull,
validators: {
query: z.any(),
hasSourceOrg: false,
} satisfies RouteValidator,
},
push: {
controllerFn: () => push,
validators: {
query: z.any(),
body: z.any(),
hasSourceOrg: false,
} satisfies RouteValidator,
},
};
const pull = createRoute(routeDefinition.pull.validators, async ({ query }) => {
try {
const { authTokens, extIdentifier } = getTokens();
return await fetch(`${ENV.SERVER_URL}/desktop-app/data-sync/pull?${new URLSearchParams(query).toString()}`, {
method: 'GET',
headers: {
Accept: 'application/json',
Authorization: `Bearer ${authTokens?.accessToken}`,
[HTTP.HEADERS.X_EXT_DEVICE_ID]: extIdentifier.id,
[HTTP.HEADERS.X_APP_VERSION]: app.getVersion(),
},
});
} catch (ex) {
return handleErrorResponse(ex);
}
});
const push = createRoute(routeDefinition.push.validators, async ({ query, body }) => {
try {
const { authTokens, extIdentifier } = getTokens();
return await fetch(`${ENV.SERVER_URL}/desktop-app/data-sync/push?${new URLSearchParams(query).toString()}`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${authTokens?.accessToken}`,
[HTTP.HEADERS.X_EXT_DEVICE_ID]: extIdentifier.id,
[HTTP.HEADERS.X_APP_VERSION]: app.getVersion(),
},
body: JSON.stringify(body),
});
} catch (ex) {
return handleErrorResponse(ex);
}
});