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

fix: type event.$fetch and event.fetch #1343

Merged
merged 11 commits into from
Jun 28, 2023
12 changes: 7 additions & 5 deletions src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
createApp,
createRouter,
eventHandler,
H3Event,
lazyEventHandler,
Router,
toNodeListener,
Expand All @@ -16,10 +15,11 @@ import {
createFetch as createLocalFetch,
} from "unenv/runtime/fetch/index";
import { createHooks, Hookable } from "hookable";
import { NitroRuntimeHooks } from "./types";
import type { NitroRuntimeHooks } from "./types";
import { useRuntimeConfig } from "./config";
import { cachedEventHandler } from "./cache";
import { createRouteRulesHandler, getRouteRulesForPath } from "./route-rules";
import type { $Fetch, NitroFetchRequest } from "nitropack";
import { plugins } from "#internal/nitro/virtual/plugins";
import errorHandler from "#internal/nitro/virtual/error-handler";
import { handlers } from "#internal/nitro/virtual/server-handlers";
Expand Down Expand Up @@ -69,9 +69,11 @@ function createNitroApp(): NitroApp {
}
// Assign bound fetch to context
event.fetch = (req, init) =>
fetchWithEvent(event, req as any, init, { fetch: localFetch });
event.$fetch = (req, init) =>
fetchWithEvent(event, req as any, init, { fetch: $fetch });
fetchWithEvent(event, req, init, { fetch: localFetch });
event.$fetch = ((req, init) =>
fetchWithEvent(event, req, init as RequestInit, {
fetch: $fetch,
})) as $Fetch<unknown, NitroFetchRequest>;
})
);

Expand Down
11 changes: 1 addition & 10 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { H3Event } from "h3";
import type { RenderResponse } from "./renderer";

import type { RenderResponse } from "./types";
export type { NitroApp } from "./app";
export type {
CacheEntry,
Expand All @@ -11,15 +11,6 @@ export type {
export type { NitroAppPlugin } from "./plugin";
export type { RenderResponse, RenderHandler } from "./renderer";

declare module "h3" {
interface H3Event {
/** @experimental Calls fetch with same context and request headers */
fetch: typeof globalThis.fetch;
/** @experimental Calls fetch with same context and request headers */
$fetch: typeof globalThis.fetch;
}
}

export interface NitroRuntimeHooks {
"render:response": (
response: Partial<RenderResponse>,
Expand Down
19 changes: 19 additions & 0 deletions src/types/h3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { NitroFetchRequest, $Fetch } from "./fetch";

export type H3EventFetch = (
request: NitroFetchRequest,
init?: RequestInit
) => Promise<Response>;

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

declare module "h3" {
interface H3Event {
/** @experimental Calls fetch with same context and request headers */
fetch: H3EventFetch;
/** @experimental Calls fetch with same context and request headers */
$fetch: H3Event$Fetch;
}
}

export {};
Comment on lines +1 to +19
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locating these types here will not result in augmenting H3Event in a Nuxt/Nitro project

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing build archive against nitro-deploys, seems to be working fine:

image image

1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./fetch";
export * from "./global";
export * from "./h3";
export * from "./serialize";
export * from "./nitro";
export * from "./handler";
Expand Down
4 changes: 2 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './dist/index'
export * from './dist/runtime/types'
export * from "./dist/index";
export * from "./dist/runtime/types";