@outlawdesigns/loe-rest-client 1.0.2 → 1.0.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.
- package/package.json +1 -1
- package/src/core.js +26 -13
package/package.json
CHANGED
package/src/core.js
CHANGED
|
@@ -8,26 +8,36 @@ import createHoldingbay from './models/holdingBay.js';
|
|
|
8
8
|
import createMovies from './models/movie.js';
|
|
9
9
|
import createSongs from './models/song.js';
|
|
10
10
|
|
|
11
|
+
|
|
11
12
|
export function createApiClient(baseURL, requestedScope){
|
|
12
13
|
const oauthScope = requestedScope;
|
|
13
14
|
const oauthResource = baseURL;
|
|
14
|
-
const oauthRefreshBuffer = 300;
|
|
15
15
|
const axiosInstance = axios.create({baseURL:baseURL});
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
let onRefreshCallback;
|
|
17
|
+
authClient.onTokenUpdate((tokenSet)=>{
|
|
18
|
+
axiosInstance.defaults.headers.common['Authorization'] = `Bearer ${tokenSet.access_token}`;
|
|
19
|
+
if(onRefreshCallback){
|
|
20
|
+
onRefreshCallback(tokenSet);
|
|
21
|
+
}
|
|
18
22
|
});
|
|
19
23
|
axiosInstance.interceptors.request.use(async (config)=>{
|
|
20
|
-
|
|
24
|
+
let token = authClient.getAccessToken();
|
|
21
25
|
if(!token) throw new Error(`Authenticate before making API calls.`);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
let user;
|
|
27
|
+
try{
|
|
28
|
+
user = await authClient.verifyAccessToken(token,[oauthResource]);
|
|
29
|
+
}catch(err){
|
|
30
|
+
if(err.code === 'ERR_JWT_EXPIRED'){
|
|
31
|
+
const refreshToken = authClient.getRefreshToken();
|
|
32
|
+
if(refreshToken){
|
|
33
|
+
await authClient.refreshToken(oauthScope,[oauthResource]);
|
|
34
|
+
}else{
|
|
35
|
+
await authClient.clientCredentialFlow(oauthScope,[oauthResource]);
|
|
36
|
+
}
|
|
37
|
+
token = authClient.getAccessToken();
|
|
38
|
+
config.headers['Authorization'] = `Bearer ${token}`;
|
|
29
39
|
}else{
|
|
30
|
-
|
|
40
|
+
throw err;
|
|
31
41
|
}
|
|
32
42
|
}
|
|
33
43
|
return config;
|
|
@@ -39,6 +49,9 @@ export function createApiClient(baseURL, requestedScope){
|
|
|
39
49
|
holdingBay:createHoldingbay(axiosInstance),
|
|
40
50
|
episodes:createEpisodes(axiosInstance),
|
|
41
51
|
docs:createDocs(axiosInstance),
|
|
42
|
-
anime:createAnime(axiosInstance)
|
|
52
|
+
anime:createAnime(axiosInstance),
|
|
53
|
+
onRefresh(cb){
|
|
54
|
+
onRefreshCallback = cb;
|
|
55
|
+
}
|
|
43
56
|
}
|
|
44
57
|
}
|