API
generateApi
It takes the base URL with the headers and a config of all endpoints, and returns a typed api function. The generic type describes the body, response, path and query of every endpoint.
utils/api.ts
import { generateApi } from "react-better-html";
type User = { id: number; name: string };
export const api = generateApi<{
getUsers: {
body: undefined;
response: User[];
query: { page: number; search?: string };
};
getUser: {
body: undefined;
response: User;
path: [number];
};
createUser: {
body: { name: string };
response: User;
};
}>(
{
baseUrl: "https://api.example.com", // without a trailing slash
getHeaders: {
Authorization: () => `Bearer ${localStorage.getItem("token")}`,
},
},
{
getUsers: { method: "GET", path: "/users", includeHeaders: ["Authorization"] },
getUser: { method: "GET", path: "/users", includeHeaders: ["Authorization"] },
createUser: { method: "POST", path: "/users", includeHeaders: ["Authorization"] },
},
);
We advice to create an api.ts file inside a utils folder and export the api function from it.
Calling an endpoint
const { data: users } = await api("getUsers", { query: { page: 1 } }); // GET /users?page=1
const { data: user } = await api("getUser", { path: [12] }); // GET /users/12
await api("createUser", { body: { name: "John" } }); // POST /users
The response contains data, statusCode, statusText, headers, url, ok and redirected.
Endpoint options
method- the HTTP methodpath- the path of the endpoint. Thepatharray from the call is appended with/includeHeaders- which of thegetHeadersare sent with this endpointheaders- static headers for this endpointbodyWithFormData- sends the body asFormData(for file uploads)responseIsOctetStream- returns the response as aBlob(for file downloads)meta- any data, available ingetAdditionalHeaders
getAdditionalHeaders receives the endpoint config and returns headers for every request. Headers can also be passed per call with the headers key.
isApiError
When the response is not ok (or the request fails) the promise is rejected with an APIError object. isApiError checks if a caught error is one.
import { isApiError } from "react-better-html";
try {
await api("createUser", { body: { name: "" } });
} catch (error) {
if (isApiError<{ message: string }>(error)) {
console.log(error.statusCode, error.data.message);
}
}
tip
When devMode is enabled every request and response is logged in the console.