From a47d47e07a69e5aaa4813533f84b451d92c281c3 Mon Sep 17 00:00:00 2001 From: Andre Hsu <4470828+andrehsu@users.noreply.github.com> Date: Tue, 26 Jan 2021 22:26:59 +0800 Subject: [PATCH] Change type of GetServerSidePropsContext.req.cookies the be the same as NextApiRequest.cookies (#21336) The original type for `GetServerSidePropsContext.req.cookies` was introduced in #19724. In the PR, the test at `test/integration/typescript/test/index.test.js` expects req.cookies to always exist, so `req.cookies` will never be undefined. I'm guessing that req.cookies is parsed using the same cookie middleware as `NextApiRequest`, in which case `req.cookies` should be `{ [key: string]: string }`, not `{ [key: string]: any }`. --- packages/next/next-server/server/render.tsx | 10 ++++++++-- packages/next/types/index.d.ts | 7 ++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/next/next-server/server/render.tsx b/packages/next/next-server/server/render.tsx index 5cda0722c6553ce..2044373d6a44774 100644 --- a/packages/next/next-server/server/render.tsx +++ b/packages/next/next-server/server/render.tsx @@ -44,7 +44,11 @@ import { NextComponentType, RenderPage, } from '../lib/utils' -import { tryGetPreviewData, __ApiPreviewProps } from './api-utils' +import { + tryGetPreviewData, + NextApiRequestCookies, + __ApiPreviewProps, +} from './api-utils' import { denormalizePagePath } from './denormalize-page-path' import { FontManifest, getFontDefinitionFromManifest } from './font-utils' import { LoadComponentsReturnType, ManifestItem } from './load-components' @@ -796,7 +800,9 @@ export async function renderToHTML( try { data = await getServerSideProps({ - req, + req: req as IncomingMessage & { + cookies: NextApiRequestCookies + }, res, query, resolvedUrl: renderOpts.resolvedUrl as string, diff --git a/packages/next/types/index.d.ts b/packages/next/types/index.d.ts index 3a18b705f9a330e..8c70d3ecfc0a4b1 100644 --- a/packages/next/types/index.d.ts +++ b/packages/next/types/index.d.ts @@ -15,6 +15,11 @@ import { // @ts-ignore This path is generated at build time and conflicts otherwise } from '../dist/next-server/lib/utils' +import { + NextApiRequestCookies, + // @ts-ignore This path is generated at build time and conflicts otherwise +} from '../dist/next-server/server/api-utils' + // @ts-ignore This path is generated at build time and conflicts otherwise import next from '../dist/server/next' @@ -130,7 +135,7 @@ export type GetServerSidePropsContext< Q extends ParsedUrlQuery = ParsedUrlQuery > = { req: IncomingMessage & { - cookies?: { [key: string]: any } + cookies: NextApiRequestCookies } res: ServerResponse params?: Q