@veritree/services 1.0.0-1 → 1.0.0-4

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.
@@ -1,31 +1,25 @@
1
1
  import Api from '../helpers/api';
2
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}?${_getUrlParams()}`;
12
- return await Api.get(url);
13
- },
14
- };
3
+ class OrgsApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = 'orgs';
7
+ }
15
8
 
16
- const _getStatsEndpoint = (isPublic) => {
17
- return isPublic ? 'pstats' : 'stats';
9
+ async stats() {
10
+ const url = `${this._geStatstUrl()}`;
11
+ return await this.get(url);
18
12
  }
19
13
 
20
- const _getUrl = () => {
21
- return `${Api.baseUrl}/${resource}`;
22
- };
14
+ async publicStats() {
15
+ const url = `${this._geStatstUrl(true)}`;
16
+ return await this.get(url);
17
+ }
23
18
 
24
- const _getUrlParams = () => {
25
- return `org_id=${orgId}&org_type=${orgType}`
19
+ _geStatstUrl = (isPublic) => {
20
+ const endpoint = isPublic ? 'pstats' : 'stats';
21
+ return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
26
22
  }
23
+ }
27
24
 
28
- return {
29
- stats,
30
- };
31
- };
25
+ export const Orgs = new OrgsApi();
@@ -1,41 +1,5 @@
1
1
  import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
2
 
5
- export const createRegionsApiService = () => {
6
- const resource = "regions";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "regions";
8
4
 
9
- if (!orgId && !orgType) {
10
- throw new Error("Organization id and type are required");
11
- }
12
-
13
- const get = {
14
- async all() {
15
- const url = `${_getUrl()}?${_getParams(arguments)}`;
16
-
17
- return await Api.get(url);
18
- },
19
- };
20
-
21
- const post = (data) => {
22
- const url = `${_getUrl()}?${_getParams()}`;
23
-
24
- return Api.post(url, data);
25
- };
26
-
27
- const _getUrl = () => {
28
- return `${Api.baseUrl}/${resource}`;
29
- };
30
-
31
- const _getParams = (args) => {
32
- const paramsString = createParamsStringFromArgs(args)
33
-
34
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
35
- };
36
-
37
- return {
38
- get,
39
- post,
40
- };
41
- };
5
+ export const Regions = new Api(resource);
@@ -1,31 +1,28 @@
1
1
  import Api from '../helpers/api';
2
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()}/${orgId}/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()}/${orgId}/profile`;
19
- return await Api.get(url);
20
- },
21
- };
22
-
23
- const _getUrl = () => {
24
- return `${Api.baseUrl}/${resource}`;
25
- };
26
-
27
- return {
28
- map,
29
- profile,
30
- };
31
- };
3
+ class SponsorsApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = 'sponsors';
7
+ }
8
+
9
+ async map() {
10
+ this.setOrg();
11
+
12
+ if(!this.orgId) throw new Error('No org id provided');
13
+
14
+ const url = `${this.getUrl()}/${this.orgId}/map-data`;
15
+ return await this.get(url);
16
+ }
17
+
18
+ async profile() {
19
+ this.setOrg();
20
+
21
+ if(!this.orgId) throw new Error('No org id provided');
22
+
23
+ const url = `${this.getUrl()}/${this.orgId}/profile`;
24
+ return await this.get(url);
25
+ }
26
+ }
27
+
28
+ export const Sponsors = new SponsorsApi();
@@ -0,0 +1,17 @@
1
+ import Api from '../helpers/api';
2
+
3
+ class StatsApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ }
7
+
8
+ async crumb(id) {
9
+ this.resource = 'pstats';
10
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
11
+
12
+ return await this.get(url);
13
+ }
14
+ }
15
+
16
+ export const Stats = new StatsApi();
17
+
@@ -1,26 +1,5 @@
1
1
  import Api from '../helpers/api';
2
2
 
3
- export const createSubdomainsApiService = () => {
4
- const resource = 'subdomains';
3
+ const resource = 'subdomains';
5
4
 
6
- const get = {
7
- /**
8
- * Gets org data related to subdomain
9
- *
10
- * @param {string} id (name or id)
11
- * @returns {object} evenlop
12
- */
13
- async specific(id) {
14
- const url = `${_getUrl()}/${id}`;
15
- return await Api.get(url);
16
- },
17
- };
18
-
19
- function _getUrl() {
20
- return `${Api.baseUrl}/${resource}`;
21
- }
22
-
23
- return {
24
- get,
25
- };
26
- };
5
+ export const Subdomains = new Api(resource);
@@ -1,70 +1,5 @@
1
1
  import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
2
 
5
- export const createSubsiteTypesApiService = () => {
6
- const resource = "subsite-types";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "subsite-types";
8
4
 
9
- if (!orgId && !orgType) {
10
- throw new Error("Organization id and type are required");
11
- }
12
-
13
- const get = {
14
- /**
15
- * @returns {obj} envelope
16
- */
17
- async all() {
18
- const url = `${_getUrl()}?${_getUrlParams(arguments)}`;
19
-
20
- return await Api.get(url);
21
- },
22
-
23
- /**
24
- * Gets a speficific subsite of an organization
25
- *
26
- * @param {number/string} id
27
- * @param {number/string} page
28
- * @returns {object} envelope
29
- */
30
- async specific(id) {
31
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
32
-
33
- return await Api.get(url);
34
- },
35
-
36
- async stats(id) {
37
- const url = `${_getUrl()}/${id}/stats`;
38
-
39
- return await Api.get(url);
40
- }
41
- };
42
-
43
- const post = (data) => {
44
- const url = `${_getUrl()}?${_getUrlParams()}`;
45
-
46
- return Api.post(url, data);
47
- }
48
-
49
- const update = (id, data) => {
50
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
51
-
52
- return Api.update(url, data);
53
- }
54
-
55
- const _getUrl = () => {
56
- return `${Api.baseUrl}/${resource}`;
57
- };
58
-
59
- const _getUrlParams = (args) => {
60
- const paramsString = createParamsStringFromArgs(args);
61
-
62
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
63
- };
64
-
65
- return {
66
- get,
67
- post,
68
- update
69
- };
70
- };
5
+ export const SubsiteTypes = new Api(resource);
@@ -1,70 +1,5 @@
1
1
  import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
2
 
5
- export const createSubsitesApiService = () => {
6
- const resource = "subsites";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "subsites";
8
4
 
9
- if (!orgId && !orgType) {
10
- throw new Error("Organization id and type are required");
11
- }
12
-
13
- const get = {
14
- /**
15
- * @returns {obj} envelope
16
- */
17
- async all() {
18
- const url = `${_getUrl()}?${_getUrlParams(arguments)}`;
19
-
20
- return await Api.get(url);
21
- },
22
-
23
- /**
24
- * Gets a speficific subsite of an organization
25
- *
26
- * @param {number/string} id
27
- * @param {number/string} page
28
- * @returns {object} envelope
29
- */
30
- async specific(id) {
31
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
32
-
33
- return await Api.get(url);
34
- },
35
-
36
- async stats(id) {
37
- const url = `${_getUrl()}/${id}/stats`;
38
-
39
- return await Api.get(url);
40
- },
41
- };
42
-
43
- const post = (data) => {
44
- const url = `${_getUrl()}?${_getUrlParams()}`;
45
-
46
- return Api.post(url, data);
47
- };
48
-
49
- const update = (id, data) => {
50
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
51
-
52
- return Api.update(url, data);
53
- };
54
-
55
- const _getUrl = () => {
56
- return `${Api.baseUrl}/${resource}`;
57
- };
58
-
59
- const _getUrlParams = (args) => {
60
- const paramsString = createParamsStringFromArgs(args);
61
-
62
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
63
- };
64
-
65
- return {
66
- get,
67
- post,
68
- update,
69
- };
70
- };
5
+ export const Subsites = new Api(resource);
@@ -1,42 +1,5 @@
1
1
  import Api from '../helpers/api';
2
2
 
3
- export const createTreeOrdersApiService = (orgId, orgType) => {
4
- const resource = 'tree-orders';
3
+ const resource = 'tree-orders';
4
+ export const TreeOrders = new Api(resource);
5
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
- };
@@ -0,0 +1,15 @@
1
+ import Api from '../helpers/api';
2
+
3
+ class UserApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = 'me';
7
+ }
8
+
9
+ async me() {
10
+ const url = `${this.getUrl()}${this.getUrlParams()}`;
11
+ return await this.get(url);
12
+ }
13
+ }
14
+
15
+ export const User = new UserApi();
@@ -1,162 +0,0 @@
1
- import { getCookie } from "./cookies";
2
- import { getSession } from "./session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export default function Api(resource) {
6
- const baseUrl = `${process.env.API_VERITREE_URL}/api`;
7
- const session = getSession();
8
- if(!session) return;
9
- const { orgId, orgType } = session;
10
-
11
-
12
- if (!orgId && !orgType) {
13
- throw new Error("Organization id and type are required");
14
- }
15
-
16
- /**
17
- *
18
- * @returns {promise}
19
- */
20
- this.all = async function () {
21
- const url = `${this.getUrl()}?${this.getUrlParams(arguments)}`;
22
- return await this.get(url);
23
- };
24
-
25
- /**
26
- *
27
- * @param {string, number} id
28
- * @returns {promise}
29
- */
30
- this.single = async function (id) {
31
- const url = `${this.getUrl()}/${id}?${this.getUrlParams()}`;
32
- return await this.get(url);
33
- };
34
-
35
- /**
36
- *
37
- * @param {object} data
38
- * @returns {promise}
39
- */
40
- this.create = async function (data) {
41
- const url = `${this.getUrl()}?${this.getUrlParams()}`;
42
- return await this.post(url, data);
43
- };
44
-
45
- /**
46
- *
47
- * @param {string} url
48
- * @param {object} data
49
- * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
50
- * @returns {promise}
51
- */
52
- this.update = async function (id, data, as = 'put') {
53
- const url = `${this.getUrl()}/${id}?${this.getUrlParams()}`;
54
- return await this.post(url, data, as);
55
- };
56
-
57
- // ----------
58
- // --
59
- this.getUrl = () => {
60
- return `${baseUrl}/${resource}`;
61
- }
62
-
63
- this.getUrlParams = (args) => {
64
- return `org_id=${orgId}&org_type=${orgType}${createParamsStringFromArgs(args)}`;
65
- }
66
-
67
- /**
68
- *
69
- * @param {string} url
70
- * @returns {promise}
71
- */
72
- this.get = async function (url) {
73
- return await this.unWrap(url);
74
- };
75
-
76
- /**
77
- *
78
- * @param {string} url
79
- * @param {object} data
80
- * @returns {promise}
81
- */
82
- this.post = async function (url, data, as) {
83
- return await this.unWrap(url, "post", data, as);
84
- };
85
-
86
- /**
87
- * Deals with all fetch requests
88
- *
89
- * @param {string} url
90
- * @param {string} method
91
- * @param {object} data
92
- * @returns {object} envelope
93
- */
94
- this.unWrap = async function (url, method = "get", data, as) {
95
- url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
96
- const config = this.getConfig(method, data, as);
97
-
98
- try {
99
- const response = await fetch(url, config);
100
- const envelope = await response.json();
101
-
102
- return envelope;
103
- } catch (err) {
104
- throw new Error(err);
105
- }
106
- };
107
-
108
- /**
109
- * Handles how the data should be sent in the fetch method
110
- *
111
- * @param {string} method
112
- * @param {object} body
113
- * @returns {object} data
114
- */
115
- this.getConfig = function (method, data, as) {
116
- const isGet = method === "get";
117
- const isPut = as === "put";
118
- const isFormData = this.isFormData(data);
119
- const accessToken = `Bearer ${getCookie("access_token")}`;
120
-
121
- const config = {
122
- method,
123
- headers: {
124
- Authorization: accessToken,
125
- },
126
- };
127
-
128
- if (!isFormData) {
129
- config.headers["Content-Type"] = "application/json";
130
- }
131
-
132
- // TODO: improve this ifs and elses
133
- if (!isGet) {
134
- if (isFormData) {
135
- if (isPut) data.set("_method", "PUT");
136
- config.body = data;
137
- } else {
138
- if (isPut) data._method = "PUT";
139
- config.body = JSON.stringify(data);
140
- }
141
- }
142
-
143
- return config;
144
- };
145
-
146
- /**
147
- * Adds the envelope argument to the url
148
- *
149
- * @param {string} url
150
- * @returns {string} url
151
- */
152
- this.handleEnvelopParam = function (url) {
153
- if (!url || url.includes("_result=1")) return url;
154
-
155
- const urlHasArgs = url.includes("?");
156
- const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
157
-
158
- return `${url}${urlEvenlopeArg}`;
159
- };
160
-
161
- this.isFormData = (data) => data instanceof FormData;
162
- }
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "countries";
4
-
5
- export const Countries = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "forest-type-species";
4
-
5
- export const ForestTypeSpecies = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "forest-types";
4
-
5
- export const ForestTypes = new Api(resource);
@@ -1,33 +0,0 @@
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
- };
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "images";
4
-
5
- export const Images = new Api(resource);
@@ -1,20 +0,0 @@
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
- };
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "regions";
4
-
5
- export const Regions = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "subsites";
4
-
5
- export const Subsites = new Api(resource);
@@ -1,16 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
-
4
- export const all = async (url) => {
5
- return await Api.get(url);
6
- };
7
-
8
- export const getUrl = (resource) => {
9
- return `${Api.baseUrl}/${resource}`;
10
- };
11
-
12
- export const getUrlParams = (args) => {
13
- const paramsString = createParamsStringFromArgs(args);
14
-
15
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
16
- };