Skip to content

Commit

Permalink
types: fix type for event.$fetch (#2913)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Dec 3, 2024
1 parent 61e3f88 commit 066c478
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/runtime/internal/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, NitroFetchRequest>;
});

// https://github.com/nitrojs/nitro/issues/1420
event.waitUntil = (promise) => {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/internal/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown, NitroFetchRequest>;
});
event.context = incomingEvent.context;
event.context.cache = {
options: _opts,
Expand Down
34 changes: 19 additions & 15 deletions src/types/fetch/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,28 @@ export type ExtractedRouteMethod<
? Lowercase<Exclude<O["method"], undefined>>
: "get";

export type Base$Fetch<
DefaultT = unknown,
DefaultR extends NitroFetchRequest = NitroFetchRequest,
> = <
T = DefaultT,
R extends NitroFetchRequest = DefaultR,
O extends NitroFetchOptions<R> = NitroFetchOptions<R>,
>(
request: R,
opts?: O
) => Promise<
TypedInternalResponse<
R,
T,
NitroFetchOptions<R> extends O ? "get" : ExtractedRouteMethod<R, O>
>
>;

export interface $Fetch<
DefaultT = unknown,
DefaultR extends NitroFetchRequest = NitroFetchRequest,
> {
<
T = DefaultT,
R extends NitroFetchRequest = DefaultR,
O extends NitroFetchOptions<R> = NitroFetchOptions<R>,
>(
request: R,
opts?: O
): Promise<
TypedInternalResponse<
R,
T,
NitroFetchOptions<R> extends O ? "get" : ExtractedRouteMethod<R, O>
>
>;
> extends Base$Fetch<DefaultT, DefaultR> {
raw<
T = DefaultT,
R extends NitroFetchRequest = DefaultR,
Expand Down
4 changes: 2 additions & 2 deletions src/types/h3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response>;

export type H3Event$Fetch = $Fetch<unknown, NitroFetchRequest>;
export type H3Event$Fetch = Base$Fetch<unknown, NitroFetchRequest>;

declare module "h3" {
interface H3Event {
Expand Down

0 comments on commit 066c478

Please sign in to comment.