Skip to content

openapi-ui/nodejs-openapi-ui

Repository files navigation

NodeJS-OpenAPI-UI

A middleware collection for using the OpenAPI-UI with NodeJS

List of Contents

Usage

NestJS

import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { openApiUIReference } from "@openapi-ui/nestjs-openapi-ui";

const app = await NestFactory.create(AppModule);

const config = new DocumentBuilder()
  .setTitle('Cats example')
  .setDescription('The cats API description')
  .setVersion('1.0')
  .addTag('cats')
  .build()

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup("swagger", app, document, {
  jsonDocumentUrl: "/openapi.json",
});

app.use(
  "/openapi",
  openApiUIReference({
    specPath: "/openapi.json",
  }),
);

Read more: @openapi-ui/nestjs-openapi-ui

Express

import { openApiUIReference } from '@openapi-ui/express-openapi-ui';
import swaggerJsdoc from "swagger-jsdoc";

const openApiSpec = swaggerJsdoc({
  definition: {
    openapi: "3.0.0",
    info: {
      title: "Hello World",
      version: "1.0",
    },
  },
  apis: ["./src/*.ts"], // files containing annotations as above
});

app.get('/openapi.json', (req, res) => {
  res.json(openApiSpec);
});

app.use(
  '/openapi',
  openApiUIReference({
    specPath: '/openapi.json',
  }),
);

Read more: @openapi-ui/express-openapi-ui

Hono

import { openApiUIReference } from '@openapi-ui/hono-openapi-ui';

app.doc('/openapi.json', {
  info: {
    title: 'Example API',
    description: 'Example API description',
    version: '1.0.0',
  },
  openapi: '3.0.0',
});

app.use(
  '/openapi',
  openApiUIReference({
    specPath: '/openapi.json',
  }),
);

Read more: @openapi-ui/hono-openapi-ui

License

MIT