Skip to content

Commit

Permalink
Add an escape hatch you can use to make arbitrary customizations to O…
Browse files Browse the repository at this point in the history
…penAPI specs (#3)
  • Loading branch information
bengotow authored Jul 26, 2024
1 parent 4cddc7b commit c0dea33
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
}
"source.organizeImports": "explicit"
},
"typescript.tsdk": "node_modules/typescript/lib"
}
25 changes: 21 additions & 4 deletions src/openAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
OpenAPIGenerator,
OpenAPIRegistry,
ResponseConfig,
RouteConfig,
} from "@asteasolutions/zod-to-openapi";
import { RequestHandler, Router } from "express";
import { z, ZodArray, ZodEffects, ZodObject } from "zod";
Expand Down Expand Up @@ -63,8 +64,19 @@ export function buildOpenAPIDocument(args: {
// Attach all the API routes, referencing the named components where
// possible, and falling back to inlining the Zod shapes.
getRoutes(routers).forEach(({ path, method, handler }) => {
const { tag, body, params, query, response, description, summary, security, deprecated, responseContentType } =
getSchemaOfOpenAPIRoute(handler) || {};
const {
tag,
body,
params,
query,
response,
description,
summary,
security,
deprecated,
finalizeRouteConfig,
responseContentType,
} = getSchemaOfOpenAPIRoute(handler) || {};

//Express: /path/to/:variable/something -> OpenAPI /path/to/{variable}/something
const pathOpenAPIFormat = path
Expand Down Expand Up @@ -124,7 +136,7 @@ export function buildOpenAPIDocument(args: {
} else {
responses[204] = z.void().openapi({ description: "No content - successful operation" });
}
registry.registerPath({
let openapiRouteConfig: RouteConfig = {
tags: [tag || "default"],
method: method,
summary: summary,
Expand All @@ -138,7 +150,12 @@ export function buildOpenAPIDocument(args: {
body: referencingNamedSchemas(body),
},
responses: responses,
});
};
if (finalizeRouteConfig) {
openapiRouteConfig = finalizeRouteConfig(openapiRouteConfig);
}

registry.registerPath(openapiRouteConfig);
});

const generator = new OpenAPIGenerator(registry.definitions);
Expand Down
6 changes: 6 additions & 0 deletions src/openAPIRoute.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { RouteConfig } from "@asteasolutions/zod-to-openapi";
import { NextFunction, Request, RequestHandler, Response } from "express";
import { ZodError, ZodSchema, ZodTypeAny, z } from "zod";
import { ErrorResponse } from "./schemas";
Expand Down Expand Up @@ -46,6 +47,11 @@ type SchemaDefinition<
responseContentType?: string;
/** Mark the route as deprecated in generated OpenAPI docs. Does not have any impact on routing. */
deprecated?: boolean;

/** Provide a function to apply additional post-processing to the OpenAPI route configuration generated
* based on your API handler. This is the last function to run before the OpenAPI route is added to the registry.
*/
finalizeRouteConfig?: (config: RouteConfig) => RouteConfig;
};

const check = <TType>(obj?: any, schema?: ZodSchema<TType>): z.SafeParseReturnType<TType, TType> => {
Expand Down

0 comments on commit c0dea33

Please sign in to comment.