From 066c4789f9b36f9fd332760333710a2df4a52fa7 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 3 Dec 2024 11:08:34 +0000 Subject: [PATCH] types: fix type for `event.$fetch` (#2913) --- src/runtime/internal/app.ts | 4 ++-- src/runtime/internal/cache.ts | 4 ++-- src/types/fetch/fetch.ts | 34 +++++++++++++++++++--------------- src/types/h3.ts | 4 ++-- 4 files changed, 25 insertions(+), 21 deletions(-) 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 {