UNPKG

2.59 kBJavaScriptView Raw
1const Joi = require('@hapi/joi');
2const { validate, prepare } = require('../../utils/options');
3
4// Validation
5const _id = Joi.number().min(1);
6const _relationship_type = Joi.string().min(1);
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 Relationship Records (by Object Record or Type)
19 *
20 * GET /api/sunshine/objects/records/{id}/relationships/{relationship_type}
21 * https://developer.zendesk.com/rest_api/docs/sunshine/relationships#list-relationship-records-by-object-record
22 *
23 * GET /api/sunshine/relationships/records?type={relationship_type}
24 * https://developer.zendesk.com/rest_api/docs/sunshine/relationships#list-relationship-records-by-type
25 */
26 list: (options = {}) => {
27 const { error } = Joi.object({
28 id: _id,
29 relationship_type: _relationship_type.required()
30 }).validate(options);
31 if (error) throw new Error(error.details[0].message);
32
33 const { id, relationship_type } = options;
34
35 return {
36 method: 'GET',
37 url: `${url}/api/sunshine/${
38 id
39 ? `objects/records/${id}/relationships/${relationship_type}`
40 : `relationships/records?type=${relationship_type}`
41 }`,
42 headers
43 };
44 },
45
46 /**
47 * Create Relationship Record
48 *
49 * POST /api/sunshine/relationships/records
50 * https://developer.zendesk.com/rest_api/docs/sunshine/relationships#create-relationship-record
51 */
52 create: (options = {}) => {
53 const { error } = Joi.object({
54 data: _data.required()
55 }).validate(options);
56 if (error) throw new Error(error.details[0].message);
57
58 const { data } = options;
59 return {
60 method: 'POST',
61 url: `${url}/api/sunshine/relationships/records`,
62 headers,
63 data
64 };
65 },
66
67 /**
68 * Delete Relationship Record
69 *
70 * DELETE /api/sunshine/relationships/records/{id}
71 * https://developer.zendesk.com/rest_api/docs/sunshine/relationships#delete-relationship-record
72 */
73 delete: (options = {}) => {
74 const { error } = Joi.object({
75 id: _id.required()
76 }).validate(options);
77 if (error) throw new Error(error.details[0].message);
78
79 const { id } = options;
80 return {
81 method: 'DELETE',
82 url: `${url}/api/sunshine/relationships/records/${id}`,
83 headers
84 };
85 }
86 };
87};