diff --git a/README.md b/README.md index 2d25e07..48c4432 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,14 @@ const authTokenCallback = () => { }; const unauthorizationCallback = () => { - // Implement redirection to login page here + // Implement unauthorization logic here }; -ApiKitClient.initialize(BASE_URL, authTokenCallback, unauthorizationCallback); +const globalErrorCallback = () => { + // Implement global error logic here +}; + +ApiKitClient.initialize(BASE_URL, authTokenCallback, unauthorizationCallback, globalErrorCallback); ``` ## Making Requests @@ -124,4 +128,7 @@ ApiKitClient.delete('/users/1') ``` ## Handling Unauthorized Access -The unauthorized access callback provided during initialization will be called automatically for any request that receives a 401 Unauthorized response, allowing you to handle such scenarios globally (e.g., redirecting the user to a login page). +The `unauthorizationCallback` provided during initialization will be called automatically for any request that receives a 401 Unauthorized response, allowing you to handle such scenarios globally (e.g., redirecting the user to a login page). + +## Handling Global Error +The `globalErrorCallback` provided during initialization will be called if needed to handle custom errors, allowing you to handle such scenarios globally (e.g., throwing a custom error text). \ No newline at end of file diff --git a/src/ApiKitClient.ts b/src/ApiKitClient.ts index 2302a80..b0fc530 100644 --- a/src/ApiKitClient.ts +++ b/src/ApiKitClient.ts @@ -2,16 +2,24 @@ import axios, { AxiosInstance, AxiosResponse, AxiosError } from 'axios'; import { PaginatedResponse } from './types'; type UnauthorizationCallback = () => void; +type GlobalErrorCallback = (error: AxiosError) => void; type AuthTokenCallback = () => Promise; export class ApiKitClient { private static instance: AxiosInstance; private static authTokenCallback: AuthTokenCallback; private static unauthorizationCallback: UnauthorizationCallback; + private static globalErrorCallback: GlobalErrorCallback; - public static initialize(baseURL: string, authTokenCallback: AuthTokenCallback, unauthorizationCallback?: UnauthorizationCallback): void { + public static initialize( + baseURL: string, + authTokenCallback: AuthTokenCallback, + unauthorizationCallback?: UnauthorizationCallback, + globalErrorCallback?: GlobalErrorCallback, + ): void { this.authTokenCallback = authTokenCallback; this.unauthorizationCallback = unauthorizationCallback; + this.globalErrorCallback = globalErrorCallback; this.instance = axios.create({ baseURL }); @@ -28,8 +36,11 @@ export class ApiKitClient { }); this.instance.interceptors.response.use((response: AxiosResponse) => response, (error: AxiosError) => { - if (error.response?.status === 401 && this.unauthorizationCallback) { + const { status } = error.response; + if (status === 401 && this.unauthorizationCallback) { this.unauthorizationCallback(); + } else if (this.globalErrorCallback) { + this.globalErrorCallback(error); } return Promise.reject(error); });