@juriadams/timed-fetch
v0.0.3
Published
A tiny fetch wrapper that tracks request duration, latency, and server timings.
Readme
@juriadams/timed-fetch
A tiny fetch wrapper that tracks request duration, latency, and server timings.
Installation
Add @juriadams/timed-fetch to your project using your package manager of choice (bun, npm, pnpm, ...).
bun add @juriadams/timed-fetchUsage
The simplest way to use this library is to replace fetch with timedFetch:
import { timedFetch } from '@juriadams/timed-fetch';
const res = await timedFetch('https://api.acme.co/v1/health');
// ^ `TimedResponse`
console.log(res.timing);
// ^ `Timing`
// `Timing`:
// {
// total: 21.2, // Total duration in milliseconds.
// network: 11.4, // Calculated network latency in milliseconds.
// server: { // Values parsed from returned `Server-Timing` header.
// total: 9.8,
// auth: 1.7,
// db: 8.1,
// },
// }You can also integrate timedFetch seamlessly with other libraries, such as Hono:
import { timedFetch } from '@juriadams/timed-fetch';
import { Hono } from 'hono';
import { hc } from 'hono/client';
const api = new Hono().get('/v1/health', (c) =>
c.json({ data: null, error: null })
);
type Api = typeof api;
const apiClient = hc<Api>('https://api.acme.co');
// Pass `timedFetch` as the `fetch` option.
const res = await apiClient.v1.health.$get({}, { fetch: timedFetch });
// ^ `TimedResponse` ...