Skip to content

Commit

Permalink
refactor: server/api-utils.ts (#21148)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunama committed Jan 26, 2021
1 parent 235b4cd commit c110e45
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions packages/next/next-server/server/api-utils.ts
Expand Up @@ -28,7 +28,7 @@ export async function apiResolver(
apiContext: __ApiPreviewProps,
propagateError: boolean,
onError?: ({ err }: { err: any }) => Promise<void>
) {
): Promise<void> {
const apiReq = req as NextApiRequest
const apiRes = res as NextApiResponse

Expand Down Expand Up @@ -166,7 +166,9 @@ function parseJson(str: string): object {
* Parse cookies from `req` header
* @param req request object
*/
export function getCookieParser(req: IncomingMessage) {
export function getCookieParser(
req: IncomingMessage
): () => NextApiRequestCookies {
return function parseCookie(): NextApiRequestCookies {
const header: undefined | string | string[] = req.headers.cookie

Expand Down Expand Up @@ -362,29 +364,24 @@ export function tryGetPreviewData(
}
}

function isNotValidData(str: string): boolean {
return typeof str !== 'string' || str.length < 16
}

function setPreviewData<T>(
res: NextApiResponse<T>,
data: object | string, // TODO: strict runtime type checking
options: {
maxAge?: number
} & __ApiPreviewProps
): NextApiResponse<T> {
if (
typeof options.previewModeId !== 'string' ||
options.previewModeId.length < 16
) {
if (isNotValidData(options.previewModeId)) {
throw new Error('invariant: invalid previewModeId')
}
if (
typeof options.previewModeEncryptionKey !== 'string' ||
options.previewModeEncryptionKey.length < 16
) {
if (isNotValidData(options.previewModeEncryptionKey)) {
throw new Error('invariant: invalid previewModeEncryptionKey')
}
if (
typeof options.previewModeSigningKey !== 'string' ||
options.previewModeSigningKey.length < 16
) {
if (isNotValidData(options.previewModeSigningKey)) {
throw new Error('invariant: invalid previewModeSigningKey')
}

Expand Down

0 comments on commit c110e45

Please sign in to comment.