Created
December 21, 2023 16:47
-
-
Save thepuskar/e3266dabb6b7defae09920acedb84b10 to your computer and use it in GitHub Desktop.
Request handler in react js + Typescript for API call using axios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AxiosError, AxiosResponse } from 'axios' | |
/** | |
* A type representing a function that takes an optional object of type T and returns a promise resolving to an axios Response with a generic type V. | |
*/ | |
type BaseRequest<T, V> = (params?: T) => Promise<AxiosResponse<V>> | |
/** | |
* A type representing the shape of a successful API response, which includes a 'code' property set to 'success', and a 'data' property containing the payload. | |
*/ | |
type SuccessResponse<V> = { | |
code: 'success' | |
data: V | |
} | |
/** | |
* A type representing the shape of an unsuccessful API response, which includes a 'code' property set to 'error', and an 'error' property containing the error details. The error can be of any type, but it is recommended to use the AxiosError type provided by axios library. | |
*/ | |
type ErrorResponse<E = AxiosError> = { | |
code: 'error' | |
error: E | |
} | |
/** | |
* A type representing a promise that will eventually resolve to either a SuccessResponse or an ErrorResponse. | |
*/ | |
type BaseResponse<V, E> = Promise<SuccessResponse<V> | ErrorResponse<E>> | |
/** | |
* This higher-order function accepts a base request function as its argument and returns another function that wraps around the original one with additional behavior such as handling errors and returning appropriate responses. It allows you to create new functions with added functionality based on existing ones. In this case, it adds error handling logic to your base request function. | |
* @template T - Parameters object type | |
* @template V - Data returned from the API endpoint | |
* @template E - Error type | |
* @param {BaseRequest<T, V>} request - Function making HTTP requests using axios | |
* @return {(params?: T) => Promise<SuccessResponse<V>|ErrorResponse<E>>} - Returns a wrapped version of the input request function with error handling | |
*/ | |
export const requestHandler = | |
<T, V, E = AxiosError>( | |
request: BaseRequest<T, V>, | |
): ((params?: T) => Promise<SuccessResponse<V> | ErrorResponse<E>>) => | |
async (params?: T): BaseResponse<V, E> => { | |
try { | |
const response = await request(params) | |
return { | |
code: 'success', | |
data: response.data, | |
} | |
} catch (error) { | |
return { | |
code: 'error', | |
error: error as E, | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example | |
*/ | |
import axios from "axios"; | |
import { requestHandler } from "./requestHandler"; | |
interface User { | |
id: number; | |
name: string; | |
} | |
interface GetUsersParams { | |
limit?: number; | |
page?: number; | |
} | |
export const getUsers = requestHandler<GetUsersParams, User[]>((params) => | |
axios.get("/api/users", { params }) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment