@veritree/services 0.8.1 → 0.9.1
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/package.json +1 -1
- package/src/helpers/api.js +28 -21
- package/src/helpers/session.js +7 -0
- package/src/services/field-updates.js +22 -18
package/package.json
CHANGED
package/src/helpers/api.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getCookie } from
|
|
1
|
+
import { getCookie } from "./cookies";
|
|
2
2
|
|
|
3
3
|
const Api = {
|
|
4
4
|
baseUrl: `${process.env.API_VERITREE_URL}/api`,
|
|
@@ -8,12 +8,12 @@ const Api = {
|
|
|
8
8
|
return await this.unWrap(url);
|
|
9
9
|
},
|
|
10
10
|
|
|
11
|
-
async post(url, data
|
|
12
|
-
return await this.unWrap(url,
|
|
11
|
+
async post(url, data) {
|
|
12
|
+
return await this.unWrap(url, "post", data);
|
|
13
13
|
},
|
|
14
14
|
|
|
15
|
-
async update(url, data
|
|
16
|
-
return await this.unWrap(url,
|
|
15
|
+
async update(url, data) {
|
|
16
|
+
return await this.unWrap(url, "put", data);
|
|
17
17
|
},
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -24,9 +24,9 @@ const Api = {
|
|
|
24
24
|
* @param {object} data
|
|
25
25
|
* @returns {object} envelope
|
|
26
26
|
*/
|
|
27
|
-
async unWrap(url, method =
|
|
28
|
-
if (this.forceEnvelopeResponse) url = this.
|
|
29
|
-
const config = this.getConfig(method, data
|
|
27
|
+
async unWrap(url, method = "get", data) {
|
|
28
|
+
if (this.forceEnvelopeResponse) url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
|
|
29
|
+
const config = this.getConfig(method, data);
|
|
30
30
|
|
|
31
31
|
try {
|
|
32
32
|
const response = await fetch(url, config);
|
|
@@ -45,22 +45,24 @@ const Api = {
|
|
|
45
45
|
* @param {object} body
|
|
46
46
|
* @returns {object} data
|
|
47
47
|
*/
|
|
48
|
-
getConfig(method, data
|
|
49
|
-
const
|
|
50
|
-
const
|
|
48
|
+
getConfig(method, data) {
|
|
49
|
+
const isGet = method === "get";
|
|
50
|
+
const isFormData = this._isFormData(data);
|
|
51
|
+
const accessToken = `Bearer ${getCookie('access_token')}`;
|
|
51
52
|
|
|
52
53
|
const config = {
|
|
53
54
|
method,
|
|
54
55
|
headers: {
|
|
55
|
-
|
|
56
|
-
Authorization: token
|
|
56
|
+
Authorization: accessToken,
|
|
57
57
|
},
|
|
58
58
|
};
|
|
59
59
|
|
|
60
|
-
if
|
|
61
|
-
config.
|
|
62
|
-
}
|
|
63
|
-
|
|
60
|
+
if(!isFormData) {
|
|
61
|
+
config.headers['Content-Type'] = 'application/json';
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!isGet) {
|
|
65
|
+
isFormData ? (config.body = data) : (config.body = JSON.stringify(data));
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
return config;
|
|
@@ -72,13 +74,18 @@ const Api = {
|
|
|
72
74
|
* @param {string} url
|
|
73
75
|
* @returns {string} url
|
|
74
76
|
*/
|
|
75
|
-
|
|
76
|
-
if (!url || url.includes(
|
|
77
|
-
|
|
78
|
-
const
|
|
77
|
+
handleEnvelopParam(url) {
|
|
78
|
+
if (!url || url.includes("_result=1")) return url;
|
|
79
|
+
|
|
80
|
+
const urlHasArgs = url.includes("?");
|
|
81
|
+
const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
|
|
79
82
|
|
|
80
83
|
return `${url}${urlEvenlopeArg}`;
|
|
81
84
|
},
|
|
85
|
+
|
|
86
|
+
_isFormData(data) {
|
|
87
|
+
return data instanceof FormData;
|
|
88
|
+
},
|
|
82
89
|
};
|
|
83
90
|
|
|
84
91
|
export default Api;
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import Api from
|
|
1
|
+
import Api from "../helpers/api";
|
|
2
|
+
import { getSession } from "../helpers/session";
|
|
2
3
|
|
|
3
|
-
export const createFieldUpdatesApiService = (
|
|
4
|
+
export const createFieldUpdatesApiService = () => {
|
|
4
5
|
const resource = `field-updates`;
|
|
6
|
+
const { orgId, orgType } = getSession();
|
|
5
7
|
|
|
6
8
|
const get = {
|
|
7
|
-
async all(countryId, regionId, subsiteId, page = 1, pageSize = 10) {
|
|
9
|
+
async all(countryId, regionId, subsiteId, page = 1, orderBy = 'date_planted-desc', pageSize = 10) {
|
|
8
10
|
const params = `${_getParams(
|
|
9
11
|
countryId,
|
|
10
12
|
regionId,
|
|
11
13
|
subsiteId,
|
|
12
14
|
page,
|
|
15
|
+
orderBy,
|
|
13
16
|
pageSize
|
|
14
17
|
)}`;
|
|
15
18
|
const url = `${_getUrl()}?${params}`;
|
|
@@ -19,34 +22,35 @@ export const createFieldUpdatesApiService = (orgId, orgType) => {
|
|
|
19
22
|
};
|
|
20
23
|
|
|
21
24
|
/**
|
|
22
|
-
*
|
|
23
|
-
* @param {string} fieldUpdateId
|
|
24
|
-
* @param {object} data
|
|
25
|
-
* @returns
|
|
25
|
+
*
|
|
26
|
+
* @param {string} fieldUpdateId
|
|
27
|
+
* @param {object} data
|
|
28
|
+
* @returns
|
|
26
29
|
*/
|
|
27
|
-
const update = (fieldUpdateId, data
|
|
30
|
+
const update = (fieldUpdateId, data) => {
|
|
28
31
|
const url = `${_getUrl()}/${fieldUpdateId}?${_getParams()}`;
|
|
29
32
|
|
|
30
|
-
return Api.
|
|
31
|
-
}
|
|
33
|
+
return Api.post(url, data);
|
|
34
|
+
};
|
|
32
35
|
|
|
33
36
|
const _getUrl = () => {
|
|
34
37
|
return `${Api.baseUrl}/${resource}`;
|
|
35
38
|
};
|
|
36
39
|
|
|
37
|
-
const _getParams = (countryId, regionId, subsiteId, page, pageSize) => {
|
|
38
|
-
const countryIdParam = countryId ? `&country_id=${countryId}` :
|
|
39
|
-
const regionIdParam = regionId ? `®ion_id=${regionId}` :
|
|
40
|
-
const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` :
|
|
41
|
-
const pageParam = page ? `&page=${page}` :
|
|
42
|
-
const
|
|
43
|
-
const
|
|
40
|
+
const _getParams = (countryId, regionId, subsiteId, page, orderBy, pageSize) => {
|
|
41
|
+
const countryIdParam = countryId ? `&country_id=${countryId}` : "";
|
|
42
|
+
const regionIdParam = regionId ? `®ion_id=${regionId}` : "";
|
|
43
|
+
const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : "";
|
|
44
|
+
const pageParam = page ? `&page=${page}` : "";
|
|
45
|
+
const orderByParam = orderBy ? `&orderBy=${orderBy}` : "";
|
|
46
|
+
const pageSizeParam = pageSize ? `&page_size=${pageSize}` : "";
|
|
47
|
+
const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}${orderByParam}`;
|
|
44
48
|
|
|
45
49
|
return `org_id=${orgId}&org_type=${orgType}${params}`;
|
|
46
50
|
};
|
|
47
51
|
|
|
48
52
|
return {
|
|
49
53
|
get,
|
|
50
|
-
update
|
|
54
|
+
update,
|
|
51
55
|
};
|
|
52
56
|
};
|