UNPKG

5.72 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _object_type = Joi.string().min(1);
6const _type = Joi.string().min(1);
7const _id = Joi.number().min(1);
8const _ids = Joi.string().min(1);
9const _relationship_type = Joi.string().min(1);
10const _external_id = Joi.string().min(1);
11const _external_ids = Joi.string().min(1);
12const _data = Joi.object();
13
14// Initialize Endpoint
15module.exports = (options = {}) => {
16 const { error } = validate(options);
17 if (error) throw new Error(error.details[0].message);
18
19 const { url, headers } = prepare(options);
20
21 return {
22 /**
23 * List Object Records (by Object Type, IDs, or Relationship)
24 *
25 * GET /api/sunshine/objects/records?type={object_type}
26 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#list-object-records-by-type
27 *
28 * GET /api/sunshine/objects/records?ids={id, id, ...}
29 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#list-object-records-by-id
30 *
31 * GET /api/sunshine/objects/records/{id}/related/{relationship_type}
32 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#list-related-object-records
33 */
34 list: (options = {}) => {
35 const { error } = Joi.object({
36 object_type: _object_type,
37 ids: _ids,
38 id: _id,
39 relationship_type: _relationship_type
40 }).validate(options);
41 if (error) throw new Error(error.details[0].message);
42
43 const { object_type, ids, id, relationship_type } = options;
44 if (!object_type && !ids && !(id && relationship_type))
45 throw new Error(
46 'object_type, ids, or id with relationship_type required'
47 );
48
49 return {
50 method: 'GET',
51 url: `${url}/api/sunshine/objects/records${
52 object_type
53 ? `?type=${object_type}`
54 : ids
55 ? `?ids=${ids}`
56 : `/${id}/related/${relationship_type}`
57 }`,
58 headers
59 };
60 },
61
62 /**
63 * Show Object Record (by ID, External ID, or External IDs)
64 *
65 * GET /api/sunshine/objects/records/{id}
66 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#show-object-record
67 *
68 * GET /api/sunshine/objects/records?type={object_type}&external_id={id}
69 * GET /api/sunshine/objects/records?type={object_type}&external_ids={id, id, ...}
70 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#show-object-record-by-external-id
71 *
72 */
73 show: (options = {}) => {
74 const { error } = Joi.object({
75 id: _id,
76 object_type: _object_type,
77 external_id: _external_id,
78 external_ids: _external_ids
79 }).validate(options);
80 if (error) throw new Error(error.details[0].message);
81
82 const { id, object_type, external_id, external_ids } = options;
83 if (!id && !object_type) throw new Error('id or object_type required');
84 if (object_type && !external_id && !external_ids)
85 throw new Error(
86 'object_type requires either external_id or external_ids set'
87 );
88
89 return {
90 method: 'GET',
91 url: `${url}/api/sunshine/objects/records${
92 id
93 ? `/${id}`
94 : `?type=${object_type}&external_id${
95 external_id ? `=${external_id}` : `s=${external_ids}`
96 }`
97 }`,
98 headers
99 };
100 },
101
102 /**
103 * Create Object Record
104 *
105 * POST /api/sunshine/objects/records
106 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#create-object-record
107 */
108 create: (options = {}) => {
109 const { error } = Joi.object({
110 data: _data.required()
111 }).validate(options);
112 if (error) throw new Error(error.details[0].message);
113
114 const { data } = options;
115 return {
116 method: 'POST',
117 url: `${url}/api/sunshine/objects/records`,
118 headers,
119 data
120 };
121 },
122
123 /**
124 * Update Object Record (or Set by External ID)
125 *
126 * PATCH /api/sunshine/objects/records/{id}
127 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#update-object-record
128 *
129 * PATCH /api/sunshine/objects/records
130 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#set-object-record-by-external-id
131 */
132 update: (options = {}) => {
133 const { error } = Joi.object({
134 id: _id,
135 data: _data.required()
136 }).validate(options);
137 if (error) throw new Error(error.details[0].message);
138
139 const { id, data } = options;
140 return {
141 method: 'PATCH',
142 url: `${url}/api/sunshine/objects/records${id ? `/${id}` : ''}`,
143 headers,
144 data
145 };
146 },
147
148 /**
149 * Delete Object Record
150 *
151 * DELETE /api/sunshine/objects/records/{id}
152 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#delete-object-record
153 *
154 * DELETE /api/sunshine/objects/records?external_id={external_id}&type={type}
155 * https://developer.zendesk.com/rest_api/docs/sunshine/resources#delete-object-record-by-external-id
156 */
157 delete: (options = {}) => {
158 const { error } = Joi.object({
159 id: _id,
160 external_id: _external_id,
161 type: _type
162 }).validate(options);
163 if (error) throw new Error(error.details[0].message);
164
165 const { id, external_id, type } = options;
166 if (!id && !(external_id && type))
167 throw new Error('id or external_id with type required');
168
169 return {
170 method: 'DELETE',
171 url: `${url}/api/sunshine/objects/records${
172 id ? `/${id}` : `?external_id=${external_id}&type=${type}`
173 }`,
174 headers
175 };
176 }
177 };
178};