Skip to content

Commit

Permalink
Move getCookieParser into separate file to fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Sep 14, 2023
1 parent 85ec577 commit 7e49a89
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
21 changes: 21 additions & 0 deletions packages/next/src/server/api-utils/get-cookie-parser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { NextApiRequestCookies } from '.'

/**
* Parse cookies from the `headers` of request
* @param req request object
*/

export function getCookieParser(headers: {
[key: string]: string | string[] | null | undefined
}): () => NextApiRequestCookies {
return function parseCookie(): NextApiRequestCookies {
const { cookie } = headers

if (!cookie) {
return {}
}

const { parse: parseCookieFn } = require('next/dist/compiled/cookie')
return parseCookieFn(Array.isArray(cookie) ? cookie.join('; ') : cookie)
}
}
19 changes: 0 additions & 19 deletions packages/next/src/server/api-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@ export type __ApiPreviewProps = {
previewModeSigningKey: string
}

/**
* Parse cookies from the `headers` of request
* @param req request object
*/
export function getCookieParser(headers: {
[key: string]: string | string[] | null | undefined
}): () => NextApiRequestCookies {
return function parseCookie(): NextApiRequestCookies {
const { cookie } = headers

if (!cookie) {
return {}
}

const { parse: parseCookieFn } = require('next/dist/compiled/cookie')
return parseCookieFn(Array.isArray(cookie) ? cookie.join('; ') : cookie)
}
}

/**
*
* @param res response object
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/api-utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import isError from '../../lib/is-error'
import { isResSent } from '../../shared/lib/utils'
import { interopDefault } from '../../lib/interop-default'
import {
getCookieParser,
setLazyProp,
sendStatusCode,
redirect,
Expand All @@ -27,6 +26,7 @@ import {
SYMBOL_PREVIEW_DATA,
RESPONSE_LIMIT_DEFAULT,
} from './index'
import { getCookieParser } from './get-cookie-parser'
import { getTracer } from '../lib/trace/tracer'
import { NodeSpan } from '../lib/trace/constants'
import { RequestCookies } from '../web/spec-extension/cookies'
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/server/base-http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { IncomingHttpHeaders, OutgoingHttpHeaders } from 'http'
import type { I18NConfig } from '../config-shared'

import { PERMANENT_REDIRECT_STATUS } from '../../shared/lib/constants'
import { getCookieParser, NextApiRequestCookies } from '../api-utils'
import { NextApiRequestCookies } from '../api-utils'
import { getCookieParser } from '../api-utils/get-cookie-parser'

export interface BaseNextRequestConfig {
basePath: string | undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/server/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import type {
import type { ImageConfigComplete } from '../shared/lib/image-config'
import type { Redirect } from '../lib/load-custom-routes'
import {
getCookieParser,
type NextApiRequestCookies,
type __ApiPreviewProps,
setLazyProp,
} from './api-utils'
import { getCookieParser } from './api-utils/get-cookie-parser'
import type { FontManifest, FontConfig } from './font-utils'
import type { LoadComponentsReturnType, ManifestItem } from './load-components'
import type {
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/shared/lib/i18n/get-locale-redirect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { acceptLanguage } from '../../../server/accept-header'
import { denormalizePagePath } from '../page-path/denormalize-page-path'
import { detectDomainLocale } from './detect-domain-locale'
import { formatUrl } from '../router/utils/format-url'
import { getCookieParser } from '../../../server/api-utils'
import { getCookieParser } from '../../../server/api-utils/get-cookie-parser'

interface Options {
defaultLocale: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
isInterceptionRouteAppPath,
} from '../../../../server/future/helpers/interception-routes'
import { NEXT_RSC_UNION_QUERY } from '../../../../client/components/app-router-headers'
import { getCookieParser } from '../../../../server/api-utils'
import { getCookieParser } from '../../../../server/api-utils/get-cookie-parser'

/**
* Ensure only a-zA-Z are used for param names for proper interpolating
Expand Down

0 comments on commit 7e49a89

Please sign in to comment.