diff options
author | 2021-09-18 11:15:25 +0200 | |
---|---|---|
committer | 2021-09-18 11:15:25 +0200 | |
commit | d4101a48b3eee8b1fb177831aa02a4b4fbec2588 (patch) | |
tree | c92f2fbe91197fde8589207463d0d6526b4ff76b /src/features/workspaces/api.ts | |
parent | 5.6.3-nightly.6 [skip ci] (diff) | |
download | ferdium-app-d4101a48b3eee8b1fb177831aa02a4b4fbec2588.tar.gz ferdium-app-d4101a48b3eee8b1fb177831aa02a4b4fbec2588.tar.zst ferdium-app-d4101a48b3eee8b1fb177831aa02a4b4fbec2588.zip |
chore: convert various files from JS to TS (#1959)
Diffstat (limited to 'src/features/workspaces/api.ts')
-rw-r--r-- | src/features/workspaces/api.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/features/workspaces/api.ts b/src/features/workspaces/api.ts new file mode 100644 index 000000000..8447fc247 --- /dev/null +++ b/src/features/workspaces/api.ts | |||
@@ -0,0 +1,86 @@ | |||
1 | import { pick } from 'lodash'; | ||
2 | import { sendAuthRequest } from '../../api/utils/auth'; | ||
3 | import Request from '../../stores/lib/Request'; | ||
4 | import Workspace from './models/Workspace'; | ||
5 | import apiBase from '../../api/apiBase'; | ||
6 | |||
7 | const debug = require('debug')('Ferdi:feature:workspaces:api'); | ||
8 | |||
9 | export const workspaceApi = { | ||
10 | getUserWorkspaces: async () => { | ||
11 | const url = `${apiBase()}/workspace`; | ||
12 | debug('getUserWorkspaces GET', url); | ||
13 | const result = await sendAuthRequest(url, { method: 'GET' }); | ||
14 | debug('getUserWorkspaces RESULT', result); | ||
15 | if (!result.ok) { | ||
16 | throw new Error("Couldn't getUserWorkspaces"); | ||
17 | } | ||
18 | const workspaces = await result.json(); | ||
19 | return workspaces.map(data => new Workspace(data)); | ||
20 | }, | ||
21 | |||
22 | createWorkspace: async name => { | ||
23 | const url = `${apiBase()}/workspace`; | ||
24 | const options = { | ||
25 | method: 'POST', | ||
26 | body: JSON.stringify({ name }), | ||
27 | }; | ||
28 | debug('createWorkspace POST', url, options); | ||
29 | const result = await sendAuthRequest(url, options); | ||
30 | debug('createWorkspace RESULT', result); | ||
31 | if (!result.ok) { | ||
32 | throw new Error("Couldn't createWorkspace"); | ||
33 | } | ||
34 | return new Workspace(await result.json()); | ||
35 | }, | ||
36 | |||
37 | deleteWorkspace: async workspace => { | ||
38 | const url = `${apiBase()}/workspace/${workspace.id}`; | ||
39 | debug('deleteWorkspace DELETE', url); | ||
40 | const result = await sendAuthRequest(url, { method: 'DELETE' }); | ||
41 | debug('deleteWorkspace RESULT', result); | ||
42 | if (!result.ok) { | ||
43 | throw new Error("Couldn't deleteWorkspace"); | ||
44 | } | ||
45 | return true; | ||
46 | }, | ||
47 | |||
48 | updateWorkspace: async workspace => { | ||
49 | const url = `${apiBase()}/workspace/${workspace.id}`; | ||
50 | const options = { | ||
51 | method: 'PUT', | ||
52 | body: JSON.stringify(pick(workspace, ['name', 'services'])), | ||
53 | }; | ||
54 | debug('updateWorkspace UPDATE', url, options); | ||
55 | const result = await sendAuthRequest(url, options); | ||
56 | debug('updateWorkspace RESULT', result); | ||
57 | if (!result.ok) { | ||
58 | throw new Error("Couldn't updateWorkspace"); | ||
59 | } | ||
60 | return new Workspace(await result.json()); | ||
61 | }, | ||
62 | }; | ||
63 | |||
64 | export const getUserWorkspacesRequest = new Request( | ||
65 | workspaceApi, | ||
66 | 'getUserWorkspaces', | ||
67 | ); | ||
68 | export const createWorkspaceRequest = new Request( | ||
69 | workspaceApi, | ||
70 | 'createWorkspace', | ||
71 | ); | ||
72 | export const deleteWorkspaceRequest = new Request( | ||
73 | workspaceApi, | ||
74 | 'deleteWorkspace', | ||
75 | ); | ||
76 | export const updateWorkspaceRequest = new Request( | ||
77 | workspaceApi, | ||
78 | 'updateWorkspace', | ||
79 | ); | ||
80 | |||
81 | export const resetApiRequests = () => { | ||
82 | getUserWorkspacesRequest.reset(); | ||
83 | createWorkspaceRequest.reset(); | ||
84 | deleteWorkspaceRequest.reset(); | ||
85 | updateWorkspaceRequest.reset(); | ||
86 | }; | ||