@veritree/services 0.17.1 → 1.0.0-10

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.
Files changed (40) hide show
  1. package/index.js +38 -28
  2. package/package.json +1 -1
  3. package/src/endpoints/countries.js +5 -0
  4. package/src/endpoints/field-udpate-verifications.js +5 -0
  5. package/src/endpoints/field-updates.js +5 -0
  6. package/src/endpoints/forest-type-species.js +5 -0
  7. package/src/endpoints/forest-types-profiles.js +5 -0
  8. package/src/endpoints/forest-types.js +5 -0
  9. package/src/endpoints/form-submissions.js +5 -0
  10. package/src/endpoints/images.js +5 -0
  11. package/src/endpoints/notes.js +15 -0
  12. package/src/endpoints/orgs.js +25 -0
  13. package/src/endpoints/regions.js +5 -0
  14. package/src/endpoints/sdgs.js +5 -0
  15. package/src/endpoints/sponsors.js +34 -0
  16. package/src/endpoints/stats.js +17 -0
  17. package/src/endpoints/subdomains.js +5 -0
  18. package/src/endpoints/subsite-types.js +5 -0
  19. package/src/endpoints/subsites.js +5 -0
  20. package/src/endpoints/trees-orders.js +6 -0
  21. package/src/endpoints/user.js +15 -0
  22. package/src/helpers/api.js +171 -79
  23. package/src/helpers/session.js +0 -4
  24. package/src/utils/args.js +17 -22
  25. package/package-lock.json +0 -13
  26. package/src/services/countries.js +0 -35
  27. package/src/services/field-udpate-verifications.js +0 -34
  28. package/src/services/field-updates.js +0 -77
  29. package/src/services/forest-types.js +0 -48
  30. package/src/services/form-submissions.js +0 -33
  31. package/src/services/forms.js +0 -33
  32. package/src/services/images.js +0 -39
  33. package/src/services/me.js +0 -20
  34. package/src/services/orgs.js +0 -31
  35. package/src/services/regions.js +0 -41
  36. package/src/services/sponsors.js +0 -31
  37. package/src/services/subdomains.js +0 -26
  38. package/src/services/subsite-types.js +0 -70
  39. package/src/services/subsites.js +0 -70
  40. package/src/services/trees-orders.js +0 -42
package/index.js CHANGED
@@ -1,31 +1,41 @@
1
- import { createCountriesApiService } from './src/services/countries.js';
2
- import { createFieldUpdatesApiService } from './src/services/field-updates.js';
3
- import { createFieldUpdateVerificationsApiService } from './src/services/field-udpate-verifications.js';
4
- import { createFormSubmissionsApiService } from './src/services/form-submissions.js';
5
- import { createForestTypesApiService } from './src/services/forest-types.js';
6
- import { createImagesApiService } from './src/services/images.js';
7
- import { createMeApiService } from './src/services/me.js';
8
- import { createOrgsApiService } from './src/services/orgs.js';
9
- import { createRegionsApiService } from './src/services/regions.js';
10
- import { createSponsorsApiService } from './src/services/sponsors.js';
11
- import { createSubdomainsApiService } from './src/services/subdomains.js';
12
- import { createSubsitesApiService } from './src/services/subsites.js';
13
- import { createSubsiteTypesApiService } from './src/services/subsite-types.js';
14
- import { createTreeOrdersApiService } from './src/services/trees-orders.js';
1
+ import { Countries } from './src/endpoints/countries';
2
+ import { FieldUpdates } from './src/endpoints/field-updates';
3
+ import { FieldUpdateVerifications } from './src/endpoints/field-udpate-verifications';
4
+ import { FormSubmissions } from './src/endpoints/form-submissions';
5
+ import { ForestTypeSpecies } from './src/endpoints/forest-type-species';
6
+ import { ForestTypes } from './src/endpoints/forest-types';
7
+ import { ForestTypeProfiles } from './src/endpoints/forest-types-profiles';
8
+ import { Images } from './src/endpoints/images';
9
+ import { Orgs } from './src/endpoints/orgs';
10
+ import { Notes } from './src/endpoints/notes';
11
+ import { Regions } from './src/endpoints/regions';
12
+ import { SDGs } from './src/endpoints/sdgs';
13
+ import { Sponsors } from './src/endpoints/sponsors';
14
+ import { Stats } from './src/endpoints/stats';
15
+ import { Subdomains } from './src/endpoints/subdomains';
16
+ import { Subsites } from './src/endpoints/subsites';
17
+ import { SubsiteTypes } from './src/endpoints/subsite-types';
18
+ import { TreeOrders } from './src/endpoints/trees-orders';
19
+ import { User } from './src/endpoints/user';
15
20
 
16
21
  export {
17
- createCountriesApiService,
18
- createFieldUpdatesApiService,
19
- createFieldUpdateVerificationsApiService,
20
- createForestTypesApiService,
21
- createFormSubmissionsApiService,
22
- createOrgsApiService,
23
- createImagesApiService,
24
- createMeApiService,
25
- createRegionsApiService,
26
- createSponsorsApiService,
27
- createSubdomainsApiService,
28
- createSubsitesApiService,
29
- createSubsiteTypesApiService,
30
- createTreeOrdersApiService,
22
+ Countries,
23
+ FieldUpdates,
24
+ FieldUpdateVerifications,
25
+ ForestTypeSpecies,
26
+ ForestTypes,
27
+ ForestTypeProfiles,
28
+ FormSubmissions,
29
+ Images,
30
+ Orgs,
31
+ Notes,
32
+ Regions,
33
+ SDGs,
34
+ Sponsors,
35
+ Stats,
36
+ Subdomains,
37
+ Subsites,
38
+ SubsiteTypes,
39
+ TreeOrders,
40
+ User
31
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.17.1",
3
+ "version": "1.0.0-10",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "countries";
4
+
5
+ export const Countries = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "field-update-verifications";
4
+
5
+ export const FieldUpdateVerifications = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "field-updates";
4
+
5
+ export const FieldUpdates = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "forest-type-species";
4
+
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);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "forest-types";
4
+
5
+ export const ForestTypes = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "form-submissions";
4
+
5
+ export const FormSubmissions = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "images";
4
+
5
+ export const Images = new Api(resource);
@@ -0,0 +1,15 @@
1
+ import Api from "../helpers/api";
2
+
3
+ class NotesApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = "notes";
7
+ }
8
+
9
+ async relation(prefix, id, args) {
10
+ const url = `${this.baseUrl}/${prefix}/${id}/${this.resource}${this.getUrlParams(args)}`;
11
+ return await this.unWrap(url);
12
+ }
13
+ }
14
+
15
+ export const Notes = new NotesApi();
@@ -0,0 +1,25 @@
1
+ import Api from "../helpers/api";
2
+
3
+ class OrgsApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = "orgs";
7
+ }
8
+
9
+ async stats() {
10
+ const url = `${this._geStatstUrl()}`;
11
+ return await this.get(url);
12
+ }
13
+
14
+ async publicStats() {
15
+ const url = `${this._geStatstUrl(true)}`;
16
+ return await this.get(url);
17
+ }
18
+
19
+ _geStatstUrl(isPublic) {
20
+ const endpoint = isPublic ? "pstats" : "stats";
21
+ return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
22
+ }
23
+ }
24
+
25
+ export const Orgs = new OrgsApi();
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "regions";
4
+
5
+ export const Regions = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "sdgs";
4
+
5
+ export const SDGs = new Api(resource);
@@ -0,0 +1,34 @@
1
+ import Api from '../helpers/api';
2
+
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
+ async plantingStats(args) {
28
+ const url = `${this.getUrl()}/planting-stats${this.getUrlParams(args)}`;
29
+ return await this.get(url);
30
+ }
31
+
32
+ }
33
+
34
+ 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
+
@@ -0,0 +1,5 @@
1
+ import Api from '../helpers/api';
2
+
3
+ const resource = 'subdomains';
4
+
5
+ export const Subdomains = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "subsite-types";
4
+
5
+ export const SubsiteTypes = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api";
2
+
3
+ const resource = "subsites";
4
+
5
+ export const Subsites = new Api(resource);
@@ -0,0 +1,6 @@
1
+ import Api from '../helpers/api';
2
+
3
+ const resource = 'tree-orders';
4
+
5
+ export const TreeOrders = new Api(resource);
6
+
@@ -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,117 +1,209 @@
1
1
  import { getCookie } from "./cookies";
2
+ import { createParamsStringFromArgs } from "../utils/args";
3
+ import { getSession } from "./session";
4
+
5
+ /**
6
+ * Adds the envelope argument to the url
7
+ *
8
+ * @param {string} url
9
+ * @returns {string} url
10
+ */
11
+ function addEnvelopeParam(url) {
12
+ if (!url || url.includes("_result=1")) return url;
13
+
14
+ const urlHasArgs = url.includes("?");
15
+ const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
16
+
17
+ return `${url}${urlEvenlopeArg}`;
18
+ }
19
+
20
+ /**
21
+ * Handles how the data should be sent in the fetch method
22
+ *
23
+ * @param {string} method
24
+ * @param {object} body
25
+ * @returns {object} data
26
+ */
27
+ function getConfig(method, data, as) {
28
+ const isGet = method === "get";
29
+ const isSpoofing = as !== undefined;
30
+ const isFormData = data instanceof FormData;
31
+ const accessToken = `Bearer ${getCookie("access_token")}`;
32
+
33
+ const config = {
34
+ method,
35
+ headers: {
36
+ Authorization: accessToken,
37
+ },
38
+ };
39
+
40
+ if (!isFormData) {
41
+ config.headers["Content-Type"] = "application/json";
42
+ }
43
+
44
+ // TODO: improve this ifs and elses
45
+ if (!isGet) {
46
+ if (!data) data = {};
47
+
48
+ if (isFormData) {
49
+ if (isSpoofing) data.set("_method", as.toUpperCase());
50
+ config.body = data;
51
+ } else {
52
+ if (isSpoofing) data._method = as.toUpperCase();
53
+ config.body = JSON.stringify(data);
54
+ }
55
+ }
56
+
57
+ return config;
58
+ }
2
59
 
3
- const Api = {
4
- baseUrl: `${process.env.API_VERITREE_URL}/api`,
5
- forceEnvelopeResponse: true, // TODO: remove when API is fully migrated to envelopes
60
+ export default class Api {
61
+ constructor(resource) {
62
+ this.baseUrl = `${process.env.API_VERITREE_URL}/api`;
63
+ this.resource = resource ? resource : "";
64
+ this.orgId = null;
65
+ this.orgType = null;
66
+ }
6
67
 
7
68
  /**
8
- *
9
- * @param {string} url
69
+ *
10
70
  * @returns {promise}
11
71
  */
12
- async get(url) {
13
- return await this.unWrap(url);
14
- },
72
+ async all(args) {
73
+ const url = `${this.getUrl()}${this.getUrlParams(args)}`;
74
+ return await this.get(url);
75
+ }
15
76
 
16
77
  /**
17
- *
18
- * @param {string} url
19
- * @param {object} data
78
+ *
79
+ * @param {string, number} id
80
+ * @param {object} args/params
20
81
  * @returns {promise}
21
82
  */
22
- async post(url, data, as) {
23
- return await this.unWrap(url, "post", data, as);
24
- },
83
+ async single(id, args) {
84
+ const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
85
+ return await this.get(url);
86
+ }
87
+
88
+ /**
89
+ *
90
+ * @param {object} data
91
+ * @returns {promise}
92
+ */
93
+ async create(data) {
94
+ return await this.post(null, data);
95
+ }
25
96
 
26
97
  /**
27
- *
28
- * @param {string} url
29
- * @param {object} data
98
+ *
99
+ * @param {string} url
100
+ * @param {object} data
30
101
  * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
31
102
  * @returns {promise}
32
103
  */
33
- async update(url, data, as = "put") {
104
+ async update(id, data, as = "put") {
105
+ const url = `${this.getUrl(id)}${this.getUrlParams()}`;
34
106
  return await this.post(url, data, as);
35
- },
107
+ }
36
108
 
37
109
  /**
38
- * Deals with all fetch requests
39
110
  *
40
111
  * @param {string} url
41
- * @param {string} method
42
112
  * @param {object} data
43
- * @returns {object} envelope
113
+ * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
114
+ * @returns {promise}
44
115
  */
45
- 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
47
- const config = this.getConfig(method, data, as);
116
+ async delete(id) {
117
+ const url = `${this.getUrl(id)}${this.getUrlParams()}`;
118
+ return await this.post(url, null, "delete");
119
+ }
120
+
121
+ /**
122
+ *
123
+ * @param {string} url
124
+ * @returns {promise}
125
+ */
126
+ async get(url) {
127
+ return await this.unWrap(url);
128
+ }
48
129
 
49
- try {
50
- const response = await fetch(url, config);
51
- const envelope = await response.json();
130
+ /**
131
+ *
132
+ * @param {string} url
133
+ * @param {object} data
134
+ * @returns {promise}
135
+ */
136
+ async post(url, data, as) {
137
+ if (!url) url = `${this.getUrl()}${this.getUrlParams()}`;
138
+ return await this.unWrap(url, "post", data, as);
139
+ }
52
140
 
53
- return envelope;
54
- } catch (err) {
55
- throw new Error(err);
56
- }
57
- },
141
+ // ----------
142
+ // --
143
+ /**
144
+ *
145
+ * @param {string, number} id
146
+ * @returns
147
+ */
148
+ getUrl(id) {
149
+ id = id ? `/${id}` : "";
150
+ return `${this.baseUrl}/${this.resource}${id}`;
151
+ }
58
152
 
59
153
  /**
60
- * Handles how the data should be sent in the fetch method
61
154
  *
62
- * @param {string} method
63
- * @param {object} body
64
- * @returns {object} data
155
+ * @param {object} args
156
+ * @returns {string}
65
157
  */
66
- getConfig(method, data, as) {
67
- const isGet = method === "get";
68
- const isPut = as === "put";
69
- const isFormData = this._isFormData(data);
70
- const accessToken = `Bearer ${getCookie('access_token')}`;
71
-
72
- const config = {
73
- method,
74
- headers: {
75
- Authorization: accessToken,
76
- },
77
- };
78
-
79
- if(!isFormData) {
80
- config.headers['Content-Type'] = 'application/json';
158
+ getUrlParams(args) {
159
+ this.setOrg();
160
+ let isOrgLess = false;
161
+ let orgIdParam = "";
162
+ let orgTypeParam = "";
163
+
164
+ // while most of endpoints require an org id and type, some endpoints do not
165
+ if (args && args.length) {
166
+ isOrgLess = Object.hasOwnProperty.call(...args, "orgless");
81
167
  }
82
168
 
83
- // TODO: improve this ifs and elses
84
- if (!isGet) {
85
- if(isFormData) {
86
- if(isPut) data.set('_method', 'PUT');
87
- config.body = data;
88
- } else {
89
- if(isPut) data._method = 'PUT';
90
- config.body = JSON.stringify(data);
91
- }
169
+ if (!isOrgLess) {
170
+ if (this.orgId) orgIdParam = `org_id=${this.orgId}`;
171
+ if (this.orgType) orgTypeParam = `&org_type=${this.orgType}`;
92
172
  }
93
173
 
94
- return config;
95
- },
174
+ return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(args)}`;
175
+ }
96
176
 
97
177
  /**
98
- * Adds the envelope argument to the url
178
+ * Deals with all fetch requests
99
179
  *
100
180
  * @param {string} url
101
- * @returns {string} url
181
+ * @param {string} method
182
+ * @param {object} data
183
+ * @returns {object} envelope
102
184
  */
103
- handleEnvelopParam(url) {
104
- if (!url || url.includes("_result=1")) return url;
105
-
106
- const urlHasArgs = url.includes("?");
107
- const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
108
-
109
- return `${url}${urlEvenlopeArg}`;
110
- },
111
-
112
- _isFormData(data) {
113
- return data instanceof FormData;
114
- },
115
- };
185
+ async unWrap(url, method = "get", data, as) {
186
+ url = addEnvelopeParam(url);
187
+ const config = getConfig(method, data, as);
188
+ const response = await fetch(url, config);
189
+
190
+ return new Promise((resolve, reject) => {
191
+ if (response.ok && response.status === 204) {
192
+ resolve();
193
+ return;
194
+ }
116
195
 
117
- export default Api;
196
+ response.json().then((json) => {
197
+ response.ok ? resolve(json) : reject(json);
198
+ });
199
+ });
200
+ }
201
+
202
+ setOrg() {
203
+ const session = getSession();
204
+ if (!session) return;
205
+ const { orgId, orgType } = session;
206
+ this.orgId = orgId;
207
+ this.orgType = orgType;
208
+ }
209
+ }
@@ -1,7 +1,3 @@
1
- export const getSessionOrgId = () => {
2
- return getSession().orgId;
3
- };
4
-
5
1
  export const getSession = () => {
6
2
  return JSON.parse(localStorage.getItem('session'));
7
3
  };
package/src/utils/args.js CHANGED
@@ -1,32 +1,27 @@
1
1
  export const createParamsStringFromArgs = (args) => {
2
- if(!args) return '';
2
+ if (!args && typeof args !== 'object' && !Array.isArray(args)) {
3
+ return "";
4
+ }
3
5
 
4
6
  const paramsString = [];
5
7
  let arr = [];
6
8
 
7
- for(const arg of args) {
8
- if(typeof arg === 'object') {
9
- for(const key in arg) {
10
- if(arg[key] === null) return;
11
-
12
- if(Array.isArray(arg[key])) {
13
- arr = arg[key].map((item, index) => {
14
- const param = `${key}[]=${item}`;
9
+ Object.entries(args).forEach(([key, value]) => {
10
+ if (value === null) return;
15
11
 
16
- return index === 0 ? `${param}` : `&${param}`;
17
- }).join('');
12
+ if (Array.isArray(value)) {
13
+ arr = value.map((item, index) => {
14
+ const param = `${key}[]=${item}`;
15
+ return index === 0 ? `${param}` : `&${param}`;
16
+ }).join("");
18
17
 
19
- paramsString.push(arr);
20
- } else {
21
- if(arg[key] !== undefined) {
22
- paramsString.push(`${key}=${arg[key]}`);
23
- }
24
- }
18
+ paramsString.push(arr);
19
+ } else {
20
+ if (value !== undefined) {
21
+ paramsString.push(`${key}=${value}`);
25
22
  }
26
23
  }
27
- }
28
-
29
- if(!paramsString.length) return '';
24
+ });
30
25
 
31
- return `&${paramsString.join('&')}`;
32
- }
26
+ return paramsString.length ? `&${paramsString.join("&")}` : "";
27
+ };
package/package-lock.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "name": "@veritree/services",
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
- }
13
- }
@@ -1,35 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- export const createCountriesApiService = () => {
4
- const resource = 'countries';
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
-
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
- };
@@ -1,34 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
-
4
- export const createFieldUpdateVerificationsApiService = () => {
5
- const resource = `field-update-verifications`;
6
- const { orgId, orgType } = getSession();
7
-
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
- };
@@ -1,77 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createFieldUpdatesApiService = () => {
6
- const resource = `field-updates`;
7
- const { orgId, orgType } = getSession();
8
-
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
- };
@@ -1,48 +0,0 @@
1
- import Api from '../helpers/api';
2
- import { createParamsStringFromArgs } from '../utils/args';
3
- import { getSession } from "../helpers/session";
4
-
5
- export const createForestTypesApiService = () => {
6
- const resource = 'forest-types';
7
- const { orgId, orgType } = getSession();
8
-
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()}?${_getParams(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}?${_getParams()}`;
30
-
31
- return await Api.get(url);
32
- },
33
- };
34
-
35
- const _getUrl = () => {
36
- return `${Api.baseUrl}/${resource}`;
37
- };
38
-
39
- const _getParams = (args) => {
40
- const paramsString = createParamsStringFromArgs(args);
41
-
42
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
43
- };
44
-
45
- return {
46
- get,
47
- };
48
- };
@@ -1,33 +0,0 @@
1
- import Api from '../helpers/api';
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from '../utils/args';
4
-
5
- export const createFormSubmissionsApiService = () => {
6
- const resource = 'form-submissions';
7
- const { orgId, orgType } = getSession();
8
-
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
- };
@@ -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,39 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createImagesApiService = () => {
6
- const resource = "images";
7
- const { orgId, orgType } = getSession();
8
-
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
- };
@@ -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,31 +0,0 @@
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}?${_getUrlParams()}`;
12
- return await Api.get(url);
13
- },
14
- };
15
-
16
- const _getStatsEndpoint = (isPublic) => {
17
- return isPublic ? 'pstats' : 'stats';
18
- }
19
-
20
- const _getUrl = () => {
21
- return `${Api.baseUrl}/${resource}`;
22
- };
23
-
24
- const _getUrlParams = () => {
25
- return `org_id=${orgId}&org_type=${orgType}`
26
- }
27
-
28
- return {
29
- stats,
30
- };
31
- };
@@ -1,41 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createRegionsApiService = () => {
6
- const resource = "regions";
7
- const { orgId, orgType } = getSession();
8
-
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
- };
@@ -1,31 +0,0 @@
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()}/${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
- };
@@ -1,26 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- export const createSubdomainsApiService = () => {
4
- const resource = 'subdomains';
5
-
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
- };
@@ -1,70 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createSubsiteTypesApiService = () => {
6
- const resource = "subsite-types";
7
- const { orgId, orgType } = getSession();
8
-
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
- };
@@ -1,70 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createSubsitesApiService = () => {
6
- const resource = "subsites";
7
- const { orgId, orgType } = getSession();
8
-
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
- };
@@ -1,42 +0,0 @@
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
- };