@veritree/services 2.33.0 → 2.34.0-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 +3 -1
- package/jsdoc.json +16 -0
- package/package.json +3 -2
- package/src/endpoints/organizations.js +8 -2
- package/src/endpoints/orgs.js +5 -5
- package/src/endpoints/password.js +18 -8
- package/src/endpoints/planting-sites.js +15 -34
- package/src/endpoints/sponsors.js +27 -13
- package/src/endpoints/standards.js +2 -87
- package/src/endpoints/stats.js +2 -3
- package/src/endpoints/tree-codes.js +43 -0
- package/src/helpers/api.js +33 -151
- package/src/endpoints/trees-orders-statuses.js +0 -5
package/index.js
CHANGED
|
@@ -37,6 +37,7 @@ import { Organizations } from './src/endpoints/organizations';
|
|
|
37
37
|
import { Tasks } from './src/endpoints/tasks';
|
|
38
38
|
import { Geometries } from './src/endpoints/geometries';
|
|
39
39
|
import { Password } from './src/endpoints/password';
|
|
40
|
+
import { TreeCodes } from './src/endpoints/tree-codes';
|
|
40
41
|
|
|
41
42
|
export {
|
|
42
43
|
BulkUploads,
|
|
@@ -77,5 +78,6 @@ export {
|
|
|
77
78
|
Organizations,
|
|
78
79
|
Tasks,
|
|
79
80
|
Geometries,
|
|
80
|
-
Password
|
|
81
|
+
Password,
|
|
82
|
+
TreeCodes
|
|
81
83
|
};
|
package/jsdoc.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"source": {
|
|
3
|
+
"include": ["src/endpoints"],
|
|
4
|
+
"includePattern": ".js$",
|
|
5
|
+
"excludePattern": "(node_modules/|docs)"
|
|
6
|
+
},
|
|
7
|
+
"plugins": ["plugins/markdown"],
|
|
8
|
+
"templates": {
|
|
9
|
+
"cleverLinks": true,
|
|
10
|
+
"monospaceLinks": true
|
|
11
|
+
},
|
|
12
|
+
"opts": {
|
|
13
|
+
"recurse": true,
|
|
14
|
+
"destination": "./documentation/"
|
|
15
|
+
}
|
|
16
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@veritree/services",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.34.0-0",
|
|
4
4
|
"description": "A collection of javascript functions/services to talk to veritree API",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "index.js",
|
|
@@ -12,5 +12,6 @@
|
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
14
|
"np": "^7.6.2"
|
|
15
|
-
}
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {}
|
|
16
17
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import Api from
|
|
1
|
+
import Api from "../helpers/api";
|
|
2
2
|
|
|
3
3
|
class OrganizationsApi extends Api {
|
|
4
4
|
constructor(resource) {
|
|
5
5
|
super(resource);
|
|
6
|
-
this.resource =
|
|
6
|
+
this.resource = "organizations";
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
tasks(orgId) {
|
|
@@ -14,6 +14,12 @@ class OrganizationsApi extends Api {
|
|
|
14
14
|
return await this.get(url);
|
|
15
15
|
};
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Creates a new task with the specified data.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} taskData - An object containing the data for the new task.
|
|
21
|
+
* @returns {Promise<Object>} - A Promise that resolves to the created task object.
|
|
22
|
+
*/
|
|
17
23
|
const create = async (data) => {
|
|
18
24
|
url = `${url}${this.getUrlParams()}`;
|
|
19
25
|
return await this.post(url, data);
|
package/src/endpoints/orgs.js
CHANGED
|
@@ -24,12 +24,12 @@ class OrgsApi extends Api {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
async stats() {
|
|
27
|
-
const url = `${this.
|
|
27
|
+
const url = `${this._getStatstUrl()}`;
|
|
28
28
|
return await this.get(url);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
async publicStats() {
|
|
32
|
-
const url = `${this.
|
|
31
|
+
async publicStats(args) {
|
|
32
|
+
const url = `${this._getStatstUrl(true, args)}`;
|
|
33
33
|
return await this.get(url);
|
|
34
34
|
}
|
|
35
35
|
|
|
@@ -56,9 +56,9 @@ class OrgsApi extends Api {
|
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
_getStatstUrl(isPublic, args) {
|
|
60
60
|
const endpoint = isPublic ? 'pstats' : 'stats';
|
|
61
|
-
return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
|
|
61
|
+
return `${this.getUrl()}/${endpoint}${this.getUrlParams(args)}`;
|
|
62
62
|
}
|
|
63
63
|
}
|
|
64
64
|
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import Api from "../helpers/api";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* This is a namespace that contains documentation elements
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
4
|
+
* This is a namespace that contains documentation elements belonging to the Password domain.
|
|
5
|
+
*
|
|
6
|
+
* @class
|
|
7
|
+
* @module Password
|
|
8
8
|
*/
|
|
9
9
|
class PasswordApi extends Api {
|
|
10
10
|
constructor(resource) {
|
|
@@ -12,13 +12,18 @@ class PasswordApi extends Api {
|
|
|
12
12
|
this.resource = "password";
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* @function
|
|
17
|
+
* @name recovery
|
|
18
|
+
* @returns {Object}
|
|
19
|
+
*/
|
|
15
20
|
recovery() {
|
|
16
21
|
/**
|
|
17
22
|
* Sends a recovery password email to the specified email address.
|
|
18
23
|
*
|
|
19
24
|
* @param {Object} data - An object containing the following properties:
|
|
20
|
-
*
|
|
21
|
-
*
|
|
25
|
+
* @param {string} data.email - The email address to send the recovery email to.
|
|
26
|
+
* @param {string} [data.return_url] - The URL to redirect the user to after they reset their password.
|
|
22
27
|
* @returns {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the email was sent successfully.
|
|
23
28
|
*/
|
|
24
29
|
const post = async (data) => {
|
|
@@ -32,6 +37,11 @@ class PasswordApi extends Api {
|
|
|
32
37
|
};
|
|
33
38
|
}
|
|
34
39
|
|
|
40
|
+
/**
|
|
41
|
+
* @function
|
|
42
|
+
* @name reset
|
|
43
|
+
* @returns {Object}
|
|
44
|
+
*/
|
|
35
45
|
reset() {
|
|
36
46
|
/**
|
|
37
47
|
* Sends a POST request to reset a password with the specified email, token, and password.
|
|
@@ -44,7 +54,7 @@ class PasswordApi extends Api {
|
|
|
44
54
|
* @returns {Promise<Object>} - A Promise that resolves to the result of the POST request.
|
|
45
55
|
*/
|
|
46
56
|
const post = async (data) => {
|
|
47
|
-
const url = `${this.getUrl()}/reset
|
|
57
|
+
const url = `${this.getUrl()}/reset`;
|
|
48
58
|
|
|
49
59
|
return await this.post(url, data);
|
|
50
60
|
};
|
|
@@ -55,4 +65,4 @@ class PasswordApi extends Api {
|
|
|
55
65
|
}
|
|
56
66
|
}
|
|
57
67
|
|
|
58
|
-
export const Password = new PasswordApi();
|
|
68
|
+
export const Password = new PasswordApi();
|
|
@@ -1,9 +1,20 @@
|
|
|
1
|
-
import Api from
|
|
1
|
+
import Api from "../helpers/api";
|
|
2
2
|
|
|
3
3
|
class PlantingSitesApi extends Api {
|
|
4
4
|
constructor(resource) {
|
|
5
5
|
super(resource);
|
|
6
|
-
this.resource =
|
|
6
|
+
this.resource = "planting-sites";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public() {
|
|
10
|
+
const single = async (plantingSiteId) => {
|
|
11
|
+
const url = `${this.getUrl()}/public/${plantingSiteId}`;
|
|
12
|
+
return await this.get(url);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
single,
|
|
17
|
+
};
|
|
7
18
|
}
|
|
8
19
|
|
|
9
20
|
users(plantingSiteId) {
|
|
@@ -32,7 +43,7 @@ class PlantingSitesApi extends Api {
|
|
|
32
43
|
url = `${this.getUrl()}/${subsiteId}/media/images/${imageId}${this.getUrlParams(
|
|
33
44
|
params
|
|
34
45
|
)}`;
|
|
35
|
-
return await this.post(url, null,
|
|
46
|
+
return await this.post(url, null, "delete");
|
|
36
47
|
};
|
|
37
48
|
|
|
38
49
|
return {
|
|
@@ -54,7 +65,7 @@ class PlantingSitesApi extends Api {
|
|
|
54
65
|
url = `${this.getUrl()}/${subsiteId}/media/attachments/${attachmentId}${this.getUrlParams(
|
|
55
66
|
params
|
|
56
67
|
)}`;
|
|
57
|
-
return await this.post(url, null,
|
|
68
|
+
return await this.post(url, null, "delete");
|
|
58
69
|
};
|
|
59
70
|
|
|
60
71
|
return {
|
|
@@ -75,36 +86,6 @@ class PlantingSitesApi extends Api {
|
|
|
75
86
|
all,
|
|
76
87
|
};
|
|
77
88
|
}
|
|
78
|
-
|
|
79
|
-
evidence(subsiteId, params) {
|
|
80
|
-
let url = `${this.getUrl()}/${subsiteId}/evidence`;
|
|
81
|
-
|
|
82
|
-
console.log(url);
|
|
83
|
-
|
|
84
|
-
const all = async () => {
|
|
85
|
-
url = `${url}${this.getUrlParams(params)}`;
|
|
86
|
-
return await this.get(url);
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const attach = async (formData) => {
|
|
90
|
-
url = `${url}${this.getUrlParams(params)}`;
|
|
91
|
-
return await this.post(url, formData);
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const remove = async (evidenceType, evidenceId) => {
|
|
95
|
-
url = `${url}/${evidenceType}/${evidenceId}${this.getUrlParams(
|
|
96
|
-
params
|
|
97
|
-
)}`;
|
|
98
|
-
|
|
99
|
-
return await this.post(url, null, "delete");
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
all,
|
|
104
|
-
attach,
|
|
105
|
-
delete: remove,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
89
|
}
|
|
109
90
|
|
|
110
91
|
export const PlantingSites = new PlantingSitesApi();
|
|
@@ -6,28 +6,38 @@ class SponsorsApi extends Api {
|
|
|
6
6
|
this.resource = 'sponsors';
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
async map() {
|
|
10
|
-
this.setOrg();
|
|
9
|
+
async map(orgId) {
|
|
11
10
|
|
|
12
|
-
if (!
|
|
11
|
+
if (!orgId) {
|
|
13
12
|
throw new Error('No org id provided');
|
|
14
13
|
}
|
|
15
14
|
|
|
16
|
-
const url = `${this.getUrl()}/${
|
|
15
|
+
const url = `${this.getUrl()}/${orgId}/map-data`;
|
|
17
16
|
return await this.get(url);
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
async profile() {
|
|
21
|
-
this.
|
|
19
|
+
async profile(args) {
|
|
20
|
+
const params = this.getUrlParams(args);
|
|
22
21
|
|
|
23
22
|
if (!this.orgId) {
|
|
24
23
|
throw new Error('No org id provided');
|
|
25
24
|
}
|
|
26
25
|
|
|
27
|
-
const url = `${this.getUrl()}/${this.orgId}/profile`;
|
|
26
|
+
const url = `${this.getUrl()}/${this.orgId}/profile${params}`;
|
|
28
27
|
return await this.get(url);
|
|
29
28
|
}
|
|
30
29
|
|
|
30
|
+
plantingSites(orgId) {
|
|
31
|
+
const all = async () => {
|
|
32
|
+
const url = `${this.getUrl()}/${orgId}/planting-sites`;
|
|
33
|
+
return await this.get(url);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
all
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
31
41
|
async plantingStats(args) {
|
|
32
42
|
const url = `${this.getUrl()}/planting-stats${this.getUrlParams(args)}`;
|
|
33
43
|
return await this.get(url);
|
|
@@ -49,7 +59,9 @@ class SponsorsApi extends Api {
|
|
|
49
59
|
};
|
|
50
60
|
|
|
51
61
|
const imageUploadUrl = async () => {
|
|
52
|
-
const url = `${this.getUrl()}/${
|
|
62
|
+
const url = `${this.getUrl()}/${
|
|
63
|
+
this.orgId
|
|
64
|
+
}/cms/image-upload-url${urlParams}`;
|
|
53
65
|
return await this.get(url);
|
|
54
66
|
};
|
|
55
67
|
|
|
@@ -67,19 +79,21 @@ class SponsorsApi extends Api {
|
|
|
67
79
|
|
|
68
80
|
tools(args) {
|
|
69
81
|
const get = async (sponsorId) => {
|
|
70
|
-
const url = `${this.getUrl()}/${sponsorId}/tools${this.getUrlParams(
|
|
82
|
+
const url = `${this.getUrl()}/${sponsorId}/tools${this.getUrlParams(
|
|
83
|
+
args
|
|
84
|
+
)}`;
|
|
71
85
|
return await this.get(url);
|
|
72
|
-
}
|
|
86
|
+
};
|
|
73
87
|
|
|
74
88
|
const create = async (sponsorId, data) => {
|
|
75
89
|
const url = `${this.getUrl()}/${sponsorId}/tools`;
|
|
76
90
|
return await this.post(url, data);
|
|
77
|
-
}
|
|
91
|
+
};
|
|
78
92
|
|
|
79
93
|
return {
|
|
80
94
|
get,
|
|
81
|
-
create
|
|
82
|
-
}
|
|
95
|
+
create,
|
|
96
|
+
};
|
|
83
97
|
}
|
|
84
98
|
}
|
|
85
99
|
|
|
@@ -13,104 +13,19 @@ class StandardsApi extends Api {
|
|
|
13
13
|
return await this.get(url);
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
const create = async (data, args) => {
|
|
17
|
-
const url = `${this.getUrl()}/themes?${createParamsStringFromArgs(args)}`;
|
|
18
|
-
return await this.post(url, data);
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const update = async (themeId, data, args) => {
|
|
22
|
-
const url = `${this.getUrl()}/themes/${themeId}?${createParamsStringFromArgs(
|
|
23
|
-
args
|
|
24
|
-
)}`;
|
|
25
|
-
return await this.post(url, data, 'patch');
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const remove = async (args) => {
|
|
29
|
-
const url = `${this.getUrl()}/themes/${themeId}?${createParamsStringFromArgs(
|
|
30
|
-
args
|
|
31
|
-
)}`;
|
|
32
|
-
return await this.post(url, data, 'delete');
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const goals = (themeId) => {
|
|
36
|
-
const create = async (data, args) => {
|
|
37
|
-
const url = `${this.getUrl()}/themes/${themeId}/goals?${createParamsStringFromArgs(
|
|
38
|
-
args
|
|
39
|
-
)}`;
|
|
40
|
-
return await this.post(url, data);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const indicators = (goalId) => {
|
|
44
|
-
const create = async (data, args) => {
|
|
45
|
-
const url = `${this.getUrl()}/themes/${themeId}/goals/${goalId}/indicators?${createParamsStringFromArgs(
|
|
46
|
-
args
|
|
47
|
-
)}`;
|
|
48
|
-
return await this.post(url, data);
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
create,
|
|
53
|
-
};
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
create,
|
|
58
|
-
indicators,
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
|
|
62
16
|
return {
|
|
63
17
|
all,
|
|
64
|
-
create,
|
|
65
|
-
update,
|
|
66
|
-
delete: remove,
|
|
67
|
-
goals,
|
|
68
18
|
};
|
|
69
19
|
}
|
|
70
20
|
|
|
71
|
-
goals(
|
|
72
|
-
const single = async () => {
|
|
73
|
-
const url = `${this.getUrl()}/goals/${goalId}`;
|
|
74
|
-
return await this.get(url);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const update = async (data) => {
|
|
78
|
-
const url = `${this.getUrl()}/goals/${goalId}`;
|
|
79
|
-
return await this.post(url, data, 'patch');
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const remove = async () => {
|
|
21
|
+
goals() {
|
|
22
|
+
const single = async (goalId) => {
|
|
83
23
|
const url = `${this.getUrl()}/goals/${goalId}`;
|
|
84
|
-
return await this.post(url, null, 'delete');
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
return {
|
|
88
|
-
single,
|
|
89
|
-
update,
|
|
90
|
-
delete: remove,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
indicators(indicatorId) {
|
|
95
|
-
const single = async () => {
|
|
96
|
-
const url = `${this.getUrl()}/indicators/${indicatorId}`;
|
|
97
24
|
return await this.get(url);
|
|
98
25
|
};
|
|
99
26
|
|
|
100
|
-
const update = async (data) => {
|
|
101
|
-
const url = `${this.getUrl()}/indicators/${indicatorId}`;
|
|
102
|
-
return await this.post(url, data, 'patch');
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const remove = async () => {
|
|
106
|
-
const url = `${this.getUrl()}/indicators/${indicatorId}`;
|
|
107
|
-
return await this.post(url, null, 'delete');
|
|
108
|
-
};
|
|
109
|
-
|
|
110
27
|
return {
|
|
111
28
|
single,
|
|
112
|
-
update,
|
|
113
|
-
delete: remove,
|
|
114
29
|
};
|
|
115
30
|
}
|
|
116
31
|
}
|
package/src/endpoints/stats.js
CHANGED
|
@@ -5,13 +5,12 @@ class StatsApi extends Api {
|
|
|
5
5
|
super(resource);
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
async crumb(id) {
|
|
8
|
+
async crumb(id, args) {
|
|
9
9
|
this.resource = 'pstats';
|
|
10
|
-
const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
|
|
10
|
+
const url = `${this.getUrl()}/${id}${this.getUrlParams(args)}`;
|
|
11
11
|
|
|
12
12
|
return await this.get(url);
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export const Stats = new StatsApi();
|
|
17
|
-
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import Api from "../helpers/api";
|
|
2
|
+
|
|
3
|
+
class TreeCodesApi extends Api {
|
|
4
|
+
constructor(resource) {
|
|
5
|
+
super(resource);
|
|
6
|
+
this.resource = "tree-codes";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
holders() {
|
|
10
|
+
const claim = async (treeCodeHolder) => {
|
|
11
|
+
const url = `${this.getUrl()}/holders/claim/${treeCodeHolder}`;
|
|
12
|
+
return await this.get(url);
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return {
|
|
16
|
+
claim,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
stats() {
|
|
21
|
+
const all = async () => {
|
|
22
|
+
const url = `${this.getUrl()}/stats`;
|
|
23
|
+
return await this.get(url);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
all
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
status() {
|
|
32
|
+
const single = async (treeCodeHolder) => {
|
|
33
|
+
const url = `${this.getUrl()}/status/${treeCodeHolder}`;
|
|
34
|
+
return await this.get(url);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
single
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const TreeCodes = new TreeCodesApi();
|
package/src/helpers/api.js
CHANGED
|
@@ -1,80 +1,33 @@
|
|
|
1
|
-
import { getCookie } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
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
|
-
*/
|
|
1
|
+
import { getCookie } from './cookies';
|
|
2
|
+
import { getSession } from './session';
|
|
3
|
+
import { createParamsStringFromArgs } from '../utils/args';
|
|
4
|
+
|
|
44
5
|
function addVersionParam(url) {
|
|
45
6
|
// If URL is invalid or already has the result parameter, return it as is
|
|
46
|
-
if (!url || url.includes(
|
|
7
|
+
if (!url || url.includes('_result=1')) {
|
|
47
8
|
return url;
|
|
48
9
|
}
|
|
49
10
|
|
|
50
11
|
// Check if URL already has the version parameter
|
|
51
|
-
if (url.includes(
|
|
12
|
+
if (url.includes('_v=')) {
|
|
52
13
|
return url;
|
|
53
14
|
}
|
|
54
15
|
|
|
55
|
-
|
|
56
|
-
const version = process.env.API_VERITREE_VERSION || "5.0.0";
|
|
16
|
+
const version = '10.0.0';
|
|
57
17
|
|
|
58
18
|
// Append version parameter to URL
|
|
59
|
-
const urlVersionParam = url.includes(
|
|
19
|
+
const urlVersionParam = url.includes('?')
|
|
60
20
|
? `&_v=${version}`
|
|
61
21
|
: `?_v=${version}`;
|
|
62
22
|
|
|
63
23
|
return `${url}${urlVersionParam}`;
|
|
64
24
|
}
|
|
65
25
|
|
|
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
26
|
function getConfig(method, data, as) {
|
|
74
|
-
const isGet = method ===
|
|
27
|
+
const isGet = method === 'get';
|
|
75
28
|
const isSpoofing = as;
|
|
76
29
|
const isFormData = data instanceof FormData;
|
|
77
|
-
const accessToken = `Bearer ${getCookie(
|
|
30
|
+
const accessToken = `Bearer ${getCookie('access_token')}`;
|
|
78
31
|
|
|
79
32
|
const config = {
|
|
80
33
|
method,
|
|
@@ -84,7 +37,7 @@ function getConfig(method, data, as) {
|
|
|
84
37
|
};
|
|
85
38
|
|
|
86
39
|
if (!isFormData) {
|
|
87
|
-
config.headers[
|
|
40
|
+
config.headers['Content-Type'] = 'application/json';
|
|
88
41
|
}
|
|
89
42
|
|
|
90
43
|
// TODO: improve this ifs and elses
|
|
@@ -92,7 +45,7 @@ function getConfig(method, data, as) {
|
|
|
92
45
|
if (!data) data = {};
|
|
93
46
|
|
|
94
47
|
if (isFormData) {
|
|
95
|
-
if (isSpoofing) data.set(
|
|
48
|
+
if (isSpoofing) data.set('_method', as.toUpperCase());
|
|
96
49
|
config.body = data;
|
|
97
50
|
} else {
|
|
98
51
|
if (isSpoofing) data._method = as.toUpperCase();
|
|
@@ -105,17 +58,8 @@ function getConfig(method, data, as) {
|
|
|
105
58
|
|
|
106
59
|
export default class Api {
|
|
107
60
|
constructor(resource) {
|
|
108
|
-
|
|
109
|
-
|
|
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 : "";
|
|
61
|
+
this.baseUrl = null;
|
|
62
|
+
this.resource = resource ? resource : '';
|
|
119
63
|
this.orgId = null;
|
|
120
64
|
this.orgType = null;
|
|
121
65
|
}
|
|
@@ -124,110 +68,58 @@ export default class Api {
|
|
|
124
68
|
this.baseUrl = baseUrl;
|
|
125
69
|
}
|
|
126
70
|
|
|
127
|
-
/**
|
|
128
|
-
*
|
|
129
|
-
* @returns {promise}
|
|
130
|
-
*/
|
|
131
71
|
async all(args) {
|
|
132
72
|
const url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
133
73
|
return await this.get(url);
|
|
134
74
|
}
|
|
135
75
|
|
|
136
|
-
/**
|
|
137
|
-
*
|
|
138
|
-
* @param {string, number} id
|
|
139
|
-
* @param {object} args/params
|
|
140
|
-
* @returns {promise}
|
|
141
|
-
*/
|
|
142
76
|
async single(id, args) {
|
|
143
|
-
const
|
|
77
|
+
const params = this.args ? this.getUrlParams(args) : '';
|
|
78
|
+
const url = `${this.getUrl(id)}${params}`;
|
|
144
79
|
return await this.get(url);
|
|
145
80
|
}
|
|
146
81
|
|
|
147
|
-
/**
|
|
148
|
-
*
|
|
149
|
-
* @param {object} data
|
|
150
|
-
* @returns {promise}
|
|
151
|
-
*/
|
|
152
82
|
async create(data, args) {
|
|
153
83
|
return await this.post(null, data, null, args);
|
|
154
84
|
}
|
|
155
85
|
|
|
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) {
|
|
86
|
+
async update(id, data, as = 'put', args) {
|
|
164
87
|
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
165
88
|
return await this.post(url, data, as);
|
|
166
89
|
}
|
|
167
90
|
|
|
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
91
|
async delete(id, args) {
|
|
176
92
|
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
177
|
-
return await this.post(url, null,
|
|
93
|
+
return await this.post(url, null, 'delete');
|
|
178
94
|
}
|
|
179
95
|
|
|
180
|
-
/**
|
|
181
|
-
*
|
|
182
|
-
* @param {string} url
|
|
183
|
-
* @returns {promise}
|
|
184
|
-
*/
|
|
185
96
|
async get(url) {
|
|
186
97
|
return await this.unWrap(url);
|
|
187
98
|
}
|
|
188
99
|
|
|
189
|
-
/**
|
|
190
|
-
*
|
|
191
|
-
* @param {string} url
|
|
192
|
-
* @param {object} data
|
|
193
|
-
* @returns {promise}
|
|
194
|
-
*/
|
|
195
100
|
async post(url, data, as, args) {
|
|
196
101
|
if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
197
|
-
return await this.unWrap(url,
|
|
102
|
+
return await this.unWrap(url, 'post', data, as);
|
|
198
103
|
}
|
|
199
104
|
|
|
200
|
-
// ----------
|
|
201
|
-
// --
|
|
202
|
-
/**
|
|
203
|
-
*
|
|
204
|
-
* @param {string, number} id
|
|
205
|
-
* @returns
|
|
206
|
-
*/
|
|
207
105
|
getUrl(id) {
|
|
208
|
-
id = id ? `/${id}` :
|
|
106
|
+
id = id ? `/${id}` : '';
|
|
209
107
|
return `${this.baseUrl}/${this.resource}${id}`;
|
|
210
108
|
}
|
|
211
109
|
|
|
212
|
-
/**
|
|
213
|
-
*
|
|
214
|
-
* @param {object} args
|
|
215
|
-
* @returns {string}
|
|
216
|
-
*/
|
|
217
110
|
getUrlParams(args) {
|
|
218
|
-
this.setOrg();
|
|
219
111
|
let isOrgLess = false;
|
|
220
112
|
let isOrgIdAs = false;
|
|
221
113
|
let isOrgTypeAs = false;
|
|
222
|
-
let orgIdParam =
|
|
223
|
-
let orgTypeParam =
|
|
224
|
-
let argsClone =
|
|
114
|
+
let orgIdParam = '';
|
|
115
|
+
let orgTypeParam = '';
|
|
116
|
+
let argsClone = { ...args }; // avoids mutating object
|
|
225
117
|
|
|
226
118
|
// while most of endpoints require an org id and type, some endpoints do not
|
|
227
119
|
if (argsClone) {
|
|
228
|
-
isOrgLess = Object.hasOwn(argsClone,
|
|
229
|
-
isOrgIdAs = Object.hasOwn(argsClone,
|
|
230
|
-
isOrgTypeAs = Object.hasOwn(argsClone,
|
|
120
|
+
isOrgLess = Object.hasOwn(argsClone, 'orgless');
|
|
121
|
+
isOrgIdAs = Object.hasOwn(argsClone, 'org_id_as');
|
|
122
|
+
isOrgTypeAs = Object.hasOwn(argsClone, 'org_type_as');
|
|
231
123
|
}
|
|
232
124
|
|
|
233
125
|
// a super admin user can create/edit data from other orgs
|
|
@@ -253,20 +145,18 @@ export default class Api {
|
|
|
253
145
|
}
|
|
254
146
|
}
|
|
255
147
|
|
|
148
|
+
if (!this.orgId && !this.orgType) {
|
|
149
|
+
const session = getSession();
|
|
150
|
+
this.orgId = session.orgId;
|
|
151
|
+
this.orgType = session.orgType;
|
|
152
|
+
}
|
|
153
|
+
|
|
256
154
|
return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(
|
|
257
155
|
argsClone
|
|
258
156
|
)}`;
|
|
259
157
|
}
|
|
260
158
|
|
|
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) {
|
|
159
|
+
async unWrap(url, method = 'get', data, as) {
|
|
270
160
|
url = addVersionParam(url);
|
|
271
161
|
const config = getConfig(method, data, as);
|
|
272
162
|
const response = await fetch(url, config);
|
|
@@ -282,12 +172,4 @@ export default class Api {
|
|
|
282
172
|
});
|
|
283
173
|
});
|
|
284
174
|
}
|
|
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
175
|
}
|