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:
- DNS —
fetch('http://auth.localhost:3000/')throwsENOTFOUNDbecause Node (undici) doesn't resolve*.localhostto loopback (nodejs/node#50871). - Host header — Node's fetch treats
Hostas 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/fetchUsage
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
- Clone the repository:
git clone https://github.com/constructive-io/dev-utils.git- Install dependencies:
cd dev-utils
pnpm install
pnpm build- Test the package of interest:
cd packages/<packagename>
pnpm test:watchCredits
🛠 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.