@computools/react-native-template-controller 1.0.9 → 1.0.10
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
CHANGED
package/template/package.json
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import axios, {type AxiosInstance} from 'axios';
|
1
|
+
import axios, {HttpStatusCode, type AxiosInstance} from 'axios';
|
2
2
|
|
3
3
|
import {AnyJson} from '../types/any-json';
|
4
4
|
|
@@ -15,6 +15,7 @@ export class Http {
|
|
15
15
|
private baseURL: string;
|
16
16
|
private headers: Headers;
|
17
17
|
private axiosInstance: AxiosInstance;
|
18
|
+
private refreshToken: (() => Promise<string>) | null = null;
|
18
19
|
|
19
20
|
public constructor(baseURL: string = '', headers: Headers = {}) {
|
20
21
|
this.baseURL = baseURL;
|
@@ -63,5 +64,33 @@ export class Http {
|
|
63
64
|
|
64
65
|
private createAxiosInstance() {
|
65
66
|
this.axiosInstance = axios.create({baseURL: this.baseURL, headers: this.headers});
|
67
|
+
|
68
|
+
if (this.refreshToken !== null) {
|
69
|
+
this.axiosInstance.interceptors.response.use(
|
70
|
+
response => response,
|
71
|
+
async error => {
|
72
|
+
const originalRequest = error.config;
|
73
|
+
|
74
|
+
if ([HttpStatusCode.Unauthorized, HttpStatusCode.Forbidden].includes(error.response.status) && !originalRequest._retry) {
|
75
|
+
const accessToken = await this.refreshToken!();
|
76
|
+
|
77
|
+
this.addBearerToken(accessToken);
|
78
|
+
this.createAxiosInstance();
|
79
|
+
|
80
|
+
const originalRequestWithUpdatedToken = {
|
81
|
+
...originalRequest,
|
82
|
+
headers: {
|
83
|
+
...originalRequest.headers,
|
84
|
+
...this.headers,
|
85
|
+
},
|
86
|
+
_retry: true,
|
87
|
+
};
|
88
|
+
|
89
|
+
return this.axiosInstance.request(originalRequestWithUpdatedToken);
|
90
|
+
}
|
91
|
+
return Promise.reject(error);
|
92
|
+
},
|
93
|
+
);
|
94
|
+
}
|
66
95
|
}
|
67
96
|
}
|