UNPKG

5.95 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _field_id = Joi.number().min(1);
7const _data = Joi.object();
8
9// Initialize Endpoint
10module.exports = (options = {}) => {
11 const { error } = validate(options);
12 if (error) throw new Error(error.details[0].message);
13
14 const { url, headers } = prepare(options);
15
16 return {
17 /**
18 * List User Fields
19 *
20 * GET /api/v2/user_fields.json
21 * https://developer.zendesk.com/rest_api/docs/support/user_fields#list-user-fields
22 */
23 list: () => {
24 // Ignore any options
25 return {
26 method: 'GET',
27 url: `${url}/api/v2/user_fields.json`,
28 headers
29 };
30 },
31
32 /**
33 * Show User Field
34 *
35 * GET /api/v2/user_fields/{id}.json
36 * https://developer.zendesk.com/rest_api/docs/support/user_fields#show-user-field
37 */
38 show: (options = {}) => {
39 const { error } = Joi.object({
40 id: _id.required()
41 }).validate(options);
42 if (error) throw new Error(error.details[0].message);
43
44 const { id } = options;
45 return {
46 method: 'GET',
47 url: `${url}/api/v2/user_fields/${id}.json`,
48 headers
49 };
50 },
51
52 /**
53 * Create User Fields
54 *
55 * POST /api/v2/user_fields.json
56 * https://developer.zendesk.com/rest_api/docs/support/user_fields#create-user-fields
57 */
58 create: (options = {}) => {
59 const { error } = Joi.object({
60 data: _data.required()
61 }).validate(options);
62 if (error) throw new Error(error.details[0].message);
63
64 const { data } = options;
65 return {
66 method: 'POST',
67 url: `${url}/api/v2/user_fields.json`,
68 headers,
69 data
70 };
71 },
72
73 /**
74 * Update User Fields
75 *
76 * PUT /api/v2/user_fields/{id}.json
77 * https://developer.zendesk.com/rest_api/docs/support/user_fields#update-user-fields
78 */
79 update: (options = {}) => {
80 const { error } = Joi.object({
81 id: _id.required(),
82 data: _data.required()
83 }).validate(options);
84 if (error) throw new Error(error.details[0].message);
85
86 const { id, data } = options;
87 return {
88 method: 'PUT',
89 url: `${url}/api/v2/user_fields/${id}.json`,
90 headers,
91 data
92 };
93 },
94
95 /**
96 * Delete User Field
97 *
98 * DELETE /api/v2/user_fields/{id}.json
99 * https://developer.zendesk.com/rest_api/docs/support/user_fields#delete-user-field
100 */
101 delete: (options = {}) => {
102 const { error } = Joi.object({
103 id: _id.required()
104 }).validate(options);
105 if (error) throw new Error(error.details[0].message);
106
107 const { id } = options;
108 return {
109 method: 'DELETE',
110 url: `${url}/api/v2/user_fields/${id}.json`,
111 headers
112 };
113 },
114
115 /**
116 * Reorder User Field
117 *
118 * PUT /api/v2/user_fields/reorder.json
119 * https://developer.zendesk.com/rest_api/docs/support/user_fields#reorder-user-field
120 */
121 reorder: (options = {}) => {
122 const { error } = Joi.object({
123 data: _data.required()
124 }).validate(options);
125 if (error) throw new Error(error.details[0].message);
126
127 const { data } = options;
128 return {
129 method: 'PUT',
130 url: `${url}/api/v2/user_fields/reorder.json`,
131 headers,
132 data
133 };
134 },
135
136 /**
137 * List User Field Options
138 *
139 * GET /api/v2/user_fields/{field_id}/options.json
140 * https://developer.zendesk.com/rest_api/docs/support/user_fields#list-user-field-options
141 */
142 listOptions: (options = {}) => {
143 const { error } = Joi.object({
144 field_id: _field_id.required()
145 }).validate(options);
146 if (error) throw new Error(error.details[0].message);
147
148 const { field_id } = options;
149 return {
150 method: 'GET',
151 url: `${url}/api/v2/user_fields/${field_id}/options.json`,
152 headers
153 };
154 },
155
156 /**
157 * Show a User Field Option
158 *
159 * GET /api/v2/user_fields/{field_id}/options/{id}.json
160 * https://developer.zendesk.com/rest_api/docs/support/user_fields#show-a-user-field-option
161 */
162 showOption: (options = {}) => {
163 const { error } = Joi.object({
164 field_id: _field_id.required(),
165 id: _id.required()
166 }).validate(options);
167 if (error) throw new Error(error.details[0].message);
168
169 const { field_id, id } = options;
170 return {
171 method: 'GET',
172 url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
173 headers
174 };
175 },
176
177 /**
178 * Create or Update a User Field Option
179 *
180 * POST /api/v2/user_fields/{field_id}/options.json
181 * https://developer.zendesk.com/rest_api/docs/support/user_fields#create-or-update-a-user-field-option
182 */
183 createOrUpdateOption: (options = {}) => {
184 const { error } = Joi.object({
185 field_id: _field_id.required(),
186 data: _data.required()
187 }).validate(options);
188 if (error) throw new Error(error.details[0].message);
189
190 const { field_id, data } = options;
191 return {
192 method: 'POST',
193 url: `${url}/api/v2/user_fields/${field_id}/options.json`,
194 headers,
195 data
196 };
197 },
198
199 /**
200 * Delete User Field Option
201 *
202 * DELETE /api/v2/user_fields/{field_id}/options/{id}.json
203 * https://developer.zendesk.com/rest_api/docs/support/user_fields#delete-user-field-option
204 */
205 deleteOption: (options = {}) => {
206 const { error } = Joi.object({
207 field_id: _field_id.required(),
208 id: _id.required()
209 }).validate(options);
210 if (error) throw new Error(error.details[0].message);
211
212 const { field_id, id } = options;
213 return {
214 method: 'DELETE',
215 url: `${url}/api/v2/user_fields/${field_id}/options/${id}.json`,
216 headers
217 };
218 }
219 };
220};