UNPKG

2.61 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 Relationship Types
27 *
28 * GET /api/sunshine/relationships/types
29 * https://developer.zendesk.com/rest_api/docs/sunshine/relationship_types#list-relationship-types
30 */
31 list: () => {
32 // Ignore any options
33 return {
34 method: 'GET',
35 url: `${url}/api/sunshine/relationships/types`,
36 headers
37 };
38 },
39
40 /**
41 * Show Relationship Type
42 *
43 * GET /api/sunshine/relationships/types/{key}
44 * https://developer.zendesk.com/rest_api/docs/sunshine/relationship_types#show-relationship-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/relationships/types/${key}`,
56 headers
57 };
58 },
59
60 /**
61 * Create Relationship Type
62 *
63 * POST /api/sunshine/relationships/types
64 * https://developer.zendesk.com/rest_api/docs/sunshine/relationship_types#create-relationship-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/relationships/types`,
76 headers,
77 data
78 };
79 },
80
81 /**
82 * Delete Relationship Type
83 *
84 * DELETE /api/sunshine/relationships/types/{key}
85 * https://developer.zendesk.com/rest_api/docs/sunshine/relationship_types#delete-relationship-type
86 */
87 delete: (options = {}) => {
88 const { error } = Joi.object({
89 key: _key.required()
90 }).validate(options);
91 if (error) throw new Error(error.details[0].message);
92
93 const { key } = options;
94 return {
95 method: 'DELETE',
96 url: `${url}/api/sunshine/relationships/types/${key}`,
97 headers
98 };
99 }
100 };
101};