HTTP clients
Four clients, four different worlds. The generator does not put one behind another: every generated function calls the client it was made for, with that client’s own types, and returns what that client returns. Nothing is parsed, unwrapped, retried or re-thrown on the way, and no setting of the client is changed.
http |
what a generated function is | returns | client.ts |
needs |
|---|---|---|---|---|
api-client (default) |
apiClient.get(…) with api-client’s own RequestConfig |
api-client’s IRes<T> (data, statusCode, message, …) |
export const apiClient = createClient(); — finds its base URL itself |
@mrzr/api-client |
axios |
http.get<T>(url, config) on your instance, with an AxiosRequestConfig |
AxiosResponse<T>; a failed status is an AxiosError |
export const http = axios.create({ baseURL: import.meta.env.VITE_API_URL }); |
axios |
fetch |
fetch(url, init) with a RequestInit |
the platform’s Response; a failed status resolves, as fetch does. Functions only (target: "requests"): a raw Response is a stream read once and a 404 is not an error, so there is nothing a query hook could cache or retry |
export const baseUrl = import.meta.env.VITE_API_URL ?? ""; |
nothing |
angular |
a method of an @Injectable service per tag, over the injected HttpClient |
an Observable<T>; a failed status is an HttpErrorResponse |
export const baseUrl = import.meta.env.VITE_API_URL ?? ""; |
@angular/common, @angular/core, rxjs |
client.ts is yours: interceptors, tokens and plugins go on the instance there. Every client reads where the API lives from the environment, each in its own way: api-client finds it itself (it reads VITE_API_URL, among others) and gets nothing written; axios takes it as baseURL of its instance, and fetch and Angular’s HttpClient, which can neither find it nor hold it, start every call from the baseUrl that client.ts exports. The variable they read is VITE_API_URL=https://host/api/v1 unless you name another with baseUrlEnv (--base-url-env). The routes are written without apiPrefix, so that base URL carries it. import.meta.env is Vite’s; on another bundler change that one line of client.ts.
- api-client: an operation of a backend service is
apiClient.service("files").get(…)through api-client’sservicesplugin (client.tsis written withservices(API_SERVICE_BASE_URLS)only when the document has several services — one service is just the client’s own base URL, and no plugin is written; add the plugin by hand to aclient.tsthat exists). A header parameter is not an argument of these functions — headers are api-client’s own (createClient({ headers }), orheadersin the config of one call).deletetakes no body, so aDELETEoperation with one is left out and listed. - axios: the call’s config is the second-to-last argument of a body-less call, spread into the request; a query list is written the way axios writes it (
ids[]=a) unless the instance has aparamsSerializer; the run lists each such parameter. - fetch: every call starts from the
baseUrlofclient.ts(or its own service’s base URL). The query string is built in the function from the document’s parameters (URLSearchParams), a JSON body isJSON.stringify-ed with itsContent-Type, and reading the answer (response.json()) is yours. - Angular: hooks are Angular Query’s (
library: "angular-query"), and a hook injects the tag’s service (inject(PetsService)).HttpClienttakes noAbortSignal; cancel by unsubscribing from theObservable.
A multipart body is typed FormData (with @mrzr/api-client, <Body> | FormData: the call carries the client’s own isFormData: true, which writes a plain object as the form, a file or a list per entry and an object as JSON, and sends a FormData as it is); a form body is URLSearchParams (HttpParams for Angular) — the bodies each client sends as they are — so you build them yourself.
What the generator does decide, from the document, is the request: the route (filled in), the query, the headers it declares, the body’s shape and Content-Type where the client would otherwise label it wrongly, and responseType (text or blob) for an answer that is not JSON.