@veritree/services 2.18.1-4 → 2.18.1-5
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/package.json +1 -1
- package/src/helpers/api.js +32 -22
package/package.json
CHANGED
package/src/helpers/api.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { getCookie } from
|
|
2
|
-
import { createParamsStringFromArgs } from
|
|
3
|
-
import { getSession } from
|
|
1
|
+
import { getCookie } from "./cookies";
|
|
2
|
+
import { createParamsStringFromArgs } from "../utils/args";
|
|
3
|
+
import { getSession } from "./session";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Adds the envelope argument to the url
|
|
@@ -9,10 +9,15 @@ import { getSession } from './session';
|
|
|
9
9
|
* @returns {string} url
|
|
10
10
|
*/
|
|
11
11
|
function addVersionArg(url) {
|
|
12
|
-
if (!url || url.includes(
|
|
13
|
-
|
|
14
|
-
const version
|
|
15
|
-
|
|
12
|
+
if (!url || url.includes("_result=1")) return url;
|
|
13
|
+
|
|
14
|
+
const version =
|
|
15
|
+
process && process.env
|
|
16
|
+
? process.env.API_VERITREE_VERSION
|
|
17
|
+
? process.env.API_VERITREE_VERSION
|
|
18
|
+
: "5.0.0"
|
|
19
|
+
: "5.0.0" || "5.0.0";
|
|
20
|
+
const urlHasArgs = url.includes("?");
|
|
16
21
|
const urlVersionArg = urlHasArgs ? `&_v=${version}` : `?_v=${version}`;
|
|
17
22
|
|
|
18
23
|
return `${url}${urlVersionArg}`;
|
|
@@ -26,10 +31,10 @@ function addVersionArg(url) {
|
|
|
26
31
|
* @returns {object} data
|
|
27
32
|
*/
|
|
28
33
|
function getConfig(method, data, as) {
|
|
29
|
-
const isGet = method ===
|
|
34
|
+
const isGet = method === "get";
|
|
30
35
|
const isSpoofing = as;
|
|
31
36
|
const isFormData = data instanceof FormData;
|
|
32
|
-
const accessToken = `Bearer ${getCookie(
|
|
37
|
+
const accessToken = `Bearer ${getCookie("access_token")}`;
|
|
33
38
|
|
|
34
39
|
const config = {
|
|
35
40
|
method,
|
|
@@ -39,7 +44,7 @@ function getConfig(method, data, as) {
|
|
|
39
44
|
};
|
|
40
45
|
|
|
41
46
|
if (!isFormData) {
|
|
42
|
-
config.headers[
|
|
47
|
+
config.headers["Content-Type"] = "application/json";
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
// TODO: improve this ifs and elses
|
|
@@ -47,7 +52,7 @@ function getConfig(method, data, as) {
|
|
|
47
52
|
if (!data) data = {};
|
|
48
53
|
|
|
49
54
|
if (isFormData) {
|
|
50
|
-
if (isSpoofing) data.set(
|
|
55
|
+
if (isSpoofing) data.set("_method", as.toUpperCase());
|
|
51
56
|
config.body = data;
|
|
52
57
|
} else {
|
|
53
58
|
if (isSpoofing) data._method = as.toUpperCase();
|
|
@@ -60,8 +65,13 @@ function getConfig(method, data, as) {
|
|
|
60
65
|
|
|
61
66
|
export default class Api {
|
|
62
67
|
constructor(resource) {
|
|
63
|
-
this.baseUrl =
|
|
64
|
-
|
|
68
|
+
this.baseUrl =
|
|
69
|
+
process && process.env
|
|
70
|
+
? process.env.API_VERITREE_URL
|
|
71
|
+
? `${process.env.API_VERITREE_URL}/api`
|
|
72
|
+
: null
|
|
73
|
+
: null;
|
|
74
|
+
this.resource = resource ? resource : "";
|
|
65
75
|
this.orgId = null;
|
|
66
76
|
this.orgType = null;
|
|
67
77
|
}
|
|
@@ -106,7 +116,7 @@ export default class Api {
|
|
|
106
116
|
* @param {string} as - 'put' // necessary for updates because of how Laravel handles PUT requests
|
|
107
117
|
* @returns {promise}
|
|
108
118
|
*/
|
|
109
|
-
async update(id, data, as =
|
|
119
|
+
async update(id, data, as = "put", args) {
|
|
110
120
|
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
111
121
|
return await this.post(url, data, as);
|
|
112
122
|
}
|
|
@@ -120,7 +130,7 @@ export default class Api {
|
|
|
120
130
|
*/
|
|
121
131
|
async delete(id, args) {
|
|
122
132
|
const url = `${this.getUrl(id)}${this.getUrlParams(args)}`;
|
|
123
|
-
return await this.post(url, null,
|
|
133
|
+
return await this.post(url, null, "delete");
|
|
124
134
|
}
|
|
125
135
|
|
|
126
136
|
/**
|
|
@@ -140,7 +150,7 @@ export default class Api {
|
|
|
140
150
|
*/
|
|
141
151
|
async post(url, data, as, args) {
|
|
142
152
|
if (!url) url = `${this.getUrl()}${this.getUrlParams(args)}`;
|
|
143
|
-
return await this.unWrap(url,
|
|
153
|
+
return await this.unWrap(url, "post", data, as);
|
|
144
154
|
}
|
|
145
155
|
|
|
146
156
|
// ----------
|
|
@@ -151,7 +161,7 @@ export default class Api {
|
|
|
151
161
|
* @returns
|
|
152
162
|
*/
|
|
153
163
|
getUrl(id) {
|
|
154
|
-
id = id ? `/${id}` :
|
|
164
|
+
id = id ? `/${id}` : "";
|
|
155
165
|
return `${this.baseUrl}/${this.resource}${id}`;
|
|
156
166
|
}
|
|
157
167
|
|
|
@@ -164,14 +174,14 @@ export default class Api {
|
|
|
164
174
|
this.setOrg();
|
|
165
175
|
let isOrgLess = false;
|
|
166
176
|
let isOrgAs = false;
|
|
167
|
-
let orgIdParam =
|
|
168
|
-
let orgTypeParam =
|
|
177
|
+
let orgIdParam = "";
|
|
178
|
+
let orgTypeParam = "";
|
|
169
179
|
|
|
170
180
|
// while most of endpoints require an org id and type, some endpoints do not
|
|
171
181
|
if (args) {
|
|
172
|
-
isOrgLess = Object.hasOwn(args,
|
|
182
|
+
isOrgLess = Object.hasOwn(args, "orgless");
|
|
173
183
|
isOrgAs =
|
|
174
|
-
Object.hasOwn(args,
|
|
184
|
+
Object.hasOwn(args, "org_id_as") && Object.hasOwn(args, "org_type_as");
|
|
175
185
|
}
|
|
176
186
|
|
|
177
187
|
// a super admin user can create/edit data from other orgs
|
|
@@ -201,7 +211,7 @@ export default class Api {
|
|
|
201
211
|
* @param {object} data
|
|
202
212
|
* @returns {object} envelope
|
|
203
213
|
*/
|
|
204
|
-
async unWrap(url, method =
|
|
214
|
+
async unWrap(url, method = "get", data, as) {
|
|
205
215
|
url = addVersionArg(url);
|
|
206
216
|
const config = getConfig(method, data, as);
|
|
207
217
|
const response = await fetch(url, config);
|