@ronin-nexus/poly
Poly is an HTTP client library designed to interact with backend services, offering a simple, flexible, and modern API for making requests to servers. Build on top of ky
Key Features of Poly:
Promise-Based: Like many modern HTTP clients, Poly uses promises, making it easier to handle asynchronous requests with .then() and .catch() or async/await syntax.
Request Interception: It provides a way to intercept requests and responses, allowing you to modify headers, log data, or handle authentication tokens centrally before the request is sent or after a response is received.
Efficient Handling of HTTP Methods: Supports all major HTTP methods like GET, POST, PUT, DELETE, and allows for payloads in JSON or other formats.
Middleware Support: Allows the use of middleware to enhance or modify requests and responses, enabling custom logic to be applied across all requests (e.g., authentication, logging, or error handling).
Installation
pnpm add @ronin-nexus/polyIn a workspace add it to your package.json file under dependencies
//...
"dependencies": {
//..
"@ronin-nexus/poly": "workspace:*"
}Usage
KyClient
A lightweight HTTP client that provides a simple and flexible API for making requests to servers.
declare class KyClient {
private readonly opts;
constructor(opts: PolyOptions);
withEndpoint(url: string): URL;
get<T>(url: string, options?: Options): APIResponse<T>;
post<T>(url: string, options?: Options): APIResponse<T>;
put<T>(url: string, options?: Options): APIResponse<T>;
delete<T>(url: string, options?: Options): APIResponse<T>;
handleResponse<T>(resp: ResponsePromise): APIResponse<T>;
}Handlers
Consisits of a set of handlers that can be used to intercept requests and responses, allowing you to modify headers, log data, or handle authentication tokens centrally before the request is sent or after a response is received.