-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpotifyClient.js
30 lines (23 loc) · 886 Bytes
/
SpotifyClient.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Axios from 'axios';
import applyCaseMiddleware from 'axios-case-converter';
import createAuthRefreshInterceptor from 'axios-auth-refresh';
import {setupCache} from 'axios-cache-adapter';
import {updateAccessToken} from './src/helpers/authHelpers';
export const BASE_URL = 'https://api.spotify.com/';
const cache = setupCache({maxAge: 15 * 60 * 1000});
const SpotifyClient = applyCaseMiddleware(
Axios.create({
baseURL: BASE_URL,
adapter: cache.adapter,
}),
);
SpotifyClient.interceptors.response.use(response => response.data);
const refreshAuthLogic = async failedRequest => {
const {payload} = await updateAccessToken();
if (payload) {
const {accessToken} = payload;
failedRequest.response.config.headers.Authorization = `Bearer ${accessToken}`;
}
};
createAuthRefreshInterceptor(SpotifyClient, refreshAuthLogic);
export default SpotifyClient;