aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/api.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/workspaces/api.js')
-rw-r--r--src/features/workspaces/api.js51
1 files changed, 36 insertions, 15 deletions
diff --git a/src/features/workspaces/api.js b/src/features/workspaces/api.js
index 733cb5593..4e076d233 100644
--- a/src/features/workspaces/api.js
+++ b/src/features/workspaces/api.js
@@ -1,39 +1,60 @@
1import { pick } from 'lodash'; 1import { pick } from 'lodash';
2import { sendAuthRequest } from '../../api/utils/auth'; 2import { sendAuthRequest } from '../../api/utils/auth';
3import { API, API_VERSION } from '../../environment'; 3import { API, API_VERSION } from '../../environment';
4import Request from '../../stores/lib/Request';
5import CachedRequest from '../../stores/lib/CachedRequest';
6import Workspace from './models/Workspace';
4 7
5export default { 8const debug = require('debug')('Franz:feature:workspaces:api');
9
10export const workspaceApi = {
6 getUserWorkspaces: async () => { 11 getUserWorkspaces: async () => {
7 const url = `${API}/${API_VERSION}/workspace`; 12 const url = `${API}/${API_VERSION}/workspace`;
8 const request = await sendAuthRequest(url, { method: 'GET' }); 13 debug('getUserWorkspaces GET', url);
9 if (!request.ok) throw request; 14 const result = await sendAuthRequest(url, { method: 'GET' });
10 return request.json(); 15 debug('getUserWorkspaces RESULT', result);
16 if (!result.ok) throw result;
17 const workspaces = await result.json();
18 return workspaces.map(data => new Workspace(data));
11 }, 19 },
12 20
13 createWorkspace: async (name) => { 21 createWorkspace: async (name) => {
14 const url = `${API}/${API_VERSION}/workspace`; 22 const url = `${API}/${API_VERSION}/workspace`;
15 const request = await sendAuthRequest(url, { 23 const options = {
16 method: 'POST', 24 method: 'POST',
17 body: JSON.stringify({ name }), 25 body: JSON.stringify({ name }),
18 }); 26 };
19 if (!request.ok) throw request; 27 debug('createWorkspace POST', url, options);
20 return request.json(); 28 const result = await sendAuthRequest(url, options);
29 debug('createWorkspace RESULT', result);
30 if (!result.ok) throw result;
31 return new Workspace(await result.json());
21 }, 32 },
22 33
23 deleteWorkspace: async (workspace) => { 34 deleteWorkspace: async (workspace) => {
24 const url = `${API}/${API_VERSION}/workspace/${workspace.id}`; 35 const url = `${API}/${API_VERSION}/workspace/${workspace.id}`;
25 const request = await sendAuthRequest(url, { method: 'DELETE' }); 36 debug('deleteWorkspace DELETE', url);
26 if (!request.ok) throw request; 37 const result = await sendAuthRequest(url, { method: 'DELETE' });
27 return request.json(); 38 debug('deleteWorkspace RESULT', result);
39 if (!result.ok) throw result;
40 return (await result.json()).deleted;
28 }, 41 },
29 42
30 updateWorkspace: async (workspace) => { 43 updateWorkspace: async (workspace) => {
31 const url = `${API}/${API_VERSION}/workspace/${workspace.id}`; 44 const url = `${API}/${API_VERSION}/workspace/${workspace.id}`;
32 const request = await sendAuthRequest(url, { 45 const options = {
33 method: 'PUT', 46 method: 'PUT',
34 body: JSON.stringify(pick(workspace, ['name', 'services'])), 47 body: JSON.stringify(pick(workspace, ['name', 'services'])),
35 }); 48 };
36 if (!request.ok) throw request; 49 debug('updateWorkspace UPDATE', url, options);
37 return request.json(); 50 const result = await sendAuthRequest(url, options);
51 debug('updateWorkspace RESULT', result);
52 if (!result.ok) throw result;
53 return new Workspace(await result.json());
38 }, 54 },
39}; 55};
56
57export const getUserWorkspacesRequest = new CachedRequest(workspaceApi, 'getUserWorkspaces');
58export const createWorkspaceRequest = new Request(workspaceApi, 'createWorkspace');
59export const deleteWorkspaceRequest = new Request(workspaceApi, 'deleteWorkspace');
60export const updateWorkspaceRequest = new Request(workspaceApi, 'updateWorkspace');