Created
March 4, 2021 13:33
-
-
Save AlekseyA/3682a7e0d9c0866b46e5c729e98cd8de to your computer and use it in GitHub Desktop.
Axios wrapper and API samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios, { setToken } from 'api/axios'; | |
| import token from 'utils/token'; | |
| const path = '/auth'; | |
| const tokenResponseHandler = ({ access, refresh, user }) => { | |
| token.access.set(access); | |
| token.refresh.set(refresh); | |
| setToken(access); | |
| return user; | |
| }; | |
| /** | |
| * @param {{ | |
| username: string; | |
| password: string; | |
| * }} data | |
| */ | |
| export const signIn = (data) => { | |
| return axios.post(`${path}/sign-in`, data).then(tokenResponseHandler); | |
| }; | |
| /** | |
| * @param {{ | |
| email: string; | |
| login: string; | |
| password: string; | |
| * }} data | |
| */ | |
| export const signUp = (data) => { | |
| return axios.post(`${path}/sign-up`, data).then(tokenResponseHandler); | |
| }; | |
| export const forgotPassword = (email) => { | |
| return axios.post(`${path}/password-restore`, { email }); | |
| }; | |
| export const resetPassword = (data) => { | |
| return axios.post(`${path}/password-reset`, data); | |
| }; | |
| export const check = () => { | |
| if (!token.access.get()) { return; } | |
| return axios.get(`${path}/me`); | |
| }; | |
| export const signOut = () => { | |
| return axios.post(`${path}/logout`); | |
| }; | |
| export default { | |
| signIn, | |
| signUp, | |
| check, | |
| signOut, | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'axios'; | |
| import config from 'config'; | |
| import tokenUtil from 'utils/token'; | |
| import { logOut } from 'utils'; | |
| const customAxios = axios.create({ | |
| baseURL: config.apiUrl, | |
| // TO-DO: Return when we will have a cookie authorization | |
| // withCredentials: true, | |
| }); | |
| export const setToken = (token) => { | |
| customAxios.defaults.headers.Authorization = `Bearer ${token}`; | |
| }; | |
| customAxios.defaults.headers.app_id = config.appId; | |
| setToken(tokenUtil.access.get()); | |
| customAxios.interceptors.request.use((request) => { | |
| // TO-DO: Return when we will have a device check | |
| // request.headers.device = ''; | |
| return request; | |
| }); | |
| customAxios.interceptors.response.use( | |
| ({ data }) => data, | |
| async (err) => { | |
| if (err?.response?.data === 'expired') { | |
| if (!refreshPromise) { | |
| refreshPromise = refreshToken(); | |
| } | |
| await refreshPromise; | |
| // eslint-disable-next-line no-param-reassign | |
| err.config.headers.Authorization = `Bearer ${tokenUtil.access.get()}`; | |
| return customAxios(err.config); | |
| } | |
| return Promise.reject(err.response); | |
| }, | |
| ); | |
| let refreshPromise = null; | |
| const refreshToken = async () => { | |
| try { | |
| const { access, refresh } = await customAxios.post('/auth/refresh', { token: tokenUtil.refresh.get() }); | |
| tokenUtil.access.set(access); | |
| tokenUtil.refresh.set(refresh); | |
| setToken(tokenUtil.access); | |
| } catch (err) { | |
| logOut(); | |
| throw err; | |
| } finally { | |
| refreshPromise = null; | |
| } | |
| }; | |
| export default customAxios; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import axios from 'api/axios'; | |
| const path = '/user'; | |
| export const getOne = (id) => { | |
| return axios.get(`${path}/${id}`); | |
| }; | |
| /** | |
| * @param {{ | |
| pagination: { | |
| perPage: string; | |
| page: string; | |
| }; | |
| sort: { | |
| sortBy: string; | |
| sortDirection: "straight" | "reverse"; | |
| }; | |
| filter: { | |
| role: string; | |
| }; | |
| * }} params | |
| */ | |
| export const getList = (params) => { | |
| return axios.get(path, { params }); | |
| }; | |
| export const find = (params) => { | |
| return axios.get(`${path}/find`, { params }); | |
| }; | |
| export const update = (id, data) => { | |
| return axios.patch(`${path}/update/${id}`, data); | |
| }; | |
| export const deleteOne = (id) => { | |
| return axios.delete(`${path}/${id}`); | |
| }; | |
| export default { | |
| getOne, | |
| getList, | |
| find, | |
| update, | |
| deleteOne, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment