@veritree/services 1.0.0-4 → 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 +19 -17
- package/package.json +1 -1
- package/src/{services → endpoints}/countries.js +0 -0
- package/src/{services → endpoints}/field-udpate-verifications.js +0 -0
- package/src/{services → endpoints}/field-updates.js +0 -0
- package/src/{services → endpoints}/forest-type-species.js +0 -0
- package/src/{services → endpoints}/forest-types-profiles.js +0 -0
- package/src/{services → endpoints}/forest-types.js +0 -0
- package/src/{services → endpoints}/form-submissions.js +0 -0
- package/src/{services → endpoints}/images.js +0 -0
- package/src/{services → endpoints}/orgs.js +4 -4
- package/src/{services → endpoints}/regions.js +0 -0
- package/src/endpoints/sdgs.js +5 -0
- package/src/{services → endpoints}/sponsors.js +0 -0
- package/src/{services → endpoints}/stats.js +0 -0
- package/src/{services → endpoints}/subdomains.js +0 -0
- package/src/{services → endpoints}/subsite-types.js +0 -0
- package/src/{services → endpoints}/subsites.js +0 -0
- package/src/{services → endpoints}/trees-orders.js +1 -0
- package/src/{services → endpoints}/user.js +0 -0
- package/src/helpers/api.js +98 -80
- package/src/helpers/session.js +0 -4
- package/src/utils/args.js +18 -22
package/index.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import { Countries } from './src/
|
|
2
|
-
import { FieldUpdates } from './src/
|
|
3
|
-
import { FieldUpdateVerifications } from './src/
|
|
4
|
-
import { FormSubmissions } from './src/
|
|
5
|
-
import { ForestTypeSpecies } from './src/
|
|
6
|
-
import { ForestTypes } from './src/
|
|
7
|
-
import { ForestTypeProfiles } from './src/
|
|
8
|
-
import { Images } from './src/
|
|
9
|
-
import { User } from './src/
|
|
10
|
-
import { Orgs } from './src/
|
|
11
|
-
import { Regions } from './src/
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
1
|
+
import { Countries } from './src/endpoints/countries';
|
|
2
|
+
import { FieldUpdates } from './src/endpoints/field-updates';
|
|
3
|
+
import { FieldUpdateVerifications } from './src/endpoints/field-udpate-verifications';
|
|
4
|
+
import { FormSubmissions } from './src/endpoints/form-submissions';
|
|
5
|
+
import { ForestTypeSpecies } from './src/endpoints/forest-type-species';
|
|
6
|
+
import { ForestTypes } from './src/endpoints/forest-types';
|
|
7
|
+
import { ForestTypeProfiles } from './src/endpoints/forest-types-profiles';
|
|
8
|
+
import { Images } from './src/endpoints/images';
|
|
9
|
+
import { User } from './src/endpoints/user';
|
|
10
|
+
import { Orgs } from './src/endpoints/orgs';
|
|
11
|
+
import { Regions } from './src/endpoints/regions';
|
|
12
|
+
import { SDGs } from './src/endpoints/sdgs';
|
|
13
|
+
import { Sponsors } from './src/endpoints/sponsors';
|
|
14
|
+
import { Stats } from './src/endpoints/stats';
|
|
15
|
+
import { Subdomains } from './src/endpoints/subdomains';
|
|
16
|
+
import { Subsites } from './src/endpoints/subsites';
|
|
17
|
+
import { SubsiteTypes } from './src/endpoints/subsite-types';
|
|
18
|
+
import { TreeOrders } from './src/endpoints/trees-orders';
|
|
18
19
|
|
|
19
20
|
export {
|
|
20
21
|
Countries,
|
|
@@ -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
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import Api from
|
|
1
|
+
import Api from "../helpers/api";
|
|
2
2
|
|
|
3
3
|
class OrgsApi extends Api {
|
|
4
4
|
constructor(resource) {
|
|
5
5
|
super(resource);
|
|
6
|
-
this.resource =
|
|
6
|
+
this.resource = "orgs";
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
async stats() {
|
|
@@ -16,8 +16,8 @@ class OrgsApi extends Api {
|
|
|
16
16
|
return await this.get(url);
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
_geStatstUrl
|
|
20
|
-
const endpoint = isPublic ?
|
|
19
|
+
_geStatstUrl(isPublic) {
|
|
20
|
+
const endpoint = isPublic ? "pstats" : "stats";
|
|
21
21
|
return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/src/helpers/api.js
CHANGED
|
@@ -2,6 +2,59 @@ import { getCookie } from "./cookies";
|
|
|
2
2
|
import { createParamsStringFromArgs } from "../utils/args";
|
|
3
3
|
import { getSession } from "./session";
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Adds the envelope argument to the url
|
|
7
|
+
*
|
|
8
|
+
* @param {string} url
|
|
9
|
+
* @returns {string} url
|
|
10
|
+
*/
|
|
11
|
+
function handleEnvelopParam(url) {
|
|
12
|
+
if (!url || url.includes("_result=1")) return url;
|
|
13
|
+
|
|
14
|
+
const urlHasArgs = url.includes("?");
|
|
15
|
+
const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
|
|
16
|
+
|
|
17
|
+
return `${url}${urlEvenlopeArg}`;
|
|
18
|
+
}
|
|
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
|
+
|
|
5
58
|
export default class Api {
|
|
6
59
|
constructor(resource) {
|
|
7
60
|
this.baseUrl = `${process.env.API_VERITREE_URL}/api`;
|
|
@@ -14,18 +67,19 @@ export default class Api {
|
|
|
14
67
|
*
|
|
15
68
|
* @returns {promise}
|
|
16
69
|
*/
|
|
17
|
-
async all() {
|
|
18
|
-
const url = `${this.getUrl()}${this.getUrlParams(
|
|
70
|
+
async all(args) {
|
|
71
|
+
const url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
19
72
|
return await this.get(url);
|
|
20
73
|
}
|
|
21
74
|
|
|
22
75
|
/**
|
|
23
76
|
*
|
|
24
77
|
* @param {string, number} id
|
|
78
|
+
* @param {object} args/params
|
|
25
79
|
* @returns {promise}
|
|
26
80
|
*/
|
|
27
|
-
async single(id) {
|
|
28
|
-
const url = `${this.getUrl()}
|
|
81
|
+
async single(id, args) {
|
|
82
|
+
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
29
83
|
return await this.get(url);
|
|
30
84
|
}
|
|
31
85
|
|
|
@@ -46,7 +100,7 @@ export default class Api {
|
|
|
46
100
|
* @returns {promise}
|
|
47
101
|
*/
|
|
48
102
|
async update(id, data, as = "put") {
|
|
49
|
-
const url = `${this.getUrl()}
|
|
103
|
+
const url = `${this.getUrl(id)}${this.getUrlParams()}`;
|
|
50
104
|
return await this.post(url, data, as);
|
|
51
105
|
}
|
|
52
106
|
|
|
@@ -55,9 +109,9 @@ export default class Api {
|
|
|
55
109
|
* @param {string} url
|
|
56
110
|
* @returns {promise}
|
|
57
111
|
*/
|
|
58
|
-
|
|
112
|
+
async get(url) {
|
|
59
113
|
return await this.unWrap(url);
|
|
60
|
-
}
|
|
114
|
+
}
|
|
61
115
|
|
|
62
116
|
/**
|
|
63
117
|
*
|
|
@@ -65,27 +119,46 @@ export default class Api {
|
|
|
65
119
|
* @param {object} data
|
|
66
120
|
* @returns {promise}
|
|
67
121
|
*/
|
|
68
|
-
|
|
122
|
+
async post(url, data, as) {
|
|
69
123
|
if (!url) url = `${this.getUrl()}${this.getUrlParams()}`;
|
|
70
124
|
return await this.unWrap(url, "post", data, as);
|
|
71
|
-
}
|
|
125
|
+
}
|
|
72
126
|
|
|
73
127
|
// ----------
|
|
74
128
|
// --
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* @param {string, number} id
|
|
132
|
+
* @returns
|
|
133
|
+
*/
|
|
134
|
+
getUrl(id) {
|
|
135
|
+
id = id ? `/${id}` : "";
|
|
136
|
+
return `${this.baseUrl}/${this.resource}${id}`;
|
|
137
|
+
}
|
|
78
138
|
|
|
79
|
-
|
|
139
|
+
/**
|
|
140
|
+
*
|
|
141
|
+
* @param {object} args
|
|
142
|
+
* @returns {string}
|
|
143
|
+
*/
|
|
144
|
+
getUrlParams(args) {
|
|
80
145
|
this.setOrg();
|
|
146
|
+
let isOrgLess = false;
|
|
147
|
+
let orgIdParam = "";
|
|
148
|
+
let orgTypeParam = "";
|
|
149
|
+
|
|
150
|
+
// while most of endpoints require an org id and type, some endpoints do not
|
|
151
|
+
if (args && args.length) {
|
|
152
|
+
isOrgLess = Object.hasOwnProperty.call(...args, "orgless");
|
|
153
|
+
}
|
|
81
154
|
|
|
82
|
-
|
|
83
|
-
|
|
155
|
+
if (!isOrgLess) {
|
|
156
|
+
if (this.orgId) orgIdParam = `&org_id=${this.orgId}`;
|
|
157
|
+
if (this.orgType) orgTypeParam = `&org_type=${this.orgType}`;
|
|
158
|
+
}
|
|
84
159
|
|
|
85
|
-
return `?${orgIdParam}&${orgTypeParam}${createParamsStringFromArgs(
|
|
86
|
-
|
|
87
|
-
)}`;
|
|
88
|
-
};
|
|
160
|
+
return `?${orgIdParam}&${orgTypeParam}${createParamsStringFromArgs(args)}`;
|
|
161
|
+
}
|
|
89
162
|
|
|
90
163
|
/**
|
|
91
164
|
* Deals with all fetch requests
|
|
@@ -95,9 +168,9 @@ export default class Api {
|
|
|
95
168
|
* @param {object} data
|
|
96
169
|
* @returns {object} envelope
|
|
97
170
|
*/
|
|
98
|
-
|
|
99
|
-
url =
|
|
100
|
-
const config =
|
|
171
|
+
async unWrap(url, method = "get", data, as) {
|
|
172
|
+
url = handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
|
|
173
|
+
const config = getConfig(method, data, as);
|
|
101
174
|
|
|
102
175
|
try {
|
|
103
176
|
const response = await fetch(url, config);
|
|
@@ -107,68 +180,13 @@ export default class Api {
|
|
|
107
180
|
} catch (err) {
|
|
108
181
|
throw new Error(err);
|
|
109
182
|
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Handles how the data should be sent in the fetch method
|
|
114
|
-
*
|
|
115
|
-
* @param {string} method
|
|
116
|
-
* @param {object} body
|
|
117
|
-
* @returns {object} data
|
|
118
|
-
*/
|
|
119
|
-
getConfig = function (method, data, as) {
|
|
120
|
-
const isGet = method === "get";
|
|
121
|
-
const isPut = as === "put";
|
|
122
|
-
const isFormData = this.isFormData(data);
|
|
123
|
-
const accessToken = `Bearer ${getCookie("access_token")}`;
|
|
124
|
-
|
|
125
|
-
const config = {
|
|
126
|
-
method,
|
|
127
|
-
headers: {
|
|
128
|
-
Authorization: accessToken,
|
|
129
|
-
},
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
if (!isFormData) {
|
|
133
|
-
config.headers["Content-Type"] = "application/json";
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// TODO: improve this ifs and elses
|
|
137
|
-
if (!isGet) {
|
|
138
|
-
if (isFormData) {
|
|
139
|
-
if (isPut) data.set("_method", "PUT");
|
|
140
|
-
config.body = data;
|
|
141
|
-
} else {
|
|
142
|
-
if (isPut) data._method = "PUT";
|
|
143
|
-
config.body = JSON.stringify(data);
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return config;
|
|
148
|
-
};
|
|
183
|
+
}
|
|
149
184
|
|
|
150
|
-
setOrg
|
|
185
|
+
setOrg() {
|
|
151
186
|
const session = getSession();
|
|
152
|
-
if(!session) return;
|
|
187
|
+
if (!session) return;
|
|
153
188
|
const { orgId, orgType } = session;
|
|
154
189
|
this.orgId = orgId;
|
|
155
190
|
this.orgType = orgType;
|
|
156
191
|
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Adds the envelope argument to the url
|
|
160
|
-
*
|
|
161
|
-
* @param {string} url
|
|
162
|
-
* @returns {string} url
|
|
163
|
-
*/
|
|
164
|
-
handleEnvelopParam = function (url) {
|
|
165
|
-
if (!url || url.includes("_result=1")) return url;
|
|
166
|
-
|
|
167
|
-
const urlHasArgs = url.includes("?");
|
|
168
|
-
const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
|
|
169
|
-
|
|
170
|
-
return `${url}${urlEvenlopeArg}`;
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
isFormData = (data) => data instanceof FormData;
|
|
174
192
|
}
|
package/src/helpers/session.js
CHANGED
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
|
+
};
|