Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
15 changes: 13 additions & 2 deletions src/ApiKitClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;

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 });

Expand All @@ -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);
});
Expand Down