UNPKG

4.17 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _host_mapping = Joi.string().min(1);
7const _subdomain = 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 Brands
20 *
21 * GET /api/v2/brands.json
22 * https://developer.zendesk.com/rest_api/docs/support/brands#list-brands
23 */
24 list: () => {
25 // Ignore any options
26 return {
27 method: 'GET',
28 url: `${url}/api/v2/brands.json`,
29 headers
30 };
31 },
32
33 /**
34 * Show a Brand
35 *
36 * GET /api/v2/brands/{id}.json
37 * https://developer.zendesk.com/rest_api/docs/support/brands#show-a-brand
38 */
39 show: (options = {}) => {
40 const { error } = Joi.object({
41 id: _id.required()
42 }).validate(options);
43 if (error) throw new Error(error.details[0].message);
44
45 const { id } = options;
46 return {
47 method: 'GET',
48 url: `${url}/api/v2/brands/${id}.json`,
49 headers
50 };
51 },
52
53 /**
54 * Create Brand
55 *
56 * POST /api/v2/brands.json
57 * https://developer.zendesk.com/rest_api/docs/support/brands#create-brand
58 */
59 create: (options = {}) => {
60 const { error } = Joi.object({
61 data: _data.required()
62 }).validate(options);
63 if (error) throw new Error(error.details[0].message);
64
65 const { data } = options;
66 return {
67 method: 'POST',
68 url: `${url}/api/v2/brands.json`,
69 headers,
70 data
71 };
72 },
73
74 /**
75 * Update a Brand
76 *
77 * PUT /api/v2/brands/{id}.json
78 * https://developer.zendesk.com/rest_api/docs/support/brands#update-a-brand
79 */
80 update: (options = {}) => {
81 const { error } = Joi.object({
82 id: _id.required(),
83 data: _data.required()
84 }).validate(options);
85 if (error) throw new Error(error.details[0].message);
86
87 const { id, data } = options;
88 return {
89 method: 'PUT',
90 url: `${url}/api/v2/brands/${id}.json`,
91 headers,
92 data
93 };
94 },
95
96 /**
97 * Delete a Brand
98 *
99 * DELETE /api/v2/tickets/{id}.json
100 * https://developer.zendesk.com/rest_api/docs/support/brands#delete-a-brand
101 */
102 delete: (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: 'DELETE',
111 url: `${url}/api/v2/brands/${id}.json`,
112 headers
113 };
114 },
115
116 /**
117 * Check host mapping validity
118 *
119 * GET /api/v2/brands/check_host_mapping.json?host_mapping={host_mapping}&subdomain={subdomain}
120 * https://developer.zendesk.com/rest_api/docs/support/brands#check-host-mapping-validity
121 */
122 check: (options = {}) => {
123 const { error } = Joi.object({
124 host_mapping: _host_mapping.required(),
125 subdomain: _subdomain.required()
126 }).validate(options);
127 if (error) throw new Error(error.details[0].message);
128
129 const { host_mapping, subdomain } = options;
130 return {
131 method: 'GET',
132 url: `${url}/api/v2/brands/check_host_mapping.json?host_mapping=${host_mapping}&subdomain=${subdomain}`,
133 headers
134 };
135 },
136
137 /**
138 * Check host mapping validity for an existing brand
139 *
140 * GET /api/v2/brands/{id}/check_host_mapping.json
141 * https://developer.zendesk.com/rest_api/docs/support/brands#check-host-mapping-validity-for-an-existing-brand
142 */
143 check_existing: (options = {}) => {
144 const { error } = Joi.object({
145 id: _id.required()
146 }).validate(options);
147 if (error) throw new Error(error.details[0].message);
148
149 const { id } = options;
150 return {
151 method: 'PUT',
152 url: `${url}/api/v2/brands/${id}/check_host_mapping.json`,
153 headers
154 };
155 }
156 };
157};