@veritree/services 1.0.0-1 → 1.0.0-10

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.
Files changed (44) hide show
  1. package/index.js +32 -36
  2. package/package.json +1 -1
  3. package/src/{services/countries2.js → endpoints/countries.js} +1 -1
  4. package/src/endpoints/field-udpate-verifications.js +5 -0
  5. package/src/endpoints/field-updates.js +5 -0
  6. package/src/endpoints/forest-type-species.js +5 -0
  7. package/src/endpoints/forest-types-profiles.js +5 -0
  8. package/src/endpoints/forest-types.js +5 -0
  9. package/src/endpoints/form-submissions.js +5 -0
  10. package/src/{services/images2.js → endpoints/images.js} +1 -1
  11. package/src/endpoints/notes.js +15 -0
  12. package/src/endpoints/orgs.js +25 -0
  13. package/src/{services/regions2.js → endpoints/regions.js} +1 -1
  14. package/src/endpoints/sdgs.js +5 -0
  15. package/src/endpoints/sponsors.js +34 -0
  16. package/src/endpoints/stats.js +17 -0
  17. package/src/endpoints/subdomains.js +5 -0
  18. package/src/endpoints/subsite-types.js +5 -0
  19. package/src/{services/subsites2.js → endpoints/subsites.js} +1 -1
  20. package/src/endpoints/trees-orders.js +6 -0
  21. package/src/endpoints/user.js +15 -0
  22. package/src/helpers/api.js +171 -79
  23. package/src/helpers/session.js +0 -4
  24. package/src/utils/args.js +17 -22
  25. package/src/helpers/api-v2.js +0 -162
  26. package/src/services/countries.js +0 -35
  27. package/src/services/field-udpate-verifications.js +0 -34
  28. package/src/services/field-updates.js +0 -77
  29. package/src/services/forest-type-species.js +0 -63
  30. package/src/services/forest-type-species2.js +0 -5
  31. package/src/services/forest-types.js +0 -62
  32. package/src/services/forest-types2.js +0 -5
  33. package/src/services/form-submissions.js +0 -33
  34. package/src/services/forms.js +0 -33
  35. package/src/services/images.js +0 -39
  36. package/src/services/me.js +0 -20
  37. package/src/services/orgs.js +0 -31
  38. package/src/services/regions.js +0 -41
  39. package/src/services/sponsors.js +0 -31
  40. package/src/services/subdomains.js +0 -26
  41. package/src/services/subsite-types.js +0 -70
  42. package/src/services/subsites.js +0 -70
  43. package/src/services/trees-orders.js +0 -42
  44. package/src/utils/methods.js +0 -16
@@ -1,162 +0,0 @@
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
- }
@@ -1,35 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- export const createCountriesApiService = () => {
4
- const resource = 'countries';
5
-
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
- };
@@ -1,34 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
-
4
- export const createFieldUpdateVerificationsApiService = () => {
5
- const resource = `field-update-verifications`;
6
- const { orgId, orgType } = getSession();
7
-
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
- };
@@ -1,77 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createFieldUpdatesApiService = () => {
6
- const resource = `field-updates`;
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
- 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
- };
@@ -1,63 +0,0 @@
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
- };
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "forest-type-species";
4
-
5
- export const ForestTypeSpecies = new Api(resource);
@@ -1,62 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
- import { getSession } from "../helpers/session";
4
-
5
- export const createForestTypesApiService = () => {
6
- const resource = "forest-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
- * @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
- };
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "forest-types";
4
-
5
- export const ForestTypes = new Api(resource);
@@ -1,33 +0,0 @@
1
- import Api from '../helpers/api';
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from '../utils/args';
4
-
5
- export const createFormSubmissionsApiService = () => {
6
- const resource = 'form-submissions';
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
- 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
- };
@@ -1,33 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- export const createFormApiService = (orgId, orgType) => {
4
- if (!orgId && !orgType) throw new Error('orgId and orgType are required');
5
- const resource = 'orgs';
6
-
7
- const get = {
8
- async all(page = 1, pageSize = 10) {
9
- const url = `${_getUrl()}/${orgId}/forms?${_getParams(page, pageSize)}`;
10
- return await Api.get(url);
11
- },
12
-
13
- async specific(formId) {
14
- const url = `${_getUrl()}/${orgId}/forms/${formId}?${_getParams()}`;
15
- return await Api.get(url);
16
- },
17
- };
18
-
19
- const _getUrl = () => {
20
- return `${Api.baseUrl}/${resource}`;
21
- };
22
-
23
- const _getParams = (page, pageSize) => {
24
- const paramPageSize = pageSize ? `&page_size=${pageSize}` : '';
25
- const paramPage = page ? `&page=${page}` : '';
26
-
27
- return `org_type=${orgType}${paramPageSize}${paramPage}`;
28
- };
29
-
30
- return {
31
- get,
32
- };
33
- };
@@ -1,39 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createImagesApiService = () => {
6
- const resource = "images";
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
- 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
- };
@@ -1,20 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- export const createMeApiService = () => {
4
- const resource = 'me';
5
-
6
- const get = {
7
- async me() {
8
- const url = _getUrl();
9
- return await Api.get(url);
10
- },
11
- };
12
-
13
- const _getUrl = () => {
14
- return `${Api.baseUrl}/${resource}`;
15
- };
16
-
17
- return {
18
- get,
19
- };
20
- };
@@ -1,31 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- export const createOrgsApiService = (orgId, orgType) => {
4
- if (!orgId && !orgType) throw new Error('No org id and/or type provided');
5
- const resource = 'orgs';
6
-
7
- // filter for stats data
8
- const stats = {
9
- async get(isPublic) {
10
- const endpoint = _getStatsEndpoint(isPublic);
11
- const url = `${_getUrl()}/${endpoint}?${_getUrlParams()}`;
12
- return await Api.get(url);
13
- },
14
- };
15
-
16
- const _getStatsEndpoint = (isPublic) => {
17
- return isPublic ? 'pstats' : 'stats';
18
- }
19
-
20
- const _getUrl = () => {
21
- return `${Api.baseUrl}/${resource}`;
22
- };
23
-
24
- const _getUrlParams = () => {
25
- return `org_id=${orgId}&org_type=${orgType}`
26
- }
27
-
28
- return {
29
- stats,
30
- };
31
- };
@@ -1,41 +0,0 @@
1
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
4
-
5
- export const createRegionsApiService = () => {
6
- const resource = "regions";
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
- async all() {
15
- const url = `${_getUrl()}?${_getParams(arguments)}`;
16
-
17
- return await Api.get(url);
18
- },
19
- };
20
-
21
- const post = (data) => {
22
- const url = `${_getUrl()}?${_getParams()}`;
23
-
24
- return Api.post(url, data);
25
- };
26
-
27
- const _getUrl = () => {
28
- return `${Api.baseUrl}/${resource}`;
29
- };
30
-
31
- const _getParams = (args) => {
32
- const paramsString = createParamsStringFromArgs(args)
33
-
34
- return `org_id=${orgId}&org_type=${orgType}${paramsString}`;
35
- };
36
-
37
- return {
38
- get,
39
- post,
40
- };
41
- };
@@ -1,31 +0,0 @@
1
- import Api from '../helpers/api';
2
-
3
- export const createSponsorsApiService = (orgId) => {
4
- if (!orgId) throw new Error('No org id provided');
5
- const resource = 'sponsors';
6
-
7
- // filter for map data
8
- const map = {
9
- async get() {
10
- const url = `${_getUrl()}/${orgId}/map-data`;
11
- return await Api.get(url);
12
- },
13
- };
14
-
15
- // filter for profile
16
- const profile = {
17
- async get() {
18
- const url = `${_getUrl()}/${orgId}/profile`;
19
- return await Api.get(url);
20
- },
21
- };
22
-
23
- const _getUrl = () => {
24
- return `${Api.baseUrl}/${resource}`;
25
- };
26
-
27
- return {
28
- map,
29
- profile,
30
- };
31
- };