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.

TypescriptIconsrc/types/responseType.ts
export type ResponseType<T = any> = {
  data: T;
  message: string;
  statusCode: number;
  success: boolean;
};

LinkIconUsage

Fetch functions must be typed

TypescriptIcon
async function getUsers(): Promise<ResponseType<User[]>> {
  const res = await fetch("/api/users");
  return res.json();
}