This repository provides a simple yet powerful HTTP client for making requests, measuring performance, and recording metrics for observability. The implementation includes support for custom headers, URL templating, and Prometheus metrics.
- Customizable HTTP requests with support for all common HTTP methods.
- Metrics tracking for request counts, durations, and stage-specific timings.
- URL templating for dynamic path construction.
- Error handling with structured response objects.
- Memoization for efficient URL template parsing.
Install the dependencies using npm:
npm install
Create an instance of HTTPClient
with a base URL and default headers:
import { HTTPClient } from "./index";
const client = new HTTPClient({
baseUrl: "https://api.example.com",
headers: {
Authorization: "Bearer YOUR_API_KEY",
},
});
Use the request
method to send HTTP requests. Supported methods include GET
, POST
, PUT
, DELETE
, and more.
(async () => {
try {
const result = await client.request("GET", "/users/{id}", { id: 123 });
console.log(result);
} catch (error) {
console.error("Request failed:", error);
}
})();
The metrics.ts
file provides Prometheus-compatible metrics tracking for HTTP requests. To register the metrics with your Prometheus client, use the registerMetrics
function.
import { registerMetrics } from "./metrics";
import { Registry } from "prom-client";
const registry = new Registry();
registerMetrics(registry);
Expose these metrics in your application for monitoring:
app.get("/metrics", async (req, res) => {
res.set("Content-Type", registry.contentType);
res.send(await registry.metrics());
});
The changelog with instructions on upgrading can be found here