Unverified Commit c110e45b authored by tarunama's avatar tarunama Committed by GitHub

refactor: server/api-utils.ts (#21148)

- Add return type to function
- Add [isNotValidData](https://github.com/vercel/next.js/pull/21148/files#diff-64466461287cc1a70ce5597e3e1d18ac22755f9aeb7ff1865160a8c057540983R374) function for readability
parent 235b4cd0
...@@ -28,7 +28,7 @@ export async function apiResolver( ...@@ -28,7 +28,7 @@ export async function apiResolver(
apiContext: __ApiPreviewProps, apiContext: __ApiPreviewProps,
propagateError: boolean, propagateError: boolean,
onError?: ({ err }: { err: any }) => Promise<void> onError?: ({ err }: { err: any }) => Promise<void>
) { ): Promise<void> {
const apiReq = req as NextApiRequest const apiReq = req as NextApiRequest
const apiRes = res as NextApiResponse const apiRes = res as NextApiResponse
...@@ -166,7 +166,9 @@ function parseJson(str: string): object { ...@@ -166,7 +166,9 @@ function parseJson(str: string): object {
* Parse cookies from `req` header * Parse cookies from `req` header
* @param req request object * @param req request object
*/ */
export function getCookieParser(req: IncomingMessage) { export function getCookieParser(
req: IncomingMessage
): () => NextApiRequestCookies {
return function parseCookie(): NextApiRequestCookies { return function parseCookie(): NextApiRequestCookies {
const header: undefined | string | string[] = req.headers.cookie const header: undefined | string | string[] = req.headers.cookie
...@@ -362,6 +364,10 @@ export function tryGetPreviewData( ...@@ -362,6 +364,10 @@ export function tryGetPreviewData(
} }
} }
function isNotValidData(str: string): boolean {
return typeof str !== 'string' || str.length < 16
}
function setPreviewData<T>( function setPreviewData<T>(
res: NextApiResponse<T>, res: NextApiResponse<T>,
data: object | string, // TODO: strict runtime type checking data: object | string, // TODO: strict runtime type checking
...@@ -369,22 +375,13 @@ function setPreviewData<T>( ...@@ -369,22 +375,13 @@ function setPreviewData<T>(
maxAge?: number maxAge?: number
} & __ApiPreviewProps } & __ApiPreviewProps
): NextApiResponse<T> { ): NextApiResponse<T> {
if ( if (isNotValidData(options.previewModeId)) {
typeof options.previewModeId !== 'string' ||
options.previewModeId.length < 16
) {
throw new Error('invariant: invalid previewModeId') throw new Error('invariant: invalid previewModeId')
} }
if ( if (isNotValidData(options.previewModeEncryptionKey)) {
typeof options.previewModeEncryptionKey !== 'string' ||
options.previewModeEncryptionKey.length < 16
) {
throw new Error('invariant: invalid previewModeEncryptionKey') throw new Error('invariant: invalid previewModeEncryptionKey')
} }
if ( if (isNotValidData(options.previewModeSigningKey)) {
typeof options.previewModeSigningKey !== 'string' ||
options.previewModeSigningKey.length < 16
) {
throw new Error('invariant: invalid previewModeSigningKey') throw new Error('invariant: invalid previewModeSigningKey')
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment