@veritree/services 1.0.0-7 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -6,8 +6,8 @@ import { ForestTypeSpecies } from './src/endpoints/forest-type-species';
6
6
  import { ForestTypes } from './src/endpoints/forest-types';
7
7
  import { ForestTypeProfiles } from './src/endpoints/forest-types-profiles';
8
8
  import { Images } from './src/endpoints/images';
9
- import { User } from './src/endpoints/user';
10
9
  import { Orgs } from './src/endpoints/orgs';
10
+ import { Notes } from './src/endpoints/notes';
11
11
  import { Regions } from './src/endpoints/regions';
12
12
  import { SDGs } from './src/endpoints/sdgs';
13
13
  import { Sponsors } from './src/endpoints/sponsors';
@@ -15,7 +15,9 @@ import { Stats } from './src/endpoints/stats';
15
15
  import { Subdomains } from './src/endpoints/subdomains';
16
16
  import { Subsites } from './src/endpoints/subsites';
17
17
  import { SubsiteTypes } from './src/endpoints/subsite-types';
18
+ import { Tags } from './src/endpoints/tags';
18
19
  import { TreeOrders } from './src/endpoints/trees-orders';
20
+ import { User } from './src/endpoints/user';
19
21
 
20
22
  export {
21
23
  Countries,
@@ -25,9 +27,9 @@ export {
25
27
  ForestTypes,
26
28
  ForestTypeProfiles,
27
29
  FormSubmissions,
28
- Orgs,
29
30
  Images,
30
- User,
31
+ Orgs,
32
+ Notes,
31
33
  Regions,
32
34
  SDGs,
33
35
  Sponsors,
@@ -35,5 +37,7 @@ export {
35
37
  Subdomains,
36
38
  Subsites,
37
39
  SubsiteTypes,
38
- TreeOrders
40
+ Tags,
41
+ TreeOrders,
42
+ User
39
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "1.0.0-7",
3
+ "version": "1.0.0",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,5 +1,15 @@
1
1
  import Api from "../helpers/api";
2
+ import Sequestrations from '../helpers/sequestrations';
2
3
 
3
- const resource = "forest-types";
4
+ class ForestTypesApi extends Api {
5
+ constructor(resource) {
6
+ super(resource);
7
+ this.resource = "forest-types";
8
+ }
4
9
 
5
- export const ForestTypes = new Api(resource);
10
+ sequestrations(id, args) {
11
+ return Sequestrations(this, id, args);
12
+ }
13
+ }
14
+
15
+ export const ForestTypes = new ForestTypesApi();
@@ -0,0 +1,15 @@
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) {
11
+ return Relations(this, prefix, id, args);
12
+ }
13
+ }
14
+
15
+ export const Notes = new NotesApi();
@@ -23,6 +23,17 @@ class SponsorsApi extends Api {
23
23
  const url = `${this.getUrl()}/${this.orgId}/profile`;
24
24
  return await this.get(url);
25
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
+ async totalPlantingStats() {
33
+ const url = `${this.getUrl()}/total-planting-stats`;
34
+ return await this.get(url);
35
+ }
36
+
26
37
  }
27
38
 
28
39
  export const Sponsors = new SponsorsApi();
@@ -0,0 +1,15 @@
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) {
11
+ return Relations(this, prefix, id, args);
12
+ }
13
+ }
14
+
15
+ export const Tags = new TagsApi();
@@ -8,7 +8,7 @@ import { getSession } from "./session";
8
8
  * @param {string} url
9
9
  * @returns {string} url
10
10
  */
11
- function handleEnvelopParam(url) {
11
+ function addEnvelopeParam(url) {
12
12
  if (!url || url.includes("_result=1")) return url;
13
13
 
14
14
  const urlHasArgs = url.includes("?");
@@ -26,7 +26,7 @@ function handleEnvelopParam(url) {
26
26
  */
27
27
  function getConfig(method, data, as) {
28
28
  const isGet = method === "get";
29
- const isPut = as === "put";
29
+ const isSpoofing = as !== undefined;
30
30
  const isFormData = data instanceof FormData;
31
31
  const accessToken = `Bearer ${getCookie("access_token")}`;
32
32
 
@@ -43,11 +43,13 @@ function getConfig(method, data, as) {
43
43
 
44
44
  // TODO: improve this ifs and elses
45
45
  if (!isGet) {
46
+ if (!data) data = {};
47
+
46
48
  if (isFormData) {
47
- if (isPut) data.set("_method", "PUT");
49
+ if (isSpoofing) data.set("_method", as.toUpperCase());
48
50
  config.body = data;
49
51
  } else {
50
- if (isPut) data._method = "PUT";
52
+ if (isSpoofing) data._method = as.toUpperCase();
51
53
  config.body = JSON.stringify(data);
52
54
  }
53
55
  }
@@ -58,7 +60,7 @@ function getConfig(method, data, as) {
58
60
  export default class Api {
59
61
  constructor(resource) {
60
62
  this.baseUrl = `${process.env.API_VERITREE_URL}/api`;
61
- this.resource = resource;
63
+ this.resource = resource ? resource : "";
62
64
  this.orgId = null;
63
65
  this.orgType = null;
64
66
  }
@@ -104,6 +106,18 @@ export default class Api {
104
106
  return await this.post(url, data, as);
105
107
  }
106
108
 
109
+ /**
110
+ *
111
+ * @param {string} url
112
+ * @param {object} data
113
+ * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
114
+ * @returns {promise}
115
+ */
116
+ async delete(id) {
117
+ const url = `${this.getUrl(id)}${this.getUrlParams()}`;
118
+ return await this.post(url, null, "delete");
119
+ }
120
+
107
121
  /**
108
122
  *
109
123
  * @param {string} url
@@ -153,11 +167,11 @@ export default class Api {
153
167
  }
154
168
 
155
169
  if (!isOrgLess) {
156
- if (this.orgId) orgIdParam = `&org_id=${this.orgId}`;
170
+ if (this.orgId) orgIdParam = `org_id=${this.orgId}`;
157
171
  if (this.orgType) orgTypeParam = `&org_type=${this.orgType}`;
158
172
  }
159
173
 
160
- return `?${orgIdParam}&${orgTypeParam}${createParamsStringFromArgs(args)}`;
174
+ return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(args)}`;
161
175
  }
162
176
 
163
177
  /**
@@ -169,17 +183,20 @@ export default class Api {
169
183
  * @returns {object} envelope
170
184
  */
171
185
  async unWrap(url, method = "get", data, as) {
172
- url = handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
186
+ url = addEnvelopeParam(url);
173
187
  const config = getConfig(method, data, as);
174
-
175
- try {
176
- const response = await fetch(url, config);
177
- const envelope = await response.json();
178
-
179
- return envelope;
180
- } catch (err) {
181
- throw new Error(err);
182
- }
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
+ }
195
+
196
+ response.json().then((json) => {
197
+ response.ok ? resolve(json) : reject(json);
198
+ });
199
+ });
183
200
  }
184
201
 
185
202
  setOrg() {
@@ -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.get(url);
7
+ }
8
+
9
+ const single = async(tagId) => {
10
+ url = `${url}/${tagId}${api.getUrlParams(args)}`;
11
+ return await api.get(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;
@@ -0,0 +1,20 @@
1
+ const Sequestrations = (api, forestTypeId, args) => {
2
+ let url = `${api.baseUrl}/${api.resource}/${forestTypeId}/sequestrations`;
3
+
4
+ const all = async () => {
5
+ url = `${url}${api.getUrlParams(args)}`;
6
+ return await api.get(url);
7
+ }
8
+
9
+ const create = async(data) => {
10
+ url = `${url}${api.getUrlParams(args)}`;
11
+ return await api.post(url, data);
12
+ }
13
+
14
+ return {
15
+ all,
16
+ create
17
+ }
18
+ }
19
+
20
+ export default Sequestrations;
package/src/utils/args.js CHANGED
@@ -23,6 +23,5 @@ export const createParamsStringFromArgs = (args) => {
23
23
  }
24
24
  });
25
25
 
26
- if (!paramsString.length) return "";
27
- return `&${paramsString.join("&")}`;
26
+ return paramsString.length ? `&${paramsString.join("&")}` : "";
28
27
  };