diff --git a/src/runtime/internal/app.ts b/src/runtime/internal/app.ts index 66414ebbe0..9c330a89f1 100644 --- a/src/runtime/internal/app.ts +++ b/src/runtime/internal/app.ts @@ -121,10 +121,10 @@ function createNitroApp(): NitroApp { // Assign bound fetch to context event.fetch = (req, init) => fetchWithEvent(event, req, init, { fetch: localFetch }); - event.$fetch = ((req, init) => + event.$fetch = (req, init) => fetchWithEvent(event, req, init as RequestInit, { fetch: $fetch as any, - })) as $Fetch; + }); // https://github.com/nitrojs/nitro/issues/1420 event.waitUntil = (promise) => { diff --git a/src/runtime/internal/cache.ts b/src/runtime/internal/cache.ts index 8d1bc93f51..838fafe969 100644 --- a/src/runtime/internal/cache.ts +++ b/src/runtime/internal/cache.ts @@ -369,10 +369,10 @@ export function defineCachedEventHandler< fetchWithEvent(event, url, fetchOptions, { fetch: useNitroApp().localFetch as any, }); - event.$fetch = ((url, fetchOptions) => + event.$fetch = (url, fetchOptions) => fetchWithEvent(event, url, fetchOptions as RequestInit, { fetch: globalThis.$fetch as any, - })) as $Fetch; + }); event.context = incomingEvent.context; event.context.cache = { options: _opts, diff --git a/src/types/fetch/fetch.ts b/src/types/fetch/fetch.ts index 5ce97b4ba3..28751405b5 100644 --- a/src/types/fetch/fetch.ts +++ b/src/types/fetch/fetch.ts @@ -66,24 +66,28 @@ export type ExtractedRouteMethod< ? Lowercase> : "get"; +export type Base$Fetch< + DefaultT = unknown, + DefaultR extends NitroFetchRequest = NitroFetchRequest, +> = < + T = DefaultT, + R extends NitroFetchRequest = DefaultR, + O extends NitroFetchOptions = NitroFetchOptions, +>( + request: R, + opts?: O +) => Promise< + TypedInternalResponse< + R, + T, + NitroFetchOptions extends O ? "get" : ExtractedRouteMethod + > +>; + export interface $Fetch< DefaultT = unknown, DefaultR extends NitroFetchRequest = NitroFetchRequest, -> { - < - T = DefaultT, - R extends NitroFetchRequest = DefaultR, - O extends NitroFetchOptions = NitroFetchOptions, - >( - request: R, - opts?: O - ): Promise< - TypedInternalResponse< - R, - T, - NitroFetchOptions extends O ? "get" : ExtractedRouteMethod - > - >; +> extends Base$Fetch { raw< T = DefaultT, R extends NitroFetchRequest = DefaultR, diff --git a/src/types/h3.ts b/src/types/h3.ts index 75ee342111..9316b93e79 100644 --- a/src/types/h3.ts +++ b/src/types/h3.ts @@ -3,14 +3,14 @@ import type { CaptureError, CapturedErrorContext, } from "nitropack/types"; -import type { $Fetch, NitroFetchRequest } from "./fetch/fetch"; +import type { Base$Fetch, NitroFetchRequest } from "./fetch/fetch"; export type H3EventFetch = ( request: NitroFetchRequest, init?: RequestInit ) => Promise; -export type H3Event$Fetch = $Fetch; +export type H3Event$Fetch = Base$Fetch; declare module "h3" { interface H3Event {