Function swaggerMiddleware

Middleware to generate and serve Swagger UI documentation.

  • Parameters

    • routes: Route[]

      The routes to generate documentation for.

    • Optionaldocument: Document<{}>

      The OpenAPI document to extend/override the generated documentation.

    Returns Middleware

    A middleware function that serves the Swagger UI documentation.

    Example usage:

    import { start, router, json, swaggerMiddleware } from "@pulsar-http/core";
    import { z } from "zod";

    const userSchema = z.object({
    name: z.string(),
    age: z.number(),
    });

    const usersResponseSchema = z.object({
    message: z.string(),
    user: userSchema,
    }).strict();

    const routes = [
    router.get("/", async () => json({ message: "Hello, world!" })),
    router.post('/users', async ({ body }) => json({
    message: "User created",
    user: body,
    }), { bodySchema: userSchema, responseSchema: usersResponseSchema }),
    ];

    start({
    routes,
    middlewares: [swaggerMiddleware(routes)],
    });

    In this example, the /swagger route will serve the Swagger UI documentation for the provided routes.