@veritree/services 0.0.2 → 0.1.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,7 @@
1
+ import { createSponsorsApiService } from './src/services/sponsors.js';
2
+ import { createOrgsApiService } from './src/services/orgs.js';
3
+
4
+ export {
5
+ createSponsorsApiService,
6
+ createOrgsApiService,
7
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "@veritree/services",
3
+ "version": "0.0.2",
4
+ "lockfileVersion": 1
5
+ }
package/package.json CHANGED
@@ -1,12 +1,14 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.0.2",
3
+ "version": "0.1.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>",
8
9
  "license": "MIT",
9
10
  "publishConfig": {
10
11
  "access": "public"
11
- }
12
+ },
13
+ "devDependencies": {}
12
14
  }
@@ -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,26 @@
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
+ const stats = {
8
+ async get(isPublic) {
9
+ const endpoint = _getEndpoint(isPublic);
10
+ const url = _getUrl(endpoint);
11
+ return await Api.get(url);
12
+ },
13
+ };
14
+
15
+ function _getEndpoint(isPublic) {
16
+ return isPublic ? 'pstats' : 'stats';
17
+ }
18
+
19
+ const _getUrl = (endpoint) => {
20
+ return `${Api.baseUrl}/${resource}/${endpoint}?org_id=${orgId}&org_type=${orgType}`;
21
+ };
22
+
23
+ return {
24
+ stats,
25
+ };
26
+ };
@@ -0,0 +1,29 @@
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
+ const map = {
8
+ async get() {
9
+ const url = _getUrl('map-data');
10
+ return await Api.get(url);
11
+ },
12
+ };
13
+
14
+ const profile = {
15
+ async get() {
16
+ const url = _getUrl('profile');
17
+ return await Api.get(url);
18
+ },
19
+ };
20
+
21
+ const _getUrl = (endpoint) => {
22
+ return `${Api.baseUrl}/${resource}/${orgId}/${endpoint}`;
23
+ };
24
+
25
+ return {
26
+ map,
27
+ profile,
28
+ };
29
+ };