@veritree/services 0.3.0 → 0.6.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/index.js CHANGED
@@ -1,13 +1,27 @@
1
- import { createSponsorsApiService } from './src/services/sponsors.js';
1
+ import { createCountriesApiService } from './src/services/countries.js';
2
+ import { createFieldUpdatesApiService } from './src/services/field-updates.js';
3
+ import { createFormSubmissionsApiService } from './src/services/form-submissions.js';
4
+ import { createForestTypesApiService } from './src/services/forest-types.js';
5
+ import { createImagesApiService } from './src/services/images.js';
6
+ import { createMeApiService } from './src/services/me.js';
2
7
  import { createOrgsApiService } from './src/services/orgs.js';
3
- import { createForestTypesApiService } from './src/services/forestTypes.js';
8
+ import { createRegionsApiService } from './src/services/regions.js';
9
+ import { createSponsorsApiService } from './src/services/sponsors.js';
4
10
  import { createSubdomainsApiService } from './src/services/subdomains.js';
5
- import { createCountriesApiService } from './src/services/countries.js';
11
+ import { createSubsitesApiService } from './src/services/subsites.js';
12
+ import { createTreeOrdersApiService } from './src/services/trees-orders.js';
6
13
 
7
14
  export {
8
- createSponsorsApiService,
9
- createOrgsApiService,
15
+ createCountriesApiService,
16
+ createFieldUpdatesApiService,
10
17
  createForestTypesApiService,
18
+ createFormSubmissionsApiService,
19
+ createOrgsApiService,
20
+ createImagesApiService,
21
+ createMeApiService,
22
+ createRegionsApiService,
23
+ createSponsorsApiService,
11
24
  createSubdomainsApiService,
12
- createCountriesApiService
25
+ createSubsitesApiService,
26
+ createTreeOrdersApiService,
13
27
  }
package/package-lock.json CHANGED
@@ -1,5 +1,13 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.2.0",
4
- "lockfileVersion": 1
3
+ "version": "0.3.0",
4
+ "lockfileVersion": 2,
5
+ "requires": true,
6
+ "packages": {
7
+ "": {
8
+ "name": "@veritree/services",
9
+ "version": "0.3.0",
10
+ "license": "MIT"
11
+ }
12
+ }
5
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.3.0",
3
+ "version": "0.6.1",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,23 +1,35 @@
1
1
  import Api from '../helpers/api';
2
2
 
3
- export const createCountriesApiService = (countryId) => {
4
-
3
+ export const createCountriesApiService = () => {
5
4
  const resource = 'countries';
6
5
 
6
+ const get = {
7
+ async all() {
8
+ const url = _getUrl();
9
+ return await Api.get(url);
10
+ },
11
+
12
+ async specific(countriId) {
13
+ const url = `${_getUrl()}/${countriId}`;
14
+ return await Api.get(url);
15
+ }
16
+ }
17
+
7
18
  const stats = {
8
- async get() {
9
- // Look for a country ID if none provided global stats will be sent
19
+ async get(countryId) {
10
20
  const type = countryId ? `${countryId}` : 'global';
11
- const url = _getUrl(type +'/stats');
21
+ const url = `${_getUrl()}/${type}/stats`;
22
+
12
23
  return await Api.get(url);
13
24
  }
14
25
  }
15
26
 
16
- const _getUrl = (endpoint) => {
17
- return `${Api.baseUrl}/${resource}/${endpoint}`;
27
+ const _getUrl = () => {
28
+ return `${Api.baseUrl}/${resource}`;
18
29
  };
19
30
 
20
31
  return {
32
+ get,
21
33
  stats
22
34
  };
23
35
  };
@@ -0,0 +1,39 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createFieldUpdatesApiService = (orgId, orgType) => {
4
+ const resource = `field-updates`;
5
+
6
+ const get = {
7
+ async all(countryId, regionId, subsiteId, page = 1, pageSize = 10) {
8
+ const params = `${_getParams(
9
+ countryId,
10
+ regionId,
11
+ subsiteId,
12
+ page,
13
+ pageSize
14
+ )}`;
15
+ const url = `${_getUrl()}?${params}`;
16
+
17
+ return await Api.get(url);
18
+ },
19
+ };
20
+
21
+ const _getUrl = () => {
22
+ return `${Api.baseUrl}/${resource}`;
23
+ };
24
+
25
+ const _getParams = (countryId, regionId, subsiteId, page, pageSize) => {
26
+ const countryIdParam = countryId ? `&country_id=${countryId}` : '';
27
+ const regionIdParam = regionId ? `&region_id=${regionId}` : '';
28
+ const subsiteIdParam = subsiteId ? `&subsite_id=${subsiteId}` : '';
29
+ const pageParam = page ? `&page=${page}` : '';
30
+ const pageSizeParam = pageSize ? `&page_size=${pageSize}` : '';
31
+ const params = `${countryIdParam}${regionIdParam}${subsiteIdParam}${pageParam}${pageSizeParam}`;
32
+
33
+ return `org_id=${orgId}&org_type=${orgType}${params}`;
34
+ };
35
+
36
+ return {
37
+ get,
38
+ };
39
+ };
@@ -0,0 +1,41 @@
1
+ import Api from '../helpers/api';
2
+
3
+ // TODO: Add all possible parameters as in API
4
+ export const createForestTypesApiService = () => {
5
+ const get = {
6
+ /**
7
+ * Gets all forest types of an organization
8
+ *
9
+ * @param {boolean} isPublic
10
+ * @returns {object} envelope
11
+ */
12
+ async all(isPublic = false) {
13
+ const url = `${_getUrl(isPublic)}`;
14
+ return await Api.get(url);
15
+ },
16
+
17
+ /**
18
+ * Gets a speficific forest type of an organization
19
+ *
20
+ * @param {number/string} id
21
+ * @param {boolean} isPublic
22
+ * @returns {object} envelope
23
+ */
24
+ async specific(id, isPublic = false) {
25
+ const url = `${_getUrl(isPublic)}/${id}`;
26
+ return await Api.get(url);
27
+ },
28
+ };
29
+
30
+ const _getResource = (isPublic) => {
31
+ return isPublic ? 'forest-type-profiles' : `forest-types`;
32
+ };
33
+
34
+ const _getUrl = (isPublic) => {
35
+ return `${Api.baseUrl}/${_getResource(isPublic)}`
36
+ };
37
+
38
+ return {
39
+ get,
40
+ };
41
+ };
@@ -0,0 +1,34 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createFormSubmissionsApiService = (orgId, orgType) => {
4
+ if (!orgId) throw new Error('orgId and orgType are required');
5
+ const resource = 'form-submissions';
6
+
7
+ const get = {
8
+ async all(page = 1, pageSize = 10) {
9
+ const url = `${_getUrl()}?${_getParams(page, pageSize)}`;
10
+ return await Api.get(url);
11
+ },
12
+
13
+ // async specific(formId) {
14
+ // const url = `${_getUrl()}/${orgId}/forms/${formId}?${_getParams()}`;
15
+ // return await Api.get(url);
16
+ // },
17
+ };
18
+
19
+ const _getUrl = () => {
20
+ return `${Api.baseUrl}/${resource}`;
21
+ };
22
+
23
+ const _getParams = (page, pageSize) => {
24
+ const pageParam = page ? `&page=${page}` : '';
25
+ const pageSizeParam = pageSize ? `&page_size=${pageSize}` : '';
26
+ const params = `${pageParam}${pageSizeParam}`;
27
+
28
+ return `org_id=${orgId}&org_type=${orgType}${params}`;
29
+ };
30
+
31
+ return {
32
+ get,
33
+ };
34
+ };
@@ -0,0 +1,33 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createFormApiService = (orgId, orgType) => {
4
+ if (!orgId && !orgType) throw new Error('orgId and orgType are required');
5
+ const resource = 'orgs';
6
+
7
+ const get = {
8
+ async all(page = 1, pageSize = 10) {
9
+ const url = `${_getUrl()}/${orgId}/forms?${_getParams(page, pageSize)}`;
10
+ return await Api.get(url);
11
+ },
12
+
13
+ async specific(formId) {
14
+ const url = `${_getUrl()}/${orgId}/forms/${formId}?${_getParams()}`;
15
+ return await Api.get(url);
16
+ },
17
+ };
18
+
19
+ const _getUrl = () => {
20
+ return `${Api.baseUrl}/${resource}`;
21
+ };
22
+
23
+ const _getParams = (page, pageSize) => {
24
+ const paramPageSize = pageSize ? `&page_size=${pageSize}` : '';
25
+ const paramPage = page ? `&page=${page}` : '';
26
+
27
+ return `org_type=${orgType}${paramPageSize}${paramPage}`;
28
+ };
29
+
30
+ return {
31
+ get,
32
+ };
33
+ };
@@ -0,0 +1,55 @@
1
+ import Api from "../helpers/api";
2
+
3
+ export const createImagesApiService = (orgId, orgType) => {
4
+ if (!orgId && !orgType) throw new Error("orgId and orgType are required");
5
+ const resource = "images";
6
+
7
+ const get = {
8
+ async all(
9
+ purpose,
10
+ countryId,
11
+ regionId,
12
+ subsiteId,
13
+ page = 1,
14
+ pageSize = 10
15
+ ) {
16
+ const url = `${_getUrl()}?${_getParams(
17
+ purpose,
18
+ countryId,
19
+ regionId,
20
+ subsiteId,
21
+ page,
22
+ pageSize
23
+ )}`;
24
+ return await Api.get(url);
25
+ },
26
+ };
27
+
28
+ const _getUrl = () => {
29
+ return `${Api.baseUrl}/${resource}`;
30
+ };
31
+
32
+ const _getParams = (
33
+ purpose,
34
+ countryId,
35
+ regionId,
36
+ subsiteId,
37
+ page,
38
+ pageSize
39
+ ) => {
40
+ const paramPageSize = pageSize ? `&page_size=${pageSize}` : "";
41
+ const paramPurpose = purpose ? `&purpose=${purpose}` : "";
42
+ const paramCountryId = countryId ? `&country_id=${countryId}` : "";
43
+ const paramRegionId = regionId ? `&region_id=${regionId}` : "";
44
+ const paramSubsiteId = subsiteId ? `&subsite_id=${subsiteId}` : "";
45
+ const paramPage = page ? `&page=${page}` : "";
46
+
47
+ const params = `${paramPurpose}${paramPageSize}${paramCountryId}${paramRegionId}${paramSubsiteId}${paramPage}`;
48
+
49
+ return `org_id=${orgId}&org_type=${orgType}${params}`;
50
+ };
51
+
52
+ return {
53
+ get,
54
+ };
55
+ };
@@ -0,0 +1,20 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createMeApiService = () => {
4
+ const resource = 'me';
5
+
6
+ const get = {
7
+ async me() {
8
+ const url = _getUrl();
9
+ return await Api.get(url);
10
+ },
11
+ };
12
+
13
+ const _getUrl = () => {
14
+ return `${Api.baseUrl}/${resource}`;
15
+ };
16
+
17
+ return {
18
+ get,
19
+ };
20
+ };
@@ -8,7 +8,7 @@ export const createOrgsApiService = (orgId, orgType) => {
8
8
  const stats = {
9
9
  async get(isPublic) {
10
10
  const endpoint = _getStatsEndpoint(isPublic);
11
- const url = _getUrl(endpoint);
11
+ const url = `${_getUrl()}/${endpoint}?${_getUrlParams()}`;
12
12
  return await Api.get(url);
13
13
  },
14
14
  };
@@ -17,10 +17,14 @@ export const createOrgsApiService = (orgId, orgType) => {
17
17
  return isPublic ? 'pstats' : 'stats';
18
18
  }
19
19
 
20
- const _getUrl = (endpoint) => {
21
- return `${Api.baseUrl}/${resource}/${endpoint}?org_id=${orgId}&org_type=${orgType}`;
20
+ const _getUrl = () => {
21
+ return `${Api.baseUrl}/${resource}`;
22
22
  };
23
23
 
24
+ const _getUrlParams = () => {
25
+ return `org_id=${orgId}&org_type=${orgType}`
26
+ }
27
+
24
28
  return {
25
29
  stats,
26
30
  };
@@ -0,0 +1,28 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createRegionsApiService = (orgId, orgType) => {
4
+ if (!orgId && !orgType) throw new Error('orgId and orgType are required');
5
+ const resource = 'regions';
6
+
7
+ const get = {
8
+ async all(page = 1, pageSize = 10) {
9
+ const url = `${_getUrl()}?${_getParams(page, pageSize)}`;
10
+ return await Api.get(url);
11
+ },
12
+ };
13
+
14
+ const _getUrl = () => {
15
+ return `${Api.baseUrl}/${resource}`;
16
+ };
17
+
18
+ const _getParams = (page, pageSize) => {
19
+ const paramPage = page ? `&page=${page}` : '';
20
+ const paramPageSize = pageSize ? `&page_size=${pageSize}` : '';
21
+
22
+ return `org_id=${orgId}&org_type=${orgType}${paramPage}${paramPageSize}`;
23
+ };
24
+
25
+ return {
26
+ get,
27
+ };
28
+ };
@@ -7,7 +7,7 @@ export const createSponsorsApiService = (orgId) => {
7
7
  // filter for map data
8
8
  const map = {
9
9
  async get() {
10
- const url = _getUrl('map-data');
10
+ const url = `${_getUrl()}/${orgId}/map-data`;
11
11
  return await Api.get(url);
12
12
  },
13
13
  };
@@ -15,13 +15,13 @@ export const createSponsorsApiService = (orgId) => {
15
15
  // filter for profile
16
16
  const profile = {
17
17
  async get() {
18
- const url = _getUrl('profile');
18
+ const url = `${_getUrl()}/${orgId}/profile`;
19
19
  return await Api.get(url);
20
20
  },
21
21
  };
22
22
 
23
- const _getUrl = (endpoint) => {
24
- return `${Api.baseUrl}/${resource}/${orgId}/${endpoint}`;
23
+ const _getUrl = () => {
24
+ return `${Api.baseUrl}/${resource}`;
25
25
  };
26
26
 
27
27
  return {
@@ -5,17 +5,19 @@ export const createSubdomainsApiService = () => {
5
5
 
6
6
  const get = {
7
7
  /**
8
+ * Gets org data related to subdomain
9
+ *
8
10
  * @param {string} id (name or id)
9
11
  * @returns {object} evenlop
10
12
  */
11
13
  async specific(id) {
12
- const url = _getUrl(id);
14
+ const url = `${_getUrl()}/${id}`;
13
15
  return await Api.get(url);
14
16
  },
15
17
  };
16
18
 
17
- function _getUrl(id) {
18
- return `${Api.baseUrl}/${resource}/${id}`;
19
+ function _getUrl() {
20
+ return `${Api.baseUrl}/${resource}`;
19
21
  }
20
22
 
21
23
  return {
@@ -0,0 +1,43 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createSubsitesApiService = (orgId, orgType) => {
4
+ if (!orgId && !orgType) throw new Error('orgId is required');
5
+ const resource = 'subsites';
6
+
7
+ const get = {
8
+ /**
9
+ * Gets all subsites of an organization
10
+ *
11
+ * @param {number/string} page
12
+ * @returns {object} envelope
13
+ */
14
+ async all(page = 1, pageSize = 10) {
15
+ const url = `${_getUrl()}?${_getUrlParams(page, pageSize)}`;
16
+ return await Api.get(url);
17
+ },
18
+
19
+ /**
20
+ * Gets a speficific subsite of an organization
21
+ *
22
+ * @param {number/string} id
23
+ * @param {number/string} page
24
+ * @returns {object} envelope
25
+ */
26
+ async specific(id, page = 1, pageSize = 10) {
27
+ const url = `${_getUrl()}/${id}?${_getUrlParams(page, pageSize)}`;
28
+ return await Api.get(url);
29
+ },
30
+ };
31
+
32
+ const _getUrl = () => {
33
+ return `${Api.baseUrl}/${resource}`;
34
+ };
35
+
36
+ const _getUrlParams = (page, pageSize) => {
37
+ return `org_id=${orgId}&org_type=${orgType}&page=${page}&page_size=${pageSize}`;
38
+ };
39
+
40
+ return {
41
+ get,
42
+ };
43
+ };
@@ -0,0 +1,42 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createTreeOrdersApiService = (orgId, orgType) => {
4
+ const resource = 'tree-orders';
5
+
6
+ const get = {
7
+ /**
8
+ *
9
+ * @param {number/string} page
10
+ * @param {number/string} pageSize
11
+ * @returns {object} envelope
12
+ */
13
+ async all(page = 1, pageSize = 10) {
14
+ const url = `${_getUrl()}?${_getUrlParams(page, pageSize)}`;
15
+ return await Api.get(url);
16
+ },
17
+
18
+ /**
19
+ *
20
+ * @param {number/string} id
21
+ * @param {number/string} page
22
+ * @param {number/string} pageSize
23
+ * @returns {object} envelope
24
+ */
25
+ async specific(id, page = 1, pageSize = 10) {
26
+ const url = `${_getUrl()}/${id}?${_getUrlParams(page, pageSize)}`;
27
+ return await Api.get(url);
28
+ },
29
+ };
30
+
31
+ const _getUrl = () => {
32
+ return `${Api.baseUrl}/${resource}`;
33
+ };
34
+
35
+ const _getUrlParams = (page, pageSize) => {
36
+ return `org_id=${orgId}&org_type=${orgType}&page=${page}&page_size=${pageSize}&include=tree_allocations`;
37
+ };
38
+
39
+ return {
40
+ get,
41
+ };
42
+ };
@@ -1,41 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- // TODO: Add all possible parameters as in API
4
- export const createForestTypesApiService = () => {
5
- /**
6
- * Gets the right resource for the request based on the isPublic flag.
7
- *
8
- * @param {boolean} isPublic
9
- * @returns {string} resource
10
- */
11
- const _getResource = (isPublic) => {
12
- return isPublic ? 'forest-type-profiles' : `forest-types`;
13
- };
14
-
15
- const get = {
16
- async all(isPublic = false) {
17
- const url = _handleUrl(isPublic);
18
- return await Api.get(url);
19
- },
20
-
21
- async specific(id, isPublic = false) {
22
- const url = _handleUrl(isPublic, id);
23
- return await Api.get(url);
24
- },
25
- };
26
-
27
- function _handleUrl(isPublic, id) {
28
- const resource = _getResource(isPublic);
29
- return _getUrl(resource, id);
30
- }
31
-
32
- function _getUrl(resource, id) {
33
- return id
34
- ? `${Api.baseUrl}/${resource}/${id}`
35
- : `${Api.baseUrl}/${resource}`;
36
- }
37
-
38
- return {
39
- get,
40
- };
41
- };