@veritree/services 1.0.0-2 → 1.0.0-5

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,45 +1,37 @@
1
- import { createCountriesApiService } from './src/services/countries.js';
2
- import { Countries } from './src/services/countries2.js';
3
- import { createFieldUpdatesApiService } from './src/services/field-updates.js';
4
- import { createFieldUpdateVerificationsApiService } from './src/services/field-udpate-verifications.js';
5
- import { createFormSubmissionsApiService } from './src/services/form-submissions.js';
6
- import { createForestTypeSpeciesApiService } from './src/services/forest-type-species.js';
7
- import { ForestTypeSpecies } from './src/services/forest-type-species2.js';
8
- import { createForestTypesApiService } from './src/services/forest-types.js';
9
- import { ForestTypes } from './src/services/forest-types2.js';
10
- import { createImagesApiService } from './src/services/images.js';
11
- import { Images } from './src/services/images2.js';
12
- import { createMeApiService } from './src/services/me.js';
13
- import { createOrgsApiService } from './src/services/orgs.js';
14
- import { createRegionsApiService } from './src/services/regions.js';
15
- import { Regions } from './src/services/regions2.js';
16
- import { createSponsorsApiService } from './src/services/sponsors.js';
17
- import { createSubdomainsApiService } from './src/services/subdomains.js';
18
- import { createSubsitesApiService } from './src/services/subsites.js';
19
- import { Subsites } from './src/services/subsites2.js';
20
- import { createSubsiteTypesApiService } from './src/services/subsite-types.js';
21
- import { createTreeOrdersApiService } from './src/services/trees-orders.js';
1
+ import { Countries } from './src/services/countries';
2
+ import { FieldUpdates } from './src/services/field-updates';
3
+ import { FieldUpdateVerifications } from './src/services/field-udpate-verifications';
4
+ import { FormSubmissions } from './src/services/form-submissions';
5
+ import { ForestTypeSpecies } from './src/services/forest-type-species';
6
+ import { ForestTypes } from './src/services/forest-types';
7
+ import { ForestTypeProfiles } from './src/services/forest-types-profiles';
8
+ import { Images } from './src/services/images';
9
+ import { User } from './src/services/user';
10
+ import { Orgs } from './src/services/orgs';
11
+ import { Regions } from './src/services/regions';
12
+ import { Sponsors } from './src/services/sponsors';
13
+ import { Stats } from './src/services/stats';
14
+ import { Subdomains } from './src/services/subdomains';
15
+ import { Subsites } from './src/services/subsites';
16
+ import { SubsiteTypes } from './src/services/subsite-types';
17
+ import { TreeOrders } from './src/services/trees-orders';
22
18
 
23
19
  export {
24
- createCountriesApiService,
25
20
  Countries,
26
- createFieldUpdatesApiService,
27
- createFieldUpdateVerificationsApiService,
28
- createForestTypeSpeciesApiService,
21
+ FieldUpdates,
22
+ FieldUpdateVerifications,
29
23
  ForestTypeSpecies,
30
- createForestTypesApiService,
31
24
  ForestTypes,
32
- createFormSubmissionsApiService,
33
- createOrgsApiService,
34
- createImagesApiService,
25
+ ForestTypeProfiles,
26
+ FormSubmissions,
27
+ Orgs,
35
28
  Images,
36
- createMeApiService,
37
- createRegionsApiService,
29
+ User,
38
30
  Regions,
39
- createSponsorsApiService,
40
- createSubdomainsApiService,
41
- createSubsitesApiService,
31
+ Sponsors,
32
+ Stats,
33
+ Subdomains,
42
34
  Subsites,
43
- createSubsiteTypesApiService,
44
- createTreeOrdersApiService,
35
+ SubsiteTypes,
36
+ TreeOrders
45
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "1.0.0-2",
3
+ "version": "1.0.0-5",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,38 +1,89 @@
1
1
  import { getCookie } from "./cookies";
2
+ import { createParamsStringFromArgs } from "../utils/args";
3
+ import { getSession } from "./session";
2
4
 
3
- const Api = {
4
- baseUrl: `${process.env.API_VERITREE_URL}/api`,
5
- forceEnvelopeResponse: true, // TODO: remove when API is fully migrated to envelopes
5
+ export default class Api {
6
+ constructor(resource) {
7
+ this.baseUrl = `${process.env.API_VERITREE_URL}/api`;
8
+ this.resource = resource;
9
+ this.orgId = null;
10
+ this.orgType = null;
11
+ }
6
12
 
7
13
  /**
8
- *
9
- * @param {string} url
14
+ *
10
15
  * @returns {promise}
11
16
  */
12
- async get(url) {
13
- return await this.unWrap(url);
14
- },
17
+ async all() {
18
+ const url = `${this.getUrl()}${this.getUrlParams(arguments)}`;
19
+ return await this.get(url);
20
+ }
15
21
 
16
22
  /**
17
- *
18
- * @param {string} url
19
- * @param {object} data
23
+ *
24
+ * @param {string, number} id
20
25
  * @returns {promise}
21
26
  */
22
- async post(url, data, as) {
23
- return await this.unWrap(url, "post", data, as);
24
- },
27
+ async single(id) {
28
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
29
+ return await this.get(url);
30
+ }
25
31
 
26
32
  /**
27
- *
28
- * @param {string} url
29
- * @param {object} data
33
+ *
34
+ * @param {object} data
35
+ * @returns {promise}
36
+ */
37
+ async create(data) {
38
+ return await this.post(null, data);
39
+ }
40
+
41
+ /**
42
+ *
43
+ * @param {string} url
44
+ * @param {object} data
30
45
  * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
31
46
  * @returns {promise}
32
47
  */
33
- async update(url, data, as = "put") {
48
+ async update(id, data, as = "put") {
49
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
34
50
  return await this.post(url, data, as);
35
- },
51
+ }
52
+
53
+ /**
54
+ *
55
+ * @param {string} url
56
+ * @returns {promise}
57
+ */
58
+ async get(url) {
59
+ return await this.unWrap(url);
60
+ }
61
+
62
+ /**
63
+ *
64
+ * @param {string} url
65
+ * @param {object} data
66
+ * @returns {promise}
67
+ */
68
+ async post(url, data, as) {
69
+ if (!url) url = `${this.getUrl()}${this.getUrlParams()}`;
70
+ return await this.unWrap(url, "post", data, as);
71
+ }
72
+
73
+ // ----------
74
+ // --
75
+ getUrl() {
76
+ return `${this.baseUrl}/${this.resource}`;
77
+ }
78
+
79
+ getUrlParams(args) {
80
+ this.setOrg();
81
+
82
+ const orgIdParam = this.orgId ? `org_id=${this.orgId}` : "";
83
+ const orgTypeParam = this.orgType ? `org_type=${this.orgType}` : "";
84
+
85
+ return `?${orgIdParam}&${orgTypeParam}${createParamsStringFromArgs(args)}`;
86
+ }
36
87
 
37
88
  /**
38
89
  * Deals with all fetch requests
@@ -43,7 +94,7 @@ const Api = {
43
94
  * @returns {object} envelope
44
95
  */
45
96
  async unWrap(url, method = "get", data, as) {
46
- if (this.forceEnvelopeResponse) url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
97
+ url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
47
98
  const config = this.getConfig(method, data, as);
48
99
 
49
100
  try {
@@ -54,7 +105,7 @@ const Api = {
54
105
  } catch (err) {
55
106
  throw new Error(err);
56
107
  }
57
- },
108
+ }
58
109
 
59
110
  /**
60
111
  * Handles how the data should be sent in the fetch method
@@ -66,8 +117,8 @@ const Api = {
66
117
  getConfig(method, data, as) {
67
118
  const isGet = method === "get";
68
119
  const isPut = as === "put";
69
- const isFormData = this._isFormData(data);
70
- const accessToken = `Bearer ${getCookie('access_token')}`;
120
+ const isFormData = this.isFormData(data);
121
+ const accessToken = `Bearer ${getCookie("access_token")}`;
71
122
 
72
123
  const config = {
73
124
  method,
@@ -76,23 +127,31 @@ const Api = {
76
127
  },
77
128
  };
78
129
 
79
- if(!isFormData) {
80
- config.headers['Content-Type'] = 'application/json';
130
+ if (!isFormData) {
131
+ config.headers["Content-Type"] = "application/json";
81
132
  }
82
133
 
83
134
  // TODO: improve this ifs and elses
84
135
  if (!isGet) {
85
- if(isFormData) {
86
- if(isPut) data.set('_method', 'PUT');
136
+ if (isFormData) {
137
+ if (isPut) data.set("_method", "PUT");
87
138
  config.body = data;
88
139
  } else {
89
- if(isPut) data._method = 'PUT';
140
+ if (isPut) data._method = "PUT";
90
141
  config.body = JSON.stringify(data);
91
142
  }
92
143
  }
93
144
 
94
145
  return config;
95
- },
146
+ }
147
+
148
+ setOrg() {
149
+ const session = getSession();
150
+ if (!session) return;
151
+ const { orgId, orgType } = session;
152
+ this.orgId = orgId;
153
+ this.orgType = orgType;
154
+ }
96
155
 
97
156
  /**
98
157
  * Adds the envelope argument to the url
@@ -107,11 +166,9 @@ const Api = {
107
166
  const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
108
167
 
109
168
  return `${url}${urlEvenlopeArg}`;
110
- },
111
-
112
- _isFormData(data) {
113
- return data instanceof FormData;
114
- },
115
- };
169
+ }
116
170
 
117
- export default Api;
171
+ isFormData(data) {
172
+ data instanceof FormData;
173
+ }
174
+ }
@@ -1,35 +1,5 @@
1
- import Api from '../helpers/api';
1
+ import Api from "../helpers/api";
2
2
 
3
- export const createCountriesApiService = () => {
4
- const resource = 'countries';
3
+ const resource = "countries";
5
4
 
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
-
18
- const stats = {
19
- async get(countryId) {
20
- const type = countryId ? `${countryId}` : 'global';
21
- const url = `${_getUrl()}/${type}/stats`;
22
-
23
- return await Api.get(url);
24
- }
25
- }
26
-
27
- const _getUrl = () => {
28
- return `${Api.baseUrl}/${resource}`;
29
- };
30
-
31
- return {
32
- get,
33
- stats
34
- };
35
- };
5
+ export const Countries = new Api(resource);
@@ -1,34 +1,5 @@
1
1
  import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
2
 
4
- export const createFieldUpdateVerificationsApiService = () => {
5
- const resource = `field-update-verifications`;
6
- const { orgId, orgType } = getSession();
3
+ const resource = "field-update-verifications";
7
4
 
8
- if (!orgId && !orgType) {
9
- throw new Error('Organization id and type are required');
10
- }
11
-
12
- /**
13
- *
14
- * @param {object} data
15
- * @returns
16
- */
17
- const post = (data) => {
18
- const url = `${_getUrl()}?${_getParams()}`;
19
-
20
- return Api.post(url, data);
21
- };
22
-
23
- const _getUrl = () => {
24
- return `${Api.baseUrl}/${resource}`;
25
- };
26
-
27
- const _getParams = () => {
28
- return `org_id=${orgId}&org_type=${orgType}`;
29
- };
30
-
31
- return {
32
- post,
33
- };
34
- };
5
+ export const FieldUpdateVerifications = new Api(resource);
@@ -1,77 +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 createFieldUpdatesApiService = () => {
6
- const resource = `field-updates`;
7
- const { orgId, orgType } = getSession();
3
+ const resource = "field-updates";
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
- async specific(id) {
21
- const url = `${_getUrl()}/${id}?${_getParams()}`;
22
-
23
- return await Api.get(url);
24
- }
25
- };
26
-
27
- /**
28
- *
29
- * @param {object} data
30
- * @returns
31
- */
32
- const post = (data) => {
33
- const url = `${_getUrl()}?${_getParams()}`;
34
-
35
- return Api.post(url, data);
36
- };
37
-
38
- /**
39
- *
40
- * @param {string} fieldUpdateId
41
- * @param {object} data
42
- * @returns
43
- */
44
- const update = (fieldUpdateId, data) => {
45
- const url = `${_getUrl()}/${fieldUpdateId}?${_getParams()}`;
46
-
47
- return Api.post(url, data);
48
- };
49
-
50
- /**
51
- *
52
- */
53
- const remove = {
54
- async img(fieldUpdateId, data) {
55
- const url = `${_getUrl()}/${fieldUpdateId}/images/detach?${_getParams()}`;
56
-
57
- return await Api.post(url, data);
58
- }
59
- }
60
-
61
- const _getUrl = () => {
62
- return `${Api.baseUrl}/${resource}`;
63
- };
64
-
65
- const _getParams = (args) => {
66
- const paramsString = createParamsStringFromArgs(args);
67
-
68
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
69
- };
70
-
71
- return {
72
- get,
73
- post,
74
- update,
75
- remove
76
- };
77
- };
5
+ export const FieldUpdates = new Api(resource);
@@ -1,63 +1,5 @@
1
1
  import Api from "../helpers/api";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
- import { getSession } from "../helpers/session";
4
- // import { all, getUrl, getUrlParams } from "../utils/methods";
5
2
 
6
- export const createForestTypeSpeciesApiService = () => {
7
- const resource = "forest-type-species";
8
- const { orgId, orgType } = getSession();
3
+ const resource = "forest-type-species";
9
4
 
10
- if (!orgId && !orgType) {
11
- throw new Error("Organization id and type are required");
12
- }
13
-
14
- const get = {
15
- /**
16
- * @param {object} arguments
17
- * @returns {object} envelope
18
- */
19
- async all() {
20
- const url = `${_getUrl()}?${_getUrlParams(arguments)}`;
21
-
22
- return await Api.get(url);
23
- },
24
-
25
- /**
26
- * @param {number/string} id
27
- * @returns {object} envelope
28
- */
29
- async specific(id) {
30
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
31
-
32
- return await Api.get(url);
33
- },
34
- };
35
-
36
- const post = (data) => {
37
- const url = `${_getUrl()}?${_getUrlParams()}`;
38
-
39
- return Api.post(url, data);
40
- };
41
-
42
- const update = (id, data) => {
43
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
44
-
45
- return Api.update(url, data);
46
- };
47
-
48
- const _getUrl = () => {
49
- return `${Api.baseUrl}/${resource}`;
50
- };
51
-
52
- const _getUrlParams = (args) => {
53
- const paramsString = createParamsStringFromArgs(args);
54
-
55
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
56
- };
57
-
58
- return {
59
- get,
60
- post,
61
- update,
62
- };
63
- };
5
+ export const ForestTypeSpecies = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from '../helpers/api';
2
+
3
+ const resource = 'forest-type-profiles';
4
+
5
+ export const ForestTypeProfiles = new Api(resource);
@@ -1,62 +1,5 @@
1
1
  import Api from "../helpers/api";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
- import { getSession } from "../helpers/session";
4
2
 
5
- export const createForestTypesApiService = () => {
6
- const resource = "forest-types";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "forest-types";
8
4
 
9
- if (!orgId && !orgType) {
10
- throw new Error("Organization id and type are required");
11
- }
12
-
13
- const get = {
14
- /**
15
- * @param {object} arguments
16
- * @returns {object} envelope
17
- */
18
- async all() {
19
- const url = `${_getUrl()}?${_getUrlParams(arguments)}`;
20
-
21
- return await Api.get(url);
22
- },
23
-
24
- /**
25
- * @param {number/string} id
26
- * @returns {object} envelope
27
- */
28
- async specific(id) {
29
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
30
-
31
- return await Api.get(url);
32
- },
33
- };
34
-
35
- const post = (data) => {
36
- const url = `${_getUrl()}?${_getUrlParams()}`;
37
-
38
- return Api.post(url, data);
39
- };
40
-
41
- const update = (id, data) => {
42
- const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
43
-
44
- return Api.update(url, data);
45
- };
46
-
47
- const _getUrl = () => {
48
- return `${Api.baseUrl}/${resource}`;
49
- };
50
-
51
- const _getUrlParams = (args) => {
52
- const paramsString = createParamsStringFromArgs(args);
53
-
54
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
55
- };
56
-
57
- return {
58
- get,
59
- post,
60
- update,
61
- };
62
- };
5
+ export const ForestTypes = new Api(resource);
@@ -1,33 +1,5 @@
1
- import Api from '../helpers/api';
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from '../utils/args';
1
+ import Api from "../helpers/api";
4
2
 
5
- export const createFormSubmissionsApiService = () => {
6
- const resource = 'form-submissions';
7
- const { orgId, orgType } = getSession();
3
+ const resource = "form-submissions";
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
- return await Api.get(url);
17
- },
18
- };
19
-
20
- const _getUrl = () => {
21
- return `${Api.baseUrl}/${resource}`;
22
- };
23
-
24
- const _getParams = (args) => {
25
- const paramsString = createParamsStringFromArgs(args);
26
-
27
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
28
- };
29
-
30
- return {
31
- get,
32
- };
33
- };
5
+ export const FormSubmissions = new Api(resource);
@@ -1,39 +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 createImagesApiService = () => {
6
- const resource = "images";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "images";
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
- return await Api.get(url);
17
- },
18
-
19
- async specific(id) {
20
- const url = `${_getUrl()}/${id}?${_getParams()}`;
21
-
22
- return await Api.get(url);
23
- },
24
- };
25
-
26
- const _getUrl = () => {
27
- return `${Api.baseUrl}/${resource}`;
28
- };
29
-
30
- const _getParams = (args) => {
31
- const paramsString = createParamsStringFromArgs(args);
32
-
33
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
34
- };
35
-
36
- return {
37
- get,
38
- };
39
- };
5
+ export const Images = new Api(resource);
@@ -1,31 +1,25 @@
1
- import Api from '../helpers/api';
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,158 +0,0 @@
1
- import { getCookie } from "./cookies";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
- import { getSession } from "./session";
4
-
5
- export default function Api(resource, session) {
6
- const baseUrl = `${process.env.API_VERITREE_URL}/api`;
7
-
8
- /**
9
- *
10
- * @returns {promise}
11
- */
12
- this.all = async function () {
13
- const url = `${this.getUrl()}?${this.getUrlParams(arguments)}`;
14
- return await this.get(url);
15
- };
16
-
17
- /**
18
- *
19
- * @param {string, number} id
20
- * @returns {promise}
21
- */
22
- this.single = async function (id) {
23
- const url = `${this.getUrl()}/${id}?${this.getUrlParams()}`;
24
- return await this.get(url);
25
- };
26
-
27
- /**
28
- *
29
- * @param {object} data
30
- * @returns {promise}
31
- */
32
- this.create = async function (data) {
33
- const url = `${this.getUrl()}?${this.getUrlParams()}`;
34
- return await this.post(url, data);
35
- };
36
-
37
- /**
38
- *
39
- * @param {string} url
40
- * @param {object} data
41
- * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
42
- * @returns {promise}
43
- */
44
- this.update = async function (id, data, as = "put") {
45
- const url = `${this.getUrl()}/${id}?${this.getUrlParams()}`;
46
- return await this.post(url, data, as);
47
- };
48
-
49
- // ----------
50
- // --
51
- this.getUrl = () => {
52
- return `${baseUrl}/${resource}`;
53
- };
54
-
55
- this.getUrlParams = (args) => {
56
- const { orgId, orgType } = getSession();
57
-
58
- return `org_id=${orgId}&org_type=${orgType}${createParamsStringFromArgs(
59
- args
60
- )}`;
61
- };
62
-
63
- /**
64
- *
65
- * @param {string} url
66
- * @returns {promise}
67
- */
68
- this.get = async function (url) {
69
- return await this.unWrap(url);
70
- };
71
-
72
- /**
73
- *
74
- * @param {string} url
75
- * @param {object} data
76
- * @returns {promise}
77
- */
78
- this.post = async function (url, data, as) {
79
- return await this.unWrap(url, "post", data, as);
80
- };
81
-
82
- /**
83
- * Deals with all fetch requests
84
- *
85
- * @param {string} url
86
- * @param {string} method
87
- * @param {object} data
88
- * @returns {object} envelope
89
- */
90
- this.unWrap = async function (url, method = "get", data, as) {
91
- url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
92
- const config = this.getConfig(method, data, as);
93
-
94
- try {
95
- const response = await fetch(url, config);
96
- const envelope = await response.json();
97
-
98
- return envelope;
99
- } catch (err) {
100
- throw new Error(err);
101
- }
102
- };
103
-
104
- /**
105
- * Handles how the data should be sent in the fetch method
106
- *
107
- * @param {string} method
108
- * @param {object} body
109
- * @returns {object} data
110
- */
111
- this.getConfig = function (method, data, as) {
112
- const isGet = method === "get";
113
- const isPut = as === "put";
114
- const isFormData = this.isFormData(data);
115
- const accessToken = `Bearer ${getCookie("access_token")}`;
116
-
117
- const config = {
118
- method,
119
- headers: {
120
- Authorization: accessToken,
121
- },
122
- };
123
-
124
- if (!isFormData) {
125
- config.headers["Content-Type"] = "application/json";
126
- }
127
-
128
- // TODO: improve this ifs and elses
129
- if (!isGet) {
130
- if (isFormData) {
131
- if (isPut) data.set("_method", "PUT");
132
- config.body = data;
133
- } else {
134
- if (isPut) data._method = "PUT";
135
- config.body = JSON.stringify(data);
136
- }
137
- }
138
-
139
- return config;
140
- };
141
-
142
- /**
143
- * Adds the envelope argument to the url
144
- *
145
- * @param {string} url
146
- * @returns {string} url
147
- */
148
- this.handleEnvelopParam = function (url) {
149
- if (!url || url.includes("_result=1")) return url;
150
-
151
- const urlHasArgs = url.includes("?");
152
- const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
153
-
154
- return `${url}${urlEvenlopeArg}`;
155
- };
156
-
157
- this.isFormData = (data) => data instanceof FormData;
158
- }
@@ -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
- };