@veritree/services 2.21.2-2 → 2.23.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/helpers/api.js +43 -33
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veritree/services",
3
- "version": "2.21.2-2",
3
+ "version": "2.23.0",
4
4
  "description": "A collection of javascript functions/services to talk to veritree API",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -1,25 +1,35 @@
1
- import { getCookie } from './cookies';
2
- import { createParamsStringFromArgs } from '../utils/args';
3
- import { getSession } from './session';
1
+ import { getCookie } from "./cookies";
2
+ import { createParamsStringFromArgs } from "../utils/args";
3
+ import { getSession } from "./session";
4
4
 
5
5
  /**
6
- * Adds the envelope argument to the url
6
+ * Adds a version parameter to a URL if it does not already have one.
7
+ * If an environment variable called API_VERITREE_VERSION is defined, its value is used as the version,
8
+ * otherwise the default version "5.0.0" is used.
7
9
  *
8
- * @param {string} url
9
- * @returns {string} url
10
+ * @param {string} url - The URL to modify.
11
+ * @returns {string} The modified URL with the version parameter appended to it.
10
12
  */
11
- function addVersionArg(url) {
12
- if (!url || url.includes('_result=1')) return url;
13
-
14
- const version = process.env
15
- ? process.env.API_VERITREE_VERSION
16
- ? process.env.API_VERITREE_VERSION
17
- : '5.0.0'
18
- : '5.0.0' || '5.0.0';
19
- const urlHasArgs = url.includes('?');
20
- const urlVersionArg = urlHasArgs ? `&_v=${version}` : `?_v=${version}`;
21
-
22
- return `${url}${urlVersionArg}`;
13
+ function addVersionParam(url) {
14
+ // If URL is invalid or already has the result parameter, return it as is
15
+ if (!url || url.includes("_result=1")) {
16
+ return url;
17
+ }
18
+
19
+ // Check if URL already has the version parameter
20
+ if (url.includes("_v=")) {
21
+ return url;
22
+ }
23
+
24
+ // Use environment variable if available, otherwise use default version
25
+ const version = process.env.API_VERITREE_VERSION || "5.0.0";
26
+
27
+ // Append version parameter to URL
28
+ const urlVersionParam = url.includes("?")
29
+ ? `&_v=${version}`
30
+ : `?_v=${version}`;
31
+
32
+ return `${url}${urlVersionParam}`;
23
33
  }
24
34
 
25
35
  /**
@@ -30,10 +40,10 @@ function addVersionArg(url) {
30
40
  * @returns {object} data
31
41
  */
32
42
  function getConfig(method, data, as) {
33
- const isGet = method === 'get';
43
+ const isGet = method === "get";
34
44
  const isSpoofing = as;
35
45
  const isFormData = data instanceof FormData;
36
- const accessToken = `Bearer ${getCookie('access_token')}`;
46
+ const accessToken = `Bearer ${getCookie("access_token")}`;
37
47
 
38
48
  const config = {
39
49
  method,
@@ -43,7 +53,7 @@ function getConfig(method, data, as) {
43
53
  };
44
54
 
45
55
  if (!isFormData) {
46
- config.headers['Content-Type'] = 'application/json';
56
+ config.headers["Content-Type"] = "application/json";
47
57
  }
48
58
 
49
59
  // TODO: improve this ifs and elses
@@ -51,7 +61,7 @@ function getConfig(method, data, as) {
51
61
  if (!data) data = {};
52
62
 
53
63
  if (isFormData) {
54
- if (isSpoofing) data.set('_method', as.toUpperCase());
64
+ if (isSpoofing) data.set("_method", as.toUpperCase());
55
65
  config.body = data;
56
66
  } else {
57
67
  if (isSpoofing) data._method = as.toUpperCase();
@@ -65,7 +75,7 @@ function getConfig(method, data, as) {
65
75
  export default class Api {
66
76
  constructor(resource) {
67
77
  this.baseUrl = process.env ? `${process.env.API_VERITREE_URL}/api` : null;
68
- this.resource = resource ? resource : '';
78
+ this.resource = resource ? resource : "";
69
79
  this.orgId = null;
70
80
  this.orgType = null;
71
81
  }
@@ -110,7 +120,7 @@ export default class Api {
110
120
  * @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
111
121
  * @returns {promise}
112
122
  */
113
- async update(id, data, as = 'put', args) {
123
+ async update(id, data, as = "put", args) {
114
124
  const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
115
125
  return await this.post(url, data, as);
116
126
  }
@@ -124,7 +134,7 @@ export default class Api {
124
134
  */
125
135
  async delete(id, args) {
126
136
  const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
127
- return await this.post(url, null, 'delete');
137
+ return await this.post(url, null, "delete");
128
138
  }
129
139
 
130
140
  /**
@@ -144,7 +154,7 @@ export default class Api {
144
154
  */
145
155
  async post(url, data, as, args) {
146
156
  if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
147
- return await this.unWrap(url, 'post', data, as);
157
+ return await this.unWrap(url, "post", data, as);
148
158
  }
149
159
 
150
160
  // ----------
@@ -155,7 +165,7 @@ export default class Api {
155
165
  * @returns
156
166
  */
157
167
  getUrl(id) {
158
- id = id ? `/${id}` : '';
168
+ id = id ? `/${id}` : "";
159
169
  return `${this.baseUrl}/${this.resource}${id}`;
160
170
  }
161
171
 
@@ -168,14 +178,14 @@ export default class Api {
168
178
  this.setOrg();
169
179
  let isOrgLess = false;
170
180
  let isOrgAs = false;
171
- let orgIdParam = '';
172
- let orgTypeParam = '';
181
+ let orgIdParam = "";
182
+ let orgTypeParam = "";
173
183
 
174
184
  // while most of endpoints require an org id and type, some endpoints do not
175
185
  if (args) {
176
- isOrgLess = Object.hasOwn(args, 'orgless');
186
+ isOrgLess = Object.hasOwn(args, "orgless");
177
187
  isOrgAs =
178
- Object.hasOwn(args, 'org_id_as') && Object.hasOwn(args, 'org_type_as');
188
+ Object.hasOwn(args, "org_id_as") && Object.hasOwn(args, "org_type_as");
179
189
  }
180
190
 
181
191
  // a super admin user can create/edit data from other orgs
@@ -206,8 +216,8 @@ export default class Api {
206
216
  * @param {object} data
207
217
  * @returns {object} envelope
208
218
  */
209
- async unWrap(url, method = 'get', data, as) {
210
- url = addVersionArg(url);
219
+ async unWrap(url, method = "get", data, as) {
220
+ url = addVersionParam(url);
211
221
  const config = getConfig(method, data, as);
212
222
  const response = await fetch(url, config);
213
223