From d2f6530b53124fcca45b7fc77fdfb908ece0061f Mon Sep 17 00:00:00 2001 From: Mostafa Date: Wed, 13 Mar 2024 17:30:49 +0200 Subject: [PATCH 1/3] SCKT-3 | Global Error - Add global error callback for custom errors --- README.md | 13 ++++++++++--- src/ApiKitClient.ts | 16 ++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) 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..97a96db 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 = () => 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,9 +36,13 @@ 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(); } + if (this.globalErrorCallback) { + this.globalErrorCallback(); + } return Promise.reject(error); }); } From c4f1695ab8ad2bcfce1233795371cafb05450804 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Thu, 14 Mar 2024 21:28:07 +0200 Subject: [PATCH 2/3] SCKT-3 | Global Error - Make condition more restrective --- src/ApiKitClient.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ApiKitClient.ts b/src/ApiKitClient.ts index 97a96db..2122cee 100644 --- a/src/ApiKitClient.ts +++ b/src/ApiKitClient.ts @@ -39,8 +39,7 @@ export class ApiKitClient { const { status } = error.response; if (status === 401 && this.unauthorizationCallback) { this.unauthorizationCallback(); - } - if (this.globalErrorCallback) { + } else if (this.globalErrorCallback) { this.globalErrorCallback(); } return Promise.reject(error); From 7ecb706b36a42fa3649960dd349a6b39ef201de4 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Mon, 18 Mar 2024 10:46:28 +0200 Subject: [PATCH 3/3] SCKT-3 | Passing the error object to the globalErrorCallback --- src/ApiKitClient.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ApiKitClient.ts b/src/ApiKitClient.ts index 2122cee..b0fc530 100644 --- a/src/ApiKitClient.ts +++ b/src/ApiKitClient.ts @@ -2,7 +2,7 @@ import axios, { AxiosInstance, AxiosResponse, AxiosError } from 'axios'; import { PaginatedResponse } from './types'; type UnauthorizationCallback = () => void; -type GlobalErrorCallback = () => void; +type GlobalErrorCallback = (error: AxiosError) => void; type AuthTokenCallback = () => Promise; export class ApiKitClient { @@ -40,7 +40,7 @@ export class ApiKitClient { if (status === 401 && this.unauthorizationCallback) { this.unauthorizationCallback(); } else if (this.globalErrorCallback) { - this.globalErrorCallback(); + this.globalErrorCallback(error); } return Promise.reject(error); });