fix: move to mocked-exports rather than implicit unenv dep (#3232)

This commit is contained in:
Daniel Roe 2025-03-14 05:07:11 +00:00 committed by GitHub
parent 5623f87607
commit 1bf113a960
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 43 additions and 14 deletions

View file

@ -5,11 +5,8 @@ import {
defineLazyEventHandler,
toNodeListener,
} from 'h3'
import { fetchNodeRequestHandler } from 'node-mock-http'
import { createFetch } from 'ofetch'
import {
createCall,
createFetch as createLocalFetch,
} from 'unenv/runtime/fetch/index'
const handlers = [
{
@ -52,12 +49,16 @@ export default defineNuxtPlugin(async () => {
// @ts-expect-error TODO: fix
h3App.use(config.app.baseURL, router)
const localCall = createCall(toNodeListener(h3App) as any)
const localFetch = createLocalFetch(localCall, globalThis.fetch)
const nodeHandler = toNodeListener(h3App)
const localFetch: typeof fetch = async (input, init) => {
if (!input.toString().startsWith('/')) {
return globalThis.fetch(input.toString(), init)
}
return await fetchNodeRequestHandler(nodeHandler, input.toString(), init)
}
// @ts-expect-error error types are subtly different here in a future nitro version
globalThis.$fetch = createFetch({
// @ts-expect-error slight differences in api
fetch: localFetch,
Headers,
defaults: { baseURL: config.app.baseURL },