The @webroute/client package provides a simple and flexible abstraction for interacting with any REST API type-safely.
It works with any fetching library of your choice and is designed to expedite client-side development as well as reducing errors and mistakes, by better enforcing your API contract.
The typed client is an extremely lightweight, so-called "headless" client-side interface for your API. It is entirely unopinionated about how fetching is done and merely wires up end-to-end type safety, without getting in your way.
react-app/api-client.ts
// Create a lightweight client instanceconst =<>()({: async (, : { : string }) => { return { : "123" }; },});// Each API operation is represented as a single, flat key// in the form `{METHOD} {path}`const =("GET /posts");const =({ : { : 12 } }, { : "bar" });
Our custom fetcher is responsible for executing our request and parsing our response, if need be. It allows for flexible options and return types.
import { createUrl } from "@webroute/client";const myFetcher = async ( // Contains info about path, method, body etc. config, // Any additional parameters are exposed via the client instance // and fully typed. opts: AxiosRequestConfig) => { const { path, method, body, params, query } = config; // ... Fetch using any method...};