Fetch middleware for base URL resolution using standard URL constructor behavior.
npm install @qfetch/middleware-base-urlFetch middleware for resolving URLs against a configured base URL.
Resolves string request URLs against a base URL using the WHATWG URL Standard (new URL(input, base)). Relative URLs are resolved, absolute paths replace the pathname, and absolute URLs with schemes bypass the base entirely. URL and Request objects pass through unchanged.
Intended for use with @qfetch/core.
``bash`
npm install @qfetch/middleware-base-url
`typescript
import { withBaseUrl } from '@qfetch/middleware-base-url';
import { withHeaders } from '@qfetch/middleware-headers';
import { compose } from '@qfetch/core';
// Environment-aware API client
const apiBaseUrl = process.env.API_URL ?? 'https://api.example.com/v1/';
const api = compose(
withHeaders({ 'Accept': 'application/json' }),
withBaseUrl(apiBaseUrl),
)(fetch);
// Use relative paths throughout your application
const users = await api('users').then(r => r.json());
const user = await api('users/123').then(r => r.json());
const posts = await api('posts?limit=10').then(r => r.json());
``
For complete API reference, examples, and type definitions, see the API documentation.
- WHATWG URL Standard - Defines URL resolution behavior
- MDN: URL API - Browser implementation documentation