I actually extracted this into its own npm package:
github.com/davecalnan/typesafe-api/tree/main/pack…
yarn add @davekit/typesafe-api-client
It's basically just:
export const api = createApiClient({
baseUrl: "your-app.com/api"
});
const result = api.get("/some-path");
const onClick = () => api.delete("/some-other-path");
I pair it with @davekit/typesafe-api and generate types for all my backend routes that I can then import into the frontend and get type-safety over the API. But that needs a whole blog post of its own to get into.