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 8 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
31 changes: 14 additions & 17 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('Did not get data from NextApiRequestCookies')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning is not clear for end users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace warning message for end users.
119d1d2

return false
}

Expand Down Expand Up @@ -337,8 +339,8 @@ export function tryGetPreviewData(
tokenPreviewData,
options.previewModeSigningKey
) as typeof encryptedPreviewData
} catch {
// TODO: warn
} catch (e) {
console.warn(`Failed to veriry jsonwebtoken: ${e}`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This warning is not clear for end users.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point, I do not have good idea of warning message for end users.
If end users does not jwt, navigate to URL of jwt spec.

clearPreviewData(res as NextApiResponse)
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