@opencitylabs/formio-sdk 1.1.2 → 1.1.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.
Files changed (2) hide show
  1. package/index.js +36 -18
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -9,39 +9,55 @@ class FormIoHelper {
9
9
 
10
10
  constructor() {
11
11
  this.token = null;
12
+ this.tokenPromise = null;
12
13
  this.basePath = null;
13
14
  this.init()
14
15
  }
15
16
 
16
17
  init() {
17
18
  this.basePath = this.getBaseUrl();
18
- this.token = this.getCurrentToken();
19
19
  }
20
20
 
21
21
  getCurrentLocale() {
22
22
  return document.documentElement.lang.toString();
23
23
  }
24
24
 
25
- async getCurrentToken() {
25
+ async getCurrentToken() {
26
26
  if (this.token) {
27
27
  return this.token;
28
28
  }
29
29
 
30
- const rawToken = sessionStorage.getItem("auth-token");
31
- if (!rawToken) {
32
- const response = await axios.get(this.basePath + '/api/session-auth')
33
- sessionStorage.setItem("auth-token",response?.data?.token);
34
- return response?.data?.token;
30
+ if (this.tokenPromise) {
31
+ return this.tokenPromise;
35
32
  }
36
33
 
37
- try {
38
- const parsedToken = JSON.parse(rawToken);
39
- if (typeof parsedToken === "string") {
40
- return parsedToken;
34
+ this.tokenPromise = (async () => {
35
+ const rawToken = sessionStorage.getItem("auth-token");
36
+ if (!rawToken) {
37
+ const response = await axios.get(this.basePath + '/api/session-auth');
38
+ const fetchedToken = response?.data?.token || null;
39
+ if (fetchedToken) {
40
+ sessionStorage.setItem("auth-token", fetchedToken);
41
+ }
42
+ return fetchedToken;
41
43
  }
42
- return parsedToken?.value || null;
43
- } catch (error) {
44
- return rawToken;
44
+
45
+ try {
46
+ const parsedToken = JSON.parse(rawToken);
47
+ if (typeof parsedToken === "string") {
48
+ return parsedToken;
49
+ }
50
+ return parsedToken?.value || null;
51
+ } catch (error) {
52
+ return rawToken;
53
+ }
54
+ })();
55
+
56
+ try {
57
+ this.token = await this.tokenPromise;
58
+ return this.token;
59
+ } finally {
60
+ this.tokenPromise = null;
45
61
  }
46
62
  }
47
63
 
@@ -59,11 +75,12 @@ class FormIoHelper {
59
75
  }
60
76
 
61
77
  async authenticatedCall(endPoint) {
62
- if(this.token){
78
+ const token = await this.getCurrentToken();
79
+ if(token){
63
80
  const response = await axios.get(this.basePath + '/api/' + endPoint, {
64
81
  headers: {
65
82
  "Content-Type": "application/json",
66
- "Authorization": "Bearer " + this.token
83
+ "Authorization": "Bearer " + token
67
84
  }
68
85
  })
69
86
  return response.data
@@ -71,11 +88,12 @@ class FormIoHelper {
71
88
  }
72
89
 
73
90
  async authenticatedPOSTCall(endPoint,params) {
74
- if(this.token){
91
+ const token = await this.getCurrentToken();
92
+ if(token){
75
93
  const response = await axios.post(this.basePath + '/api/' + endPoint, JSON.stringify(params),{
76
94
  headers: {
77
95
  "Content-Type": "application/json",
78
- "Authorization": "Bearer " + this.token
96
+ "Authorization": "Bearer " + token
79
97
  }
80
98
  })
81
99
  return response.data
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencitylabs/formio-sdk",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "description": "Node/browser SDK helper for Form.io APIs",
5
5
  "main": "index.js",
6
6
  "exports": {