Unverified Commit 7a0db3a1 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[testmode] Fix types of `wrapRequestHandler` (#80055)

Was previously shadowed because we used an uncast `require` which results in `any`.

Fixes
```
Argument of type '<T>(request: NextRequestHint, fn: () => T) => T' is not assignable to parameter of type '(req: Request, fn: () => T) => T'.
  Types of parameters 'request' and 'req' are incompatible.
    Type 'Request' is missing the following properties from type 'NextRequestHint': sourcePage, fetchMetrics, request, respondWith, and 6 more.ts(2345)
```
parent 0b0d03a0
...@@ -5,8 +5,8 @@ export function interceptTestApis(): () => void { ...@@ -5,8 +5,8 @@ export function interceptTestApis(): () => void {
return interceptFetch(global.fetch) return interceptFetch(global.fetch)
} }
export function wrapRequestHandler<T>( export function wrapRequestHandler<T, TRequest extends Request>(
handler: (req: Request, fn: () => T) => T handler: (req: TRequest, fn: () => T) => T
): (req: Request, fn: () => T) => T { ): (req: TRequest, fn: () => T) => T {
return (req, fn) => withRequestContext(req, reader, () => handler(req, fn)) return (req, fn) => withRequestContext(req, reader, () => handler(req, fn))
} }
...@@ -634,7 +634,7 @@ export async function initialize(opts: { ...@@ -634,7 +634,7 @@ export async function initialize(opts: {
if (config.experimental.testProxy) { if (config.experimental.testProxy) {
// Intercept fetch and other testmode apis. // Intercept fetch and other testmode apis.
const { wrapRequestHandlerWorker, interceptTestApis } = const { wrapRequestHandlerWorker, interceptTestApis } =
require('next/dist/experimental/testmode/server') as typeof import('next/src/experimental/testmode/server') require('next/dist/experimental/testmode/server') as typeof import('../../experimental/testmode/server')
requestHandler = wrapRequestHandlerWorker(requestHandler) requestHandler = wrapRequestHandlerWorker(requestHandler)
interceptTestApis() interceptTestApis()
// We treat the intercepted fetch as "original" fetch that should be reset to during HMR. // We treat the intercepted fetch as "original" fetch that should be reset to during HMR.
......
...@@ -345,9 +345,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -345,9 +345,8 @@ export default class NextNodeServer extends BaseServer<
// Intercept fetch and other testmode apis. // Intercept fetch and other testmode apis.
if (this.serverOptions.experimentalTestProxy) { if (this.serverOptions.experimentalTestProxy) {
process.env.NEXT_PRIVATE_TEST_PROXY = 'true' process.env.NEXT_PRIVATE_TEST_PROXY = 'true'
const { const { interceptTestApis } =
interceptTestApis, require('next/dist/experimental/testmode/server') as typeof import('../experimental/testmode/server')
} = require('next/dist/experimental/testmode/server')
interceptTestApis() interceptTestApis()
} }
...@@ -1250,9 +1249,8 @@ export default class NextNodeServer extends BaseServer< ...@@ -1250,9 +1249,8 @@ export default class NextNodeServer extends BaseServer<
public getRequestHandler(): NodeRequestHandler { public getRequestHandler(): NodeRequestHandler {
const handler = this.makeRequestHandler() const handler = this.makeRequestHandler()
if (this.serverOptions.experimentalTestProxy) { if (this.serverOptions.experimentalTestProxy) {
const { const { wrapRequestHandlerNode } =
wrapRequestHandlerNode, require('next/dist/experimental/testmode/server') as typeof import('../experimental/testmode/server')
} = require('next/dist/experimental/testmode/server')
return wrapRequestHandlerNode(handler) return wrapRequestHandlerNode(handler)
} }
return handler return handler
......
...@@ -87,10 +87,8 @@ function ensureTestApisIntercepted() { ...@@ -87,10 +87,8 @@ function ensureTestApisIntercepted() {
if (!testApisIntercepted) { if (!testApisIntercepted) {
testApisIntercepted = true testApisIntercepted = true
if (process.env.NEXT_PRIVATE_TEST_PROXY === 'true') { if (process.env.NEXT_PRIVATE_TEST_PROXY === 'true') {
const { const { interceptTestApis, wrapRequestHandler } =
interceptTestApis, require('next/dist/experimental/testmode/server-edge') as typeof import('../../experimental/testmode/server-edge')
wrapRequestHandler,
} = require('next/dist/experimental/testmode/server-edge')
interceptTestApis() interceptTestApis()
propagator = wrapRequestHandler(propagator) propagator = wrapRequestHandler(propagator)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment