@veritree/services 2.34.0 → 2.35.0
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 +83 -83
- package/package.json +16 -16
- package/src/endpoints/bulk-uploads.js +4 -4
- package/src/endpoints/countries.js +5 -5
- package/src/endpoints/crumbs.js +20 -20
- package/src/endpoints/evidence.js +47 -47
- package/src/endpoints/external-reports.js +9 -9
- package/src/endpoints/field-reports.js +37 -37
- package/src/endpoints/field-udpate-verifications.js +5 -5
- package/src/endpoints/field-updates.js +16 -16
- package/src/endpoints/forest-type-species.js +4 -4
- package/src/endpoints/forest-types-profiles.js +4 -4
- package/src/endpoints/forest-types.js +14 -14
- package/src/endpoints/form-submissions.js +4 -4
- package/src/endpoints/geometries.js +4 -4
- package/src/endpoints/images-aggregated.js +4 -4
- package/src/endpoints/images.js +21 -21
- package/src/endpoints/methodologies.js +36 -36
- package/src/endpoints/notes.js +14 -14
- package/src/endpoints/organizations.js +59 -59
- package/src/endpoints/orgs.js +65 -65
- package/src/endpoints/password.js +58 -58
- package/src/endpoints/planting-sites.js +121 -110
- package/src/endpoints/regions.js +41 -41
- package/src/endpoints/resellers.js +37 -37
- package/src/endpoints/roles.js +4 -4
- package/src/endpoints/sdgs.js +4 -4
- package/src/endpoints/sponsors.js +86 -86
- package/src/endpoints/sso-token.js +5 -5
- package/src/endpoints/standards.js +140 -140
- package/src/endpoints/stats.js +17 -17
- package/src/endpoints/subdomains.js +4 -4
- package/src/endpoints/subsite-types.js +5 -5
- package/src/endpoints/subsites.js +88 -88
- package/src/endpoints/tags.js +14 -14
- package/src/endpoints/tasks.js +47 -47
- package/src/endpoints/tree-order-statuses.js +4 -4
- package/src/endpoints/trees-orders.js +23 -23
- package/src/endpoints/user.js +27 -27
- package/src/endpoints/users.js +14 -14
- package/src/endpoints/utilities.js +35 -35
- package/src/endpoints/verifications.js +21 -21
- package/src/helpers/api.js +293 -293
- package/src/helpers/cookies.js +36 -36
- package/src/helpers/relations.js +32 -32
- package/src/helpers/sequestrations.js +19 -19
- package/src/helpers/session.js +3 -3
- package/src/helpers/team.js +45 -45
- package/src/utils/args.js +46 -46
package/src/helpers/api.js
CHANGED
|
@@ -1,293 +1,293 @@
|
|
|
1
|
-
import { getCookie } from "./cookies";
|
|
2
|
-
import { createParamsStringFromArgs } from "../utils/args";
|
|
3
|
-
import { getSession } from "./session";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Returns the runtime configuration object for Nuxt 3.
|
|
7
|
-
*
|
|
8
|
-
* Note:
|
|
9
|
-
* This function should only be used with Nuxt 3.
|
|
10
|
-
* For Nuxt 2, the environment variables will work as expected.
|
|
11
|
-
*
|
|
12
|
-
* @returns {Object|null} - The runtime configuration object, or null if it's not available.
|
|
13
|
-
*/
|
|
14
|
-
// function getNuxtRuntimeConfig() {
|
|
15
|
-
// if (typeof useRuntimeConfig === "function") {
|
|
16
|
-
// return useRuntimeConfig();
|
|
17
|
-
// } else {
|
|
18
|
-
// return null;
|
|
19
|
-
// }
|
|
20
|
-
// }
|
|
21
|
-
|
|
22
|
-
// function getVersion() {
|
|
23
|
-
// if (process) {
|
|
24
|
-
// process?.env?.API_VERITREE_VERSION;
|
|
25
|
-
// } else {
|
|
26
|
-
// const nuxtRuntimeConfig = getNuxtRuntimeConfig();
|
|
27
|
-
|
|
28
|
-
// if (nuxtRuntimeConfig) {
|
|
29
|
-
// return nuxtRuntimeConfig.public.API_VERITREE_VERSION;
|
|
30
|
-
// }
|
|
31
|
-
// }
|
|
32
|
-
|
|
33
|
-
// return "5.0.0";
|
|
34
|
-
// }
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Adds a version parameter to a URL if it does not already have one.
|
|
38
|
-
* If an environment variable called API_VERITREE_VERSION is defined, its value is used as the version,
|
|
39
|
-
* otherwise the default version "5.0.0" is used.
|
|
40
|
-
*
|
|
41
|
-
* @param {string} url - The URL to modify.
|
|
42
|
-
* @returns {string} The modified URL with the version parameter appended to it.
|
|
43
|
-
*/
|
|
44
|
-
function addVersionParam(url) {
|
|
45
|
-
// If URL is invalid or already has the result parameter, return it as is
|
|
46
|
-
if (!url || url.includes("_result=1")) {
|
|
47
|
-
return url;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// Check if URL already has the version parameter
|
|
51
|
-
if (url.includes("_v=")) {
|
|
52
|
-
return url;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// Use environment variable if available, otherwise use default version
|
|
56
|
-
const version = process.env.API_VERITREE_VERSION || "5.0.0";
|
|
57
|
-
|
|
58
|
-
// Append version parameter to URL
|
|
59
|
-
const urlVersionParam = url.includes("?")
|
|
60
|
-
? `&_v=${version}`
|
|
61
|
-
: `?_v=${version}`;
|
|
62
|
-
|
|
63
|
-
return `${url}${urlVersionParam}`;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Handles how the data should be sent in the fetch method
|
|
68
|
-
*
|
|
69
|
-
* @param {string} method
|
|
70
|
-
* @param {object} body
|
|
71
|
-
* @returns {object} data
|
|
72
|
-
*/
|
|
73
|
-
function getConfig(method, data, as) {
|
|
74
|
-
const isGet = method === "get";
|
|
75
|
-
const isSpoofing = as;
|
|
76
|
-
const isFormData = data instanceof FormData;
|
|
77
|
-
const accessToken = `Bearer ${getCookie("access_token")}`;
|
|
78
|
-
|
|
79
|
-
const config = {
|
|
80
|
-
method,
|
|
81
|
-
headers: {
|
|
82
|
-
Authorization: accessToken,
|
|
83
|
-
},
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
if (!isFormData) {
|
|
87
|
-
config.headers["Content-Type"] = "application/json";
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// TODO: improve this ifs and elses
|
|
91
|
-
if (!isGet) {
|
|
92
|
-
if (!data) data = {};
|
|
93
|
-
|
|
94
|
-
if (isFormData) {
|
|
95
|
-
if (isSpoofing) data.set("_method", as.toUpperCase());
|
|
96
|
-
config.body = data;
|
|
97
|
-
} else {
|
|
98
|
-
if (isSpoofing) data._method = as.toUpperCase();
|
|
99
|
-
config.body = JSON.stringify(data);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return config;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export default class Api {
|
|
107
|
-
constructor(resource) {
|
|
108
|
-
// Nuxt 3
|
|
109
|
-
// const nuxtConfig = getNuxtRuntimeConfig();
|
|
110
|
-
// const url =
|
|
111
|
-
// nuxtConfig?.public?.API_VERITREE_URL ||
|
|
112
|
-
// process?.env?.API_VERITREE_URL ||
|
|
113
|
-
// null;
|
|
114
|
-
|
|
115
|
-
// this.baseUrl = url + "/api";
|
|
116
|
-
|
|
117
|
-
this.baseUrl = process.env ? `${process.env.API_VERITREE_URL}/api` : null;
|
|
118
|
-
this.resource = resource ? resource : "";
|
|
119
|
-
this.orgId = null;
|
|
120
|
-
this.orgType = null;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
setBaseUrl(baseUrl) {
|
|
124
|
-
this.baseUrl = baseUrl;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
*
|
|
129
|
-
* @returns {promise}
|
|
130
|
-
*/
|
|
131
|
-
async all(args) {
|
|
132
|
-
const url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
133
|
-
return await this.get(url);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
*
|
|
138
|
-
* @param {string, number} id
|
|
139
|
-
* @param {object} args/params
|
|
140
|
-
* @returns {promise}
|
|
141
|
-
*/
|
|
142
|
-
async single(id, args) {
|
|
143
|
-
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
144
|
-
return await this.get(url);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
*
|
|
149
|
-
* @param {object} data
|
|
150
|
-
* @returns {promise}
|
|
151
|
-
*/
|
|
152
|
-
async create(data, args) {
|
|
153
|
-
return await this.post(null, data, null, args);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
*
|
|
158
|
-
* @param {string} url
|
|
159
|
-
* @param {object} data
|
|
160
|
-
* @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
|
|
161
|
-
* @returns {promise}
|
|
162
|
-
*/
|
|
163
|
-
async update(id, data, as = "put", args) {
|
|
164
|
-
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
165
|
-
return await this.post(url, data, as);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
*
|
|
170
|
-
* @param {string} url
|
|
171
|
-
* @param {object} data
|
|
172
|
-
* @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
|
|
173
|
-
* @returns {promise}
|
|
174
|
-
*/
|
|
175
|
-
async delete(id, args) {
|
|
176
|
-
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
177
|
-
return await this.post(url, null, "delete");
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
/**
|
|
181
|
-
*
|
|
182
|
-
* @param {string} url
|
|
183
|
-
* @returns {promise}
|
|
184
|
-
*/
|
|
185
|
-
async get(url) {
|
|
186
|
-
return await this.unWrap(url);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/**
|
|
190
|
-
*
|
|
191
|
-
* @param {string} url
|
|
192
|
-
* @param {object} data
|
|
193
|
-
* @returns {promise}
|
|
194
|
-
*/
|
|
195
|
-
async post(url, data, as, args) {
|
|
196
|
-
if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
197
|
-
return await this.unWrap(url, "post", data, as);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// ----------
|
|
201
|
-
// --
|
|
202
|
-
/**
|
|
203
|
-
*
|
|
204
|
-
* @param {string, number} id
|
|
205
|
-
* @returns
|
|
206
|
-
*/
|
|
207
|
-
getUrl(id) {
|
|
208
|
-
id = id ? `/${id}` : "";
|
|
209
|
-
return `${this.baseUrl}/${this.resource}${id}`;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
*
|
|
214
|
-
* @param {object} args
|
|
215
|
-
* @returns {string}
|
|
216
|
-
*/
|
|
217
|
-
getUrlParams(args) {
|
|
218
|
-
this.setOrg();
|
|
219
|
-
let isOrgLess = false;
|
|
220
|
-
let isOrgIdAs = false;
|
|
221
|
-
let isOrgTypeAs = false;
|
|
222
|
-
let orgIdParam = "";
|
|
223
|
-
let orgTypeParam = "";
|
|
224
|
-
let argsClone = structuredClone(args); // avoids mutating object
|
|
225
|
-
|
|
226
|
-
// while most of endpoints require an org id and type, some endpoints do not
|
|
227
|
-
if (argsClone) {
|
|
228
|
-
isOrgLess = Object.hasOwn(argsClone, "orgless");
|
|
229
|
-
isOrgIdAs = Object.hasOwn(argsClone, "org_id_as");
|
|
230
|
-
isOrgTypeAs = Object.hasOwn(argsClone, "org_type_as");
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// a super admin user can create/edit data from other orgs
|
|
234
|
-
// and for that, we can't use the org id and type in
|
|
235
|
-
// the session.
|
|
236
|
-
if (isOrgIdAs) {
|
|
237
|
-
this.orgId = argsClone.org_id_as;
|
|
238
|
-
delete argsClone.org_id_as;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
if (isOrgTypeAs) {
|
|
242
|
-
this.orgType = argsClone.org_type_as;
|
|
243
|
-
delete argsClone.org_type_as;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (!isOrgLess) {
|
|
247
|
-
if (this.orgId) {
|
|
248
|
-
orgIdParam = `org_id=${this.orgId}`;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
if (this.orgType) {
|
|
252
|
-
orgTypeParam = `&org_type=${this.orgType}`;
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(
|
|
257
|
-
argsClone
|
|
258
|
-
)}`;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Deals with all fetch requests
|
|
263
|
-
*
|
|
264
|
-
* @param {string} url
|
|
265
|
-
* @param {string} method
|
|
266
|
-
* @param {object} data
|
|
267
|
-
* @returns {object} envelope
|
|
268
|
-
*/
|
|
269
|
-
async unWrap(url, method = "get", data, as) {
|
|
270
|
-
url = addVersionParam(url);
|
|
271
|
-
const config = getConfig(method, data, as);
|
|
272
|
-
const response = await fetch(url, config);
|
|
273
|
-
|
|
274
|
-
return new Promise((resolve, reject) => {
|
|
275
|
-
if (response.ok && response.status === 204) {
|
|
276
|
-
resolve();
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
response.json().then((json) => {
|
|
281
|
-
response.ok ? resolve(json) : reject(json);
|
|
282
|
-
});
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
setOrg() {
|
|
287
|
-
const session = getSession();
|
|
288
|
-
if (!session) return;
|
|
289
|
-
const { orgId, orgType } = session;
|
|
290
|
-
this.orgId = orgId;
|
|
291
|
-
this.orgType = orgType;
|
|
292
|
-
}
|
|
293
|
-
}
|
|
1
|
+
import { getCookie } from "./cookies";
|
|
2
|
+
import { createParamsStringFromArgs } from "../utils/args";
|
|
3
|
+
import { getSession } from "./session";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns the runtime configuration object for Nuxt 3.
|
|
7
|
+
*
|
|
8
|
+
* Note:
|
|
9
|
+
* This function should only be used with Nuxt 3.
|
|
10
|
+
* For Nuxt 2, the environment variables will work as expected.
|
|
11
|
+
*
|
|
12
|
+
* @returns {Object|null} - The runtime configuration object, or null if it's not available.
|
|
13
|
+
*/
|
|
14
|
+
// function getNuxtRuntimeConfig() {
|
|
15
|
+
// if (typeof useRuntimeConfig === "function") {
|
|
16
|
+
// return useRuntimeConfig();
|
|
17
|
+
// } else {
|
|
18
|
+
// return null;
|
|
19
|
+
// }
|
|
20
|
+
// }
|
|
21
|
+
|
|
22
|
+
// function getVersion() {
|
|
23
|
+
// if (process) {
|
|
24
|
+
// process?.env?.API_VERITREE_VERSION;
|
|
25
|
+
// } else {
|
|
26
|
+
// const nuxtRuntimeConfig = getNuxtRuntimeConfig();
|
|
27
|
+
|
|
28
|
+
// if (nuxtRuntimeConfig) {
|
|
29
|
+
// return nuxtRuntimeConfig.public.API_VERITREE_VERSION;
|
|
30
|
+
// }
|
|
31
|
+
// }
|
|
32
|
+
|
|
33
|
+
// return "5.0.0";
|
|
34
|
+
// }
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Adds a version parameter to a URL if it does not already have one.
|
|
38
|
+
* If an environment variable called API_VERITREE_VERSION is defined, its value is used as the version,
|
|
39
|
+
* otherwise the default version "5.0.0" is used.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} url - The URL to modify.
|
|
42
|
+
* @returns {string} The modified URL with the version parameter appended to it.
|
|
43
|
+
*/
|
|
44
|
+
function addVersionParam(url) {
|
|
45
|
+
// If URL is invalid or already has the result parameter, return it as is
|
|
46
|
+
if (!url || url.includes("_result=1")) {
|
|
47
|
+
return url;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Check if URL already has the version parameter
|
|
51
|
+
if (url.includes("_v=")) {
|
|
52
|
+
return url;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Use environment variable if available, otherwise use default version
|
|
56
|
+
const version = process.env.API_VERITREE_VERSION || "5.0.0";
|
|
57
|
+
|
|
58
|
+
// Append version parameter to URL
|
|
59
|
+
const urlVersionParam = url.includes("?")
|
|
60
|
+
? `&_v=${version}`
|
|
61
|
+
: `?_v=${version}`;
|
|
62
|
+
|
|
63
|
+
return `${url}${urlVersionParam}`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Handles how the data should be sent in the fetch method
|
|
68
|
+
*
|
|
69
|
+
* @param {string} method
|
|
70
|
+
* @param {object} body
|
|
71
|
+
* @returns {object} data
|
|
72
|
+
*/
|
|
73
|
+
function getConfig(method, data, as) {
|
|
74
|
+
const isGet = method === "get";
|
|
75
|
+
const isSpoofing = as;
|
|
76
|
+
const isFormData = data instanceof FormData;
|
|
77
|
+
const accessToken = `Bearer ${getCookie("access_token")}`;
|
|
78
|
+
|
|
79
|
+
const config = {
|
|
80
|
+
method,
|
|
81
|
+
headers: {
|
|
82
|
+
Authorization: accessToken,
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
if (!isFormData) {
|
|
87
|
+
config.headers["Content-Type"] = "application/json";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// TODO: improve this ifs and elses
|
|
91
|
+
if (!isGet) {
|
|
92
|
+
if (!data) data = {};
|
|
93
|
+
|
|
94
|
+
if (isFormData) {
|
|
95
|
+
if (isSpoofing) data.set("_method", as.toUpperCase());
|
|
96
|
+
config.body = data;
|
|
97
|
+
} else {
|
|
98
|
+
if (isSpoofing) data._method = as.toUpperCase();
|
|
99
|
+
config.body = JSON.stringify(data);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return config;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default class Api {
|
|
107
|
+
constructor(resource) {
|
|
108
|
+
// Nuxt 3
|
|
109
|
+
// const nuxtConfig = getNuxtRuntimeConfig();
|
|
110
|
+
// const url =
|
|
111
|
+
// nuxtConfig?.public?.API_VERITREE_URL ||
|
|
112
|
+
// process?.env?.API_VERITREE_URL ||
|
|
113
|
+
// null;
|
|
114
|
+
|
|
115
|
+
// this.baseUrl = url + "/api";
|
|
116
|
+
|
|
117
|
+
this.baseUrl = process.env ? `${process.env.API_VERITREE_URL}/api` : null;
|
|
118
|
+
this.resource = resource ? resource : "";
|
|
119
|
+
this.orgId = null;
|
|
120
|
+
this.orgType = null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
setBaseUrl(baseUrl) {
|
|
124
|
+
this.baseUrl = baseUrl;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
*
|
|
129
|
+
* @returns {promise}
|
|
130
|
+
*/
|
|
131
|
+
async all(args) {
|
|
132
|
+
const url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
133
|
+
return await this.get(url);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
*
|
|
138
|
+
* @param {string, number} id
|
|
139
|
+
* @param {object} args/params
|
|
140
|
+
* @returns {promise}
|
|
141
|
+
*/
|
|
142
|
+
async single(id, args) {
|
|
143
|
+
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
144
|
+
return await this.get(url);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
*
|
|
149
|
+
* @param {object} data
|
|
150
|
+
* @returns {promise}
|
|
151
|
+
*/
|
|
152
|
+
async create(data, args) {
|
|
153
|
+
return await this.post(null, data, null, args);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
*
|
|
158
|
+
* @param {string} url
|
|
159
|
+
* @param {object} data
|
|
160
|
+
* @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
|
|
161
|
+
* @returns {promise}
|
|
162
|
+
*/
|
|
163
|
+
async update(id, data, as = "put", args) {
|
|
164
|
+
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
165
|
+
return await this.post(url, data, as);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
*
|
|
170
|
+
* @param {string} url
|
|
171
|
+
* @param {object} data
|
|
172
|
+
* @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
|
|
173
|
+
* @returns {promise}
|
|
174
|
+
*/
|
|
175
|
+
async delete(id, args) {
|
|
176
|
+
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
177
|
+
return await this.post(url, null, "delete");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
*
|
|
182
|
+
* @param {string} url
|
|
183
|
+
* @returns {promise}
|
|
184
|
+
*/
|
|
185
|
+
async get(url) {
|
|
186
|
+
return await this.unWrap(url);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
*
|
|
191
|
+
* @param {string} url
|
|
192
|
+
* @param {object} data
|
|
193
|
+
* @returns {promise}
|
|
194
|
+
*/
|
|
195
|
+
async post(url, data, as, args) {
|
|
196
|
+
if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
197
|
+
return await this.unWrap(url, "post", data, as);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// ----------
|
|
201
|
+
// --
|
|
202
|
+
/**
|
|
203
|
+
*
|
|
204
|
+
* @param {string, number} id
|
|
205
|
+
* @returns
|
|
206
|
+
*/
|
|
207
|
+
getUrl(id) {
|
|
208
|
+
id = id ? `/${id}` : "";
|
|
209
|
+
return `${this.baseUrl}/${this.resource}${id}`;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
*
|
|
214
|
+
* @param {object} args
|
|
215
|
+
* @returns {string}
|
|
216
|
+
*/
|
|
217
|
+
getUrlParams(args) {
|
|
218
|
+
this.setOrg();
|
|
219
|
+
let isOrgLess = false;
|
|
220
|
+
let isOrgIdAs = false;
|
|
221
|
+
let isOrgTypeAs = false;
|
|
222
|
+
let orgIdParam = "";
|
|
223
|
+
let orgTypeParam = "";
|
|
224
|
+
let argsClone = structuredClone(args); // avoids mutating object
|
|
225
|
+
|
|
226
|
+
// while most of endpoints require an org id and type, some endpoints do not
|
|
227
|
+
if (argsClone) {
|
|
228
|
+
isOrgLess = Object.hasOwn(argsClone, "orgless");
|
|
229
|
+
isOrgIdAs = Object.hasOwn(argsClone, "org_id_as");
|
|
230
|
+
isOrgTypeAs = Object.hasOwn(argsClone, "org_type_as");
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// a super admin user can create/edit data from other orgs
|
|
234
|
+
// and for that, we can't use the org id and type in
|
|
235
|
+
// the session.
|
|
236
|
+
if (isOrgIdAs) {
|
|
237
|
+
this.orgId = argsClone.org_id_as;
|
|
238
|
+
delete argsClone.org_id_as;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (isOrgTypeAs) {
|
|
242
|
+
this.orgType = argsClone.org_type_as;
|
|
243
|
+
delete argsClone.org_type_as;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (!isOrgLess) {
|
|
247
|
+
if (this.orgId) {
|
|
248
|
+
orgIdParam = `org_id=${this.orgId}`;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (this.orgType) {
|
|
252
|
+
orgTypeParam = `&org_type=${this.orgType}`;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(
|
|
257
|
+
argsClone
|
|
258
|
+
)}`;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Deals with all fetch requests
|
|
263
|
+
*
|
|
264
|
+
* @param {string} url
|
|
265
|
+
* @param {string} method
|
|
266
|
+
* @param {object} data
|
|
267
|
+
* @returns {object} envelope
|
|
268
|
+
*/
|
|
269
|
+
async unWrap(url, method = "get", data, as) {
|
|
270
|
+
url = addVersionParam(url);
|
|
271
|
+
const config = getConfig(method, data, as);
|
|
272
|
+
const response = await fetch(url, config);
|
|
273
|
+
|
|
274
|
+
return new Promise((resolve, reject) => {
|
|
275
|
+
if (response.ok && response.status === 204) {
|
|
276
|
+
resolve();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
response.json().then((json) => {
|
|
281
|
+
response.ok ? resolve(json) : reject(json);
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
setOrg() {
|
|
287
|
+
const session = getSession();
|
|
288
|
+
if (!session) return;
|
|
289
|
+
const { orgId, orgType } = session;
|
|
290
|
+
this.orgId = orgId;
|
|
291
|
+
this.orgType = orgType;
|
|
292
|
+
}
|
|
293
|
+
}
|
package/src/helpers/cookies.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
const converter = {
|
|
2
|
-
read(value) {
|
|
3
|
-
if (value[0] === '"') {
|
|
4
|
-
value = value.slice(1, -1);
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
|
|
8
|
-
},
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export function getCookie(name) {
|
|
12
|
-
if (typeof document === 'undefined' || (arguments.length && !name)) {
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// To prevent the for loop in the first place assign an empty array
|
|
17
|
-
// in case there are no cookies at all.
|
|
18
|
-
const cookies = document.cookie ? document.cookie.split('; ') : [];
|
|
19
|
-
|
|
20
|
-
const jar = {};
|
|
21
|
-
for (let i = 0; i < cookies.length; i++) {
|
|
22
|
-
const parts = cookies[i].split('=');
|
|
23
|
-
const value = parts.slice(1).join('=');
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
const found = decodeURIComponent(parts[0]);
|
|
27
|
-
jar[found] = converter.read(value, found);
|
|
28
|
-
|
|
29
|
-
if (name === found) {
|
|
30
|
-
break;
|
|
31
|
-
}
|
|
32
|
-
} catch (e) {}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return name ? jar[name] : jar;
|
|
36
|
-
}
|
|
1
|
+
const converter = {
|
|
2
|
+
read(value) {
|
|
3
|
+
if (value[0] === '"') {
|
|
4
|
+
value = value.slice(1, -1);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
|
|
8
|
+
},
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function getCookie(name) {
|
|
12
|
+
if (typeof document === 'undefined' || (arguments.length && !name)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// To prevent the for loop in the first place assign an empty array
|
|
17
|
+
// in case there are no cookies at all.
|
|
18
|
+
const cookies = document.cookie ? document.cookie.split('; ') : [];
|
|
19
|
+
|
|
20
|
+
const jar = {};
|
|
21
|
+
for (let i = 0; i < cookies.length; i++) {
|
|
22
|
+
const parts = cookies[i].split('=');
|
|
23
|
+
const value = parts.slice(1).join('=');
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
const found = decodeURIComponent(parts[0]);
|
|
27
|
+
jar[found] = converter.read(value, found);
|
|
28
|
+
|
|
29
|
+
if (name === found) {
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
} catch (e) {}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return name ? jar[name] : jar;
|
|
36
|
+
}
|