@veritree/services 1.0.0-3 → 1.0.0-6

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
@@ -1,20 +1,20 @@
1
- import { Countries } from './src/services/countries';
2
- import { FieldUpdates } from './src/services/field-updates';
3
- import { FieldUpdateVerifications } from './src/services/field-udpate-verifications';
4
- import { FormSubmissions } from './src/services/form-submissions';
5
- import { ForestTypeSpecies } from './src/services/forest-type-species';
6
- import { ForestTypes } from './src/services/forest-types';
7
- import { ForestTypeProfiles } from './src/services/forest-types-profiles';
8
- import { Images } from './src/services/images';
9
- import { User } from './src/services/user';
10
- import { Orgs } from './src/services/orgs';
11
- import { Regions } from './src/services/regions';
12
- import { Sponsors } from './src/services/sponsors';
13
- import { Stats } from './src/services/stats';
14
- import { Subdomains } from './src/services/subdomains';
15
- import { Subsites } from './src/services/subsites';
16
- import { SubsiteTypes } from './src/services/subsite-types';
17
- import { TreeOrders } from './src/services/trees-orders';
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 { User } from './src/endpoints/user';
10
+ import { Orgs } from './src/endpoints/orgs';
11
+ import { Regions } from './src/endpoints/regions';
12
+ import { Sponsors } from './src/endpoints/sponsors';
13
+ import { Stats } from './src/endpoints/stats';
14
+ import { Subdomains } from './src/endpoints/subdomains';
15
+ import { Subsites } from './src/endpoints/subsites';
16
+ import { SubsiteTypes } from './src/endpoints/subsite-types';
17
+ import { TreeOrders } from './src/endpoints/trees-orders';
18
18
 
19
19
  export {
20
20
  Countries,
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-6",
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
 
@@ -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
 
@@ -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
 
@@ -1,9 +1,9 @@
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) {
5
5
  super(resource);
6
- this.resource = 'orgs';
6
+ this.resource = "orgs";
7
7
  }
8
8
 
9
9
  async stats() {
@@ -16,8 +16,8 @@ class OrgsApi extends Api {
16
16
  return await this.get(url);
17
17
  }
18
18
 
19
- _geStatstUrl = (isPublic) => {
20
- const endpoint = isPublic ? 'pstats' : 'stats';
19
+ _geStatstUrl(isPublic) {
20
+ const endpoint = isPublic ? "pstats" : "stats";
21
21
  return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
22
22
  }
23
23
  }
@@ -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) {
@@ -0,0 +1,5 @@
1
+ import Api from '../helpers/api';
2
+
3
+ const resource = 'subdomains';
4
+
5
+ export const Subdomains = 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 = "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,5 +1,6 @@
1
- import Api from '../helpers/api-v2';
1
+ import Api from '../helpers/api';
2
2
 
3
3
  const resource = 'tree-orders';
4
+
4
5
  export const TreeOrders = new Api(resource);
5
6
 
@@ -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,38 +1,114 @@
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 handleEnvelopParam(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
+ export default class Api {
21
+ constructor(resource) {
22
+ this.baseUrl = `${process.env.API_VERITREE_URL}/api`;
23
+ this.resource = resource;
24
+ this.orgId = null;
25
+ this.orgType = null;
26
+ }
2
27
 
3
- const Api = {
4
- baseUrl: `${process.env.API_VERITREE_URL}/api`,
5
- forceEnvelopeResponse: true, // TODO: remove when API is fully migrated to envelopes
28
+ /**
29
+ *
30
+ * @returns {promise}
31
+ */
32
+ async all() {
33
+ const url = `${this.getUrl()}${this.getUrlParams(arguments)}`;
34
+ return await this.get(url);
35
+ }
6
36
 
7
37
  /**
8
- *
9
- * @param {string} url
38
+ *
39
+ * @param {string, number} id
10
40
  * @returns {promise}
11
41
  */
12
- async get(url) {
13
- return await this.unWrap(url);
14
- },
42
+ async single(id) {
43
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
44
+ return await this.get(url);
45
+ }
15
46
 
16
47
  /**
17
- *
18
- * @param {string} url
19
- * @param {object} data
48
+ *
49
+ * @param {object} data
20
50
  * @returns {promise}
21
51
  */
22
- async post(url, data, as) {
23
- return await this.unWrap(url, "post", data, as);
24
- },
52
+ async create(data) {
53
+ return await this.post(null, data);
54
+ }
25
55
 
26
56
  /**
27
- *
28
- * @param {string} url
29
- * @param {object} data
57
+ *
58
+ * @param {string} url
59
+ * @param {object} data
30
60
  * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
31
61
  * @returns {promise}
32
62
  */
33
- async update(url, data, as = "put") {
63
+ async update(id, data, as = "put") {
64
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
34
65
  return await this.post(url, data, as);
35
- },
66
+ }
67
+
68
+ /**
69
+ *
70
+ * @param {string} url
71
+ * @returns {promise}
72
+ */
73
+ async get(url) {
74
+ return await this.unWrap(url);
75
+ }
76
+
77
+ /**
78
+ *
79
+ * @param {string} url
80
+ * @param {object} data
81
+ * @returns {promise}
82
+ */
83
+ async post(url, data, as) {
84
+ if (!url) url = `${this.getUrl()}${this.getUrlParams()}`;
85
+ return await this.unWrap(url, "post", data, as);
86
+ }
87
+
88
+ // ----------
89
+ // --
90
+ getUrl() {
91
+ return `${this.baseUrl}/${this.resource}`;
92
+ }
93
+
94
+ getUrlParams(args) {
95
+ this.setOrg();
96
+ let isOrgLess = false;
97
+ let orgIdParam = "";
98
+ let orgTypeParam = "";
99
+
100
+ // while most of endpoints require an org id and type, some endpoints do not
101
+ if (args && args.length) {
102
+ isOrgLess = Object.hasOwnProperty.call(...args, "orgless");
103
+ }
104
+
105
+ if (!isOrgLess) {
106
+ if (this.orgId) orgIdParam = `&org_id=${this.orgId}`;
107
+ if (this.orgType) orgTypeParam = `&org_type=${this.orgType}`;
108
+ }
109
+
110
+ return `?${orgIdParam}&${orgTypeParam}${createParamsStringFromArgs(args)}`;
111
+ }
36
112
 
37
113
  /**
38
114
  * Deals with all fetch requests
@@ -43,7 +119,7 @@ const Api = {
43
119
  * @returns {object} envelope
44
120
  */
45
121
  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
122
+ url = handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
47
123
  const config = this.getConfig(method, data, as);
48
124
 
49
125
  try {
@@ -54,7 +130,7 @@ const Api = {
54
130
  } catch (err) {
55
131
  throw new Error(err);
56
132
  }
57
- },
133
+ }
58
134
 
59
135
  /**
60
136
  * Handles how the data should be sent in the fetch method
@@ -66,8 +142,8 @@ const Api = {
66
142
  getConfig(method, data, as) {
67
143
  const isGet = method === "get";
68
144
  const isPut = as === "put";
69
- const isFormData = this._isFormData(data);
70
- const accessToken = `Bearer ${getCookie('access_token')}`;
145
+ const isFormData = data instanceof FormData;
146
+ const accessToken = `Bearer ${getCookie("access_token")}`;
71
147
 
72
148
  const config = {
73
149
  method,
@@ -76,42 +152,29 @@ const Api = {
76
152
  },
77
153
  };
78
154
 
79
- if(!isFormData) {
80
- config.headers['Content-Type'] = 'application/json';
155
+ if (!isFormData) {
156
+ config.headers["Content-Type"] = "application/json";
81
157
  }
82
158
 
83
159
  // TODO: improve this ifs and elses
84
160
  if (!isGet) {
85
- if(isFormData) {
86
- if(isPut) data.set('_method', 'PUT');
161
+ if (isFormData) {
162
+ if (isPut) data.set("_method", "PUT");
87
163
  config.body = data;
88
164
  } else {
89
- if(isPut) data._method = 'PUT';
165
+ if (isPut) data._method = "PUT";
90
166
  config.body = JSON.stringify(data);
91
167
  }
92
168
  }
93
169
 
94
170
  return config;
95
- },
96
-
97
- /**
98
- * Adds the envelope argument to the url
99
- *
100
- * @param {string} url
101
- * @returns {string} url
102
- */
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
- };
116
-
117
- export default Api;
171
+ }
172
+
173
+ setOrg() {
174
+ const session = getSession();
175
+ if (!session) return;
176
+ const { orgId, orgType } = session;
177
+ this.orgId = orgId;
178
+ this.orgType = orgType;
179
+ }
180
+ }
@@ -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
  };
@@ -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
- }
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "forest-type-species";
4
-
5
- export const ForestTypeSpecies = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from '../helpers/api-v2';
2
-
3
- const resource = 'forest-type-profiles';
4
-
5
- export const ForestTypeProfiles = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "forest-types";
4
-
5
- export const ForestTypes = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "form-submissions";
4
-
5
- export const FormSubmissions = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from '../helpers/api-v2';
2
-
3
- const resource = 'subdomains';
4
-
5
- export const Subdomains = new Api(resource);