Skip to content

Commit

Permalink
Add query string support to specification
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez committed Oct 13, 2024
1 parent 95e37bf commit b91640d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/common/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ export function getOpenAPISpec(): OpenAPIObject {

export function registerPathSpecification<
Params extends AnyZodObject,
Query extends AnyZodObject,
Responses extends ResponsesObject,
Body extends AnyZodObject,
>(specification: Specification<Params, Responses, Body>): void {
>(specification: Specification<Params, Query, Responses, Body>): void {
// Convert specification into RouteConfig
const { method, path, tag, role, summary, description, parameters: params } = specification;
const { method, path, tag, role, summary, description, parameters: params, query } = specification;
const security = role
? [
{
Expand Down Expand Up @@ -84,7 +85,7 @@ export function registerPathSpecification<
};
}

const request: RouteConfig["request"] = { params };
const request: RouteConfig["request"] = { params, query };
if (specification.body) {
request.body = {
content: {
Expand Down
12 changes: 8 additions & 4 deletions src/middleware/specification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ export interface ResponsesObject {
[statusCode: string]: ResponseObject;
}

export interface Specification<Params = AnyZodObject, Responses = ResponsesObject, Body = AnyZodObject> {
export interface Specification<Params = AnyZodObject, Query = AnyZodObject, Responses = ResponsesObject, Body = AnyZodObject> {
path: string;
method: Method;
tag: Tag;
role: Role | null;
summary: string;
description?: string;
parameters?: Params;
query?: Query;
body?: Body;
responses: Responses;
}
Expand All @@ -52,9 +53,12 @@ export interface Specification<Params = AnyZodObject, Responses = ResponsesObjec
type InferResponseBody<T> = T extends ResponseObject ? z.infer<T["schema"]> : never;
type ResponseBody<T extends ResponsesObject> = InferResponseBody<T[keyof T]>;

export default function specification<Params extends AnyZodObject, Responses extends ResponsesObject, Body extends AnyZodObject>(
spec: Specification<Params, Responses, Body>,
): RequestHandler<z.infer<Params>, ResponseBody<Responses>, z.infer<Body>> {
export default function specification<
Params extends AnyZodObject,
Query extends AnyZodObject,
Responses extends ResponsesObject,
Body extends AnyZodObject,
>(spec: Specification<Params, Query, Responses, Body>): RequestHandler<z.infer<Params>, ResponseBody<Responses>, z.infer<Body>> {
registerPathSpecification(spec);

return async (req: Request, res: Response, next: NextFunction) => {
Expand Down

0 comments on commit b91640d

Please sign in to comment.