Skip to content

Commit

Permalink
refactor: add getServerSideProps validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed Nov 28, 2024
1 parent bc893c6 commit 38122c7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
20 changes: 20 additions & 0 deletions apps/deploy-web/src/lib/nextjs/getValidatedServerSIdeProps.ts
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 });
};
};
12 changes: 5 additions & 7 deletions apps/deploy-web/src/pages/deployments/[dseq]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import type { GetServerSideProps } from "next";
import { z } from "zod";

import { DeploymentDetail, DeploymentDetailProps } from "@src/components/deployments/DeploymentDetail";
import { DeploymentDetail } from "@src/components/deployments/DeploymentDetail";
import { CI_CD_TEMPLATE_ID } from "@src/config/remote-deploy.config";
import { getValidatedServerSideProps } from "@src/lib/nextjs/getValidatedServerSIdeProps";
import { services } from "@src/services/http/http-server.service";

export default DeploymentDetail;

const contextSchema = z.object({
params: z.object({
dseq: z.string()
dseq: z.string().regex(/^\d+$/)
})
});
type Params = z.infer<typeof contextSchema>["params"];

export const getServerSideProps: GetServerSideProps<DeploymentDetailProps, Params> = async context => {
const { params } = contextSchema.parse(context);
export const getServerSideProps = getValidatedServerSideProps(contextSchema, async ({ params }) => {
const remoteDeployTemplate = await services.template.findById(CI_CD_TEMPLATE_ID);

return {
Expand All @@ -24,4 +22,4 @@ export const getServerSideProps: GetServerSideProps<DeploymentDetailProps, Param
dseq: params.dseq
}
};
};
});
8 changes: 3 additions & 5 deletions apps/deploy-web/src/pages/templates/[templateId]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GetServerSideProps } from "next";
import { z } from "zod";

import { TemplateDetail, TemplateDetailProps } from "@src/components/templates/TemplateDetail";
import { getValidatedServerSideProps } from "@src/lib/nextjs/getValidatedServerSIdeProps";
import { services } from "@src/services/http/http-server.service";

export default TemplateDetail;
Expand All @@ -11,10 +11,8 @@ const contextSchema = z.object({
templateId: z.string()
})
});
type Params = z.infer<typeof contextSchema>["params"];

export const getServerSideProps: GetServerSideProps<TemplateDetailProps, Params> = async context => {
const { params } = contextSchema.parse(context);
export const getServerSideProps = getValidatedServerSideProps<TemplateDetailProps>(contextSchema, async ({ params }) => {
const template = await services.template.findById(params.templateId);

if (!template) {
Expand All @@ -29,4 +27,4 @@ export const getServerSideProps: GetServerSideProps<TemplateDetailProps, Params>
template
}
};
};
});

0 comments on commit 38122c7

Please sign in to comment.