1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _id = Joi.number().min(1);
|
6 | const _host_mapping = Joi.string().min(1);
|
7 | const _subdomain = 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/brands.json`,
|
29 | headers
|
30 | };
|
31 | },
|
32 |
|
33 | |
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | show: (options = {}) => {
|
40 | const { error } = Joi.object({
|
41 | id: _id.required()
|
42 | }).validate(options);
|
43 | if (error) throw new Error(error.details[0].message);
|
44 |
|
45 | const { id } = options;
|
46 | return {
|
47 | method: 'GET',
|
48 | url: `${url}/api/v2/brands/${id}.json`,
|
49 | headers
|
50 | };
|
51 | },
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | create: (options = {}) => {
|
60 | const { error } = Joi.object({
|
61 | data: _data.required()
|
62 | }).validate(options);
|
63 | if (error) throw new Error(error.details[0].message);
|
64 |
|
65 | const { data } = options;
|
66 | return {
|
67 | method: 'POST',
|
68 | url: `${url}/api/v2/brands.json`,
|
69 | headers,
|
70 | data
|
71 | };
|
72 | },
|
73 |
|
74 | |
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | update: (options = {}) => {
|
81 | const { error } = Joi.object({
|
82 | id: _id.required(),
|
83 | data: _data.required()
|
84 | }).validate(options);
|
85 | if (error) throw new Error(error.details[0].message);
|
86 |
|
87 | const { id, data } = options;
|
88 | return {
|
89 | method: 'PUT',
|
90 | url: `${url}/api/v2/brands/${id}.json`,
|
91 | headers,
|
92 | data
|
93 | };
|
94 | },
|
95 |
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | delete: (options = {}) => {
|
103 | const { error } = Joi.object({
|
104 | id: _id.required()
|
105 | }).validate(options);
|
106 | if (error) throw new Error(error.details[0].message);
|
107 |
|
108 | const { id } = options;
|
109 | return {
|
110 | method: 'DELETE',
|
111 | url: `${url}/api/v2/brands/${id}.json`,
|
112 | headers
|
113 | };
|
114 | },
|
115 |
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | check: (options = {}) => {
|
123 | const { error } = Joi.object({
|
124 | host_mapping: _host_mapping.required(),
|
125 | subdomain: _subdomain.required()
|
126 | }).validate(options);
|
127 | if (error) throw new Error(error.details[0].message);
|
128 |
|
129 | const { host_mapping, subdomain } = options;
|
130 | return {
|
131 | method: 'GET',
|
132 | url: `${url}/api/v2/brands/check_host_mapping.json?host_mapping=${host_mapping}&subdomain=${subdomain}`,
|
133 | headers
|
134 | };
|
135 | },
|
136 |
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 | check_existing: (options = {}) => {
|
144 | const { error } = Joi.object({
|
145 | id: _id.required()
|
146 | }).validate(options);
|
147 | if (error) throw new Error(error.details[0].message);
|
148 |
|
149 | const { id } = options;
|
150 | return {
|
151 | method: 'PUT',
|
152 | url: `${url}/api/v2/brands/${id}/check_host_mapping.json`,
|
153 | headers
|
154 | };
|
155 | }
|
156 | };
|
157 | };
|