T8 SDK Factory
RequestService
The RequestService class helps create a thin type-safe entrypoint to an API:
import { RequestService } from "@t8/sdk-factory";
let service = new RequestService<APISchema>(requestHandler);
The constructor accepts a custom requestHandler. A specific request handler isn't built into the package, since it can vary in many ways depending on the purpose and environment of the application: it can make use of fetch, axios, logging, default headers, or whatever necessary.
The purpose of RequestService is to offer a single environment-agnostic interface to request handling on top of a typed API schema.
🔹 A typed schema allows to prevalidate request inputs at compile-time and highlight mismatches in a type-aware IDE.
🔹 The environment-agnostic interface works consistently throughout the client and the server:
let service = new RequestService<APISchema>(browserHandler);
let service = new RequestService<APISchema>(serverHandler);
The same API with different environment-specific request handlers under the hood results in reusable isomorphic code:
// browser or server
let { ok, status, body } = await service.send("GET /items");
← Intro | Schema definition →