@veritree/services 1.0.0-8 → 1.0.0-9

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';
@@ -16,6 +16,7 @@ import { Subdomains } from './src/endpoints/subdomains';
16
16
  import { Subsites } from './src/endpoints/subsites';
17
17
  import { SubsiteTypes } from './src/endpoints/subsite-types';
18
18
  import { TreeOrders } from './src/endpoints/trees-orders';
19
+ import { User } from './src/endpoints/user';
19
20
 
20
21
  export {
21
22
  Countries,
@@ -25,9 +26,9 @@ export {
25
26
  ForestTypes,
26
27
  ForestTypeProfiles,
27
28
  FormSubmissions,
28
- Orgs,
29
29
  Images,
30
- User,
30
+ Orgs,
31
+ Notes,
31
32
  Regions,
32
33
  SDGs,
33
34
  Sponsors,
@@ -35,5 +36,6 @@ export {
35
36
  Subdomains,
36
37
  Subsites,
37
38
  SubsiteTypes,
38
- TreeOrders
39
+ TreeOrders,
40
+ User
39
41
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "1.0.0-8",
3
+ "version": "1.0.0-9",
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,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();
@@ -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
@@ -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() {