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