Best patterns on HTTP responses
When designing API responses, it’s important to maintain consistency across endpoints. A good pattern is to wrap every responses in a standard structure with both data and metadata.
src/types/responseType.ts
export type ResponseType<T = any> = {
data: T;
message: string;
statusCode: number;
success: boolean;
};
Usage
Fetch functions must be typed
async function getUsers(): Promise<ResponseType<User[]>> {
const res = await fetch("/api/users");
return res.json();
}