Skip to content

Commit

Permalink
Let body and responses be any zod type
Browse files Browse the repository at this point in the history
Not just objects, as an array can be a return type
  • Loading branch information
Timothy-Gonzalez committed Oct 16, 2024
1 parent 7671e9e commit b0694e7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/common/openapi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OpenApiGeneratorV31, OpenAPIRegistry, RouteConfig } from "@asteasolutions/zod-to-openapi";
import { AnyZodObject } from "zod";
import { AnyZodObject, ZodType } from "zod";
import type { InfoObject, OpenAPIObject, ServerObject } from "openapi3-ts/oas31";
import Config from "./config";
import { ResponsesObject, Specification } from "../middleware/specification";
Expand Down Expand Up @@ -54,7 +54,7 @@ export function registerPathSpecification<
Params extends AnyZodObject,
Query extends AnyZodObject,
Responses extends ResponsesObject,
Body extends AnyZodObject,
Body extends ZodType,
>(specification: Specification<Params, Query, Responses, Body>): void {
// Convert specification into RouteConfig
const { method, path, tag, role, summary, description, parameters: params, query } = specification;
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/specification.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestHandler } from "express";
import { AnyZodObject, z } from "zod";
import { AnyZodObject, z, ZodType } from "zod";
import StatusCode from "status-code-enum";
import { Response, Request, NextFunction } from "express";
import { registerPathSpecification } from "../common/openapi";
Expand Down Expand Up @@ -30,13 +30,13 @@ export enum Tag {

export interface ResponseObject {
description: string;
schema: AnyZodObject;
schema: ZodType;
}
export interface ResponsesObject {
[statusCode: string]: ResponseObject;
}

export interface Specification<Params = AnyZodObject, Query = AnyZodObject, Responses = ResponsesObject, Body = AnyZodObject> {
export interface Specification<Params = AnyZodObject, Query = AnyZodObject, Responses = ResponsesObject, Body = ZodType> {
path: string;
method: Method;
tag: Tag;
Expand All @@ -57,7 +57,7 @@ export default function specification<
Params extends AnyZodObject,
Query extends AnyZodObject,
Responses extends ResponsesObject,
Body extends AnyZodObject,
Body extends ZodType,
>(spec: Specification<Params, Query, Responses, Body>): RequestHandler<z.infer<Params>, ResponseBody<Responses>, z.infer<Body>> {
registerPathSpecification(spec);

Expand Down

0 comments on commit b0694e7

Please sign in to comment.