@veritree/services 0.15.0 → 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,29 +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';
20
+ import { createSubsiteTypesApiService } from './src/services/subsite-types.js';
13
21
  import { createTreeOrdersApiService } from './src/services/trees-orders.js';
14
22
 
15
23
  export {
16
24
  createCountriesApiService,
25
+ Countries,
17
26
  createFieldUpdatesApiService,
18
27
  createFieldUpdateVerificationsApiService,
28
+ createForestTypeSpeciesApiService,
29
+ ForestTypeSpecies,
19
30
  createForestTypesApiService,
31
+ ForestTypes,
20
32
  createFormSubmissionsApiService,
21
33
  createOrgsApiService,
22
34
  createImagesApiService,
35
+ Images,
23
36
  createMeApiService,
24
37
  createRegionsApiService,
38
+ Regions,
25
39
  createSponsorsApiService,
26
40
  createSubdomainsApiService,
27
41
  createSubsitesApiService,
42
+ Subsites,
43
+ createSubsiteTypesApiService,
28
44
  createTreeOrdersApiService,
29
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "0.15.0",
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
+ }
@@ -4,16 +4,34 @@ const Api = {
4
4
  baseUrl: `${process.env.API_VERITREE_URL}/api`,
5
5
  forceEnvelopeResponse: true, // TODO: remove when API is fully migrated to envelopes
6
6
 
7
+ /**
8
+ *
9
+ * @param {string} url
10
+ * @returns {promise}
11
+ */
7
12
  async get(url) {
8
13
  return await this.unWrap(url);
9
14
  },
10
15
 
11
- async post(url, data) {
12
- return await this.unWrap(url, "post", data);
16
+ /**
17
+ *
18
+ * @param {string} url
19
+ * @param {object} data
20
+ * @returns {promise}
21
+ */
22
+ async post(url, data, as) {
23
+ return await this.unWrap(url, "post", data, as);
13
24
  },
14
25
 
15
- async update(url, data) {
16
- return await this.unWrap(url, "put", data);
26
+ /**
27
+ *
28
+ * @param {string} url
29
+ * @param {object} data
30
+ * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
31
+ * @returns {promise}
32
+ */
33
+ async update(url, data, as = "put") {
34
+ return await this.post(url, data, as);
17
35
  },
18
36
 
19
37
  /**
@@ -24,9 +42,9 @@ const Api = {
24
42
  * @param {object} data
25
43
  * @returns {object} envelope
26
44
  */
27
- async unWrap(url, method = "get", data) {
45
+ async unWrap(url, method = "get", data, as) {
28
46
  if (this.forceEnvelopeResponse) url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
29
- const config = this.getConfig(method, data);
47
+ const config = this.getConfig(method, data, as);
30
48
 
31
49
  try {
32
50
  const response = await fetch(url, config);
@@ -45,8 +63,9 @@ const Api = {
45
63
  * @param {object} body
46
64
  * @returns {object} data
47
65
  */
48
- getConfig(method, data) {
66
+ getConfig(method, data, as) {
49
67
  const isGet = method === "get";
68
+ const isPut = as === "put";
50
69
  const isFormData = this._isFormData(data);
51
70
  const accessToken = `Bearer ${getCookie('access_token')}`;
52
71
 
@@ -61,8 +80,15 @@ const Api = {
61
80
  config.headers['Content-Type'] = 'application/json';
62
81
  }
63
82
 
83
+ // TODO: improve this ifs and elses
64
84
  if (!isGet) {
65
- isFormData ? (config.body = data) : (config.body = JSON.stringify(data));
85
+ if(isFormData) {
86
+ if(isPut) data.set('_method', 'PUT');
87
+ config.body = data;
88
+ } else {
89
+ if(isPut) data._method = 'PUT';
90
+ config.body = JSON.stringify(data);
91
+ }
66
92
  }
67
93
 
68
94
  return config;
@@ -0,0 +1,5 @@
1
+ import Api from "../helpers/api-v2";
2
+
3
+ const resource = "countries";
4
+
5
+ export const Countries = new Api(resource);
@@ -24,13 +24,8 @@ export const createFieldUpdateVerificationsApiService = () => {
24
24
  return `${Api.baseUrl}/${resource}`;
25
25
  };
26
26
 
27
- const _getParams = (page, orderBy, pageSize) => {
28
- const pageParam = page ? `&page=${page}` : "";
29
- const orderByParam = orderBy ? `&orderBy=${orderBy}` : "";
30
- const pageSizeParam = pageSize ? `&page_size=${pageSize}` : "";
31
- const params = `${pageParam}${pageSizeParam}${orderByParam}`;
32
-
33
- return `org_id=${orgId}&org_type=${orgType}${params}`;
27
+ const _getParams = () => {
28
+ return `org_id=${orgId}&org_type=${orgType}`;
34
29
  };
35
30
 
36
31
  return {
@@ -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);
@@ -0,0 +1,70 @@
1
+ import Api from "../helpers/api";
2
+ import { getSession } from "../helpers/session";
3
+ import { createParamsStringFromArgs } from "../utils/args";
4
+
5
+ export const createSubsiteTypesApiService = () => {
6
+ const resource = "subsite-types";
7
+ const { orgId, orgType } = getSession();
8
+
9
+ if (!orgId && !orgType) {
10
+ throw new Error("Organization id and type are required");
11
+ }
12
+
13
+ const get = {
14
+ /**
15
+ * @returns {obj} envelope
16
+ */
17
+ async all() {
18
+ const url = `${_getUrl()}?${_getUrlParams(arguments)}`;
19
+
20
+ return await Api.get(url);
21
+ },
22
+
23
+ /**
24
+ * Gets a speficific subsite of an organization
25
+ *
26
+ * @param {number/string} id
27
+ * @param {number/string} page
28
+ * @returns {object} envelope
29
+ */
30
+ async specific(id) {
31
+ const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
32
+
33
+ return await Api.get(url);
34
+ },
35
+
36
+ async stats(id) {
37
+ const url = `${_getUrl()}/${id}/stats`;
38
+
39
+ return await Api.get(url);
40
+ }
41
+ };
42
+
43
+ const post = (data) => {
44
+ const url = `${_getUrl()}?${_getUrlParams()}`;
45
+
46
+ return Api.post(url, data);
47
+ }
48
+
49
+ const update = (id, data) => {
50
+ const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
51
+
52
+ return Api.update(url, data);
53
+ }
54
+
55
+ const _getUrl = () => {
56
+ return `${Api.baseUrl}/${resource}`;
57
+ };
58
+
59
+ const _getUrlParams = (args) => {
60
+ const paramsString = createParamsStringFromArgs(args);
61
+
62
+ return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
63
+ };
64
+
65
+ return {
66
+ get,
67
+ post,
68
+ update
69
+ };
70
+ };
@@ -37,14 +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
+
49
+ const update = (id, data) => {
50
+ const url = `${_getUrl()}/${id}?${_getUrlParams()}`;
51
+
52
+ return Api.update(url, data);
53
+ };
48
54
 
49
55
  const _getUrl = () => {
50
56
  return `${Api.baseUrl}/${resource}`;
@@ -58,6 +64,7 @@ export const createSubsitesApiService = () => {
58
64
 
59
65
  return {
60
66
  get,
61
- post
67
+ post,
68
+ update,
62
69
  };
63
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
- }