@veritree/services 1.0.0-0 → 1.0.0-11

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 (46) hide show
  1. package/index.js +34 -36
  2. package/package.json +1 -1
  3. package/src/{services/countries2.js → endpoints/countries.js} +1 -1
  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/{services/images2.js → endpoints/images.js} +1 -1
  11. package/src/endpoints/notes.js +13 -0
  12. package/src/endpoints/orgs.js +25 -0
  13. package/src/{services/regions2.js → endpoints/regions.js} +1 -1
  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/{services/subsites2.js → endpoints/subsites.js} +1 -1
  20. package/src/endpoints/tags.js +13 -0
  21. package/src/endpoints/trees-orders.js +6 -0
  22. package/src/endpoints/user.js +15 -0
  23. package/src/helpers/api.js +171 -79
  24. package/src/helpers/relations.js +32 -0
  25. package/src/helpers/session.js +0 -4
  26. package/src/utils/args.js +17 -22
  27. package/src/helpers/api-v2.js +0 -159
  28. package/src/services/countries.js +0 -35
  29. package/src/services/field-udpate-verifications.js +0 -34
  30. package/src/services/field-updates.js +0 -77
  31. package/src/services/forest-type-species.js +0 -63
  32. package/src/services/forest-type-species2.js +0 -5
  33. package/src/services/forest-types.js +0 -62
  34. package/src/services/forest-types2.js +0 -5
  35. package/src/services/form-submissions.js +0 -33
  36. package/src/services/forms.js +0 -33
  37. package/src/services/images.js +0 -39
  38. package/src/services/me.js +0 -20
  39. package/src/services/orgs.js +0 -31
  40. package/src/services/regions.js +0 -41
  41. package/src/services/sponsors.js +0 -31
  42. package/src/services/subdomains.js +0 -26
  43. package/src/services/subsite-types.js +0 -70
  44. package/src/services/subsites.js +0 -70
  45. package/src/services/trees-orders.js +0 -42
  46. package/src/utils/methods.js +0 -16
package/index.js CHANGED
@@ -1,45 +1,43 @@
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/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 { Tags } from './src/endpoints/tags';
19
+ import { TreeOrders } from './src/endpoints/trees-orders';
20
+ import { User } from './src/endpoints/user';
22
21
 
23
22
  export {
24
- createCountriesApiService,
25
23
  Countries,
26
- createFieldUpdatesApiService,
27
- createFieldUpdateVerificationsApiService,
28
- createForestTypeSpeciesApiService,
24
+ FieldUpdates,
25
+ FieldUpdateVerifications,
29
26
  ForestTypeSpecies,
30
- createForestTypesApiService,
31
27
  ForestTypes,
32
- createFormSubmissionsApiService,
33
- createOrgsApiService,
34
- createImagesApiService,
28
+ ForestTypeProfiles,
29
+ FormSubmissions,
35
30
  Images,
36
- createMeApiService,
37
- createRegionsApiService,
31
+ Orgs,
32
+ Notes,
38
33
  Regions,
39
- createSponsorsApiService,
40
- createSubdomainsApiService,
41
- createSubsitesApiService,
34
+ SDGs,
35
+ Sponsors,
36
+ Stats,
37
+ Subdomains,
42
38
  Subsites,
43
- createSubsiteTypesApiService,
44
- createTreeOrdersApiService,
39
+ SubsiteTypes,
40
+ Tags,
41
+ TreeOrders,
42
+ User
45
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "1.0.0-0",
3
+ "version": "1.0.0-11",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "countries";
4
4
 
@@ -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);
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "images";
4
4
 
@@ -0,0 +1,13 @@
1
+ import Api from "../helpers/api";
2
+ import Relations from "../helpers/relations";
3
+
4
+ class NotesApi extends Api {
5
+ constructor(resource) {
6
+ super(resource);
7
+ this.resource = "notes";
8
+ }
9
+
10
+ relation = (prefix, id, args) => new Relations(this, prefix, id, args);
11
+ }
12
+
13
+ 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();
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "regions";
4
4
 
@@ -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);
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "subsites";
4
4
 
@@ -0,0 +1,13 @@
1
+ import Api from "../helpers/api";
2
+ import Relations from "../helpers/relations";
3
+
4
+ class TagsApi extends Api {
5
+ constructor(resource) {
6
+ super(resource);
7
+ this.resource = "tags";
8
+ }
9
+
10
+ relation = (prefix, id, args) => new Relations(this, prefix, id, args);
11
+ }
12
+
13
+ export const Tags = new TagsApi();
@@ -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
+ }
@@ -0,0 +1,32 @@
1
+ const Relations = (api, prefix, id, args) => {
2
+ let url = `${api.baseUrl}/${prefix}/${id}/${api.resource}`;
3
+
4
+ const all = async () => {
5
+ url = `${url}${api.getUrlParams(args)}`;
6
+ return await api.unWrap(url);
7
+ }
8
+
9
+ const single = async(tagId) => {
10
+ url = `${url}/${tagId}${api.getUrlParams(args)}`;
11
+ return await api.unWrap(url);
12
+ }
13
+
14
+ const attach = async(tagId, data) => {
15
+ url = `${url}/${tagId}/attach${api.getUrlParams(args)}`;
16
+ return await api.post(url, data);
17
+ }
18
+
19
+ const detach = async(tagId, data) => {
20
+ url = `${url}/${tagId}/detach${api.getUrlParams(args)}`;
21
+ return await api.delete(url, data);
22
+ }
23
+
24
+ return {
25
+ all,
26
+ single,
27
+ attach,
28
+ detach
29
+ }
30
+ }
31
+
32
+ export default Relations;
@@ -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
  };