UNPKG

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