@acrool/react-fetcher 0.0.6 → 0.0.7-alpha.1

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.
@@ -1,15 +1,16 @@
1
1
  import { AxiosInstance } from 'axios';
2
2
  import { default as React } from 'react';
3
- import { TCheckIsRefreshTokenRequest, TGetResponseFormatError, TResponseOnErrorCallback } from './types';
3
+ import { TCheckIsErrorResponse, TCheckIsRefreshTokenRequest, TGetResponseFormatError, TOnResponseError } from './types';
4
4
  export declare const AxiosClientContext: React.Context<AxiosInstance | null>;
5
5
  export declare const useAxiosClient: () => null;
6
6
  interface IProps {
7
7
  children: React.ReactNode;
8
8
  axiosInstance: AxiosInstance;
9
9
  checkIsRefreshTokenRequest?: TCheckIsRefreshTokenRequest;
10
+ checkIsErrorResponse?: TCheckIsErrorResponse;
10
11
  locale?: string;
11
12
  getResponseFormatError?: TGetResponseFormatError;
12
- onError?: TResponseOnErrorCallback;
13
+ onResponseError?: TOnResponseError;
13
14
  authorizationPrefix?: string;
14
15
  isDebug?: boolean;
15
16
  }
@@ -18,7 +19,7 @@ interface IProps {
18
19
  * @param children
19
20
  * @param authTokensManager
20
21
  * @param locale
21
- * @param onError
22
+ * @param onResponseError
22
23
  */
23
- declare const FetcherProvider: ({ children, axiosInstance, locale, getResponseFormatError, onError, checkIsRefreshTokenRequest, authorizationPrefix, isDebug, }: IProps) => import("react/jsx-runtime").JSX.Element;
24
+ declare const FetcherProvider: ({ children, axiosInstance, locale, getResponseFormatError, onResponseError, checkIsRefreshTokenRequest, checkIsErrorResponse, authorizationPrefix, isDebug, }: IProps) => import("react/jsx-runtime").JSX.Element;
24
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, TResponseOnErrorCallback, TCheckIsRefreshTokenRequest } from './types';
3
+ export type { TGetResponseFormatError, IFormatResponseErrorReturn, TOnResponseError, TCheckIsRefreshTokenRequest, TCheckIsErrorResponse } from './types';
@@ -11,4 +11,5 @@ export interface IFormatResponseErrorReturn {
11
11
  }
12
12
  export type TGetResponseFormatError = (axiosError?: AxiosError) => IFormatResponseErrorReturn;
13
13
  export type TCheckIsRefreshTokenRequest = (config: IInternalRequestConfig) => boolean;
14
- export type TResponseOnErrorCallback = (error: IFormatResponseErrorReturn) => void;
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
- onError,
699
+ onResponseError,
700
700
  checkIsRefreshTokenRequest,
701
+ checkIsErrorResponse,
701
702
  authorizationPrefix = "Bearer",
702
703
  isDebug = false
703
704
  }) => {
@@ -763,16 +764,20 @@ 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) => {
773
+ if (isDebug) u.danger("[FetcherProvider] interceptorsResponseError", { axiosError });
769
774
  const response = axiosError.response;
770
775
  const originalConfig = axiosError.config;
771
776
  const status = axiosError.status;
772
777
  const responseFirstError = getResponseFormatError(axiosError);
773
- if (isDebug) u.warning("[FetcherProvider] interceptorsResponseError", { status, responseFirstError });
774
- if (onError && originalConfig.ignoreErrorCallback !== true) {
775
- onError(responseFirstError);
778
+ if (isDebug) u.warning("[FetcherProvider] interceptorsResponseError(2)", { status, responseFirstError });
779
+ if (onResponseError && originalConfig.ignoreErrorCallback !== true) {
780
+ onResponseError(responseFirstError);
776
781
  }
777
782
  const isRefresh = originalConfig && checkIsRefreshTokenRequest ? checkIsRefreshTokenRequest(originalConfig) : false;
778
783
  if (response && originalConfig) {