UNPKG

4.26 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _params = Joi.string().min(1);
6const _id = Joi.number().positive();
7const _ids = Joi.string().min(3);
8const _data = Joi.object();
9
10// Initialize Endpoint
11module.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 * List Workspaces
20 *
21 * GET /api/v2/workspaces.json
22 * https://developer.zendesk.com/rest_api/docs/support/workspaces#list-workspaces
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 * Show Workspace
41 *
42 * GET /api/v2/workspaces/{id}.json
43 * https://developer.zendesk.com/rest_api/docs/support/workspaces#show-workspace
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 * Create Workspace
61 *
62 * POST /api/v2/workspaces.json
63 * https://developer.zendesk.com/rest_api/docs/support/workspaces#create-workspace
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 * Update Workspace
82 *
83 * PUT /api/v2/workspaces/{id}.json
84 * https://developer.zendesk.com/rest_api/docs/support/workspaces#update-workspace
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 * Delete Workspace
104 *
105 * DELETE /api/v2/workspaces/{id}.json
106 * https://developer.zendesk.com/rest_api/docs/support/workspaces#delete-workspace
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 * Bulk Delete Workspaces
124 *
125 * DELETE /api/v2/workspaces/destroy_many.json?ids={ids}
126 * https://developer.zendesk.com/rest_api/docs/support/workspaces#bulk-delete-workspaces
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 * Reorder Workspaces
144 *
145 * PUT /api/v2/workspaces/reorder.json
146 * https://developer.zendesk.com/rest_api/docs/support/workspaces#reorder-workspaces
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};