UNPKG

9.1 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _ids = Joi.string().min(3);
7const _query = Joi.string().min(1);
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 Views
20 *
21 * GET /api/v2/views.json
22 * https://developer.zendesk.com/rest_api/docs/support/views#list-views
23 *
24 * List Views by ID
25 *
26 * GET /api/v2/views/show_many.json?ids=1,2,3
27 * https://developer.zendesk.com/rest_api/docs/support/views#list-views-by-id
28 */
29 list: (options = {}) => {
30 const { error } = Joi.object({
31 ids: _ids
32 }).validate(options);
33 if (error) throw new Error(error.details[0].message);
34
35 const { ids = '' } = options;
36 const params = ids ? `?ids=${ids}` : '';
37
38 return {
39 method: 'GET',
40 url: `${url}/api/v2/views.json${params}`,
41 headers
42 };
43 },
44
45 /**
46 * Update Many Views
47 *
48 * PUT /api/v2/views/update_many.json
49 * https://developer.zendesk.com/rest_api/docs/support/views#update-many-views
50 */
51 update_many: (options = {}) => {
52 const { error } = Joi.object({
53 data: _data.required()
54 }).validate(options);
55 if (error) throw new Error(error.details[0].message);
56
57 const { data } = options;
58 return {
59 method: 'PUT',
60 url: `${url}/api/v2/views/update_many.json`,
61 headers,
62 data
63 };
64 },
65
66 /**
67 * List Active Views
68 *
69 * GET /api/v2/views/active.json
70 * https://developer.zendesk.com/rest_api/docs/support/views#list-active-views
71 */
72 active: () => {
73 // Ignore any options
74 return {
75 method: 'GET',
76 url: `${url}/api/v2/views/active.json`,
77 headers
78 };
79 },
80
81 /**
82 * List Views - Compact
83 *
84 * GET /api/v2/views/compact.json
85 * https://developer.zendesk.com/rest_api/docs/support/views#list-views---compact
86 */
87 compact: () => {
88 // Ignore any options
89 return {
90 method: 'GET',
91 url: `${url}/api/v2/views/compact.json`,
92 headers
93 };
94 },
95
96 /**
97 * Show View
98 *
99 * GET /api/v2/views/{id}.json
100 * https://developer.zendesk.com/rest_api/docs/support/views#show-view
101 */
102 show: (options = {}) => {
103 const { error } = Joi.object({
104 id: _id.required()
105 }).validate(options);
106 if (error) throw new Error(error.details[0].message);
107
108 const { id } = options;
109 return {
110 method: 'GET',
111 url: `${url}/api/v2/views/${id}.json`,
112 headers
113 };
114 },
115
116 /**
117 * Create View
118 *
119 * POST /api/v2/views.json
120 * https://developer.zendesk.com/rest_api/docs/support/views#create-view
121 */
122 create: (options = {}) => {
123 const { error } = Joi.object({
124 data: _data.required()
125 }).validate(options);
126 if (error) throw new Error(error.details[0].message);
127
128 const { data } = options;
129 return {
130 method: 'POST',
131 url: `${url}/api/v2/views.json`,
132 headers,
133 data
134 };
135 },
136
137 /**
138 * Update View
139 *
140 * PUT /api/v2/views/{id}.json
141 * https://developer.zendesk.com/rest_api/docs/support/views#update-view
142 */
143 update: (options = {}) => {
144 const { error } = Joi.object({
145 id: _id.required(),
146 data: _data.required()
147 }).validate(options);
148 if (error) throw new Error(error.details[0].message);
149
150 const { id, data } = options;
151 return {
152 method: 'PUT',
153 url: `${url}/api/v2/views/${id}.json`,
154 headers,
155 data
156 };
157 },
158
159 /**
160 * Delete View
161 *
162 * DELETE /api/v2/views/{id}.json
163 * https://developer.zendesk.com/rest_api/docs/support/views#delete-view
164 */
165 delete: (options = {}) => {
166 const { error } = Joi.object({
167 id: _id.required()
168 }).validate(options);
169 if (error) throw new Error(error.details[0].message);
170
171 const { id } = options;
172 return {
173 method: 'DELETE',
174 url: `${url}/api/v2/views/${id}.json`,
175 headers
176 };
177 },
178
179 /**
180 * Bulk Delete Views
181 *
182 * DELETE /api/v2/views/destroy_many.json
183 * https://developer.zendesk.com/rest_api/docs/support/views#bulk-delete-views
184 */
185 delete_many: (options = {}) => {
186 const { error } = Joi.object({
187 data: _data.required()
188 }).validate(options);
189 if (error) throw new Error(error.details[0].message);
190
191 const { data } = options;
192 return {
193 method: 'DELETE',
194 url: `${url}/api/v2/views/destroy_many.json`,
195 headers,
196 data
197 };
198 },
199
200 /**
201 * Execute View
202 *
203 * GET /api/v2/views/{id}/execute.json
204 * https://developer.zendesk.com/rest_api/docs/support/views#execute-view
205 */
206 execute: (options = {}) => {
207 const { error } = Joi.object({
208 id: _id.required()
209 }).validate(options);
210 if (error) throw new Error(error.details[0].message);
211
212 const { id } = options;
213 return {
214 method: 'GET',
215 url: `${url}/api/v2/views/${id}/execute.json`,
216 headers
217 };
218 },
219
220 /**
221 * List Tickets from a View
222 *
223 * GET /api/v2/views/{id}/tickets.json
224 * https://developer.zendesk.com/rest_api/docs/support/views#list-tickets-from-a-view
225 */
226 tickets: (options = {}) => {
227 const { error } = Joi.object({
228 id: _id.required()
229 }).validate(options);
230 if (error) throw new Error(error.details[0].message);
231
232 const { id } = options;
233 return {
234 method: 'GET',
235 url: `${url}/api/v2/views/${id}/tickets.json`,
236 headers
237 };
238 },
239
240 /**
241 * Get View Counts
242 *
243 * GET /api/v2/views/count_many.json?ids={view_id},{view_id}
244 * https://developer.zendesk.com/rest_api/docs/support/views#get-view-counts
245 */
246 count_many: (options = {}) => {
247 const { error } = Joi.object({
248 ids: _ids.required()
249 }).validate(options);
250 if (error) throw new Error(error.details[0].message);
251
252 const { ids } = options;
253 return {
254 method: 'GET',
255 url: `${url}/api/v2/views/count_many.json?ids=${ids}`,
256 headers
257 };
258 },
259
260 /**
261 * Get View Count
262 *
263 * GET /api/v2/views/{id}/count.json
264 * https://developer.zendesk.com/rest_api/docs/support/views#get-view-count
265 */
266 count: (options = {}) => {
267 const { error } = Joi.object({
268 id: _id.required()
269 }).validate(options);
270 if (error) throw new Error(error.details[0].message);
271
272 const { id } = options;
273 return {
274 method: 'GET',
275 url: `${url}/api/v2/views/${id}/count.json`,
276 headers
277 };
278 },
279
280 /**
281 * Export View
282 *
283 * GET /api/v2/views/{id}/export.json
284 * https://developer.zendesk.com/rest_api/docs/support/views#export-view
285 */
286 export: (options = {}) => {
287 const { error } = Joi.object({
288 id: _id.required()
289 }).validate(options);
290 if (error) throw new Error(error.details[0].message);
291
292 const { id } = options;
293 return {
294 method: 'GET',
295 url: `${url}/api/v2/views/${id}/export.json`,
296 headers
297 };
298 },
299
300 /**
301 * Search Views
302 *
303 * GET /api/v2/views/search.json
304 * https://developer.zendesk.com/rest_api/docs/support/views#search-views
305 */
306 search: (options = {}) => {
307 const { error } = Joi.object({
308 query: _query.required()
309 }).validate(options);
310 if (error) throw new Error(error.details[0].message);
311
312 const { query } = options;
313 return {
314 method: 'GET',
315 url: `${url}/api/v2/views/search.json?query=${query}`,
316 headers
317 };
318 },
319
320 /**
321 * Previewing Views
322 *
323 * POST /api/v2/views/preview.json
324 * https://developer.zendesk.com/rest_api/docs/support/views#previewing-views
325 */
326 preview: (options = {}) => {
327 const { error } = Joi.object({
328 data: _data.required()
329 }).validate(options);
330 if (error) throw new Error(error.details[0].message);
331
332 const { data } = options;
333 return {
334 method: 'POST',
335 url: `${url}/api/v2/views/preview.json`,
336 headers,
337 data
338 };
339 },
340
341 /**
342 * Preview Count
343 *
344 * POST /api/v2/views/preview/count.json
345 * https://developer.zendesk.com/rest_api/docs/support/views#preview-count
346 */
347 preview_count: (options = {}) => {
348 const { error } = Joi.object({
349 data: _data.required()
350 }).validate(options);
351 if (error) throw new Error(error.details[0].message);
352
353 const { data } = options;
354 return {
355 method: 'POST',
356 url: `${url}/api/v2/views/preview/count.json`,
357 headers,
358 data
359 };
360 }
361 };
362};