@veritree/services 0.16.0 → 1.0.0-1

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