@acrool/react-fetcher 0.0.6-alpha.0 → 0.0.7-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/AuthStateProvider/index.d.ts +1 -1
- package/dist/FetcherProvider/FetcherProvider.d.ts +6 -6
- package/dist/FetcherProvider/index.d.ts +1 -1
- package/dist/FetcherProvider/types.d.ts +4 -0
- package/dist/acrool-react-fetcher.es.js +7 -3
- package/dist/acrool-react-fetcher.es.js.map +1 -1
- package/dist/fetchers/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,7 +57,7 @@ import "@acrool/react-fetcher/dist/index.css";
|
|
|
57
57
|
Wrap your app with `AuthStateProvider` and `AxiosClientProvider`. It is recommended to use `AppFetcherProvider` to automatically wrap all necessary providers:
|
|
58
58
|
|
|
59
59
|
```tsx
|
|
60
|
-
import AppFetcherProvider from '@/library/
|
|
60
|
+
import AppFetcherProvider from '@/library/react-fetcher';
|
|
61
61
|
|
|
62
62
|
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
|
|
63
63
|
<AppFetcherProvider>
|
|
@@ -73,7 +73,7 @@ For Redux Toolkit Query, create `baseApi.ts`:
|
|
|
73
73
|
```ts
|
|
74
74
|
import { createGraphQLFetcher } from '@acrool/react-fetcher';
|
|
75
75
|
import { createApi } from '@reduxjs/toolkit/query/react';
|
|
76
|
-
import { axiosInstance } from '@/library/
|
|
76
|
+
import { axiosInstance } from '@/library/react-fetcher';
|
|
77
77
|
|
|
78
78
|
export const baseApi = createApi({
|
|
79
79
|
reducerPath: 'api',
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { default as AuthStateProvider, useAuthState } from './AuthStateProvider';
|
|
2
|
-
export type { IAuthTokens } from './types';
|
|
2
|
+
export type { IAuthTokens, TOnForceLogout, TOnGetTokens, TOnRefreshToken, TOnSetTokens } from './types';
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { AxiosInstance } from 'axios';
|
|
2
2
|
import { default as React } from 'react';
|
|
3
|
-
import {
|
|
4
|
-
import { IFormatResponseErrorReturn, TGetResponseFormatError } from './types';
|
|
3
|
+
import { TCheckIsErrorResponse, TCheckIsRefreshTokenRequest, TGetResponseFormatError, TOnResponseError } from './types';
|
|
5
4
|
export declare const AxiosClientContext: React.Context<AxiosInstance | null>;
|
|
6
5
|
export declare const useAxiosClient: () => null;
|
|
7
6
|
interface IProps {
|
|
8
7
|
children: React.ReactNode;
|
|
9
8
|
axiosInstance: AxiosInstance;
|
|
10
|
-
checkIsRefreshTokenRequest?:
|
|
9
|
+
checkIsRefreshTokenRequest?: TCheckIsRefreshTokenRequest;
|
|
10
|
+
checkIsErrorResponse?: TCheckIsErrorResponse;
|
|
11
11
|
locale?: string;
|
|
12
12
|
getResponseFormatError?: TGetResponseFormatError;
|
|
13
|
-
|
|
13
|
+
onResponseError?: TOnResponseError;
|
|
14
14
|
authorizationPrefix?: string;
|
|
15
15
|
isDebug?: boolean;
|
|
16
16
|
}
|
|
@@ -19,7 +19,7 @@ interface IProps {
|
|
|
19
19
|
* @param children
|
|
20
20
|
* @param authTokensManager
|
|
21
21
|
* @param locale
|
|
22
|
-
* @param
|
|
22
|
+
* @param onResponseError
|
|
23
23
|
*/
|
|
24
|
-
declare const FetcherProvider: ({ children, axiosInstance, locale, getResponseFormatError,
|
|
24
|
+
declare const FetcherProvider: ({ children, axiosInstance, locale, getResponseFormatError, onResponseError, checkIsRefreshTokenRequest, checkIsErrorResponse, authorizationPrefix, isDebug, }: IProps) => import("react/jsx-runtime").JSX.Element;
|
|
25
25
|
export default FetcherProvider;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
export { default as FetcherProvider } from './FetcherProvider';
|
|
2
2
|
export { getGraphQLResponseFormatError, getRestFulResponseFormatError } from './utils';
|
|
3
|
-
export type { TGetResponseFormatError, IFormatResponseErrorReturn } from './types';
|
|
3
|
+
export type { TGetResponseFormatError, IFormatResponseErrorReturn, TOnResponseError, TCheckIsRefreshTokenRequest, TCheckIsErrorResponse } from './types';
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
|
|
2
|
+
import { IInternalRequestConfig } from '../fetchers/types';
|
|
2
3
|
export type TInterceptorRequest = (value: InternalAxiosRequestConfig<any>) => InternalAxiosRequestConfig<any> | Promise<InternalAxiosRequestConfig<any>>;
|
|
3
4
|
export type TInterceptorResponseSuccess = (value: AxiosResponse<any>) => AxiosResponse<any> | Promise<AxiosResponse<any>>;
|
|
4
5
|
export type TInterceptorResponseError = (error: AxiosError<any>) => AxiosResponse<any> | Promise<AxiosResponse<any>>;
|
|
@@ -9,3 +10,6 @@ export interface IFormatResponseErrorReturn {
|
|
|
9
10
|
args?: any;
|
|
10
11
|
}
|
|
11
12
|
export type TGetResponseFormatError = (axiosError?: AxiosError) => IFormatResponseErrorReturn;
|
|
13
|
+
export type TCheckIsRefreshTokenRequest = (config: IInternalRequestConfig) => boolean;
|
|
14
|
+
export type TCheckIsErrorResponse = (response: AxiosResponse) => boolean;
|
|
15
|
+
export type TOnResponseError = (error: IFormatResponseErrorReturn) => void;
|
|
@@ -696,8 +696,9 @@ const FetcherProvider = ({
|
|
|
696
696
|
axiosInstance,
|
|
697
697
|
locale = "en-US",
|
|
698
698
|
getResponseFormatError = getRestFulResponseFormatError,
|
|
699
|
-
|
|
699
|
+
onResponseError,
|
|
700
700
|
checkIsRefreshTokenRequest,
|
|
701
|
+
checkIsErrorResponse,
|
|
701
702
|
authorizationPrefix = "Bearer",
|
|
702
703
|
isDebug = false
|
|
703
704
|
}) => {
|
|
@@ -763,6 +764,9 @@ const FetcherProvider = ({
|
|
|
763
764
|
};
|
|
764
765
|
const interceptorsResponseSuccess = (response) => {
|
|
765
766
|
if (isDebug) u.info("[FetcherProvider] interceptorsResponseSuccess", { response });
|
|
767
|
+
if (checkIsErrorResponse && checkIsErrorResponse(response)) {
|
|
768
|
+
return Promise.reject(response);
|
|
769
|
+
}
|
|
766
770
|
return response;
|
|
767
771
|
};
|
|
768
772
|
const interceptorsResponseError = (axiosError) => {
|
|
@@ -771,8 +775,8 @@ const FetcherProvider = ({
|
|
|
771
775
|
const status = axiosError.status;
|
|
772
776
|
const responseFirstError = getResponseFormatError(axiosError);
|
|
773
777
|
if (isDebug) u.warning("[FetcherProvider] interceptorsResponseError", { status, responseFirstError });
|
|
774
|
-
if (
|
|
775
|
-
|
|
778
|
+
if (onResponseError && originalConfig.ignoreErrorCallback !== true) {
|
|
779
|
+
onResponseError(responseFirstError);
|
|
776
780
|
}
|
|
777
781
|
const isRefresh = originalConfig && checkIsRefreshTokenRequest ? checkIsRefreshTokenRequest(originalConfig) : false;
|
|
778
782
|
if (response && originalConfig) {
|