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
Changes from 8 commits
3842579
af93cfd
3781f0b
4f23b89
7c7c312
044c1bb
6ac5ddb
fea0ac1
119d1d2
685879d
6b27d59
a6eb637
08fd66f
519c5bc
dfd2e57
d7bfae6
31be4e0
c04002b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -302,7 +304,7 @@ export function tryGetPreviewData( | |
try { | ||
cookies = getCookies() | ||
} catch { | ||
// TODO: warn | ||
console.warn('Did not get data from NextApiRequestCookies') | ||
return false | ||
} | ||
|
||
|
@@ -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}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This warning is not clear for end users. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
clearPreviewData(res as NextApiResponse) | ||
return false | ||
} | ||
|
@@ -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') | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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