diff options
Diffstat (limited to 'src/features/workspaces/api.js')
-rw-r--r-- | src/features/workspaces/api.js | 51 |
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 @@ | |||
1 | import { pick } from 'lodash'; | 1 | import { pick } from 'lodash'; |
2 | import { sendAuthRequest } from '../../api/utils/auth'; | 2 | import { sendAuthRequest } from '../../api/utils/auth'; |
3 | import { API, API_VERSION } from '../../environment'; | 3 | import { API, API_VERSION } from '../../environment'; |
4 | import Request from '../../stores/lib/Request'; | ||
5 | import CachedRequest from '../../stores/lib/CachedRequest'; | ||
6 | import Workspace from './models/Workspace'; | ||
4 | 7 | ||
5 | export default { | 8 | const debug = require('debug')('Franz:feature:workspaces:api'); |
9 | |||
10 | export 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 | |||
57 | export const getUserWorkspacesRequest = new CachedRequest(workspaceApi, 'getUserWorkspaces'); | ||
58 | export const createWorkspaceRequest = new Request(workspaceApi, 'createWorkspace'); | ||
59 | export const deleteWorkspaceRequest = new Request(workspaceApi, 'deleteWorkspace'); | ||
60 | export const updateWorkspaceRequest = new Request(workspaceApi, 'updateWorkspace'); | ||