@veritree/services 1.0.0-0 → 1.0.0-3

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-0",
3
+ "version": "1.0.0-3",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,43 +1,42 @@
1
1
  import { getCookie } from "./cookies";
2
- import { getSession } from "./session";
3
2
  import { createParamsStringFromArgs } from "../utils/args";
3
+ import { getSession } from "./session";
4
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");
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
11
  }
12
12
 
13
13
  /**
14
- *
14
+ *
15
15
  * @returns {promise}
16
16
  */
17
- this.all = async function () {
18
- const url = `${this.getUrl()}?${this.getUrlParams(arguments)}`;
17
+ async all() {
18
+ const url = `${this.getUrl()}${this.getUrlParams(arguments)}`;
19
19
  return await this.get(url);
20
- };
20
+ }
21
21
 
22
22
  /**
23
- *
24
- * @param {string, number} id
23
+ *
24
+ * @param {string, number} id
25
25
  * @returns {promise}
26
26
  */
27
- this.single = async function (id) {
28
- const url = `${this.getUrl()}/${id}?${this.getUrlParams()}`;
27
+ async single(id) {
28
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
29
29
  return await this.get(url);
30
- };
30
+ }
31
31
 
32
32
  /**
33
- *
34
- * @param {object} data
33
+ *
34
+ * @param {object} data
35
35
  * @returns {promise}
36
36
  */
37
- this.create = async function (data) {
38
- const url = `${this.getUrl()}?${this.getUrlParams()}`;
39
- return await this.post(url, data);
40
- };
37
+ async create(data) {
38
+ return await this.post(null, data);
39
+ }
41
40
 
42
41
  /**
43
42
  *
@@ -46,19 +45,9 @@ export default function Api(resource) {
46
45
  * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
47
46
  * @returns {promise}
48
47
  */
49
- this.update = async function (id, data, as = 'put') {
50
- const url = `${this.getUrl()}/${id}?${this.getUrlParams()}`;
48
+ async update(id, data, as = "put") {
49
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
51
50
  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
51
  }
63
52
 
64
53
  /**
@@ -66,7 +55,7 @@ export default function Api(resource) {
66
55
  * @param {string} url
67
56
  * @returns {promise}
68
57
  */
69
- this.get = async function (url) {
58
+ get = async function (url) {
70
59
  return await this.unWrap(url);
71
60
  };
72
61
 
@@ -76,10 +65,28 @@ export default function Api(resource) {
76
65
  * @param {object} data
77
66
  * @returns {promise}
78
67
  */
79
- this.post = async function (url, data, as) {
68
+ post = async function (url, data, as) {
69
+ if (!url) url = `${this.getUrl()}${this.getUrlParams()}`;
80
70
  return await this.unWrap(url, "post", data, as);
81
71
  };
82
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
+ };
89
+
83
90
  /**
84
91
  * Deals with all fetch requests
85
92
  *
@@ -88,7 +95,7 @@ export default function Api(resource) {
88
95
  * @param {object} data
89
96
  * @returns {object} envelope
90
97
  */
91
- this.unWrap = async function (url, method = "get", data, as) {
98
+ unWrap = async function (url, method = "get", data, as) {
92
99
  url = this.handleEnvelopParam(url, data); // TODO: remove when API is fully migrated to envelopes
93
100
  const config = this.getConfig(method, data, as);
94
101
 
@@ -109,7 +116,7 @@ export default function Api(resource) {
109
116
  * @param {object} body
110
117
  * @returns {object} data
111
118
  */
112
- this.getConfig = function (method, data, as) {
119
+ getConfig = function (method, data, as) {
113
120
  const isGet = method === "get";
114
121
  const isPut = as === "put";
115
122
  const isFormData = this.isFormData(data);
@@ -140,13 +147,21 @@ export default function Api(resource) {
140
147
  return config;
141
148
  };
142
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
+ }
157
+
143
158
  /**
144
159
  * Adds the envelope argument to the url
145
160
  *
146
161
  * @param {string} url
147
162
  * @returns {string} url
148
163
  */
149
- this.handleEnvelopParam = function (url) {
164
+ handleEnvelopParam = function (url) {
150
165
  if (!url || url.includes("_result=1")) return url;
151
166
 
152
167
  const urlHasArgs = url.includes("?");
@@ -155,5 +170,5 @@ export default function Api(resource) {
155
170
  return `${url}${urlEvenlopeArg}`;
156
171
  };
157
172
 
158
- this.isFormData = (data) => data instanceof FormData;
173
+ isFormData = (data) => data instanceof FormData;
159
174
  }
@@ -1,35 +1,5 @@
1
- import Api from '../helpers/api';
1
+ import Api from "../helpers/api-v2";
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
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
1
+ import Api from "../helpers/api-v2";
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
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
1
+ import Api from "../helpers/api-v2";
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
- 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";
1
+ import Api from "../helpers/api-v2";
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-v2';
2
+
3
+ const resource = 'forest-type-profiles';
4
+
5
+ export const ForestTypeProfiles = new Api(resource);
@@ -1,62 +1,5 @@
1
- import Api from "../helpers/api";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
- import { getSession } from "../helpers/session";
1
+ import Api from "../helpers/api-v2";
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-v2";
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
- import Api from "../helpers/api";
2
- import { getSession } from "../helpers/session";
3
- import { createParamsStringFromArgs } from "../utils/args";
1
+ import Api from "../helpers/api-v2";
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);
@@ -1,31 +1,25 @@
1
- import Api from '../helpers/api';
1
+ import Api from '../helpers/api-v2';
2
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
- };
3
+ class OrgsApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = 'orgs';
7
+ }
15
8
 
16
- const _getStatsEndpoint = (isPublic) => {
17
- return isPublic ? 'pstats' : 'stats';
9
+ async stats() {
10
+ const url = `${this._geStatstUrl()}`;
11
+ return await this.get(url);
18
12
  }
19
13
 
20
- const _getUrl = () => {
21
- return `${Api.baseUrl}/${resource}`;
22
- };
14
+ async publicStats() {
15
+ const url = `${this._geStatstUrl(true)}`;
16
+ return await this.get(url);
17
+ }
23
18
 
24
- const _getUrlParams = () => {
25
- return `org_id=${orgId}&org_type=${orgType}`
19
+ _geStatstUrl = (isPublic) => {
20
+ const endpoint = isPublic ? 'pstats' : 'stats';
21
+ return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
26
22
  }
23
+ }
27
24
 
28
- return {
29
- stats,
30
- };
31
- };
25
+ export const Orgs = new OrgsApi();
@@ -1,41 +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-v2";
4
2
 
5
- export const createRegionsApiService = () => {
6
- const resource = "regions";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "regions";
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
-
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
- };
5
+ export const Regions = new Api(resource);
@@ -1,31 +1,28 @@
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
- };
1
+ import Api from '../helpers/api-v2';
2
+
3
+ class SponsorsApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = 'sponsors';
7
+ }
8
+
9
+ async map() {
10
+ this.setOrg();
11
+
12
+ if(!this.orgId) throw new Error('No org id provided');
13
+
14
+ const url = `${this.getUrl()}/${this.orgId}/map-data`;
15
+ return await this.get(url);
16
+ }
17
+
18
+ async profile() {
19
+ this.setOrg();
20
+
21
+ if(!this.orgId) throw new Error('No org id provided');
22
+
23
+ const url = `${this.getUrl()}/${this.orgId}/profile`;
24
+ return await this.get(url);
25
+ }
26
+ }
27
+
28
+ export const Sponsors = new SponsorsApi();
@@ -0,0 +1,17 @@
1
+ import Api from '../helpers/api-v2';
2
+
3
+ class StatsApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ }
7
+
8
+ async crumb(id) {
9
+ this.resource = 'pstats';
10
+ const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
11
+
12
+ return await this.get(url);
13
+ }
14
+ }
15
+
16
+ export const Stats = new StatsApi();
17
+
@@ -1,26 +1,5 @@
1
- import Api from '../helpers/api';
1
+ import Api from '../helpers/api-v2';
2
2
 
3
- export const createSubdomainsApiService = () => {
4
- const resource = 'subdomains';
3
+ const resource = 'subdomains';
5
4
 
6
- const get = {
7
- /**
8
- * Gets org data related to subdomain
9
- *
10
- * @param {string} id (name or id)
11
- * @returns {object} evenlop
12
- */
13
- async specific(id) {
14
- const url = `${_getUrl()}/${id}`;
15
- return await Api.get(url);
16
- },
17
- };
18
-
19
- function _getUrl() {
20
- return `${Api.baseUrl}/${resource}`;
21
- }
22
-
23
- return {
24
- get,
25
- };
26
- };
5
+ export const Subdomains = new Api(resource);
@@ -1,70 +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-v2";
4
2
 
5
- export const createSubsiteTypesApiService = () => {
6
- const resource = "subsite-types";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "subsite-types";
8
4
 
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
- };
5
+ export const SubsiteTypes = new Api(resource);
@@ -1,70 +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-v2";
4
2
 
5
- export const createSubsitesApiService = () => {
6
- const resource = "subsites";
7
- const { orgId, orgType } = getSession();
3
+ const resource = "subsites";
8
4
 
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
- };
5
+ export const Subsites = new Api(resource);
@@ -1,42 +1,5 @@
1
- import Api from '../helpers/api';
1
+ import Api from '../helpers/api-v2';
2
2
 
3
- export const createTreeOrdersApiService = (orgId, orgType) => {
4
- const resource = 'tree-orders';
3
+ const resource = 'tree-orders';
4
+ export const TreeOrders = new Api(resource);
5
5
 
6
- const get = {
7
- /**
8
- *
9
- * @param {number/string} page
10
- * @param {number/string} pageSize
11
- * @returns {object} envelope
12
- */
13
- async all(page = 1, pageSize = 10) {
14
- const url = `${_getUrl()}?${_getUrlParams(page, pageSize)}`;
15
- return await Api.get(url);
16
- },
17
-
18
- /**
19
- *
20
- * @param {number/string} id
21
- * @param {number/string} page
22
- * @param {number/string} pageSize
23
- * @returns {object} envelope
24
- */
25
- async specific(id, page = 1, pageSize = 10) {
26
- const url = `${_getUrl()}/${id}?${_getUrlParams(page, pageSize)}`;
27
- return await Api.get(url);
28
- },
29
- };
30
-
31
- const _getUrl = () => {
32
- return `${Api.baseUrl}/${resource}`;
33
- };
34
-
35
- const _getUrlParams = (page, pageSize) => {
36
- return `org_id=${orgId}&org_type=${orgType}&page=${page}&page_size=${pageSize}&include=tree_allocations`;
37
- };
38
-
39
- return {
40
- get,
41
- };
42
- };
@@ -0,0 +1,15 @@
1
+ import Api from '../helpers/api-v2';
2
+
3
+ class UserApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = 'me';
7
+ }
8
+
9
+ async me() {
10
+ const url = `${this.getUrl()}${this.getUrlParams()}`;
11
+ return await this.get(url);
12
+ }
13
+ }
14
+
15
+ export const User = new UserApi();
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "countries";
4
-
5
- export const Countries = new Api(resource);
@@ -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,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
-
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,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "images";
4
-
5
- export const Images = new Api(resource);
@@ -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,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "regions";
4
-
5
- export const Regions = new Api(resource);
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api-v2";
2
-
3
- const resource = "subsites";
4
-
5
- export const Subsites = new Api(resource);
@@ -1,16 +0,0 @@
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
- };