Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: fix type for event.$fetch #2913

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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