UNPKG

2.86 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _data = Joi.object();
7
8module.exports = (options = {}) => {
9 const { error } = validate(options);
10 if (error) throw new Error(error.details[0].message);
11
12 const { url, headers } = prepare(options);
13
14 return {
15 /**
16 * List Custom Roles
17 *
18 * GET /api/v2/custom_roles.json
19 * https://developer.zendesk.com/rest_api/docs/support/custom_roles#list-custom-roles
20 */
21 list: () => {
22 // Ignore any options
23 return {
24 method: 'GET',
25 url: `${url}/api/v2/custom_roles.json`,
26 headers
27 };
28 },
29
30 /**
31 * Show Custom Role
32 *
33 * GET /api/v2/custom_roles/{id}.json
34 * https://developer.zendesk.com/rest_api/docs/support/custom_roles#show-custom-role
35 */
36 show: (options = {}) => {
37 const { error } = Joi.object({
38 id: _id.required()
39 }).validate(options);
40 if (error) throw new Error(error.details[0].message);
41
42 const { id } = options;
43 return {
44 method: 'GET',
45 url: `${url}/api/v2/custom_roles/${id}.json`,
46 headers
47 };
48 },
49
50 /**
51 * Create Custom Role
52 *
53 * POST /api/v2/custom_roles.json
54 * https://developer.zendesk.com/rest_api/docs/support/custom_roles#create-custom-role
55 */
56 create: (options = {}) => {
57 const { error } = Joi.object({
58 data: _data.required()
59 }).validate(options);
60 if (error) throw new Error(error.details[0].message);
61
62 const { data } = options;
63 return {
64 method: 'POST',
65 url: `${url}/api/v2/custom_roles.json`,
66 headers,
67 data
68 };
69 },
70
71 /**
72 * Update Custom Role
73 *
74 * PUT /api/v2/custom_roles/{id}.json
75 * https://developer.zendesk.com/rest_api/docs/support/custom_roles#update-custom-role
76 */
77 update: (options = {}) => {
78 const { error } = Joi.object({
79 id: _id.required(),
80 data: _data.required()
81 }).validate(options);
82 if (error) throw new Error(error.details[0].message);
83
84 const { id, data } = options;
85 return {
86 method: 'PUT',
87 url: `${url}/api/v2/custom_roles/${id}.json`,
88 headers,
89 data
90 };
91 },
92
93 /**
94 * Delete Custom Role
95 *
96 * DELETE /api/v2/custom_roles/{id}.json
97 * https://developer.zendesk.com/rest_api/docs/support/custom_roles#delete-custom-role
98 */
99 delete: (options = {}) => {
100 const { error } = Joi.object({
101 id: _id.required()
102 }).validate(options);
103 if (error) throw new Error(error.details[0].message);
104
105 const { id } = options;
106 return {
107 method: 'DELETE',
108 url: `${url}/api/v2/custom_roles/${id}.json`,
109 headers
110 };
111 }
112 };
113};