1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _params = Joi.string().min(1);
|
6 | const _id = Joi.number().positive();
|
7 | const _ids = Joi.string().min(3);
|
8 | const _data = Joi.object();
|
9 |
|
10 |
|
11 | module.exports = (options = {}) => {
|
12 | const { error } = validate(options);
|
13 | if (error) throw new Error(error.details[0].message);
|
14 |
|
15 | const { url, headers } = prepare(options);
|
16 |
|
17 | return {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | list: (options = {}) => {
|
25 | const { error } = Joi.object({
|
26 | params: _params
|
27 | }).validate(options);
|
28 | if (error) throw new Error(error.details[0].message);
|
29 |
|
30 | const { params = '' } = options;
|
31 | const paramStr = params ? `?${params}` : '';
|
32 | return {
|
33 | method: 'GET',
|
34 | url: `${url}/api/v2/workspaces.json${paramStr}`,
|
35 | headers
|
36 | };
|
37 | },
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | show: (options = {}) => {
|
46 | const { error } = Joi.object({
|
47 | id: _id.required()
|
48 | }).validate(options);
|
49 | if (error) throw new Error(error.details[0].message);
|
50 |
|
51 | const { id } = options;
|
52 | return {
|
53 | method: 'GET',
|
54 | url: `${url}/api/v2/workspaces/${id}.json`,
|
55 | headers
|
56 | };
|
57 | },
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | create: (options = {}) => {
|
66 | const { error } = Joi.object({
|
67 | data: _data.required()
|
68 | }).validate(options);
|
69 | if (error) throw new Error(error.details[0].message);
|
70 |
|
71 | const { data } = options;
|
72 | return {
|
73 | method: 'POST',
|
74 | url: `${url}/api/v2/workspaces.json`,
|
75 | headers,
|
76 | data
|
77 | };
|
78 | },
|
79 |
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | update: (options = {}) => {
|
87 | const { error } = Joi.object({
|
88 | id: _id.required(),
|
89 | data: _data.required()
|
90 | }).validate(options);
|
91 | if (error) throw new Error(error.details[0].message);
|
92 |
|
93 | const { id, data } = options;
|
94 | return {
|
95 | method: 'PUT',
|
96 | url: `${url}/api/v2/workspaces/${id}.json`,
|
97 | headers,
|
98 | data
|
99 | };
|
100 | },
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 | delete: (options = {}) => {
|
109 | const { error } = Joi.object({
|
110 | id: _id.required()
|
111 | }).validate(options);
|
112 | if (error) throw new Error(error.details[0].message);
|
113 |
|
114 | const { id } = options;
|
115 | return {
|
116 | method: 'DELETE',
|
117 | url: `${url}/api/v2/workspaces/${id}.json`,
|
118 | headers
|
119 | };
|
120 | },
|
121 |
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | delete_bulk: (options = {}) => {
|
129 | const { error } = Joi.object({
|
130 | ids: _ids.required()
|
131 | }).validate(options);
|
132 | if (error) throw new Error(error.details[0].message);
|
133 |
|
134 | const { ids } = options;
|
135 | return {
|
136 | method: 'DELETE',
|
137 | url: `${url}/api/v2/workspaces/destroy_many.json?ids=${ids}`,
|
138 | headers
|
139 | };
|
140 | },
|
141 |
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | reorder: (options = {}) => {
|
149 | const { error } = Joi.object({
|
150 | data: _data.required()
|
151 | }).validate(options);
|
152 | if (error) throw new Error(error.details[0].message);
|
153 |
|
154 | const { data } = options;
|
155 | return {
|
156 | method: 'PUT',
|
157 | url: `${url}/api/v2/workspaces/reorder.json`,
|
158 | headers,
|
159 | data
|
160 | };
|
161 | }
|
162 | };
|
163 | };
|