@veritree/services 1.0.0-1 → 1.0.0-4

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