UNPKG

3.27 kBJavaScriptView Raw
1const validate = require('./validate');
2
3module.exports = ({ instance, headers }) => {
4 const url = `https://${instance}.zendesk.com`;
5
6 return {
7 list: (options = null) => {
8 if (options) throw new Error('no options are allowed');
9
10 return {
11 method: 'GET',
12 url: `${url}/api/v2/user_fields.json`,
13 headers
14 };
15 },
16
17 show: (options = {}) => {
18 const { error } = validate.show(options);
19 if (error) throw new Error(error.details[0].message);
20
21 const { id } = options;
22 return {
23 method: 'GET',
24 url: `${url}/api/v2/user_fields/${id}.json`,
25 headers
26 };
27 },
28
29 create: (options = {}) => {
30 const { error } = validate.create(options);
31 if (error) throw new Error(error.details[0].message);
32
33 const { data } = options;
34 return {
35 method: 'POST',
36 url: `${url}/api/v2/user_fields.json`,
37 headers,
38 data
39 };
40 },
41
42 update: (options = {}) => {
43 const { error } = validate.update(options);
44 if (error) throw new Error(error.details[0].message);
45
46 const { id, data } = options;
47 return {
48 method: 'PUT',
49 url: `${url}/api/v2/user_fields/${id}.json`,
50 headers,
51 data
52 };
53 },
54
55 delete: (options = {}) => {
56 const { error } = validate.delete(options);
57 if (error) throw new Error(error.details[0].message);
58
59 const { id } = options;
60 return {
61 method: 'DELETE',
62 url: `${url}/api/v2/user_fields/${id}.json`,
63 headers
64 };
65 },
66
67 reorder: (options = {}) => {
68 const { error } = validate.reorder(options);
69 if (error) throw new Error(error.details[0].message);
70
71 const { data } = options;
72 return {
73 method: 'PUT',
74 url: `${url}/api/v2/user_fields/reorder.json`,
75 headers,
76 data
77 };
78 },
79
80 listOptions: (options = {}) => {
81 const { error } = validate.listOptions(options);
82 if (error) throw new Error(error.details[0].message);
83
84 const { field_id } = options;
85 return {
86 method: 'GET',
87 url: `${url}/api/v2/user_fields/${field_id}/options.json`,
88 headers
89 };
90 },
91
92 showOption: (options = {}) => {
93 const { error } = validate.showOption(options);
94 if (error) throw new Error(error.details[0].message);
95
96 const { field_id, id } = options;
97 return {
98 method: 'GET',
99 url: `${url}/api/v2/ticket_fields/${field_id}/options/${id}.json`,
100 headers
101 };
102 },
103
104 createOrUpdateOption: (options = {}) => {
105 const { error } = validate.createOrUpdateOption(options);
106 if (error) throw new Error(error.details[0].message);
107
108 const { field_id, data } = options;
109 return {
110 method: 'POST',
111 url: `${url}/api/v2/user_fields/${field_id}/options.json`,
112 headers,
113 data
114 };
115 },
116
117 deleteOption: (options = {}) => {
118 const { error } = validate.deleteOption(options);
119 if (error) throw new Error(error.details[0].message);
120
121 const { field_id, id } = options;
122 return {
123 method: 'DELETE',
124 url: `${url}/api/v2/user_fields/${field_id}/options/${id}.json`,
125 headers
126 };
127 }
128 };
129};