@veritree/services 0.17.1 → 1.0.0-2

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