@veritree/services 0.6.0 → 0.8.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.6.0",
3
+ "version": "0.8.1",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -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, 'post', data);
11
+ async post(url, data, isMultipart = false) {
12
+ return await this.unWrap(url, 'post', data, isMultipart);
13
13
  },
14
14
 
15
- async update(url, data) {
16
- return await this.unWrap(url, 'put', data);
15
+ async update(url, data, isMultipart = false) {
16
+ return await this.unWrap(url, 'put', data, isMultipart);
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 = 'get', data) {
27
+ async unWrap(url, method = 'get', data, isMultipart) {
28
28
  if (this.forceEnvelopeResponse) url = this.addEvenlopeArgToUrl(url); // TODO: remove when API is fully migrated to envelopes
29
- const config = this.getConfig(method, data);
29
+ const config = this.getConfig(method, data, isMultipart);
30
30
 
31
31
  try {
32
32
  const response = await fetch(url, config);
@@ -45,16 +45,23 @@ const Api = {
45
45
  * @param {object} body
46
46
  * @returns {object} data
47
47
  */
48
- getConfig(method, data) {
48
+ getConfig(method, data, isMultipart) {
49
+ const contentType = isMultipart ? 'multipart/form-data' : 'application/json';
50
+ const token = `Bearer ${getCookie('access_token')}`;
51
+
49
52
  const config = {
50
53
  method,
51
54
  headers: {
52
- 'Content-Type': 'application/json',
53
- Authorization: `Bearer ${getCookie('access_token')}`,
55
+ 'Content-Type': contentType,
56
+ Authorization: token
54
57
  },
55
58
  };
56
59
 
57
- if (method !== 'get') config.body = JSON.stringify(data);
60
+ if (method !== 'get' && !isMultipart) {
61
+ config.body = JSON.stringify(data);
62
+ } else {
63
+ config.body = data;
64
+ }
58
65
 
59
66
  return config;
60
67
  },
@@ -18,6 +18,18 @@ export const createFieldUpdatesApiService = (orgId, orgType) => {
18
18
  },
19
19
  };
20
20
 
21
+ /**
22
+ *
23
+ * @param {string} fieldUpdateId
24
+ * @param {object} data
25
+ * @returns
26
+ */
27
+ const update = (fieldUpdateId, data, isMultipart = false) => {
28
+ const url = `${_getUrl()}/${fieldUpdateId}?${_getParams()}`;
29
+
30
+ return Api.update(url, data, isMultipart);
31
+ }
32
+
21
33
  const _getUrl = () => {
22
34
  return `${Api.baseUrl}/${resource}`;
23
35
  };
@@ -35,5 +47,6 @@ export const createFieldUpdatesApiService = (orgId, orgType) => {
35
47
 
36
48
  return {
37
49
  get,
50
+ update
38
51
  };
39
52
  };
@@ -1,38 +1,38 @@
1
1
  import Api from '../helpers/api';
2
2
 
3
- // TODO: Add all possible parameters as in API
4
- export const createForestTypesApiService = () => {
3
+ export const createForestTypesApiService = (orgId, orgType) => {
4
+ if (!orgId || !orgType) throw new Error('orgId and orgType are required');
5
+ const resource = 'forest-types';
6
+
5
7
  const get = {
6
8
  /**
7
- * Gets all forest types of an organization
8
- *
9
- * @param {boolean} isPublic
10
9
  * @returns {object} envelope
11
10
  */
12
- async all(isPublic = false) {
13
- const url = `${_getUrl(isPublic)}`;
11
+ async all(page = 1, pageSize = 10) {
12
+ const url = `${_getUrl()}?${_getParams(page, pageSize)}`;
14
13
  return await Api.get(url);
15
14
  },
16
15
 
17
16
  /**
18
- * Gets a speficific forest type of an organization
19
- *
20
17
  * @param {number/string} id
21
- * @param {boolean} isPublic
22
18
  * @returns {object} envelope
23
19
  */
24
- async specific(id, isPublic = false) {
25
- const url = `${_getUrl(isPublic)}/${id}`;
20
+ async specific(id) {
21
+ const url = `${_getUrl()}/${id}?${_getParams(page, pageSize)}`;
26
22
  return await Api.get(url);
27
23
  },
28
24
  };
29
25
 
30
- const _getResource = (isPublic) => {
31
- return isPublic ? 'forest-type-profiles' : `forest-types`;
26
+ const _getUrl = () => {
27
+ return `${Api.baseUrl}/${resource}`;
32
28
  };
33
29
 
34
- const _getUrl = (isPublic) => {
35
- return `${Api.baseUrl}/${_getResource(isPublic)}`
30
+ const _getParams = (page, pageSize) => {
31
+ const pageParam = page ? `&page=${page}` : '';
32
+ const pageSizeParam = pageSize ? `&page_size=${pageSize}` : '';
33
+ const params = `${pageParam}${pageSizeParam}`;
34
+
35
+ return `org_id=${orgId}&org_type=${orgType}${params}`;
36
36
  };
37
37
 
38
38
  return {
@@ -7,7 +7,6 @@ export const createImagesApiService = (orgId, orgType) => {
7
7
  const get = {
8
8
  async all(
9
9
  purpose,
10
- tagName,
11
10
  countryId,
12
11
  regionId,
13
12
  subsiteId,
@@ -16,7 +15,6 @@ export const createImagesApiService = (orgId, orgType) => {
16
15
  ) {
17
16
  const url = `${_getUrl()}?${_getParams(
18
17
  purpose,
19
- tagName,
20
18
  countryId,
21
19
  regionId,
22
20
  subsiteId,
@@ -33,7 +31,6 @@ export const createImagesApiService = (orgId, orgType) => {
33
31
 
34
32
  const _getParams = (
35
33
  purpose,
36
- tagName,
37
34
  countryId,
38
35
  regionId,
39
36
  subsiteId,
@@ -42,13 +39,12 @@ export const createImagesApiService = (orgId, orgType) => {
42
39
  ) => {
43
40
  const paramPageSize = pageSize ? `&page_size=${pageSize}` : "";
44
41
  const paramPurpose = purpose ? `&purpose=${purpose}` : "";
45
- const paramTagName = tagName ? `&tag_name=${tagName}` : "";
46
42
  const paramCountryId = countryId ? `&country_id=${countryId}` : "";
47
43
  const paramRegionId = regionId ? `&region_id=${regionId}` : "";
48
44
  const paramSubsiteId = subsiteId ? `&subsite_id=${subsiteId}` : "";
49
45
  const paramPage = page ? `&page=${page}` : "";
50
46
 
51
- const params = `${paramPurpose}${paramPageSize}${paramTagName}${paramCountryId}${paramRegionId}${paramSubsiteId}${paramPage}`;
47
+ const params = `${paramPurpose}${paramPageSize}${paramCountryId}${paramRegionId}${paramSubsiteId}${paramPage}`;
52
48
 
53
49
  return `org_id=${orgId}&org_type=${orgType}${params}`;
54
50
  };