Skip to content

Commit 0f7c3ea

Browse files
authored
refactor: use node-mock-http instead of unenv v1 for local fetch (#3069)
1 parent 5a80d3d commit 0f7c3ea

5 files changed

Lines changed: 47 additions & 18 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
"mime": "^4.0.6",
143143
"mlly": "^1.7.4",
144144
"node-fetch-native": "^1.6.6",
145+
"node-mock-http": "^1.0.0",
145146
"ofetch": "^1.4.1",
146147
"ohash": "^1.1.4",
147148
"openapi-typescript": "^7.6.0",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/internal/app.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import type {
1717
NitroRuntimeHooks,
1818
} from "nitropack/types";
1919
import type { NitroAsyncContext } from "nitropack/types";
20-
import type { $Fetch, NitroFetchRequest } from "nitropack/types";
2120
import { Headers, createFetch } from "ofetch";
2221
import {
23-
createCall,
24-
createFetch as createLocalFetch,
25-
} from "unenv/runtime/fetch/index";
22+
fetchNodeRequestHandler,
23+
callNodeRequestHandler,
24+
type AbstractRequest,
25+
} from "node-mock-http";
2626
import errorHandler from "#nitro-internal-virtual/error-handler";
2727
import { plugins } from "#nitro-internal-virtual/plugins";
2828
import { handlers } from "#nitro-internal-virtual/server-handlers";
@@ -85,14 +85,20 @@ function createNitroApp(): NitroApp {
8585
preemptive: true,
8686
});
8787

88-
// Create local fetch callers
89-
const localCall = createCall(toNodeListener(h3App) as any);
90-
// prettier-ignore
91-
const _localFetch = createLocalFetch(localCall, (u, i) => globalThis.fetch(u, i));
92-
const localFetch: typeof fetch = (input, init) =>
93-
_localFetch(input as RequestInfo, init as any).then((response) =>
94-
normalizeFetchResponse(response)
95-
);
88+
// Create local fetch caller
89+
const nodeHandler = toNodeListener(h3App);
90+
const localCall = (aRequest: AbstractRequest) =>
91+
callNodeRequestHandler(nodeHandler, aRequest);
92+
const localFetch: typeof fetch = (input, init) => {
93+
if (!input.toString().startsWith("/")) {
94+
return globalThis.fetch(input, init);
95+
}
96+
return fetchNodeRequestHandler(
97+
nodeHandler,
98+
input as string /* TODO */,
99+
init
100+
).then((response) => normalizeFetchResponse(response));
101+
};
96102
const $fetch = createFetch({
97103
fetch: localFetch,
98104
Headers,

src/types/runtime/nitro.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import type { App as H3App, H3Event, Router } from "h3";
22
import type { Hookable } from "hookable";
33
import type { NitroRuntimeHooks as NitroTypesRuntimeHooks } from "nitropack";
4-
import type {
5-
createCall,
6-
createFetch as createLocalFetch,
7-
} from "unenv/runtime/fetch/index";
4+
import type { AbstractRequest, AbstractResponse } from "node-mock-http";
85

96
export interface NitroApp {
107
h3App: H3App;
118
router: Router;
129
hooks: Hookable<NitroRuntimeHooks>;
13-
localCall: ReturnType<typeof createCall>;
14-
localFetch: ReturnType<typeof createLocalFetch>;
10+
localCall: (aRequest: AbstractRequest) => Promise<AbstractResponse>;
11+
localFetch: (
12+
req: string | URL | Request,
13+
init?: RequestInit & AbstractRequest
14+
) => Promise<Response>;
1515
captureError: CaptureError;
1616
}
1717

test/fixture/routes/fetch.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default eventHandler(async (event) => {
2+
const nitroApp = useNitroApp();
3+
return {
4+
$fetch: await fetch("/api/hey").then((r) => r.text()),
5+
"event.fetch": await event.fetch("/api/hey").then((r) => r.text()),
6+
"event.$fetch": await event.$fetch("/api/hey"),
7+
"nitroApp.localFetch": await nitroApp
8+
.localFetch("/api/hey")
9+
.then((r) => r.text()),
10+
"nitroApp.localCall": await nitroApp
11+
.localCall({ url: "/api/hey" })
12+
.then((r) => r.body),
13+
};
14+
});

0 commit comments

Comments
 (0)