aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/workspaces/api.ts
blob: b8bb703cd5f3c250699253a935bdab14d28d35c2 (plain) (blame)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { pick } from 'lodash';
import { sendAuthRequest } from '../../api/utils/auth';
import Request from '../../stores/lib/Request';
import Workspace from './models/Workspace';
import apiBase from '../../api/apiBase';

const debug = require('../../preload-safe-debug')(
  'Ferdium:feature:workspaces:api',
);

export const workspaceApi = {
  getUserWorkspaces: async () => {
    const url = `${apiBase()}/workspace`;
    debug('getUserWorkspaces GET', url);
    const result = await sendAuthRequest(url, { method: 'GET' });
    debug('getUserWorkspaces RESULT', result);
    if (!result.ok) {
      throw new Error("Couldn't getUserWorkspaces");
    }
    const workspaces = await result.json();
    return workspaces.map(data => new Workspace(data));
  },

  createWorkspace: async name => {
    const url = `${apiBase()}/workspace`;
    const options = {
      method: 'POST',
      body: JSON.stringify({ name }),
    };
    debug('createWorkspace POST', url, options);
    const result = await sendAuthRequest(url, options);
    debug('createWorkspace RESULT', result);
    if (!result.ok) {
      throw new Error("Couldn't createWorkspace");
    }
    return new Workspace(await result.json());
  },

  deleteWorkspace: async workspace => {
    const url = `${apiBase()}/workspace/${workspace.id}`;
    debug('deleteWorkspace DELETE', url);
    const result = await sendAuthRequest(url, { method: 'DELETE' });
    debug('deleteWorkspace RESULT', result);
    if (!result.ok) {
      throw new Error("Couldn't deleteWorkspace");
    }
    return true;
  },

  updateWorkspace: async workspace => {
    const url = `${apiBase()}/workspace/${workspace.id}`;
    const options = {
      method: 'PUT',
      body: JSON.stringify(pick(workspace, ['name', 'services'])),
    };
    debug('updateWorkspace UPDATE', url, options);
    const result = await sendAuthRequest(url, options);
    debug('updateWorkspace RESULT', result);
    if (!result.ok) {
      throw new Error("Couldn't updateWorkspace");
    }
    return new Workspace(await result.json());
  },
};

export const getUserWorkspacesRequest = new Request(
  workspaceApi,
  'getUserWorkspaces',
);
export const createWorkspaceRequest = new Request(
  workspaceApi,
  'createWorkspace',
);
export const deleteWorkspaceRequest = new Request(
  workspaceApi,
  'deleteWorkspace',
);
export const updateWorkspaceRequest = new Request(
  workspaceApi,
  'updateWorkspace',
);

export const resetApiRequests = () => {
  getUserWorkspacesRequest.reset();
  createWorkspaceRequest.reset();
  deleteWorkspaceRequest.reset();
  updateWorkspaceRequest.reset();
};