@veritree/services 1.0.0-6 → 1.0.0-7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/endpoints/sdgs.js +5 -0
- package/src/helpers/api.js +58 -46
- package/src/utils/args.js +18 -22
package/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { Images } from './src/endpoints/images';
|
|
|
9
9
|
import { User } from './src/endpoints/user';
|
|
10
10
|
import { Orgs } from './src/endpoints/orgs';
|
|
11
11
|
import { Regions } from './src/endpoints/regions';
|
|
12
|
+
import { SDGs } from './src/endpoints/sdgs';
|
|
12
13
|
import { Sponsors } from './src/endpoints/sponsors';
|
|
13
14
|
import { Stats } from './src/endpoints/stats';
|
|
14
15
|
import { Subdomains } from './src/endpoints/subdomains';
|
|
@@ -28,6 +29,7 @@ export {
|
|
|
28
29
|
Images,
|
|
29
30
|
User,
|
|
30
31
|
Regions,
|
|
32
|
+
SDGs,
|
|
31
33
|
Sponsors,
|
|
32
34
|
Stats,
|
|
33
35
|
Subdomains,
|
package/package.json
CHANGED
package/src/helpers/api.js
CHANGED
|
@@ -17,6 +17,44 @@ function handleEnvelopParam(url) {
|
|
|
17
17
|
return `${url}${urlEvenlopeArg}`;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Handles how the data should be sent in the fetch method
|
|
22
|
+
*
|
|
23
|
+
* @param {string} method
|
|
24
|
+
* @param {object} body
|
|
25
|
+
* @returns {object} data
|
|
26
|
+
*/
|
|
27
|
+
function getConfig(method, data, as) {
|
|
28
|
+
const isGet = method === "get";
|
|
29
|
+
const isPut = as === "put";
|
|
30
|
+
const isFormData = data instanceof FormData;
|
|
31
|
+
const accessToken = `Bearer ${getCookie("access_token")}`;
|
|
32
|
+
|
|
33
|
+
const config = {
|
|
34
|
+
method,
|
|
35
|
+
headers: {
|
|
36
|
+
Authorization: accessToken,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
if (!isFormData) {
|
|
41
|
+
config.headers["Content-Type"] = "application/json";
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// TODO: improve this ifs and elses
|
|
45
|
+
if (!isGet) {
|
|
46
|
+
if (isFormData) {
|
|
47
|
+
if (isPut) data.set("_method", "PUT");
|
|
48
|
+
config.body = data;
|
|
49
|
+
} else {
|
|
50
|
+
if (isPut) data._method = "PUT";
|
|
51
|
+
config.body = JSON.stringify(data);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return config;
|
|
56
|
+
}
|
|
57
|
+
|
|
20
58
|
export default class Api {
|
|
21
59
|
constructor(resource) {
|
|
22
60
|
this.baseUrl = `${process.env.API_VERITREE_URL}/api`;
|
|
@@ -29,18 +67,19 @@ export default class Api {
|
|
|
29
67
|
*
|
|
30
68
|
* @returns {promise}
|
|
31
69
|
*/
|
|
32
|
-
async all() {
|
|
33
|
-
const url = `${this.getUrl()}${this.getUrlParams(
|
|
70
|
+
async all(args) {
|
|
71
|
+
const url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
34
72
|
return await this.get(url);
|
|
35
73
|
}
|
|
36
74
|
|
|
37
75
|
/**
|
|
38
76
|
*
|
|
39
77
|
* @param {string, number} id
|
|
78
|
+
* @param {object} args/params
|
|
40
79
|
* @returns {promise}
|
|
41
80
|
*/
|
|
42
|
-
async single(id) {
|
|
43
|
-
const url = `${this.getUrl()}
|
|
81
|
+
async single(id, args) {
|
|
82
|
+
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
44
83
|
return await this.get(url);
|
|
45
84
|
}
|
|
46
85
|
|
|
@@ -61,7 +100,7 @@ export default class Api {
|
|
|
61
100
|
* @returns {promise}
|
|
62
101
|
*/
|
|
63
102
|
async update(id, data, as = "put") {
|
|
64
|
-
const url = `${this.getUrl()}
|
|
103
|
+
const url = `${this.getUrl(id)}${this.getUrlParams()}`;
|
|
65
104
|
return await this.post(url, data, as);
|
|
66
105
|
}
|
|
67
106
|
|
|
@@ -87,10 +126,21 @@ export default class Api {
|
|
|
87
126
|
|
|
88
127
|
// ----------
|
|
89
128
|
// --
|
|
90
|
-
|
|
91
|
-
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* @param {string, number} id
|
|
132
|
+
* @returns
|
|
133
|
+
*/
|
|
134
|
+
getUrl(id) {
|
|
135
|
+
id = id ? `/${id}` : "";
|
|
136
|
+
return `${this.baseUrl}/${this.resource}${id}`;
|
|
92
137
|
}
|
|
93
138
|
|
|
139
|
+
/**
|
|
140
|
+
*
|
|
141
|
+
* @param {object} args
|
|
142
|
+
* @returns {string}
|
|
143
|
+
*/
|
|
94
144
|
getUrlParams(args) {
|
|
95
145
|
this.setOrg();
|
|
96
146
|
let isOrgLess = false;
|
|
@@ -120,7 +170,7 @@ export default class Api {
|
|
|
120
170
|
*/
|
|
121
171
|
async unWrap(url, method = "get", data, as) {
|
|
122
172
|
url = handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
|
|
123
|
-
const config =
|
|
173
|
+
const config = getConfig(method, data, as);
|
|
124
174
|
|
|
125
175
|
try {
|
|
126
176
|
const response = await fetch(url, config);
|
|
@@ -132,44 +182,6 @@ export default class Api {
|
|
|
132
182
|
}
|
|
133
183
|
}
|
|
134
184
|
|
|
135
|
-
/**
|
|
136
|
-
* Handles how the data should be sent in the fetch method
|
|
137
|
-
*
|
|
138
|
-
* @param {string} method
|
|
139
|
-
* @param {object} body
|
|
140
|
-
* @returns {object} data
|
|
141
|
-
*/
|
|
142
|
-
getConfig(method, data, as) {
|
|
143
|
-
const isGet = method === "get";
|
|
144
|
-
const isPut = as === "put";
|
|
145
|
-
const isFormData = data instanceof FormData;
|
|
146
|
-
const accessToken = `Bearer ${getCookie("access_token")}`;
|
|
147
|
-
|
|
148
|
-
const config = {
|
|
149
|
-
method,
|
|
150
|
-
headers: {
|
|
151
|
-
Authorization: accessToken,
|
|
152
|
-
},
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
if (!isFormData) {
|
|
156
|
-
config.headers["Content-Type"] = "application/json";
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// TODO: improve this ifs and elses
|
|
160
|
-
if (!isGet) {
|
|
161
|
-
if (isFormData) {
|
|
162
|
-
if (isPut) data.set("_method", "PUT");
|
|
163
|
-
config.body = data;
|
|
164
|
-
} else {
|
|
165
|
-
if (isPut) data._method = "PUT";
|
|
166
|
-
config.body = JSON.stringify(data);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return config;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
185
|
setOrg() {
|
|
174
186
|
const session = getSession();
|
|
175
187
|
if (!session) return;
|
package/src/utils/args.js
CHANGED
|
@@ -1,32 +1,28 @@
|
|
|
1
1
|
export const createParamsStringFromArgs = (args) => {
|
|
2
|
-
if(!args
|
|
2
|
+
if (!args && typeof args !== 'object' && !Array.isArray(args)) {
|
|
3
|
+
return "";
|
|
4
|
+
}
|
|
3
5
|
|
|
4
6
|
const paramsString = [];
|
|
5
7
|
let arr = [];
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
if(
|
|
9
|
-
for(const key in arg) {
|
|
10
|
-
if(arg[key] === null) return;
|
|
11
|
-
|
|
12
|
-
if(Array.isArray(arg[key])) {
|
|
13
|
-
arr = arg[key].map((item, index) => {
|
|
14
|
-
const param = `${key}[]=${item}`;
|
|
9
|
+
Object.entries(args).forEach(([key, value]) => {
|
|
10
|
+
if (value === null) return;
|
|
15
11
|
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
if (Array.isArray(value)) {
|
|
13
|
+
arr = value.map((item, index) => {
|
|
14
|
+
const param = `${key}[]=${item}`;
|
|
15
|
+
return index === 0 ? `${param}` : `&${param}`;
|
|
16
|
+
}).join("");
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
}
|
|
18
|
+
paramsString.push(arr);
|
|
19
|
+
} else {
|
|
20
|
+
if (value !== undefined) {
|
|
21
|
+
paramsString.push(`${key}=${value}`);
|
|
25
22
|
}
|
|
26
23
|
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if(!paramsString.length) return '';
|
|
24
|
+
});
|
|
30
25
|
|
|
31
|
-
|
|
32
|
-
}
|
|
26
|
+
if (!paramsString.length) return "";
|
|
27
|
+
return `&${paramsString.join("&")}`;
|
|
28
|
+
};
|