@opencitylabs/formio-sdk 1.1.2 → 1.1.4
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 +43 -28
- package/package.json +4 -3
package/index.js
CHANGED
|
@@ -1,47 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { baseUrl } = require("./BaseUrl");
|
|
6
|
-
const { jwtDecode } = require("jwt-decode");
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import get from "lodash.get";
|
|
3
|
+
import { baseUrl } from "./BaseUrl.js";
|
|
4
|
+
import { jwtDecode } from "jwt-decode";
|
|
7
5
|
|
|
8
6
|
class FormIoHelper {
|
|
9
7
|
|
|
10
8
|
constructor() {
|
|
11
9
|
this.token = null;
|
|
10
|
+
this.tokenPromise = null;
|
|
12
11
|
this.basePath = null;
|
|
13
12
|
this.init()
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
init() {
|
|
17
16
|
this.basePath = this.getBaseUrl();
|
|
18
|
-
this.token = this.getCurrentToken();
|
|
19
17
|
}
|
|
20
18
|
|
|
21
19
|
getCurrentLocale() {
|
|
22
20
|
return document.documentElement.lang.toString();
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
async getCurrentToken() {
|
|
26
24
|
if (this.token) {
|
|
27
25
|
return this.token;
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const response = await axios.get(this.basePath + '/api/session-auth')
|
|
33
|
-
sessionStorage.setItem("auth-token",response?.data?.token);
|
|
34
|
-
return response?.data?.token;
|
|
28
|
+
if (this.tokenPromise) {
|
|
29
|
+
return this.tokenPromise;
|
|
35
30
|
}
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
40
|
-
|
|
32
|
+
this.tokenPromise = (async () => {
|
|
33
|
+
const rawToken = sessionStorage.getItem("auth-token");
|
|
34
|
+
if (!rawToken) {
|
|
35
|
+
const response = await axios.get(this.basePath + '/api/session-auth');
|
|
36
|
+
const fetchedToken = response?.data?.token || null;
|
|
37
|
+
if (fetchedToken) {
|
|
38
|
+
sessionStorage.setItem("auth-token", fetchedToken);
|
|
39
|
+
}
|
|
40
|
+
return fetchedToken;
|
|
41
41
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const parsedToken = JSON.parse(rawToken);
|
|
45
|
+
if (typeof parsedToken === "string") {
|
|
46
|
+
return parsedToken;
|
|
47
|
+
}
|
|
48
|
+
return parsedToken?.value || null;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
return rawToken;
|
|
51
|
+
}
|
|
52
|
+
})();
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
this.token = await this.tokenPromise;
|
|
56
|
+
return this.token;
|
|
57
|
+
} finally {
|
|
58
|
+
this.tokenPromise = null;
|
|
45
59
|
}
|
|
46
60
|
}
|
|
47
61
|
|
|
@@ -59,11 +73,12 @@ class FormIoHelper {
|
|
|
59
73
|
}
|
|
60
74
|
|
|
61
75
|
async authenticatedCall(endPoint) {
|
|
62
|
-
|
|
76
|
+
const token = await this.getCurrentToken();
|
|
77
|
+
if(token){
|
|
63
78
|
const response = await axios.get(this.basePath + '/api/' + endPoint, {
|
|
64
79
|
headers: {
|
|
65
80
|
"Content-Type": "application/json",
|
|
66
|
-
"Authorization": "Bearer " +
|
|
81
|
+
"Authorization": "Bearer " + token
|
|
67
82
|
}
|
|
68
83
|
})
|
|
69
84
|
return response.data
|
|
@@ -71,11 +86,12 @@ class FormIoHelper {
|
|
|
71
86
|
}
|
|
72
87
|
|
|
73
88
|
async authenticatedPOSTCall(endPoint,params) {
|
|
74
|
-
|
|
89
|
+
const token = await this.getCurrentToken();
|
|
90
|
+
if(token){
|
|
75
91
|
const response = await axios.post(this.basePath + '/api/' + endPoint, JSON.stringify(params),{
|
|
76
92
|
headers: {
|
|
77
93
|
"Content-Type": "application/json",
|
|
78
|
-
"Authorization": "Bearer " +
|
|
94
|
+
"Authorization": "Bearer " + token
|
|
79
95
|
}
|
|
80
96
|
})
|
|
81
97
|
return response.data
|
|
@@ -549,7 +565,6 @@ function createFormioHelper(options = {}) {
|
|
|
549
565
|
return new FormIoHelper(options);
|
|
550
566
|
}
|
|
551
567
|
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
module.exports.createFormioHelper = createFormioHelper;
|
|
568
|
+
export { FormIoHelper, createFormioHelper };
|
|
569
|
+
export { FormIoHelper as FormioHelper };
|
|
570
|
+
export default FormIoHelper;
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opencitylabs/formio-sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
4
4
|
"description": "Node/browser SDK helper for Form.io APIs",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"main": "index.js",
|
|
6
7
|
"exports": {
|
|
7
8
|
".": "./index.js"
|
|
@@ -12,7 +13,7 @@
|
|
|
12
13
|
"README.md"
|
|
13
14
|
],
|
|
14
15
|
"scripts": {
|
|
15
|
-
"test": "node -e \"
|
|
16
|
+
"test": "node --input-type=module -e \"import('./index.js')\""
|
|
16
17
|
},
|
|
17
18
|
"keywords": [
|
|
18
19
|
"formio",
|
|
@@ -26,6 +27,6 @@
|
|
|
26
27
|
"axios": "^1.13.6",
|
|
27
28
|
"jwt-decode": "^4.0.0",
|
|
28
29
|
"lodash.get": "^4.4.2",
|
|
29
|
-
"sweetalert2": "^11.26.
|
|
30
|
+
"sweetalert2": "^11.26.23"
|
|
30
31
|
}
|
|
31
32
|
}
|