Skip to content

Commit

Permalink
feat: Merge pull request #78 from seamapi/fix-json-response-must-be-z…
Browse files Browse the repository at this point in the history
…od-schema

BREAKING CHANGE: existing `jsonResponse` fields may need to be wrapped with `z.object()`
  • Loading branch information
codetheweb authored Jul 6, 2023
2 parents 3831e43 + f192ce5 commit 0feab1d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { withRouteSpec, checkRouteSpec } from "lib/middlewares"
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

Expand All @@ -8,14 +8,14 @@ export const jsonBody = z.object({
completed: z.boolean().optional().default(false),
})

export const route_spec = checkRouteSpec({
export const route_spec = {
methods: ["POST"],
auth: "auth_token",
jsonBody,
jsonResponse: z.object({
ok: z.string(),
}),
})
} as const

export default withRouteSpec(route_spec)(async (req, res) => {
return res.status(200).json({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { withRouteSpec } from "lib/middlewares"
import { z } from "zod"
import { v4 as uuidv4 } from "uuid"

export const jsonBody = z.object({
id: z.string().uuid().optional().default(uuidv4()),
title: z.string(),
completed: z.boolean().optional().default(false),
})

export const route_spec = {
methods: ["POST"],
auth: "auth_token",
jsonBody,
jsonResponse: {
ok: z.string(),
},
} as const

// @ts-expect-error
export default withRouteSpec(route_spec)(async (req, res) => {})
13 changes: 12 additions & 1 deletion packages/nextlove/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ export type CreateWithRouteSpecFunction = <
SP extends SetupParams<AuthMiddlewares, any>
>(
setupParams: SP
) => <RS extends RouteSpec<any, any, any, any, any, any, any, any>>(
) => <
RS extends RouteSpec<
string,
any,
any,
any,
any,
any,
z.ZodObject<any, any, any, any, any>,
any
>
>(
route_spec: RS
) => (next: RouteFunction<SP, RS>) => any

0 comments on commit 0feab1d

Please sign in to comment.