1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _type = Joi.string().valid('tickets', 'organizations', 'users');
|
6 | const _id = Joi.number().min(1);
|
7 | const _name = Joi.string().min(1);
|
8 | const _data = Joi.object();
|
9 |
|
10 |
|
11 | module.exports = (options = {}) => {
|
12 | const { error } = validate(options);
|
13 | if (error) throw new Error(error.details[0].message);
|
14 |
|
15 | const { url, headers } = prepare(options);
|
16 |
|
17 | return {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | list: () => {
|
25 |
|
26 | return {
|
27 | method: 'GET',
|
28 | url: `${url}/api/v2/tags.json`,
|
29 | headers
|
30 | };
|
31 | },
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | show: (options = {}) => {
|
42 | const { error } = Joi.object({
|
43 | type: _type.required(),
|
44 | id: _id.required()
|
45 | }).validate(options);
|
46 | if (error) throw new Error(error.details[0].message);
|
47 |
|
48 | const { type, id } = options;
|
49 | return {
|
50 | method: 'GET',
|
51 | url: `${url}/api/v2/${type}/${id}/tags.json`,
|
52 | headers
|
53 | };
|
54 | },
|
55 |
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | set: (options = {}) => {
|
65 | const { error } = Joi.object({
|
66 | type: _type.required(),
|
67 | id: _id.required(),
|
68 | data: _data.required()
|
69 | }).validate(options);
|
70 | if (error) throw new Error(error.details[0].message);
|
71 |
|
72 | const { type, id, data } = options;
|
73 | return {
|
74 | method: 'POST',
|
75 | url: `${url}/api/v2/${type}/${id}/tags.json`,
|
76 | headers,
|
77 | data
|
78 | };
|
79 | },
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | add: (options = {}) => {
|
90 | const { error } = Joi.object({
|
91 | type: _type.required(),
|
92 | id: _id.required(),
|
93 | data: _data.required()
|
94 | }).validate(options);
|
95 | if (error) throw new Error(error.details[0].message);
|
96 |
|
97 | const { type, id, data } = options;
|
98 | return {
|
99 | method: 'PUT',
|
100 | url: `${url}/api/v2/${type}/${id}/tags.json`,
|
101 | headers,
|
102 | data
|
103 | };
|
104 | },
|
105 |
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | remove: (options = {}) => {
|
115 | const { error } = Joi.object({
|
116 | type: _type.required(),
|
117 | id: _id.required(),
|
118 | data: _data.required()
|
119 | }).validate(options);
|
120 | if (error) throw new Error(error.details[0].message);
|
121 |
|
122 | const { type, id, data } = options;
|
123 | return {
|
124 | method: 'DELETE',
|
125 | url: `${url}/api/v2/${type}/${id}/tags.json`,
|
126 | headers,
|
127 | data
|
128 | };
|
129 | },
|
130 |
|
131 | |
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 | autocomplete: (options = {}) => {
|
138 | const { error } = Joi.object({
|
139 | name: _name.required()
|
140 | }).validate(options);
|
141 | if (error) throw new Error(error.details[0].message);
|
142 |
|
143 | const { name } = options;
|
144 | return {
|
145 | method: 'GET',
|
146 | url: `${url}/api/v2/autocomplete/tags.json?name=${name}`,
|
147 | headers
|
148 | };
|
149 | }
|
150 | };
|
151 | };
|