swagger-json-builder
v1.0.3
Published
Building swagger automatically based on json files
Readme
swagger-json-builder
swagger-json-builder is an NPM package that generates Swagger documentation based on JSON files. It simplifies the process of setting up Swagger UI for your Express.js API by using a structured directory of JSON files.
Installation
npm install swagger-json-builderUsage
Here's an example of how to integrate swagger-json-builder into an Express.js application:
import express, { Request, Response, NextFunction } from "express";
import "dotenv/config";
const { setupSwagger } = require('swagger-json-builder');
const port: number = Number(process.env.PORT) || 3000;
const app = express();
setupSwagger(app, {
swaggerSpecDir: "swagger-specs", // Directory containing Swagger JSON files
swaggerUrl: '/api-docs', // URL path for Swagger UI
port: process.env.PORT
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
console.log(`Swagger Docs available at http://localhost:${port}/api-docs`);
});Configuration Options
The setupSwagger function takes an options object with the following properties:
| Option | Type | Description |
|-----------------|----------|-------------|
| swaggerSpecDir | string | Path to the directory containing Swagger JSON files. |
| swaggerUrl | string | URL path where Swagger UI will be served. |
| port | number | Port on which the server is running. |
How It Works
- Place your Swagger specification JSON files inside the
swagger-specsdirectory. swagger-json-builderreads these files and sets up Swagger UI for your Express app.- Navigate to
/api-docsin your browser to view the generated API documentation.
Example Directory Structure
my-project/
│── swagger-specs/
│ │── user.json
│ │── product.json
│── index.ts
│── package.jsonLicense
This package is licensed under the MIT License.
