@veritree/services 2.35.0 → 2.35.1-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
@@ -21,7 +21,6 @@ import { Subsites } from './src/endpoints/subsites';
21
21
  import { SubsiteTypes } from './src/endpoints/subsite-types';
22
22
  import { Tags } from './src/endpoints/tags';
23
23
  import { TreeOrders } from './src/endpoints/trees-orders';
24
- import { TreeOrderStatuses } from './src/endpoints/tree-order-statuses';
25
24
  import { User } from './src/endpoints/user';
26
25
  import { Users } from './src/endpoints/users';
27
26
  import { Utilities } from './src/endpoints/utilities';
@@ -38,6 +37,7 @@ import { Organizations } from './src/endpoints/organizations';
38
37
  import { Tasks } from './src/endpoints/tasks';
39
38
  import { Geometries } from './src/endpoints/geometries';
40
39
  import { Password } from './src/endpoints/password';
40
+ import { TreeCodes } from './src/endpoints/tree-codes';
41
41
 
42
42
  export {
43
43
  BulkUploads,
@@ -63,7 +63,6 @@ export {
63
63
  SubsiteTypes,
64
64
  Tags,
65
65
  TreeOrders,
66
- TreeOrderStatuses,
67
66
  User,
68
67
  Users,
69
68
  Utilities,
@@ -79,5 +78,6 @@ export {
79
78
  Organizations,
80
79
  Tasks,
81
80
  Geometries,
82
- Password
81
+ Password,
82
+ TreeCodes
83
83
  };
package/jsdoc.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "source": {
3
+ "include": ["src/endpoints"],
4
+ "includePattern": ".js$",
5
+ "excludePattern": "(node_modules/|docs)"
6
+ },
7
+ "plugins": ["plugins/markdown"],
8
+ "templates": {
9
+ "cleverLinks": true,
10
+ "monospaceLinks": true
11
+ },
12
+ "opts": {
13
+ "recurse": true,
14
+ "destination": "./documentation/"
15
+ }
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "2.35.0",
3
+ "version": "2.35.1-0",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -8,9 +8,17 @@
8
8
  "author": "cyroveritree <cyro@veritree.com>",
9
9
  "license": "MIT",
10
10
  "publishConfig": {
11
- "access": "public"
11
+ "registry": "https://registry.npmjs.org"
12
12
  },
13
- "dependencies": {
14
- "np": "^7.6.2"
13
+ "scripts": {
14
+ "release": "np --no-tests --no-2fa --branch=next"
15
+ },
16
+ "devDependencies": {
17
+ "np": "^8.0.4",
18
+ "prettier": "^2.7.1"
19
+ },
20
+ "engines": {
21
+ "npm": ">=8.0.0",
22
+ "node": ">=18.0.0"
15
23
  }
16
24
  }
@@ -28,11 +28,6 @@ class FieldReportsApi extends Api {
28
28
  detach
29
29
  }
30
30
  }
31
-
32
- blockchain(fieldReportId, params) {
33
- const url = `${this.getUrl()}/${fieldReportId}${this.getUrlParams(params)}`;
34
- return this.get(url)
35
- }
36
31
  }
37
32
 
38
33
  export const FieldReports = new FieldReportsApi();
@@ -1,9 +1,9 @@
1
- import Api from '../helpers/api';
1
+ import Api from "../helpers/api";
2
2
 
3
3
  class OrganizationsApi extends Api {
4
4
  constructor(resource) {
5
5
  super(resource);
6
- this.resource = 'organizations';
6
+ this.resource = "organizations";
7
7
  }
8
8
 
9
9
  tasks(orgId) {
@@ -14,6 +14,12 @@ class OrganizationsApi extends Api {
14
14
  return await this.get(url);
15
15
  };
16
16
 
17
+ /**
18
+ * Creates a new task with the specified data.
19
+ *
20
+ * @param {Object} taskData - An object containing the data for the new task.
21
+ * @returns {Promise<Object>} - A Promise that resolves to the created task object.
22
+ */
17
23
  const create = async (data) => {
18
24
  url = `${url}${this.getUrlParams()}`;
19
25
  return await this.post(url, data);
@@ -24,12 +24,12 @@ class OrgsApi extends Api {
24
24
  }
25
25
 
26
26
  async stats() {
27
- const url = `${this._geStatstUrl()}`;
27
+ const url = `${this._getStatstUrl()}`;
28
28
  return await this.get(url);
29
29
  }
30
30
 
31
- async publicStats() {
32
- const url = `${this._geStatstUrl(true)}`;
31
+ async publicStats(args) {
32
+ const url = `${this._getStatstUrl(true, args)}`;
33
33
  return await this.get(url);
34
34
  }
35
35
 
@@ -56,9 +56,9 @@ class OrgsApi extends Api {
56
56
  };
57
57
  }
58
58
 
59
- _geStatstUrl(isPublic) {
59
+ _getStatstUrl(isPublic, args) {
60
60
  const endpoint = isPublic ? 'pstats' : 'stats';
61
- return `${this.getUrl()}/${endpoint}${this.getUrlParams()}`;
61
+ return `${this.getUrl()}/${endpoint}${this.getUrlParams(args)}`;
62
62
  }
63
63
  }
64
64
 
@@ -1,10 +1,10 @@
1
1
  import Api from "../helpers/api";
2
2
 
3
3
  /**
4
- * This is a namespace that contains documentation elements
5
- * belonging to the Password domain.
6
- *
7
- * @namespace Password
4
+ * This is a namespace that contains documentation elements belonging to the Password domain.
5
+ *
6
+ * @class
7
+ * @module Password
8
8
  */
9
9
  class PasswordApi extends Api {
10
10
  constructor(resource) {
@@ -12,13 +12,18 @@ class PasswordApi extends Api {
12
12
  this.resource = "password";
13
13
  }
14
14
 
15
+ /**
16
+ * @function
17
+ * @name recovery
18
+ * @returns {Object}
19
+ */
15
20
  recovery() {
16
21
  /**
17
22
  * Sends a recovery password email to the specified email address.
18
23
  *
19
24
  * @param {Object} data - An object containing the following properties:
20
- * @param {string} data.email - The email address to send the recovery email to.
21
- * @param {string} [data.return_url] - The URL to redirect the user to after they reset their password.
25
+ * @param {string} data.email - The email address to send the recovery email to.
26
+ * @param {string} [data.return_url] - The URL to redirect the user to after they reset their password.
22
27
  * @returns {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the email was sent successfully.
23
28
  */
24
29
  const post = async (data) => {
@@ -32,6 +37,11 @@ class PasswordApi extends Api {
32
37
  };
33
38
  }
34
39
 
40
+ /**
41
+ * @function
42
+ * @name reset
43
+ * @returns {Object}
44
+ */
35
45
  reset() {
36
46
  /**
37
47
  * Sends a POST request to reset a password with the specified email, token, and password.
@@ -44,7 +54,7 @@ class PasswordApi extends Api {
44
54
  * @returns {Promise<Object>} - A Promise that resolves to the result of the POST request.
45
55
  */
46
56
  const post = async (data) => {
47
- const url = `${this.getUrl()}/reset`
57
+ const url = `${this.getUrl()}/reset`;
48
58
 
49
59
  return await this.post(url, data);
50
60
  };
@@ -55,4 +65,4 @@ class PasswordApi extends Api {
55
65
  }
56
66
  }
57
67
 
58
- export const Password = new PasswordApi();
68
+ export const Password = new PasswordApi();
@@ -1,9 +1,20 @@
1
- import Api from '../helpers/api';
1
+ import Api from "../helpers/api";
2
2
 
3
3
  class PlantingSitesApi extends Api {
4
4
  constructor(resource) {
5
5
  super(resource);
6
- this.resource = 'planting-sites';
6
+ this.resource = "planting-sites";
7
+ }
8
+
9
+ public() {
10
+ const single = async (plantingSiteId) => {
11
+ const url = `${this.getUrl()}/public/${plantingSiteId}`;
12
+ return await this.get(url);
13
+ };
14
+
15
+ return {
16
+ single,
17
+ };
7
18
  }
8
19
 
9
20
  users(plantingSiteId) {
@@ -32,7 +43,7 @@ class PlantingSitesApi extends Api {
32
43
  url = `${this.getUrl()}/${subsiteId}/media/images/${imageId}${this.getUrlParams(
33
44
  params
34
45
  )}`;
35
- return await this.post(url, null, 'delete');
46
+ return await this.post(url, null, "delete");
36
47
  };
37
48
 
38
49
  return {
@@ -54,7 +65,7 @@ class PlantingSitesApi extends Api {
54
65
  url = `${this.getUrl()}/${subsiteId}/media/attachments/${attachmentId}${this.getUrlParams(
55
66
  params
56
67
  )}`;
57
- return await this.post(url, null, 'delete');
68
+ return await this.post(url, null, "delete");
58
69
  };
59
70
 
60
71
  return {
@@ -75,47 +86,6 @@ class PlantingSitesApi extends Api {
75
86
  all,
76
87
  };
77
88
  }
78
-
79
- evidence(subsiteId, params) {
80
- let url = `${this.getUrl()}/${subsiteId}/evidence`;
81
-
82
- console.log(url);
83
-
84
- const all = async () => {
85
- url = `${url}${this.getUrlParams(params)}`;
86
- return await this.get(url);
87
- };
88
-
89
- const attach = async (formData) => {
90
- url = `${url}${this.getUrlParams(params)}`;
91
- return await this.post(url, formData);
92
- };
93
-
94
- const remove = async (evidenceType, evidenceId) => {
95
- url = `${url}/${evidenceType}/${evidenceId}${this.getUrlParams(
96
- params
97
- )}`;
98
-
99
- return await this.post(url, null, "delete");
100
- };
101
-
102
- return {
103
- all,
104
- attach,
105
- delete: remove,
106
- };
107
- }
108
-
109
- stats(subsiteId) {
110
- const all = async (args) => {
111
- const url = `${this.getUrl()}/${subsiteId}/stats${this.getUrlParams(args)}`;
112
- return await this.get(url);
113
- };
114
-
115
- return {
116
- all,
117
- };
118
- }
119
89
  }
120
90
 
121
91
  export const PlantingSites = new PlantingSitesApi();
@@ -27,15 +27,6 @@ class RegionsApi extends Api {
27
27
  all,
28
28
  };
29
29
  }
30
- listRegions(orgId,orgType) {
31
- const all = async () => {
32
- const url = `${this.getUrl()}/public?org_id=${orgId}&org_type=${orgType}&_v=10.0.0`;
33
- return await this.get(url);
34
- };
35
- return {
36
- all,
37
- };
38
- }
39
30
  }
40
31
 
41
32
  export const Regions = new RegionsApi();
@@ -6,28 +6,38 @@ class SponsorsApi extends Api {
6
6
  this.resource = 'sponsors';
7
7
  }
8
8
 
9
- async map() {
10
- this.setOrg();
9
+ async map(orgId) {
11
10
 
12
- if (!this.orgId) {
11
+ if (!orgId) {
13
12
  throw new Error('No org id provided');
14
13
  }
15
14
 
16
- const url = `${this.getUrl()}/${this.orgId}/map-data`;
15
+ const url = `${this.getUrl()}/${orgId}/map-data`;
17
16
  return await this.get(url);
18
17
  }
19
18
 
20
- async profile() {
21
- this.setOrg();
19
+ async profile(args) {
20
+ const params = this.getUrlParams(args);
22
21
 
23
22
  if (!this.orgId) {
24
23
  throw new Error('No org id provided');
25
24
  }
26
25
 
27
- const url = `${this.getUrl()}/${this.orgId}/profile`;
26
+ const url = `${this.getUrl()}/${this.orgId}/profile${params}`;
28
27
  return await this.get(url);
29
28
  }
30
29
 
30
+ plantingSites(orgId) {
31
+ const all = async () => {
32
+ const url = `${this.getUrl()}/${orgId}/planting-sites`;
33
+ return await this.get(url);
34
+ };
35
+
36
+ return {
37
+ all
38
+ };
39
+ }
40
+
31
41
  async plantingStats(args) {
32
42
  const url = `${this.getUrl()}/planting-stats${this.getUrlParams(args)}`;
33
43
  return await this.get(url);
@@ -49,7 +59,9 @@ class SponsorsApi extends Api {
49
59
  };
50
60
 
51
61
  const imageUploadUrl = async () => {
52
- const url = `${this.getUrl()}/${this.orgId}/cms/image-upload-url${urlParams}`;
62
+ const url = `${this.getUrl()}/${
63
+ this.orgId
64
+ }/cms/image-upload-url${urlParams}`;
53
65
  return await this.get(url);
54
66
  };
55
67
 
@@ -67,19 +79,21 @@ class SponsorsApi extends Api {
67
79
 
68
80
  tools(args) {
69
81
  const get = async (sponsorId) => {
70
- const url = `${this.getUrl()}/${sponsorId}/tools${this.getUrlParams(args)}`;
82
+ const url = `${this.getUrl()}/${sponsorId}/tools${this.getUrlParams(
83
+ args
84
+ )}`;
71
85
  return await this.get(url);
72
- }
86
+ };
73
87
 
74
88
  const create = async (sponsorId, data) => {
75
89
  const url = `${this.getUrl()}/${sponsorId}/tools`;
76
90
  return await this.post(url, data);
77
- }
91
+ };
78
92
 
79
93
  return {
80
94
  get,
81
- create
82
- }
95
+ create,
96
+ };
83
97
  }
84
98
  }
85
99
 
@@ -13,126 +13,19 @@ class StandardsApi extends Api {
13
13
  return await this.get(url);
14
14
  };
15
15
 
16
- const create = async (data, args) => {
17
- const url = `${this.getUrl()}/themes?${createParamsStringFromArgs(args)}`;
18
- return await this.post(url, data);
19
- };
20
-
21
- const update = async (themeId, data, args) => {
22
- const url = `${this.getUrl()}/themes/${themeId}?${createParamsStringFromArgs(
23
- args
24
- )}`;
25
- return await this.post(url, data, 'patch');
26
- };
27
-
28
- const remove = async (args) => {
29
- const url = `${this.getUrl()}/themes/${themeId}?${createParamsStringFromArgs(
30
- args
31
- )}`;
32
- return await this.post(url, data, 'delete');
33
- };
34
-
35
- const goals = (themeId) => {
36
- const create = async (data, args) => {
37
- const url = `${this.getUrl()}/themes/${themeId}/goals?${createParamsStringFromArgs(
38
- args
39
- )}`;
40
- return await this.post(url, data);
41
- };
42
-
43
- const indicators = (goalId) => {
44
- const create = async (data, args) => {
45
- const url = `${this.getUrl()}/themes/${themeId}/goals/${goalId}/indicators?${createParamsStringFromArgs(
46
- args
47
- )}`;
48
- return await this.post(url, data);
49
- };
50
-
51
- return {
52
- create,
53
- };
54
- };
55
-
56
- return {
57
- create,
58
- indicators,
59
- };
60
- };
61
-
62
- const UNSDGs = (themeId) => {
63
- const create = async (data) => {
64
- console.log('this.getUrl:', this.getUrl())
65
- const url = `${this.getUrl()}/themes/${themeId}/attach-sdg`;
66
- return await this.post(url, data);
67
- };
68
- return {
69
- create
70
- };
71
- };
72
-
73
16
  return {
74
17
  all,
75
- create,
76
- update,
77
- delete: remove,
78
- goals,
79
- UNSDGs
80
18
  };
81
19
  }
82
20
 
83
- goals(goalId) {
84
- const single = async () => {
21
+ goals() {
22
+ const single = async (goalId) => {
85
23
  const url = `${this.getUrl()}/goals/${goalId}`;
86
24
  return await this.get(url);
87
25
  };
88
26
 
89
- const update = async (data) => {
90
- const url = `${this.getUrl()}/goals/${goalId}`;
91
- return await this.post(url, data, 'patch');
92
- };
93
-
94
- const remove = async () => {
95
- const url = `${this.getUrl()}/goals/${goalId}`;
96
- return await this.post(url, null, 'delete');
97
- };
98
-
99
- return {
100
- single,
101
- update,
102
- delete: remove,
103
- };
104
- }
105
-
106
- indicators(indicatorId) {
107
- const single = async () => {
108
- const url = `${this.getUrl()}/indicators/${indicatorId}`;
109
- return await this.get(url);
110
- };
111
-
112
- const update = async (data) => {
113
- const url = `${this.getUrl()}/indicators/${indicatorId}`;
114
- return await this.post(url, data, 'patch');
115
- };
116
-
117
- const remove = async () => {
118
- const url = `${this.getUrl()}/indicators/${indicatorId}`;
119
- return await this.post(url, null, 'delete');
120
- };
121
-
122
27
  return {
123
28
  single,
124
- update,
125
- delete: remove,
126
- };
127
- }
128
- UNSDGs(themeId){
129
- const attach = async (data) => {
130
- const url = `${this.getUrl()}/themes/${themeId}/attach-sdg`;
131
- return await this.update(url,{"sdg_id":data})
132
- };
133
-
134
- return {
135
- attach
136
29
  };
137
30
  }
138
31
  }
@@ -5,13 +5,12 @@ class StatsApi extends Api {
5
5
  super(resource);
6
6
  }
7
7
 
8
- async crumb(id) {
8
+ async crumb(id, args) {
9
9
  this.resource = 'pstats';
10
- const url = `${this.getUrl()}/${id}${this.getUrlParams()}`;
10
+ const url = `${this.getUrl()}/${id}${this.getUrlParams(args)}`;
11
11
 
12
12
  return await this.get(url);
13
13
  }
14
14
  }
15
15
 
16
16
  export const Stats = new StatsApi();
17
-
@@ -0,0 +1,43 @@
1
+ import Api from "../helpers/api";
2
+
3
+ class TreeCodesApi extends Api {
4
+ constructor(resource) {
5
+ super(resource);
6
+ this.resource = "tree-codes";
7
+ }
8
+
9
+ holders() {
10
+ const claim = async (treeCodeHolder) => {
11
+ const url = `${this.getUrl()}/holders/claim/${treeCodeHolder}`;
12
+ return await this.get(url);
13
+ };
14
+
15
+ return {
16
+ claim,
17
+ };
18
+ }
19
+
20
+ stats() {
21
+ const all = async () => {
22
+ const url = `${this.getUrl()}/stats`;
23
+ return await this.get(url);
24
+ }
25
+
26
+ return {
27
+ all
28
+ }
29
+ }
30
+
31
+ status() {
32
+ const single = async (treeCodeHolder) => {
33
+ const url = `${this.getUrl()}/status/${treeCodeHolder}`;
34
+ return await this.get(url);
35
+ }
36
+
37
+ return {
38
+ single
39
+ }
40
+ }
41
+ }
42
+
43
+ export const TreeCodes = new TreeCodesApi();
@@ -1,80 +1,33 @@
1
- import { getCookie } from "./cookies";
2
- import { createParamsStringFromArgs } from "../utils/args";
3
- import { getSession } from "./session";
4
-
5
- /**
6
- * Returns the runtime configuration object for Nuxt 3.
7
- *
8
- * Note:
9
- * This function should only be used with Nuxt 3.
10
- * For Nuxt 2, the environment variables will work as expected.
11
- *
12
- * @returns {Object|null} - The runtime configuration object, or null if it's not available.
13
- */
14
- // function getNuxtRuntimeConfig() {
15
- // if (typeof useRuntimeConfig === "function") {
16
- // return useRuntimeConfig();
17
- // } else {
18
- // return null;
19
- // }
20
- // }
21
-
22
- // function getVersion() {
23
- // if (process) {
24
- // process?.env?.API_VERITREE_VERSION;
25
- // } else {
26
- // const nuxtRuntimeConfig = getNuxtRuntimeConfig();
27
-
28
- // if (nuxtRuntimeConfig) {
29
- // return nuxtRuntimeConfig.public.API_VERITREE_VERSION;
30
- // }
31
- // }
32
-
33
- // return "5.0.0";
34
- // }
35
-
36
- /**
37
- * Adds a version parameter to a URL if it does not already have one.
38
- * If an environment variable called API_VERITREE_VERSION is defined, its value is used as the version,
39
- * otherwise the default version "5.0.0" is used.
40
- *
41
- * @param {string} url - The URL to modify.
42
- * @returns {string} The modified URL with the version parameter appended to it.
43
- */
1
+ import { getCookie } from './cookies';
2
+ import { getSession } from './session';
3
+ import { createParamsStringFromArgs } from '../utils/args';
4
+
44
5
  function addVersionParam(url) {
45
6
  // If URL is invalid or already has the result parameter, return it as is
46
- if (!url || url.includes("_result=1")) {
7
+ if (!url || url.includes('_result=1')) {
47
8
  return url;
48
9
  }
49
10
 
50
11
  // Check if URL already has the version parameter
51
- if (url.includes("_v=")) {
12
+ if (url.includes('_v=')) {
52
13
  return url;
53
14
  }
54
15
 
55
- // Use environment variable if available, otherwise use default version
56
- const version = process.env.API_VERITREE_VERSION || "5.0.0";
16
+ const version = '10.0.0';
57
17
 
58
18
  // Append version parameter to URL
59
- const urlVersionParam = url.includes("?")
19
+ const urlVersionParam = url.includes('?')
60
20
  ? `&_v=${version}`
61
21
  : `?_v=${version}`;
62
22
 
63
23
  return `${url}${urlVersionParam}`;
64
24
  }
65
25
 
66
- /**
67
- * Handles how the data should be sent in the fetch method
68
- *
69
- * @param {string} method
70
- * @param {object} body
71
- * @returns {object} data
72
- */
73
- function getConfig(method, data, as) {
74
- const isGet = method === "get";
26
+ function getConfig(method, data, as, $api) {
27
+ const isGet = method === 'get';
75
28
  const isSpoofing = as;
76
29
  const isFormData = data instanceof FormData;
77
- const accessToken = `Bearer ${getCookie("access_token")}`;
30
+ const accessToken = $api.getAccessToken();
78
31
 
79
32
  const config = {
80
33
  method,
@@ -84,7 +37,7 @@ function getConfig(method, data, as) {
84
37
  };
85
38
 
86
39
  if (!isFormData) {
87
- config.headers["Content-Type"] = "application/json";
40
+ config.headers['Content-Type'] = 'application/json';
88
41
  }
89
42
 
90
43
  // TODO: improve this ifs and elses
@@ -92,7 +45,7 @@ function getConfig(method, data, as) {
92
45
  if (!data) data = {};
93
46
 
94
47
  if (isFormData) {
95
- if (isSpoofing) data.set("_method", as.toUpperCase());
48
+ if (isSpoofing) data.set('_method', as.toUpperCase());
96
49
  config.body = data;
97
50
  } else {
98
51
  if (isSpoofing) data._method = as.toUpperCase();
@@ -105,17 +58,9 @@ function getConfig(method, data, as) {
105
58
 
106
59
  export default class Api {
107
60
  constructor(resource) {
108
- // Nuxt 3
109
- // const nuxtConfig = getNuxtRuntimeConfig();
110
- // const url =
111
- // nuxtConfig?.public?.API_VERITREE_URL ||
112
- // process?.env?.API_VERITREE_URL ||
113
- // null;
114
-
115
- // this.baseUrl = url + "/api";
116
-
117
- this.baseUrl = process.env ? `${process.env.API_VERITREE_URL}/api` : null;
118
- this.resource = resource ? resource : "";
61
+ this.accessToken = null;
62
+ this.baseUrl = null;
63
+ this.resource = resource ? resource : '';
119
64
  this.orgId = null;
120
65
  this.orgType = null;
121
66
  }
@@ -124,110 +69,66 @@ export default class Api {
124
69
  this.baseUrl = baseUrl;
125
70
  }
126
71
 
127
- /**
128
- *
129
- * @returns {promise}
130
- */
72
+ setAccessToken(accessToken) {
73
+ this.accessToken = accessToken;
74
+ }
75
+
76
+ getAccessToken() {
77
+ return `Bearer ${this.accessToken || getCookie("access_token")}`;
78
+ }
79
+
131
80
  async all(args) {
132
81
  const url = `${this.getUrl()}${this.getUrlParams(args)}`;
133
82
  return await this.get(url);
134
83
  }
135
84
 
136
- /**
137
- *
138
- * @param {string, number} id
139
- * @param {object} args/params
140
- * @returns {promise}
141
- */
142
85
  async single(id, args) {
143
- const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
86
+ const params = this.args ? this.getUrlParams(args) : '';
87
+ const url = `${this.getUrl(id)}${params}`;
144
88
  return await this.get(url);
145
89
  }
146
90
 
147
- /**
148
- *
149
- * @param {object} data
150
- * @returns {promise}
151
- */
152
91
  async create(data, args) {
153
92
  return await this.post(null, data, null, args);
154
93
  }
155
94
 
156
- /**
157
- *
158
- * @param {string} url
159
- * @param {object} data
160
- * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
161
- * @returns {promise}
162
- */
163
- async update(id, data, as = "put", args) {
95
+ async update(id, data, as = 'put', args) {
164
96
  const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
165
97
  return await this.post(url, data, as);
166
98
  }
167
99
 
168
- /**
169
- *
170
- * @param {string} url
171
- * @param {object} data
172
- * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
173
- * @returns {promise}
174
- */
175
100
  async delete(id, args) {
176
101
  const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
177
- return await this.post(url, null, "delete");
102
+ return await this.post(url, null, 'delete');
178
103
  }
179
104
 
180
- /**
181
- *
182
- * @param {string} url
183
- * @returns {promise}
184
- */
185
105
  async get(url) {
186
106
  return await this.unWrap(url);
187
107
  }
188
108
 
189
- /**
190
- *
191
- * @param {string} url
192
- * @param {object} data
193
- * @returns {promise}
194
- */
195
109
  async post(url, data, as, args) {
196
110
  if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
197
- return await this.unWrap(url, "post", data, as);
111
+ return await this.unWrap(url, 'post', data, as);
198
112
  }
199
113
 
200
- // ----------
201
- // --
202
- /**
203
- *
204
- * @param {string, number} id
205
- * @returns
206
- */
207
114
  getUrl(id) {
208
- id = id ? `/${id}` : "";
115
+ id = id ? `/${id}` : '';
209
116
  return `${this.baseUrl}/${this.resource}${id}`;
210
117
  }
211
118
 
212
- /**
213
- *
214
- * @param {object} args
215
- * @returns {string}
216
- */
217
119
  getUrlParams(args) {
218
- this.setOrg();
219
120
  let isOrgLess = false;
220
121
  let isOrgIdAs = false;
221
122
  let isOrgTypeAs = false;
222
- let orgIdParam = "";
223
- let orgTypeParam = "";
224
- let argsClone = structuredClone(args); // avoids mutating object
123
+ let orgIdParam = '';
124
+ let orgTypeParam = '';
125
+ let argsClone = { ...args }; // avoids mutating object
225
126
 
226
127
  // while most of endpoints require an org id and type, some endpoints do not
227
128
  if (argsClone) {
228
- isOrgLess = Object.hasOwn(argsClone, "orgless");
229
- isOrgIdAs = Object.hasOwn(argsClone, "org_id_as");
230
- isOrgTypeAs = Object.hasOwn(argsClone, "org_type_as");
129
+ isOrgLess = Object.hasOwn(argsClone, 'orgless');
130
+ isOrgIdAs = Object.hasOwn(argsClone, 'org_id_as');
131
+ isOrgTypeAs = Object.hasOwn(argsClone, 'org_type_as');
231
132
  }
232
133
 
233
134
  // a super admin user can create/edit data from other orgs
@@ -253,22 +154,23 @@ export default class Api {
253
154
  }
254
155
  }
255
156
 
157
+ if (!this.orgId && !this.orgType) {
158
+ const session = getSession();
159
+
160
+ if(session) {
161
+ this.orgId = session?.orgId;
162
+ this.orgType = session?.orgType;
163
+ }
164
+ }
165
+
256
166
  return `?${orgIdParam}${orgTypeParam}${createParamsStringFromArgs(
257
167
  argsClone
258
168
  )}`;
259
169
  }
260
170
 
261
- /**
262
- * Deals with all fetch requests
263
- *
264
- * @param {string} url
265
- * @param {string} method
266
- * @param {object} data
267
- * @returns {object} envelope
268
- */
269
- async unWrap(url, method = "get", data, as) {
171
+ async unWrap(url, method = 'get', data, as) {
270
172
  url = addVersionParam(url);
271
- const config = getConfig(method, data, as);
173
+ const config = getConfig(method, data, as, this);
272
174
  const response = await fetch(url, config);
273
175
 
274
176
  return new Promise((resolve, reject) => {
@@ -282,12 +184,4 @@ export default class Api {
282
184
  });
283
185
  });
284
186
  }
285
-
286
- setOrg() {
287
- const session = getSession();
288
- if (!session) return;
289
- const { orgId, orgType } = session;
290
- this.orgId = orgId;
291
- this.orgType = orgType;
292
- }
293
187
  }
@@ -1,5 +0,0 @@
1
- import Api from "../helpers/api";
2
-
3
- const resource = "tree-order-statuses";
4
-
5
- export const TreeOrderStatuses = new Api(resource);