qfetch
    Preparing search index...

    Function withBaseUrl

    • Middleware that resolves string fetch requests against a base URL.

      Resolves string request URLs against the provided base URL using the WHATWG URL Standard (new URL(input, base)). URL and Request objects are passed through unchanged.

      Parameters

      • baseUrl: string | URL

        The base URL to resolve against (string or URL instance).

      Returns MiddlewareExecutor

      Resolution behavior for string inputs:

      • Relative URLs ("users") → resolved against the base URL
      • Absolute paths ("/users") → replaces the base pathname (keeps protocol + host)
      • Absolute URLs ("https://other.com") → base URL is ignored entirely

      Trailing slash matters: "https://api.example.com/v1/" (with slash) appends paths, while "https://api.example.com/v1" (without slash) replaces the last segment.

      Input type preservation: String inputs remain strings, URL objects remain URLs, and Request objects remain Requests.

      When the base URL is invalid or a string input cannot be resolved.

      import { withBaseUrl } from "@qfetch/middleware-base-url";

      const qfetch = withBaseUrl("https://api.example.com/v1/")(fetch);

      await qfetch("users"); // → https://api.example.com/v1/users
      await qfetch("/users"); // → https://api.example.com/users
      await qfetch("https://x.com"); // → https://x.com (base ignored)