Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: server/api-utils.ts #21148

Merged
merged 18 commits into from Jan 26, 2021
Merged
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 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 @@ -302,7 +304,7 @@ export function tryGetPreviewData(
try {
cookies = getCookies()
} catch {
// TODO: warn
console.warn('If you have not enabled cookies, please do so.')
timneutkens marked this conversation as resolved.
Show resolved Hide resolved
return false
}

Expand Down Expand Up @@ -369,22 +371,17 @@ function setPreviewData<T>(
maxAge?: number
} & __ApiPreviewProps
): NextApiResponse<T> {
if (
typeof options.previewModeId !== 'string' ||
options.previewModeId.length < 16
) {
const isNotValidData = (str: string): boolean => {
return typeof str !== 'string' || str.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