-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
226 additions
and
92 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// eslint-disable-next-line import/no-named-default | ||
import { default as axiosLib } from 'axios'; | ||
|
||
import { getAuthToken, getGuestToken } from '@leaa/dashboard/src/utils'; | ||
|
||
export const fetcher = axiosLib; | ||
|
||
fetcher.defaults.timeout = 10000; | ||
|
||
fetcher.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'; | ||
fetcher.defaults.headers.common.Authorization = getAuthToken() ? `Bearer ${getAuthToken()}` : ''; | ||
fetcher.defaults.headers.common.Guthorization = getGuestToken(); | ||
fetcher.defaults.headers.common.Duthorization = 'a'; | ||
|
||
// fetcher.interceptors.request.use( | ||
// (res) => { | ||
// // console.log('RRRRRRRR', res); | ||
// | ||
// return res; | ||
// }, | ||
// (err) => { | ||
// console.warn('❌ HTTP REQ ERROR', err); | ||
// | ||
// // ERROR Normalify statusCode > 400 | ||
// return Promise.resolve(err?.response); | ||
// }, | ||
// ); | ||
// | ||
// fetcher.interceptors.response.use( | ||
// (res) => { | ||
// console.log('XXXXXXXX?????????/', res); | ||
// | ||
// return res; | ||
// }, | ||
// (err) => { | ||
// // FINALLY | ||
// console.warn('❌ HTTP RES ERROR', err); | ||
// | ||
// errorMsg(err.message); | ||
// | ||
// // TIMEOUT | ||
// if (err?.code === 'ECONNABORTED') { | ||
// // console.error('❌ HTTP RESPONSE TIMEOUT', error.response); | ||
// | ||
// return { | ||
// data: err, | ||
// status: 'timeout', | ||
// }; | ||
// } | ||
// | ||
// // ERROR Normalify statusCode > 400 | ||
// return Promise.resolve(err?.response); | ||
// }, | ||
// ); | ||
|
||
export const setAjaxToken = (token: string) => { | ||
fetcher.defaults.headers.common.Authorization = `Bearer ${token}`; | ||
console.log('🔑 Token is Updated.'); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './history.lib'; | ||
export * from './fetcher.lib'; | ||
export * from './use-fetch.lib'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import useSWR, { ConfigInterface, responseInterface } from 'swr'; | ||
import { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios'; | ||
import { ICrudListQueryParams } from '@leaa/dashboard/src/interfaces'; | ||
|
||
import { fetcher } from './fetcher.lib'; | ||
|
||
// extends axios | ||
interface IAxiosRequestConfig extends AxiosRequestConfig { | ||
crudQuery?: ICrudListQueryParams; | ||
} | ||
|
||
interface IAxiosResponse<T = any> extends AxiosResponse { | ||
config: IAxiosRequestConfig; | ||
} | ||
|
||
interface IReturn<Data, Error> | ||
extends Pick< | ||
responseInterface<IAxiosResponse<Data>, AxiosError<Error>>, | ||
'isValidating' | 'revalidate' | 'error' | 'mutate' | ||
> { | ||
data: Data | undefined; | ||
loading?: boolean; | ||
response?: IAxiosResponse<Data> | undefined; | ||
} | ||
|
||
export interface IConfig<Data = unknown, Error = unknown> | ||
extends Omit<ConfigInterface<IAxiosResponse<Data>, AxiosError<Error>>, 'initialData'> { | ||
initialData?: Data; | ||
} | ||
|
||
export function useFetch<Data = unknown, Error = unknown>( | ||
request: IAxiosRequestConfig | null, | ||
{ initialData, ...config }: IConfig<Data, Error> = {}, | ||
): IReturn<Data, Error> { | ||
const { data: response, error, isValidating, revalidate, mutate } = useSWR<AxiosResponse<Data>, AxiosError<Error>>( | ||
request?.url ? JSON.stringify(request) : null, | ||
/** | ||
* NOTE: Typescript thinks `request` can be `null` here, but the fetcher | ||
* function is actually only called by `useSWR` when it isn't. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
(): any => fetcher(request!), | ||
{ | ||
revalidateOnFocus: false, | ||
// revalidateOnReconnect: false, | ||
initialData: initialData && { | ||
status: 200, | ||
statusText: 'InitialData', | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
config: request!, | ||
headers: {}, | ||
data: initialData, | ||
}, | ||
...config, | ||
}, | ||
); | ||
|
||
return { | ||
data: response?.data, | ||
// response, | ||
error, | ||
isValidating, | ||
loading: isValidating, | ||
revalidate, | ||
mutate, | ||
}; | ||
} |
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
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
Oops, something went wrong.