Skip to content
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

fix: jsonResponse doesn't assume ZodObject #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export default withRouteSpec(routeSpec)(async (req, res) => {
| `queryParams` | Any GET query parameters on the request as a zod object |
| `jsonBody` | The JSON body this endpoint accepts as a zod object |
| `commonParams` | Parameters common to both the query and json body as a zod object, this is sometimes used if a GET route also accepts POST |
| `jsonResponse` | A zod object representing the json resposne |
| `jsonResponse` | A zod object representing the json response |

### Generating OpenAPI Types (Command Line)

Expand Down
6 changes: 4 additions & 2 deletions packages/nextlove/src/generate-openapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ export async function generateOpenAPI(opts: GenerateOpenAPIOpts) {
route.responses[200].content = {
"application/json": {
schema: generateSchema(
addOkStatus
? jsonResponse.extend({ ok: z.boolean() })
addOkStatus && jsonResponse._def.typeName === "ZodObject"
? (jsonResponse as z.ZodObject<any, any, any, any, any>).extend({
ok: z.boolean(),
})
: jsonResponse
),
},
Expand Down
9 changes: 6 additions & 3 deletions packages/nextlove/src/generate-route-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { defaultMapFilePathToHTTPRoute } from "../lib/default-map-file-path-to-h
import { parseRoutesInPackage } from "../lib/parse-routes-in-package"
import { zodToTs, printNode } from "zod-to-ts"
import prettier from "prettier"
import { z } from "zod"
import { ZodObject, z } from "zod"

interface GenerateRouteTypesOpts {
packageDir: string
Expand Down Expand Up @@ -48,8 +48,11 @@ export const generateRouteTypes = async (opts: GenerateRouteTypesOpts) => {
routeSpec.jsonResponse
? printNode(
zodToTs(
setupParams.addOkStatus
? routeSpec.jsonResponse.extend({ ok: z.boolean() })
setupParams.addOkStatus &&
routeSpec.jsonResponse._def.typeName == "ZodObject"
? (
routeSpec.jsonResponse as ZodObject<any, any, any, any, any>
).extend({ ok: z.boolean() })
: routeSpec.jsonResponse
).node
)
Expand Down
2 changes: 1 addition & 1 deletion packages/nextlove/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface RouteSpec<
QueryParams extends ParamDef = z.ZodTypeAny,
CommonParams extends ParamDef = z.ZodTypeAny,
Middlewares extends readonly Middleware<any, any>[] = any[],
JsonResponse extends ParamDef = z.ZodObject<any, any, any, any, any>,
JsonResponse extends ParamDef = z.ZodTypeAny,
FormData extends ParamDef = z.ZodTypeAny
> {
methods: Methods
Expand Down