@veritree/services 0.17.1 → 1.0.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
@@ -1,31 +1,45 @@
1
1
  import { createCountriesApiService } from './src/services/countries.js';
2
+ import { Countries } from './src/services/countries2.js';
2
3
  import { createFieldUpdatesApiService } from './src/services/field-updates.js';
3
4
  import { createFieldUpdateVerificationsApiService } from './src/services/field-udpate-verifications.js';
4
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';
5
8
  import { createForestTypesApiService } from './src/services/forest-types.js';
9
+ import { ForestTypes } from './src/services/forest-types2.js';
6
10
  import { createImagesApiService } from './src/services/images.js';
11
+ import { Images } from './src/services/images2.js';
7
12
  import { createMeApiService } from './src/services/me.js';
8
13
  import { createOrgsApiService } from './src/services/orgs.js';
9
14
  import { createRegionsApiService } from './src/services/regions.js';
15
+ import { Regions } from './src/services/regions2.js';
10
16
  import { createSponsorsApiService } from './src/services/sponsors.js';
11
17
  import { createSubdomainsApiService } from './src/services/subdomains.js';
12
18
  import { createSubsitesApiService } from './src/services/subsites.js';
19
+ import { Subsites } from './src/services/subsites2.js';
13
20
  import { createSubsiteTypesApiService } from './src/services/subsite-types.js';
14
21
  import { createTreeOrdersApiService } from './src/services/trees-orders.js';
15
22
 
16
23
  export {
17
24
  createCountriesApiService,
25
+ Countries,
18
26
  createFieldUpdatesApiService,
19
27
  createFieldUpdateVerificationsApiService,
28
+ createForestTypeSpeciesApiService,
29
+ ForestTypeSpecies,
20
30
  createForestTypesApiService,
31
+ ForestTypes,
21
32
  createFormSubmissionsApiService,
22
33
  createOrgsApiService,
23
34
  createImagesApiService,
35
+ Images,
24
36
  createMeApiService,
25
37
  createRegionsApiService,
38
+ Regions,
26
39
  createSponsorsApiService,
27
40
  createSubdomainsApiService,
28
41
  createSubsitesApiService,
42
+ Subsites,
29
43
  createSubsiteTypesApiService,
30
44
  createTreeOrdersApiService,
31
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.17.1",
3
+ "version": "1.0.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",
@@ -0,0 +1,159 @@
1
+ import { getCookie } from "./cookies";
2
+ import { getSession } from "./session";
3
+ import { createParamsStringFromArgs } from "../utils/args";
4
+
5
+ export default function Api(resource) {
6
+ const baseUrl = `${process.env.API_VERITREE_URL}/api`;
7
+ const { orgId, orgType } = getSession();
8
+
9
+ if (!orgId && !orgType) {
10
+ throw new Error("Organization id and type are required");
11
+ }
12
+
13
+ /**
14
+ *
15
+ * @returns {promise}
16
+ */
17
+ this.all = async function () {
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
+ this.single = async function (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
+ this.create = async function (data) {
38
+ const url = `${this.getUrl()}?${this.getUrlParams()}`;
39
+ return await this.post(url, data);
40
+ };
41
+
42
+ /**
43
+ *
44
+ * @param {string} url
45
+ * @param {object} data
46
+ * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
47
+ * @returns {promise}
48
+ */
49
+ this.update = async function (id, data, as = 'put') {
50
+ const url = `${this.getUrl()}/${id}?${this.getUrlParams()}`;
51
+ return await this.post(url, data, as);
52
+ };
53
+
54
+ // ----------
55
+ // --
56
+ this.getUrl = () => {
57
+ return `${baseUrl}/${resource}`;
58
+ }
59
+
60
+ this.getUrlParams = (args) => {
61
+ return `org_id=${orgId}&org_type=${orgType}${createParamsStringFromArgs(args)}`;
62
+ }
63
+
64
+ /**
65
+ *
66
+ * @param {string} url
67
+ * @returns {promise}
68
+ */
69
+ this.get = async function (url) {
70
+ return await this.unWrap(url);
71
+ };
72
+
73
+ /**
74
+ *
75
+ * @param {string} url
76
+ * @param {object} data
77
+ * @returns {promise}
78
+ */
79
+ this.post = async function (url, data, as) {
80
+ return await this.unWrap(url, "post", data, as);
81
+ };
82
+
83
+ /**
84
+ * Deals with all fetch requests
85
+ *
86
+ * @param {string} url
87
+ * @param {string} method
88
+ * @param {object} data
89
+ * @returns {object} envelope
90
+ */
91
+ this.unWrap = async function (url, method = "get", data, as) {
92
+ url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
93
+ const config = this.getConfig(method, data, as);
94
+
95
+ try {
96
+ const response = await fetch(url, config);
97
+ const envelope = await response.json();
98
+
99
+ return envelope;
100
+ } catch (err) {
101
+ throw new Error(err);
102
+ }
103
+ };
104
+
105
+ /**
106
+ * Handles how the data should be sent in the fetch method
107
+ *
108
+ * @param {string} method
109
+ * @param {object} body
110
+ * @returns {object} data
111
+ */
112
+ this.getConfig = function (method, data, as) {
113
+ const isGet = method === "get";
114
+ const isPut = as === "put";
115
+ const isFormData = this.isFormData(data);
116
+ const accessToken = `Bearer ${getCookie("access_token")}`;
117
+
118
+ const config = {
119
+ method,
120
+ headers: {
121
+ Authorization: accessToken,
122
+ },
123
+ };
124
+
125
+ if (!isFormData) {
126
+ config.headers["Content-Type"] = "application/json";
127
+ }
128
+
129
+ // TODO: improve this ifs and elses
130
+ if (!isGet) {
131
+ if (isFormData) {
132
+ if (isPut) data.set("_method", "PUT");
133
+ config.body = data;
134
+ } else {
135
+ if (isPut) data._method = "PUT";
136
+ config.body = JSON.stringify(data);
137
+ }
138
+ }
139
+
140
+ return config;
141
+ };
142
+
143
+ /**
144
+ * Adds the envelope argument to the url
145
+ *
146
+ * @param {string} url
147
+ * @returns {string} url
148
+ */
149
+ this.handleEnvelopParam = function (url) {
150
+ if (!url || url.includes("_result=1")) return url;
151
+
152
+ const urlHasArgs = url.includes("?");
153
+ const urlEvenlopeArg = urlHasArgs ? "&_result=1" : "?_result=1";
154
+
155
+ return `${url}${urlEvenlopeArg}`;
156
+ };
157
+
158
+ this.isFormData = (data) => data instanceof FormData;
159
+ }
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api-v2";
2
+
3
+ const resource = "countries";
4
+
5
+ export const Countries = new Api(resource);
@@ -0,0 +1,63 @@
1
+ import Api from "../helpers/api";
2
+ import { createParamsStringFromArgs } from "../utils/args";
3
+ import { getSession } from "../helpers/session";
4
+ // import { all, getUrl, getUrlParams } from "../utils/methods";
5
+
6
+ export const createForestTypeSpeciesApiService = () => {
7
+ const resource = "forest-type-species";
8
+ const { orgId, orgType } = getSession();
9
+
10
+ if (!orgId && !orgType) {
11
+ throw new Error("Organization id and type are required");
12
+ }
13
+
14
+ const get = {
15
+ /**
16
+ * @param {object} arguments
17
+ * @returns {object} envelope
18
+ */
19
+ async all() {
20
+ const url = `${_getUrl()}?${_getUrlParams(arguments)}`;
21
+
22
+ return await Api.get(url);
23
+ },
24
+
25
+ /**
26
+ * @param {number/string} id
27
+ * @returns {object} envelope
28
+ */
29
+ async specific(id) {
30
+ const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
31
+
32
+ return await Api.get(url);
33
+ },
34
+ };
35
+
36
+ const post = (data) => {
37
+ const url = `${_getUrl()}?${_getUrlParams()}`;
38
+
39
+ return Api.post(url, data);
40
+ };
41
+
42
+ const update = (id, data) => {
43
+ const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
44
+
45
+ return Api.update(url, data);
46
+ };
47
+
48
+ const _getUrl = () => {
49
+ return `${Api.baseUrl}/${resource}`;
50
+ };
51
+
52
+ const _getUrlParams = (args) => {
53
+ const paramsString = createParamsStringFromArgs(args);
54
+
55
+ return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
56
+ };
57
+
58
+ return {
59
+ get,
60
+ post,
61
+ update,
62
+ };
63
+ };
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api-v2";
2
+
3
+ const resource = "forest-type-species";
4
+
5
+ export const ForestTypeSpecies = new Api(resource);
@@ -1,13 +1,13 @@
1
- import Api from '../helpers/api';
2
- import { createParamsStringFromArgs } from '../utils/args';
1
+ import Api from "../helpers/api";
2
+ import { createParamsStringFromArgs } from "../utils/args";
3
3
  import { getSession } from "../helpers/session";
4
4
 
5
5
  export const createForestTypesApiService = () => {
6
- const resource = 'forest-types';
6
+ const resource = "forest-types";
7
7
  const { orgId, orgType } = getSession();
8
8
 
9
9
  if (!orgId && !orgType) {
10
- throw new Error('Organization id and type are required');
10
+ throw new Error("Organization id and type are required");
11
11
  }
12
12
 
13
13
  const get = {
@@ -15,8 +15,8 @@ export const createForestTypesApiService = () => {
15
15
  * @param {object} arguments
16
16
  * @returns {object} envelope
17
17
  */
18
- async all() {
19
- const url = `${_getUrl()}?${_getParams(arguments)}`;
18
+ async all() {
19
+ const url = `${_getUrl()}?${_getUrlParams(arguments)}`;
20
20
 
21
21
  return await Api.get(url);
22
22
  },
@@ -26,17 +26,29 @@ export const createForestTypesApiService = () => {
26
26
  * @returns {object} envelope
27
27
  */
28
28
  async specific(id) {
29
- const url = `${_getUrl()}/${id}?${_getParams()}`;
29
+ const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
30
30
 
31
31
  return await Api.get(url);
32
32
  },
33
33
  };
34
34
 
35
+ const post = (data) => {
36
+ const url = `${_getUrl()}?${_getUrlParams()}`;
37
+
38
+ return Api.post(url, data);
39
+ };
40
+
41
+ const update = (id, data) => {
42
+ const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
43
+
44
+ return Api.update(url, data);
45
+ };
46
+
35
47
  const _getUrl = () => {
36
48
  return `${Api.baseUrl}/${resource}`;
37
49
  };
38
50
 
39
- const _getParams = (args) => {
51
+ const _getUrlParams = (args) => {
40
52
  const paramsString = createParamsStringFromArgs(args);
41
53
 
42
54
  return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
@@ -44,5 +56,7 @@ export const createForestTypesApiService = () => {
44
56
 
45
57
  return {
46
58
  get,
59
+ post,
60
+ update,
47
61
  };
48
62
  };
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api-v2";
2
+
3
+ const resource = "forest-types";
4
+
5
+ export const ForestTypes = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api-v2";
2
+
3
+ const resource = "images";
4
+
5
+ export const Images = new Api(resource);
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api-v2";
2
+
3
+ const resource = "regions";
4
+
5
+ export const Regions = new Api(resource);
@@ -37,20 +37,20 @@ export const createSubsitesApiService = () => {
37
37
  const url = `${_getUrl()}/${id}/stats`;
38
38
 
39
39
  return await Api.get(url);
40
- }
40
+ },
41
41
  };
42
42
 
43
43
  const post = (data) => {
44
44
  const url = `${_getUrl()}?${_getUrlParams()}`;
45
45
 
46
46
  return Api.post(url, data);
47
- }
47
+ };
48
48
 
49
49
  const update = (id, data) => {
50
50
  const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
51
51
 
52
52
  return Api.update(url, data);
53
- }
53
+ };
54
54
 
55
55
  const _getUrl = () => {
56
56
  return `${Api.baseUrl}/${resource}`;
@@ -65,6 +65,6 @@ export const createSubsitesApiService = () => {
65
65
  return {
66
66
  get,
67
67
  post,
68
- update
68
+ update,
69
69
  };
70
70
  };
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api-v2";
2
+
3
+ const resource = "subsites";
4
+
5
+ export const Subsites = new Api(resource);
@@ -0,0 +1,16 @@
1
+ import Api from "../helpers/api";
2
+ import { createParamsStringFromArgs } from "../utils/args";
3
+
4
+ export const all = async (url) => {
5
+ return await Api.get(url);
6
+ };
7
+
8
+ export const getUrl = (resource) => {
9
+ return `${Api.baseUrl}/${resource}`;
10
+ };
11
+
12
+ export const getUrlParams = (args) => {
13
+ const paramsString = createParamsStringFromArgs(args);
14
+
15
+ return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
16
+ };
package/package-lock.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "name": "@veritree/services",
3
- "version": "0.3.0",
4
- "lockfileVersion": 2,
5
- "requires": true,
6
- "packages": {
7
- "": {
8
- "name": "@veritree/services",
9
- "version": "0.3.0",
10
- "license": "MIT"
11
- }
12
- }
13
- }