1 | const Joi = require('@hapi/joi');
|
2 | const { validate, prepare } = require('../../utils/options');
|
3 |
|
4 |
|
5 | const _key = Joi.string()
|
6 | .min(2)
|
7 | .max(32);
|
8 | const _data = Joi.object({
|
9 | key: _key,
|
10 | schema: Joi.object({
|
11 | properties: Joi.object(),
|
12 | required: Joi.array()
|
13 | }),
|
14 | end_users_can_read: Joi.bool()
|
15 | });
|
16 |
|
17 |
|
18 | module.exports = (options = {}) => {
|
19 | const { error } = validate(options);
|
20 | if (error) throw new Error(error.details[0].message);
|
21 |
|
22 | const { url, headers } = prepare(options);
|
23 |
|
24 | return {
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | list: () => {
|
32 |
|
33 | return {
|
34 | method: 'GET',
|
35 | url: `${url}/api/sunshine/objects/types`,
|
36 | headers
|
37 | };
|
38 | },
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | show: (options = {}) => {
|
47 | const { error } = Joi.object({
|
48 | key: _key.required()
|
49 | }).validate(options);
|
50 | if (error) throw new Error(error.details[0].message);
|
51 |
|
52 | const { key } = options;
|
53 | return {
|
54 | method: 'GET',
|
55 | url: `${url}/api/sunshine/objects/types/${key}`,
|
56 | headers
|
57 | };
|
58 | },
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | create: (options = {}) => {
|
67 | const { error } = Joi.object({
|
68 | data: _data.required()
|
69 | }).validate(options);
|
70 | if (error) throw new Error(error.details[0].message);
|
71 |
|
72 | const { data } = options;
|
73 | return {
|
74 | method: 'POST',
|
75 | url: `${url}/api/sunshine/objects/types`,
|
76 | headers,
|
77 | data
|
78 | };
|
79 | },
|
80 |
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | update: (options = {}) => {
|
88 | const { error } = Joi.object({
|
89 | key: _key.required(),
|
90 | data: _data.required()
|
91 | }).validate(options);
|
92 | if (error) throw new Error(error.details[0].message);
|
93 |
|
94 | const { key, data } = options;
|
95 | return {
|
96 | method: 'PUT',
|
97 | url: `${url}/api/sunshine/objects/types/${key}`,
|
98 | headers,
|
99 | data
|
100 | };
|
101 | },
|
102 |
|
103 | |
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | delete: (options = {}) => {
|
110 | const { error } = Joi.object({
|
111 | key: _key.required()
|
112 | }).validate(options);
|
113 | if (error) throw new Error(error.details[0].message);
|
114 |
|
115 | const { key } = options;
|
116 | return {
|
117 | method: 'DELETE',
|
118 | url: `${url}/api/sunshine/objects/types/${key}`,
|
119 | headers
|
120 | };
|
121 | }
|
122 | };
|
123 | };
|