RequestService

The RequestService class helps create a type-safe entrypoint to an API:

import {RequestService} from 'reqsrv';

let service = new RequestService<APISchema>(fetchData);

The constructor accepts a custom request handler fetchData. 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 might make use of fetch, node-fetch, axios, logging, default headers, or whatever necessary).

The purpose of RequestService is to offer a consistent 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 browserClient = new RequestService<APISchema>(clientHandler);
let serverClient = new RequestService<APISchema>(serverHandler);

Same API schema, same request interface, same reusable related code, different environment-specific request handlers.