@qfetch/middleware-base-url
v0.1.0
Published
Fetch middleware for base URL resolution using standard URL constructor behavior.
Maintainers
Readme
@qfetch/middleware-base-url
Fetch middleware for resolving URLs against a configured base URL.
Overview
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.
Installation
npm install @qfetch/middleware-base-urlQuick Start
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());Documentation
For complete API reference, examples, and type definitions, see the API documentation.
Standards References
- WHATWG URL Standard - Defines URL resolution behavior
- MDN: URL API - Browser implementation documentation
