diff --git a/README.md b/README.md index 2d25e07..462cf40 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ A versatile, type-safe API client designed for TypeScript applications. It simpl - **Authorization Support**: Automatically includes authorization tokens in requests. - **Customizable Unauthorized Access Handling**: Executes a callback function when encountering a 401 Unauthorized response, allowing for custom reaction strategies such as redirecting to a login page. - **Simplified API Requests**: Offers methods for common HTTP requests (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`) with a straightforward, promise-based API. +- **Flexible Response Handling**: Allows specifying the `responseType` for requests, enabling direct handling of blobs, JSON, and other formats. ## Installation @@ -46,7 +47,7 @@ Use the `ApiKitClient` instance to make API requests. Here are some examples: ### Fetching Data -Fetch a list of resources: +Fetch a list of resources specifying the expected response type: ```ts interface User { @@ -68,6 +69,14 @@ ApiKitClient.getOne('/users/1') .catch(error => console.error(error)); ``` +Fetch a blob data: + +```ts +ApiKitClient.get('/download', { responseType: 'blob' }) + .then(response => console.log(response.data)) // Expected to be of type Blob + .catch(error => console.error(error)); +``` + Fetch a paginated resource: ```ts @@ -86,7 +95,7 @@ const newUser: User = { email: 'john@example.com', }; -ApiKitClient.post('/users', newUser) +ApiKitClient.post('/users', newUser, { responseType: 'json' }) // by default is json, so you don't need to explicitly set it unless you need a different type (like 'blob', 'document', 'arraybuffer', or 'text'). .then(response => console.log(response.data)) // Expected to be of type User .catch(error => console.error(error)); ``` diff --git a/src/ApiKitClient.ts b/src/ApiKitClient.ts index 2302a80..b22aed1 100644 --- a/src/ApiKitClient.ts +++ b/src/ApiKitClient.ts @@ -1,8 +1,12 @@ -import axios, { AxiosInstance, AxiosResponse, AxiosError } from 'axios'; +import axios, { AxiosInstance, AxiosResponse, AxiosError, AxiosRequestConfig } from 'axios'; import { PaginatedResponse } from './types'; type UnauthorizationCallback = () => void; type AuthTokenCallback = () => Promise; +type Options = { + responseType?: AxiosRequestConfig['responseType'], + params?: URLSearchParams +} export class ApiKitClient { private static instance: AxiosInstance; @@ -41,38 +45,52 @@ export class ApiKitClient { } } - public static async get(endpoint: string, params?: URLSearchParams): Promise> { + private static createConfig(options?: Options): AxiosRequestConfig { + return { + responseType: options?.responseType, + params: options?.params + }; + } + + public static async get(endpoint: string, options?: Options): Promise> { this.checkInitialization(); - return this.instance!.get(endpoint, { params }); + const config = this.createConfig(options); + return this.instance!.get(endpoint, config); } - - public static async getOne(endpoint: string, params?: URLSearchParams): Promise> { + + public static async getOne(endpoint: string, options?: Options): Promise> { this.checkInitialization(); - return this.instance!.get(endpoint, { params }); + const config = this.createConfig(options); + return this.instance!.get(endpoint, config); } - public static async getPaginated(endpoint: string, params?: URLSearchParams): Promise>> { + public static async getPaginated(endpoint: string, options?: Options): Promise>> { this.checkInitialization(); - return this.instance!.get>(endpoint, { params }); + const config = this.createConfig(options); + return this.instance!.get>(endpoint, config); } - public static async post(endpoint: string, data: T): Promise> { + public static async post(endpoint: string, data: T, options?: Options): Promise> { this.checkInitialization(); - return this.instance!.post(endpoint, data); + const config = this.createConfig(options); + return this.instance!.post(endpoint, data, config); } - public static async put(endpoint: string, data: T): Promise> { + public static async put(endpoint: string, data: T, options?: Options): Promise> { this.checkInitialization(); - return this.instance!.put(endpoint, data); + const config = this.createConfig(options); + return this.instance!.put(endpoint, data, config); } - public static async patch(endpoint: string, data: T): Promise> { + public static async patch(endpoint: string, data: T, options?: Options): Promise> { this.checkInitialization(); - return this.instance!.patch(endpoint, data); + const config = this.createConfig(options); + return this.instance!.patch(endpoint, data, config); } - public static async delete(endpoint: string): Promise> { + public static async delete(endpoint: string, options?: Options): Promise> { this.checkInitialization(); - return this.instance!.delete(endpoint); + const config = this.createConfig(options); + return this.instance!.delete(endpoint, config); } }