Skip to content

Commit

Permalink
refactor: more precise types for req/res in withRequestStore (#70357)
Browse files Browse the repository at this point in the history
- removes IncomingMessage/ServerResponse from accepted types, we never
actually pass those
- narrow down the possible combinations of req/res to what we actually
use
  • Loading branch information
lubieowoce authored Sep 24, 2024
1 parent 21f0c30 commit f57cada
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
10 changes: 6 additions & 4 deletions packages/next/src/server/async-storage/with-request-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { BaseNextRequest, BaseNextResponse } from '../base-http'
import type { IncomingHttpHeaders, IncomingMessage, ServerResponse } from 'http'
import type { IncomingHttpHeaders } from 'http'
import type { AsyncLocalStorage } from 'async_hooks'
import type { RequestStore } from '../../client/components/request-async-storage.external'
import type { RenderOpts } from '../app-render/types'
Expand Down Expand Up @@ -55,8 +55,7 @@ export type WrapperRenderOpts = RequestLifecycleOpts &
previewProps?: __ApiPreviewProps
}

export type RequestContext = {
req: IncomingMessage | BaseNextRequest | NextRequest
export type RequestContext = RequestResponsePair & {
/**
* The URL of the request. This only specifies the pathname and the search
* part of the URL. This is only undefined when generating static paths (ie,
Expand All @@ -74,12 +73,15 @@ export type RequestContext = {
*/
search?: string
}
res?: ServerResponse | BaseNextResponse
renderOpts?: WrapperRenderOpts
isHmrRefresh?: boolean
serverComponentsHmrCache?: ServerComponentsHmrCache
}

type RequestResponsePair =
| { req: BaseNextRequest; res: BaseNextResponse } // for an app page
| { req: NextRequest; res: undefined } // in an api route or middleware

/**
* If middleware set cookies in this request (indicated by `x-middleware-set-cookie`),
* then merge those into the existing cookie object, so that when `cookies()` is accessed
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/route-modules/app-route/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export class AppRouteRouteModule extends RouteModule<
// Get the context for the request.
const requestContext: RequestContext = {
req: rawRequest,
res: undefined,
url: rawRequest.nextUrl,
renderOpts: {
previewProps: context.prerenderManifest.preview,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/web/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ export async function adapter(
requestAsyncStorage,
{
req: request,
res: undefined,
url: request.nextUrl,
renderOpts: {
onUpdateCookies: (cookies) => {
Expand Down

0 comments on commit f57cada

Please sign in to comment.