UNPKG

5.52 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 Ticket Fields
19 *
20 * GET /api/v2/ticket_fields.json
21 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#list-ticket-fields
22 */
23 list: () => {
24 // Ignore any options
25 return {
26 method: 'GET',
27 url: `${url}/api/v2/ticket_fields.json`,
28 headers
29 };
30 },
31
32 /**
33 * Show Ticket Field
34 *
35 * GET /api/v2/ticket_fields/{id}.json
36 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#show-ticket-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/ticket_fields/${id}.json`,
48 headers
49 };
50 },
51
52 /**
53 * Create Ticket Field
54 *
55 * POST /api/v2/ticket_fields.json
56 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#create-ticket-field
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/ticket_fields.json`,
68 headers,
69 data
70 };
71 },
72
73 /**
74 * Update Ticket Field
75 *
76 * PUT /api/v2/ticket_fields/{id}.json
77 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#update-ticket-field
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/ticket_fields/${id}.json`,
90 headers,
91 data
92 };
93 },
94
95 /**
96 * Delete Ticket Field
97 *
98 * DELETE /api/v2/ticket_fields/{id}.json
99 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#delete-ticket-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/ticket_fields/${id}.json`,
111 headers
112 };
113 },
114
115 /**
116 * List Ticket Field Options
117 *
118 * GET /api/v2/ticket_fields/{field_id}/options.json
119 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#list-ticket-field-options
120 */
121 listOptions: (options = {}) => {
122 const { error } = Joi.object({
123 field_id: _field_id.required()
124 }).validate(options);
125 if (error) throw new Error(error.details[0].message);
126
127 const { field_id } = options;
128 return {
129 method: 'GET',
130 url: `${url}/api/v2/ticket_fields/${field_id}/options.json`,
131 headers
132 };
133 },
134
135 /**
136 * Show Ticket Field Option
137 *
138 * GET /api/v2/ticket_fields/{field_id}/options/{id}.json
139 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#show-ticket-field-option
140 */
141 showOption: (options = {}) => {
142 const { error } = Joi.object({
143 field_id: _field_id.required(),
144 id: _id.required()
145 }).validate(options);
146 if (error) throw new Error(error.details[0].message);
147
148 const { field_id, id } = options;
149 return {
150 method: 'GET',
151 url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
152 headers
153 };
154 },
155
156 /**
157 * Create or Update Ticket Field Option
158 *
159 * POST /api/v2/ticket_fields/{field_id}/options.json
160 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#create-or-update-ticket-field-option
161 */
162 createOrUpdateOption: (options = {}) => {
163 const { error } = Joi.object({
164 field_id: _field_id.required(),
165 id: _id.required(),
166 data: _data.required()
167 }).validate(options);
168 if (error) throw new Error(error.details[0].message);
169
170 const { field_id, id, data } = options;
171 return {
172 method: 'POST',
173 url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
174 headers,
175 data
176 };
177 },
178
179 /**
180 * Delete Ticket Field Option
181 *
182 * DELETE /api/v2/ticket_fields/{field_id}/options/{id}.json
183 * https://developer.zendesk.com/rest_api/docs/support/ticket_fields#delete-ticket-field-option
184 */
185 deleteOption: (options = {}) => {
186 const { error } = Joi.object({
187 field_id: _field_id.required(),
188 id: _id.required()
189 }).validate(options);
190 if (error) throw new Error(error.details[0].message);
191
192 const { field_id, id } = options;
193 return {
194 method: 'DELETE',
195 url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
196 headers
197 };
198 }
199 };
200};