Created
July 30, 2020 04:21
-
-
Save ihsaneddin/013b1a163c5f1b7104f3797aff772796 to your computer and use it in GitHub Desktop.
This file contains 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 {AxiosRequestConfig, AxiosResponse, AxiosInstance, AxiosError} from 'axios'; | |
import {MetaParams, ApiResponseErrorValidation, AxiosRequestConfigCustom} from "./types"; | |
import store from "@/store"; | |
import {default as axios} from "./api"; | |
const getMetaParams = (config: AxiosRequestConfigCustom): MetaParams|undefined => { | |
if(config.meta){ | |
return config.meta | |
} | |
return undefined | |
} | |
const setMetaParams = (config: AxiosRequestConfigCustom, meta: MetaParams) => { | |
config.meta = meta | |
return config | |
} | |
//implement this | |
const getToken = (): string => { | |
return store.getters["auth0/access_token"] | |
} | |
//append Bearer token to Authorization header | |
export const beforeRequestAppendAuthorizationHeader = (api: AxiosInstance, config: AxiosRequestConfigCustom): AxiosRequestConfigCustom => { | |
const token = getToken() | |
if(config.headers["Authorization"] == undefined){ | |
config.headers["Authorization"] = "Bearer" | |
if(token){ | |
config.headers["Authorization"] = `Bearer ${token}` | |
} | |
} | |
return config; | |
} | |
export const beforeRequestDataPrefixParams = (api: AxiosInstance, config: AxiosRequestConfigCustom): AxiosRequestConfigCustom => { | |
const data = config.data | |
if(data !== undefined){ | |
if(!data.data){ | |
const meta = data.meta | |
// if(meta){ | |
// delete data.meta | |
// } | |
config.data = { | |
data: data, | |
meta: meta || {} | |
} | |
} | |
} | |
return config; | |
} | |
export const beforeRequestRemoveDataPrefixParams = (api: AxiosInstance, config: AxiosRequestConfigCustom): AxiosRequestConfigCustom => { | |
const data = config.data | |
if(data !== undefined){ | |
if(data.data){ | |
const meta = data.meta | |
const formData = data.formData | |
config.data = data.data | |
config.meta = meta | |
config.formData = formData | |
} | |
} | |
return config; | |
} | |
export const beforeRequestStoreMetaParams = (api: AxiosInstance, config: AxiosRequestConfigCustom): AxiosRequestConfigCustom => { | |
if(config.data){ | |
if(config.data.meta){ | |
config = setMetaParams(config, config.data.meta) | |
delete config.data.meta | |
} | |
} | |
return config | |
} | |
const startLoading = (meta: MetaParams) => { | |
if(meta.loading){ | |
store.dispatch("loading/start", meta.loading) | |
} | |
} | |
export const beforeRequestStartLoading = (api: AxiosInstance, config: AxiosRequestConfigCustom): AxiosRequestConfig => { | |
const meta = getMetaParams(config) | |
if(meta){ | |
startLoading(meta) | |
} | |
return config | |
} | |
const endLoading = (meta: MetaParams) => { | |
if(meta.loading){ | |
store.dispatch("loading/finish", meta.loading) | |
} | |
} | |
export const afterResponse = (api: AxiosInstance, response: AxiosResponse): AxiosResponse => { | |
const meta = getMetaParams(response.config as AxiosRequestConfigCustom) | |
if(meta){ | |
endLoading(meta) | |
} | |
return response | |
} | |
export const afterResponseRemoveDataPrefix = (api: AxiosInstance, response: AxiosResponse): AxiosResponse => { | |
const data = response.data | |
if(data.data){ | |
response.data = data.data | |
} | |
return response | |
} | |
export const afterErrorEndLoading = (error: AxiosError) => { | |
const meta = getMetaParams(error.config as AxiosRequestConfigCustom ) | |
if(meta){ | |
endLoading(meta) | |
} | |
return error | |
} | |
//always place this interceptor on the bottom | |
export const afterErrorValidation = (error: AxiosError) => { | |
if(error.response){ | |
if(error.response.status == 422){ | |
const response = error.response.data as ApiResponseErrorValidation | |
return response | |
} | |
} | |
return error; | |
} | |
// display alert after error | |
const displayAlert = (alert: { | |
id?: string; | |
message?: string; | |
type?: string; }) => { | |
store.dispatch("alert/pushAlert", alert) | |
} | |
export const afterErrorAlert = (error: AxiosError) => { | |
const meta = getMetaParams(error.config as AxiosRequestConfigCustom) | |
if(meta){ | |
if (meta.skipErrorAlert){ | |
return error | |
} | |
} | |
const alertd = { type: "error", message: "An error has ocurred", id: "general" } | |
if(meta){ | |
if(meta.alert){ | |
alertd.id = meta.alert | |
} | |
} | |
if(error.request){ | |
alertd.message = error.message | |
} | |
if(error.response){ | |
if(error.response.status === 422) | |
alertd.message = "Unprocessable entity!" | |
else | |
alertd.message = error.response.data.error || error.response.data.message | |
} | |
displayAlert(alertd) | |
return error; | |
} | |
let isRefreshing = false; | |
let refreshSubscribers: Array<Function> = []; | |
const refresh = (): Promise<AxiosResponse> => { | |
return store.dispatch('authentication/refresh') | |
} | |
const subscribeTokenRefresh = (cb: Function) => { | |
refreshSubscribers.push(cb); | |
} | |
const onRrefreshed = (token: string) => { | |
refreshSubscribers.map(cb => cb(token)); | |
} | |
export const afterErrorUnauthorized = (error: AxiosError) => { | |
if(error.response){ | |
const originalRequest = error.config as AxiosRequestConfigCustom | |
if(error.response.status === 401 && originalRequest.url !== `${process.env.VUE_APP_API_URL}/api/v1/admin/auth0_sessions`){ | |
if(originalRequest.method && originalRequest.method.toLowerCase() === 'post'){ | |
if (!isRefreshing) { | |
isRefreshing = true; | |
refresh() | |
.then((res: AxiosResponse) => { | |
if(res){ | |
isRefreshing = false; | |
onRrefreshed(res.data); | |
} | |
refreshSubscribers = [] | |
}); | |
} | |
return new Promise((resolve, reject) => { | |
subscribeTokenRefresh((token: string) => { | |
originalRequest.headers['Authorization'] = `Bearer ${token}`; | |
resolve(axios(originalRequest)); | |
}); | |
}); | |
} | |
} | |
} | |
return; | |
} | |
import { serialize } from 'object-to-formdata'; | |
const dataToFormData = (object: any) => { | |
const options = { | |
/** | |
* include array indices in FormData keys | |
* defaults to false | |
*/ | |
indices: false, | |
/** | |
* treat null values like undefined values and ignore them | |
* defaults to false | |
*/ | |
nullsAsUndefineds: false, | |
/** | |
* convert true or false to 1 or 0 respectively | |
* defaults to false | |
*/ | |
booleansAsIntegers: false, | |
}; | |
return serialize(object, options) | |
} | |
const setFormData = (config: AxiosRequestConfigCustom, formdata: boolean) => { | |
config.formData = formdata | |
return config | |
} | |
export const beforeRequestStoreFormData = (api: AxiosInstance, config: AxiosRequestConfigCustom): AxiosRequestConfigCustom => { | |
if(config.data){ | |
if(config.data.formData){ | |
config = setFormData(config, config.data.formData) | |
delete config.data.formData | |
} | |
} | |
return config | |
} | |
export const uploadRequest = (api: AxiosInstance, config: AxiosRequestConfigCustom): AxiosRequestConfigCustom => { | |
if(config.formData === true){ | |
config.headers["Content-Type"] = 'multipart/form-data' | |
let formdata = config.data | |
formdata = dataToFormData(formdata) | |
config.data = formdata | |
} | |
return config | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment