aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/workspaces/api.ts')
-rw-r--r--src/features/workspaces/api.ts86
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 @@
1import { pick } from 'lodash';
2import { sendAuthRequest } from '../../api/utils/auth';
3import Request from '../../stores/lib/Request';
4import Workspace from './models/Workspace';
5import apiBase from '../../api/apiBase';
6
7const debug = require('debug')('Ferdi:feature:workspaces:api');
8
9export 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
64export const getUserWorkspacesRequest = new Request(
65 workspaceApi,
66 'getUserWorkspaces',
67);
68export const createWorkspaceRequest = new Request(
69 workspaceApi,
70 'createWorkspace',
71);
72export const deleteWorkspaceRequest = new Request(
73 workspaceApi,
74 'deleteWorkspace',
75);
76export const updateWorkspaceRequest = new Request(
77 workspaceApi,
78 'updateWorkspace',
79);
80
81export const resetApiRequests = () => {
82 getUserWorkspacesRequest.reset();
83 createWorkspaceRequest.reset();
84 deleteWorkspaceRequest.reset();
85 updateWorkspaceRequest.reset();
86};