@veritree/services 1.0.0-6 → 1.0.0-9
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 +8 -4
- package/package.json +1 -1
- package/src/endpoints/notes.js +15 -0
- package/src/endpoints/sdgs.js +5 -0
- package/src/helpers/api.js +87 -58
- package/src/utils/args.js +17 -22
package/index.js
CHANGED
|
@@ -6,15 +6,17 @@ import { ForestTypeSpecies } from './src/endpoints/forest-type-species';
|
|
|
6
6
|
import { ForestTypes } from './src/endpoints/forest-types';
|
|
7
7
|
import { ForestTypeProfiles } from './src/endpoints/forest-types-profiles';
|
|
8
8
|
import { Images } from './src/endpoints/images';
|
|
9
|
-
import { User } from './src/endpoints/user';
|
|
10
9
|
import { Orgs } from './src/endpoints/orgs';
|
|
10
|
+
import { Notes } from './src/endpoints/notes';
|
|
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';
|
|
15
16
|
import { Subsites } from './src/endpoints/subsites';
|
|
16
17
|
import { SubsiteTypes } from './src/endpoints/subsite-types';
|
|
17
18
|
import { TreeOrders } from './src/endpoints/trees-orders';
|
|
19
|
+
import { User } from './src/endpoints/user';
|
|
18
20
|
|
|
19
21
|
export {
|
|
20
22
|
Countries,
|
|
@@ -24,14 +26,16 @@ export {
|
|
|
24
26
|
ForestTypes,
|
|
25
27
|
ForestTypeProfiles,
|
|
26
28
|
FormSubmissions,
|
|
27
|
-
Orgs,
|
|
28
29
|
Images,
|
|
29
|
-
|
|
30
|
+
Orgs,
|
|
31
|
+
Notes,
|
|
30
32
|
Regions,
|
|
33
|
+
SDGs,
|
|
31
34
|
Sponsors,
|
|
32
35
|
Stats,
|
|
33
36
|
Subdomains,
|
|
34
37
|
Subsites,
|
|
35
38
|
SubsiteTypes,
|
|
36
|
-
TreeOrders
|
|
39
|
+
TreeOrders,
|
|
40
|
+
User
|
|
37
41
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Api from "../helpers/api";
|
|
2
|
+
|
|
3
|
+
class NotesApi extends Api {
|
|
4
|
+
constructor(resource) {
|
|
5
|
+
super(resource);
|
|
6
|
+
this.resource = "notes";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async relation(prefix, id, args) {
|
|
10
|
+
const url = `${this.baseUrl}/${prefix}/${id}/${this.resource}${this.getUrlParams(args)}`;
|
|
11
|
+
return await this.unWrap(url);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const Notes = new NotesApi();
|
package/src/helpers/api.js
CHANGED
|
@@ -8,7 +8,7 @@ import { getSession } from "./session";
|
|
|
8
8
|
* @param {string} url
|
|
9
9
|
* @returns {string} url
|
|
10
10
|
*/
|
|
11
|
-
function
|
|
11
|
+
function addEnvelopeParam(url) {
|
|
12
12
|
if (!url || url.includes("_result=1")) return url;
|
|
13
13
|
|
|
14
14
|
const urlHasArgs = url.includes("?");
|
|
@@ -17,10 +17,50 @@ 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 isSpoofing = as !== undefined;
|
|
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 (!data) data = {};
|
|
47
|
+
|
|
48
|
+
if (isFormData) {
|
|
49
|
+
if (isSpoofing) data.set("_method", as.toUpperCase());
|
|
50
|
+
config.body = data;
|
|
51
|
+
} else {
|
|
52
|
+
if (isSpoofing) data._method = as.toUpperCase();
|
|
53
|
+
config.body = JSON.stringify(data);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return config;
|
|
58
|
+
}
|
|
59
|
+
|
|
20
60
|
export default class Api {
|
|
21
61
|
constructor(resource) {
|
|
22
62
|
this.baseUrl = `${process.env.API_VERITREE_URL}/api`;
|
|
23
|
-
this.resource = resource;
|
|
63
|
+
this.resource = resource ? resource : "";
|
|
24
64
|
this.orgId = null;
|
|
25
65
|
this.orgType = null;
|
|
26
66
|
}
|
|
@@ -29,18 +69,19 @@ export default class Api {
|
|
|
29
69
|
*
|
|
30
70
|
* @returns {promise}
|
|
31
71
|
*/
|
|
32
|
-
async all() {
|
|
33
|
-
const url = `${this.getUrl()}${this.getUrlParams(
|
|
72
|
+
async all(args) {
|
|
73
|
+
const url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
34
74
|
return await this.get(url);
|
|
35
75
|
}
|
|
36
76
|
|
|
37
77
|
/**
|
|
38
78
|
*
|
|
39
79
|
* @param {string, number} id
|
|
80
|
+
* @param {object} args/params
|
|
40
81
|
* @returns {promise}
|
|
41
82
|
*/
|
|
42
|
-
async single(id) {
|
|
43
|
-
const url = `${this.getUrl()}
|
|
83
|
+
async single(id, args) {
|
|
84
|
+
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
44
85
|
return await this.get(url);
|
|
45
86
|
}
|
|
46
87
|
|
|
@@ -61,10 +102,22 @@ export default class Api {
|
|
|
61
102
|
* @returns {promise}
|
|
62
103
|
*/
|
|
63
104
|
async update(id, data, as = "put") {
|
|
64
|
-
const url = `${this.getUrl()}
|
|
105
|
+
const url = `${this.getUrl(id)}${this.getUrlParams()}`;
|
|
65
106
|
return await this.post(url, data, as);
|
|
66
107
|
}
|
|
67
108
|
|
|
109
|
+
/**
|
|
110
|
+
*
|
|
111
|
+
* @param {string} url
|
|
112
|
+
* @param {object} data
|
|
113
|
+
* @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
|
|
114
|
+
* @returns {promise}
|
|
115
|
+
*/
|
|
116
|
+
async delete(id) {
|
|
117
|
+
const url = `${this.getUrl(id)}${this.getUrlParams()}`;
|
|
118
|
+
return await this.post(url, null, "delete");
|
|
119
|
+
}
|
|
120
|
+
|
|
68
121
|
/**
|
|
69
122
|
*
|
|
70
123
|
* @param {string} url
|
|
@@ -87,10 +140,21 @@ export default class Api {
|
|
|
87
140
|
|
|
88
141
|
// ----------
|
|
89
142
|
// --
|
|
90
|
-
|
|
91
|
-
|
|
143
|
+
/**
|
|
144
|
+
*
|
|
145
|
+
* @param {string, number} id
|
|
146
|
+
* @returns
|
|
147
|
+
*/
|
|
148
|
+
getUrl(id) {
|
|
149
|
+
id = id ? `/${id}` : "";
|
|
150
|
+
return `${this.baseUrl}/${this.resource}${id}`;
|
|
92
151
|
}
|
|
93
152
|
|
|
153
|
+
/**
|
|
154
|
+
*
|
|
155
|
+
* @param {object} args
|
|
156
|
+
* @returns {string}
|
|
157
|
+
*/
|
|
94
158
|
getUrlParams(args) {
|
|
95
159
|
this.setOrg();
|
|
96
160
|
let isOrgLess = false;
|
|
@@ -103,11 +167,11 @@ export default class Api {
|
|
|
103
167
|
}
|
|
104
168
|
|
|
105
169
|
if (!isOrgLess) {
|
|
106
|
-
if (this.orgId) orgIdParam =
|
|
170
|
+
if (this.orgId) orgIdParam = `org_id=${this.orgId}`;
|
|
107
171
|
if (this.orgType) orgTypeParam = `&org_type=${this.orgType}`;
|
|
108
172
|
}
|
|
109
173
|
|
|
110
|
-
return `?${orgIdParam}
|
|
174
|
+
return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(args)}`;
|
|
111
175
|
}
|
|
112
176
|
|
|
113
177
|
/**
|
|
@@ -119,55 +183,20 @@ export default class Api {
|
|
|
119
183
|
* @returns {object} envelope
|
|
120
184
|
*/
|
|
121
185
|
async unWrap(url, method = "get", data, as) {
|
|
122
|
-
url =
|
|
123
|
-
const config =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
} catch (err) {
|
|
131
|
-
throw new Error(err);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
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);
|
|
186
|
+
url = addEnvelopeParam(url);
|
|
187
|
+
const config = getConfig(method, data, as);
|
|
188
|
+
const response = await fetch(url, config);
|
|
189
|
+
|
|
190
|
+
return new Promise((resolve, reject) => {
|
|
191
|
+
if (response.ok && response.status === 204) {
|
|
192
|
+
resolve();
|
|
193
|
+
return;
|
|
167
194
|
}
|
|
168
|
-
}
|
|
169
195
|
|
|
170
|
-
|
|
196
|
+
response.json().then((json) => {
|
|
197
|
+
response.ok ? resolve(json) : reject(json);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
171
200
|
}
|
|
172
201
|
|
|
173
202
|
setOrg() {
|
package/src/utils/args.js
CHANGED
|
@@ -1,32 +1,27 @@
|
|
|
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
|
-
return `&${paramsString.join(
|
|
32
|
-
}
|
|
26
|
+
return paramsString.length ? `&${paramsString.join("&")}` : "";
|
|
27
|
+
};
|