@veritree/services 1.0.0-3 → 1.0.0-4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "1.0.0-3",
3
+ "version": "1.0.0-4",
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,91 @@
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
+ get = async function (url) {
59
+ return await this.unWrap(url);
60
+ };
61
+
62
+ /**
63
+ *
64
+ * @param {string} url
65
+ * @param {object} data
66
+ * @returns {promise}
67
+ */
68
+ post = async function (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(
86
+ args
87
+ )}`;
88
+ };
36
89
 
37
90
  /**
38
91
  * Deals with all fetch requests
@@ -42,8 +95,8 @@ const Api = {
42
95
  * @param {object} data
43
96
  * @returns {object} envelope
44
97
  */
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
98
+ unWrap = async function (url, method = "get", data, as) {
99
+ url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
47
100
  const config = this.getConfig(method, data, as);
48
101
 
49
102
  try {
@@ -54,7 +107,7 @@ const Api = {
54
107
  } catch (err) {
55
108
  throw new Error(err);
56
109
  }
57
- },
110
+ };
58
111
 
59
112
  /**
60
113
  * Handles how the data should be sent in the fetch method
@@ -63,11 +116,11 @@ const Api = {
63
116
  * @param {object} body
64
117
  * @returns {object} data
65
118
  */
66
- getConfig(method, data, as) {
119
+ getConfig = function (method, data, as) {
67
120
  const isGet = method === "get";
68
121
  const isPut = as === "put";
69
- const isFormData = this._isFormData(data);
70
- const accessToken = `Bearer ${getCookie('access_token')}`;
122
+ const isFormData = this.isFormData(data);
123
+ const accessToken = `Bearer ${getCookie("access_token")}`;
71
124
 
72
125
  const config = {
73
126
  method,
@@ -76,23 +129,31 @@ const Api = {
76
129
  },
77
130
  };
78
131
 
79
- if(!isFormData) {
80
- config.headers['Content-Type'] = 'application/json';
132
+ if (!isFormData) {
133
+ config.headers["Content-Type"] = "application/json";
81
134
  }
82
135
 
83
136
  // TODO: improve this ifs and elses
84
137
  if (!isGet) {
85
- if(isFormData) {
86
- if(isPut) data.set('_method', 'PUT');
138
+ if (isFormData) {
139
+ if (isPut) data.set("_method", "PUT");
87
140
  config.body = data;
88
141
  } else {
89
- if(isPut) data._method = 'PUT';
142
+ if (isPut) data._method = "PUT";
90
143
  config.body = JSON.stringify(data);
91
144
  }
92
145
  }
93
146
 
94
147
  return config;
95
- },
148
+ };
149
+
150
+ setOrg = () => {
151
+ const session = getSession();
152
+ if(!session) return;
153
+ const { orgId, orgType } = session;
154
+ this.orgId = orgId;
155
+ this.orgType = orgType;
156
+ }
96
157
 
97
158
  /**
98
159
  * Adds the envelope argument to the url
@@ -100,18 +161,14 @@ const Api = {
100
161
  * @param {string} url
101
162
  * @returns {string} url
102
163
  */
103
- handleEnvelopParam(url) {
164
+ handleEnvelopParam = function (url) {
104
165
  if (!url || url.includes("_result=1")) return url;
105
166
 
106
167
  const urlHasArgs = url.includes("?");
107
168
  const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
108
169
 
109
170
  return `${url}${urlEvenlopeArg}`;
110
- },
111
-
112
- _isFormData(data) {
113
- return data instanceof FormData;
114
- },
115
- };
171
+ };
116
172
 
117
- export default Api;
173
+ isFormData = (data) => data instanceof FormData;
174
+ }
@@ -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
 
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "field-update-verifications";
4
4
 
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "field-updates";
4
4
 
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "forest-type-species";
4
4
 
@@ -1,4 +1,4 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  const resource = 'forest-type-profiles';
4
4
 
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "forest-types";
4
4
 
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "form-submissions";
4
4
 
@@ -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
 
@@ -1,4 +1,4 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  class OrgsApi extends Api {
4
4
  constructor(resource) {
@@ -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
 
@@ -1,4 +1,4 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  class SponsorsApi extends Api {
4
4
  constructor(resource) {
@@ -1,4 +1,4 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  class StatsApi extends Api {
4
4
  constructor(resource) {
@@ -1,4 +1,4 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  const resource = 'subdomains';
4
4
 
@@ -1,4 +1,4 @@
1
- import Api from "../helpers/api-v2";
1
+ import Api from "../helpers/api";
2
2
 
3
3
  const resource = "subsite-types";
4
4
 
@@ -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
 
@@ -1,4 +1,4 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  const resource = 'tree-orders';
4
4
  export const TreeOrders = new Api(resource);
@@ -1,4 +1,4 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  class UserApi extends Api {
4
4
  constructor(resource) {
@@ -1,174 +0,0 @@
1
- import { getCookie } from "./cookies";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
- import { getSession } from "./session";
4
-
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
- }
12
-
13
- /**
14
- *
15
- * @returns {promise}
16
- */
17
- async all() {
18
- const url = `${this.getUrl()}${this.getUrlParams(arguments)}`;
19
- return await this.get(url);
20
- }
21
-
22
- /**
23
- *
24
- * @param {string, number} id
25
- * @returns {promise}
26
- */
27
- async single(id) {
28
- const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
29
- return await this.get(url);
30
- }
31
-
32
- /**
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
45
- * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
46
- * @returns {promise}
47
- */
48
- async update(id, data, as = "put") {
49
- const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
50
- return await this.post(url, data, as);
51
- }
52
-
53
- /**
54
- *
55
- * @param {string} url
56
- * @returns {promise}
57
- */
58
- get = async function (url) {
59
- return await this.unWrap(url);
60
- };
61
-
62
- /**
63
- *
64
- * @param {string} url
65
- * @param {object} data
66
- * @returns {promise}
67
- */
68
- post = async function (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(
86
- args
87
- )}`;
88
- };
89
-
90
- /**
91
- * Deals with all fetch requests
92
- *
93
- * @param {string} url
94
- * @param {string} method
95
- * @param {object} data
96
- * @returns {object} envelope
97
- */
98
- unWrap = async function (url, method = "get", data, as) {
99
- url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
100
- const config = this.getConfig(method, data, as);
101
-
102
- try {
103
- const response = await fetch(url, config);
104
- const envelope = await response.json();
105
-
106
- return envelope;
107
- } catch (err) {
108
- throw new Error(err);
109
- }
110
- };
111
-
112
- /**
113
- * Handles how the data should be sent in the fetch method
114
- *
115
- * @param {string} method
116
- * @param {object} body
117
- * @returns {object} data
118
- */
119
- getConfig = function (method, data, as) {
120
- const isGet = method === "get";
121
- const isPut = as === "put";
122
- const isFormData = this.isFormData(data);
123
- const accessToken = `Bearer ${getCookie("access_token")}`;
124
-
125
- const config = {
126
- method,
127
- headers: {
128
- Authorization: accessToken,
129
- },
130
- };
131
-
132
- if (!isFormData) {
133
- config.headers["Content-Type"] = "application/json";
134
- }
135
-
136
- // TODO: improve this ifs and elses
137
- if (!isGet) {
138
- if (isFormData) {
139
- if (isPut) data.set("_method", "PUT");
140
- config.body = data;
141
- } else {
142
- if (isPut) data._method = "PUT";
143
- config.body = JSON.stringify(data);
144
- }
145
- }
146
-
147
- return config;
148
- };
149
-
150
- setOrg = () => {
151
- const session = getSession();
152
- if(!session) return;
153
- const { orgId, orgType } = session;
154
- this.orgId = orgId;
155
- this.orgType = orgType;
156
- }
157
-
158
- /**
159
- * Adds the envelope argument to the url
160
- *
161
- * @param {string} url
162
- * @returns {string} url
163
- */
164
- handleEnvelopParam = function (url) {
165
- if (!url || url.includes("_result=1")) return url;
166
-
167
- const urlHasArgs = url.includes("?");
168
- const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
169
-
170
- return `${url}${urlEvenlopeArg}`;
171
- };
172
-
173
- isFormData = (data) => data instanceof FormData;
174
- }