fix : message d'erreur
This commit is contained in:
@@ -21,6 +21,7 @@ export type ApiClient = {
|
|||||||
export type ApiFetchOptions<ResponseType extends 'json' | 'blob'> =
|
export type ApiFetchOptions<ResponseType extends 'json' | 'blob'> =
|
||||||
FetchOptions<ResponseType> & {
|
FetchOptions<ResponseType> & {
|
||||||
toast?: boolean
|
toast?: boolean
|
||||||
|
toastOn401?: boolean
|
||||||
toastTitle?: string
|
toastTitle?: string
|
||||||
toastErrorMessage?: string
|
toastErrorMessage?: string
|
||||||
toastSuccessMessage?: string
|
toastSuccessMessage?: string
|
||||||
@@ -102,9 +103,31 @@ export const useApi = (): ApiClient => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
async onResponseError({ response, error, options }) {
|
async onResponseError({ response, error, options }) {
|
||||||
|
const apiOptions = options as ApiFetchOptions<'json'>
|
||||||
if (response?.status === 401) {
|
if (response?.status === 401) {
|
||||||
const requestUrl = typeof options?.url === 'string' ? options.url : ''
|
const requestUrl = typeof options?.url === 'string' ? options.url : ''
|
||||||
if (!requestUrl.includes('/login_check') && !requestUrl.includes('/logout')) {
|
const isLoginCheck = requestUrl.includes('/login_check')
|
||||||
|
const isLogout = requestUrl.includes('/logout')
|
||||||
|
const shouldToast401 = apiOptions?.toastOn401 === true && apiOptions?.toast !== false
|
||||||
|
|
||||||
|
if (shouldToast401) {
|
||||||
|
const errorKey = apiOptions?.toastErrorKey
|
||||||
|
const errorMessage =
|
||||||
|
errorKey ? (te(errorKey) ? t(errorKey) : errorKey) : ''
|
||||||
|
const extractedMessage = extractErrorMessage(error, response?._data)
|
||||||
|
const message =
|
||||||
|
apiOptions?.toastErrorMessage ||
|
||||||
|
errorMessage ||
|
||||||
|
extractedMessage ||
|
||||||
|
'Une erreur est survenue.'
|
||||||
|
|
||||||
|
toast.error({
|
||||||
|
title: apiOptions?.toastTitle ?? 'Erreur',
|
||||||
|
message
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isLoginCheck && !isLogout) {
|
||||||
if (!isHandlingUnauthorized) {
|
if (!isHandlingUnauthorized) {
|
||||||
isHandlingUnauthorized = true
|
isHandlingUnauthorized = true
|
||||||
auth.clearSession()
|
auth.clearSession()
|
||||||
@@ -115,10 +138,10 @@ export const useApi = (): ApiClient => {
|
|||||||
isHandlingUnauthorized = false
|
isHandlingUnauthorized = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiOptions = options as ApiFetchOptions<'json'>
|
|
||||||
if (apiOptions?.toast === false) {
|
if (apiOptions?.toast === false) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ export const getCurrentUser = () => {
|
|||||||
export const login = (username: string, password: string) => {
|
export const login = (username: string, password: string) => {
|
||||||
const api = useApi()
|
const api = useApi()
|
||||||
return api.post('/login_check', { username, password }, {
|
return api.post('/login_check', { username, password }, {
|
||||||
|
toastOn401: true,
|
||||||
toastErrorKey: 'errors.auth.login'
|
toastErrorKey: 'errors.auth.login'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user