UNPKG

4.61 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 _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 Forms
19 *
20 * GET /api/v2/ticket_forms.json
21 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#list-ticket-forms
22 */
23 list: () => {
24 // Ignore any options
25 return {
26 method: 'GET',
27 url: `${url}/api/v2/ticket_forms.json`,
28 headers
29 };
30 },
31
32 /**
33 * Create Ticket Forms
34 *
35 * POST /api/v2/ticket_forms.json
36 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#create-ticket-forms
37 */
38 create: (options = {}) => {
39 const { error } = Joi.object({
40 data: _data.required()
41 }).validate(options);
42 if (error) throw new Error(error.details[0].message);
43
44 const { data } = options;
45 return {
46 method: 'POST',
47 url: `${url}/api/v2/ticket_forms.json`,
48 headers,
49 data
50 };
51 },
52
53 /**
54 * Show Ticket Form
55 *
56 * GET /api/v2/ticket_forms/{id}.json
57 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#show-ticket-form
58 */
59 show: (options = {}) => {
60 const { error } = Joi.object({
61 id: _id.required()
62 }).validate(options);
63 if (error) throw new Error(error.details[0].message);
64
65 const { id } = options;
66 return {
67 method: 'GET',
68 url: `${url}/api/v2/ticket_forms/${id}.json`,
69 headers
70 };
71 },
72
73 /**
74 * Show Many Ticket Forms
75 *
76 * GET /api/v2/ticket_forms/show_many.json?ids={ids}
77 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#show-many-ticket-forms
78 */
79 show_many: (options = {}) => {
80 const { error } = Joi.object({
81 ids: _ids.required()
82 }).validate(options);
83 if (error) throw new Error(error.details[0].message);
84
85 const { ids } = options;
86 return {
87 method: 'GET',
88 url: `${url}/api/v2/ticket_forms/show_many.json?ids=${ids}`,
89 headers
90 };
91 },
92
93 /**
94 * Update Ticket Forms
95 *
96 * PUT /api/v2/ticket_forms/{id}.json
97 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#update-ticket-forms
98 */
99 update: (options = {}) => {
100 const { error } = Joi.object({
101 id: _id.required(),
102 data: _data.required()
103 }).validate(options);
104 if (error) throw new Error(error.details[0].message);
105
106 const { id, data } = options;
107 return {
108 method: 'PUT',
109 url: `${url}/api/v2/ticket_forms/${id}.json`,
110 headers,
111 data
112 };
113 },
114
115 /**
116 * Delete Ticket Form
117 *
118 * DELETE /api/v2/ticket_forms/{id}.json
119 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#delete-ticket-form
120 */
121 delete: (options = {}) => {
122 const { error } = Joi.object({
123 id: _id.required()
124 }).validate(options);
125 if (error) throw new Error(error.details[0].message);
126
127 const { id } = options;
128 return {
129 method: 'DELETE',
130 url: `${url}/api/v2/ticket_forms/${id}.json`,
131 headers
132 };
133 },
134
135 /**
136 * Reorder Ticket Forms
137 *
138 * PUT /api/v2/ticket_forms/reorder.json
139 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#reorder-ticket-forms
140 */
141 reorder: (options = {}) => {
142 const { error } = Joi.object({
143 data: _data.required()
144 }).validate(options);
145 if (error) throw new Error(error.details[0].message);
146
147 const { data } = options;
148 return {
149 method: 'PUT',
150 url: `${url}/api/v2/ticket_forms/reorder.json`,
151 headers,
152 data
153 };
154 },
155
156 /**
157 * Clone an already existing ticket form
158 *
159 * POST /api/v2/ticket_forms/{id}/clone.json
160 * https://developer.zendesk.com/rest_api/docs/support/ticket_forms#clone-an-already-existing-ticket-form
161 */
162 clone: (options = {}) => {
163 const { error } = Joi.object({
164 id: _id.required()
165 }).validate(options);
166 if (error) throw new Error(error.details[0].message);
167
168 const { id } = options;
169 return {
170 method: 'POST',
171 url: `${url}/api/v2/ticket_forms/${id}/clone.json`,
172 headers
173 };
174 }
175 };
176};