@veritree/services 0.0.2 → 0.3.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 ADDED
@@ -0,0 +1,13 @@
1
+ import { createSponsorsApiService } from './src/services/sponsors.js';
2
+ import { createOrgsApiService } from './src/services/orgs.js';
3
+ import { createForestTypesApiService } from './src/services/forestTypes.js';
4
+ import { createSubdomainsApiService } from './src/services/subdomains.js';
5
+ import { createCountriesApiService } from './src/services/countries.js';
6
+
7
+ export {
8
+ createSponsorsApiService,
9
+ createOrgsApiService,
10
+ createForestTypesApiService,
11
+ createSubdomainsApiService,
12
+ createCountriesApiService
13
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "@veritree/services",
3
+ "version": "0.2.0",
4
+ "lockfileVersion": 1
5
+ }
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.0.2",
3
+ "version": "0.3.0",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
+ "type": "module",
5
6
  "main": "index.js",
6
7
  "repository": "https://github.com/tentree-org/veritree-services.git",
7
8
  "author": "cyroveritree <cyro@veritree.com>",
@@ -0,0 +1,77 @@
1
+ import { getCookie } from './cookies';
2
+
3
+ const Api = {
4
+ baseUrl: `${process.env.API_VERITREE_URL}/api`,
5
+ forceEnvelopeResponse: true, // TODO: remove when API is fully migrated to envelopes
6
+
7
+ async get(url) {
8
+ return await this.unWrap(url);
9
+ },
10
+
11
+ async post(url, data) {
12
+ return await this.unWrap(url, 'post', data);
13
+ },
14
+
15
+ async update(url, data) {
16
+ return await this.unWrap(url, 'put', data);
17
+ },
18
+
19
+ /**
20
+ * Deals with all fetch requests
21
+ *
22
+ * @param {string} url
23
+ * @param {string} method
24
+ * @param {object} data
25
+ * @returns {object} envelope
26
+ */
27
+ async unWrap(url, method = 'get', data) {
28
+ if (this.forceEnvelopeResponse) url = this.addEvenlopeArgToUrl(url); // TODO: remove when API is fully migrated to envelopes
29
+ const config = this.getConfig(method, data);
30
+
31
+ try {
32
+ const response = await fetch(url, config);
33
+ const envelope = await response.json();
34
+
35
+ return envelope;
36
+ } catch (err) {
37
+ throw new Error(err);
38
+ }
39
+ },
40
+
41
+ /**
42
+ * Handles how the data should be sent in the fetch method
43
+ *
44
+ * @param {string} method
45
+ * @param {object} body
46
+ * @returns {object} data
47
+ */
48
+ getConfig(method, data) {
49
+ const config = {
50
+ method,
51
+ headers: {
52
+ 'Content-Type': 'application/json',
53
+ Authorization: `Bearer ${getCookie('access_token')}`,
54
+ },
55
+ };
56
+
57
+ if (method !== 'get') config.body = JSON.stringify(data);
58
+
59
+ return config;
60
+ },
61
+
62
+ /**
63
+ * Adds the envelope argument to the url
64
+ *
65
+ * @param {string} url
66
+ * @returns {string} url
67
+ */
68
+ addEvenlopeArgToUrl(url) {
69
+ if (!url || url.includes('_result=1')) return;
70
+ const urlHasArgs = url.includes('?');
71
+ const urlEvenlopeArg = urlHasArgs ? '&_result=1' : '?_result=1';
72
+
73
+ return `${url}${urlEvenlopeArg}`;
74
+ },
75
+ };
76
+
77
+ export default Api;
@@ -0,0 +1,36 @@
1
+ const converter = {
2
+ read(value) {
3
+ if (value[0] === '"') {
4
+ value = value.slice(1, -1);
5
+ }
6
+
7
+ return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent);
8
+ },
9
+ };
10
+
11
+ export function getCookie(name) {
12
+ if (typeof document === 'undefined' || (arguments.length && !name)) {
13
+ return;
14
+ }
15
+
16
+ // To prevent the for loop in the first place assign an empty array
17
+ // in case there are no cookies at all.
18
+ const cookies = document.cookie ? document.cookie.split('; ') : [];
19
+
20
+ const jar = {};
21
+ for (let i = 0; i < cookies.length; i++) {
22
+ const parts = cookies[i].split('=');
23
+ const value = parts.slice(1).join('=');
24
+
25
+ try {
26
+ const found = decodeURIComponent(parts[0]);
27
+ jar[found] = converter.read(value, found);
28
+
29
+ if (name === found) {
30
+ break;
31
+ }
32
+ } catch (e) {}
33
+ }
34
+
35
+ return name ? jar[name] : jar;
36
+ }
@@ -0,0 +1,23 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createCountriesApiService = (countryId) => {
4
+
5
+ const resource = 'countries';
6
+
7
+ const stats = {
8
+ async get() {
9
+ // Look for a country ID if none provided global stats will be sent
10
+ const type = countryId ? `${countryId}` : 'global';
11
+ const url = _getUrl(type +'/stats');
12
+ return await Api.get(url);
13
+ }
14
+ }
15
+
16
+ const _getUrl = (endpoint) => {
17
+ return `${Api.baseUrl}/${resource}/${endpoint}`;
18
+ };
19
+
20
+ return {
21
+ stats
22
+ };
23
+ };
@@ -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
+ /**
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
+ };
@@ -0,0 +1,27 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createOrgsApiService = (orgId, orgType) => {
4
+ if (!orgId && !orgType) throw new Error('No org id and/or type provided');
5
+ const resource = 'orgs';
6
+
7
+ // filter for stats data
8
+ const stats = {
9
+ async get(isPublic) {
10
+ const endpoint = _getStatsEndpoint(isPublic);
11
+ const url = _getUrl(endpoint);
12
+ return await Api.get(url);
13
+ },
14
+ };
15
+
16
+ const _getStatsEndpoint = (isPublic) => {
17
+ return isPublic ? 'pstats' : 'stats';
18
+ }
19
+
20
+ const _getUrl = (endpoint) => {
21
+ return `${Api.baseUrl}/${resource}/${endpoint}?org_id=${orgId}&org_type=${orgType}`;
22
+ };
23
+
24
+ return {
25
+ stats,
26
+ };
27
+ };
@@ -0,0 +1,31 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createSponsorsApiService = (orgId) => {
4
+ if (!orgId) throw new Error('No org id provided');
5
+ const resource = 'sponsors';
6
+
7
+ // filter for map data
8
+ const map = {
9
+ async get() {
10
+ const url = _getUrl('map-data');
11
+ return await Api.get(url);
12
+ },
13
+ };
14
+
15
+ // filter for profile
16
+ const profile = {
17
+ async get() {
18
+ const url = _getUrl('profile');
19
+ return await Api.get(url);
20
+ },
21
+ };
22
+
23
+ const _getUrl = (endpoint) => {
24
+ return `${Api.baseUrl}/${resource}/${orgId}/${endpoint}`;
25
+ };
26
+
27
+ return {
28
+ map,
29
+ profile,
30
+ };
31
+ };
@@ -0,0 +1,24 @@
1
+ import Api from '../helpers/api';
2
+
3
+ export const createSubdomainsApiService = () => {
4
+ const resource = 'subdomains';
5
+
6
+ const get = {
7
+ /**
8
+ * @param {string} id (name or id)
9
+ * @returns {object} evenlop
10
+ */
11
+ async specific(id) {
12
+ const url = _getUrl(id);
13
+ return await Api.get(url);
14
+ },
15
+ };
16
+
17
+ function _getUrl(id) {
18
+ return `${Api.baseUrl}/${resource}/${id}`;
19
+ }
20
+
21
+ return {
22
+ get,
23
+ };
24
+ };