JSPM

@constructive-io/fetch

1.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1416
  • Score
    100M100P100Q109921F
  • License MIT

Isomorphic fetch wrapper — resolves *.localhost subdomains and preserves Host headers across Node.js and browsers

Package Exports

  • @constructive-io/fetch
  • @constructive-io/fetch/esm/index.browser.js
  • @constructive-io/fetch/esm/index.js
  • @constructive-io/fetch/index.browser.js
  • @constructive-io/fetch/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@constructive-io/fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

@constructive-io/fetch

Isomorphic fetch that resolves *.localhost subdomains and preserves Host headers across Node.js and browsers.

Why

Node.js has two issues with *.localhost subdomains:

  1. DNSfetch('http://auth.localhost:3000/') throws ENOTFOUND because Node (undici) doesn't resolve *.localhost to loopback (nodejs/node#50871).
  2. Host header — Node's fetch treats Host as a forbidden header and silently drops it, breaking server-side subdomain routing.

Browsers handle both correctly. This package fixes both in Node by using node:http/node:https for *.localhost URLs and passing everything else through to globalThis.fetch.

Install

npm install @constructive-io/fetch

Usage

import { createFetch } from '@constructive-io/fetch';

const fetch = createFetch();

// Works in Node.js — DNS resolves, Host header preserved
const res = await fetch('http://auth.localhost:3000/graphql', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({ query: '{ currentUser { id } }' }),
});

API

createFetch(): typeof globalThis.fetch

Returns a fetch function. In Node.js, *.localhost URLs are handled via node:http/node:https. Everything else delegates to globalThis.fetch. The result is cached.

isLocalhostSubdomain(hostname: string): boolean

Returns true for *.localhost subdomains (e.g. auth.localhost), false for bare localhost and everything else.


Development

Setup

  1. Clone the repository:
git clone https://github.com/constructive-io/dev-utils.git
  1. Install dependencies:
cd dev-utils
pnpm install
pnpm build
  1. Test the package of interest:
cd packages/<packagename>
pnpm test:watch

Credits

🛠 Built by the Constructive team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on GitHub.

Disclaimer

AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.

No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.