-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add getServerSideProps validation
- Loading branch information
1 parent
bc893c6
commit 38122c7
Showing
3 changed files
with
28 additions
and
12 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
apps/deploy-web/src/lib/nextjs/getValidatedServerSIdeProps.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { GetServerSidePropsContext, GetServerSidePropsResult, PreviewData } from "next/types"; | ||
import type { ParsedUrlQuery } from "querystring"; | ||
import { z } from "zod"; | ||
|
||
export const getValidatedServerSideProps = < | ||
Props extends { [key: string]: any } = { [key: string]: any }, | ||
Params extends ParsedUrlQuery = ParsedUrlQuery, | ||
Preview extends PreviewData = PreviewData, | ||
Schema extends z.ZodObject<any, any, any> = z.ZodObject<{ params: z.ZodObject<any, any, any> }, any, any> | ||
>( | ||
schema: Schema, | ||
handler: ( | ||
context: Omit<GetServerSidePropsContext<Params, Preview>, "params"> & { params: z.infer<Schema>["params"] } | ||
) => Promise<GetServerSidePropsResult<Props>> | ||
): ((context: GetServerSidePropsContext<Params, Preview>) => Promise<GetServerSidePropsResult<Props>>) => { | ||
return context => { | ||
const { params } = schema.parse(context); | ||
return handler({ ...context, params }); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters