// index.js
import swagger from 'swagger-ui-express';
import apiDocs from './swagger.json'; // Import swagger.json
import { assert } from 'swagger-ui-express'; // Import assert for Swagger 3.0 compatibility
// Use swagger.json for Swagger UI
server.use("/api-docs", swagger.serve, swagger.setup(apiDocs));
swagger.json
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"description": "API for E-Commerce application",
"title": "E-commerce API"
},
"servers": [
{
"url": "http://localhost:3200"
}
],
"components": {
"securitySchemes": {
"JWT": {
"in": "header",
"name": "Authorization",
"type": "apiKey"
}
}
},
"paths": {
"/api/products": {
"get": {
"tags": ["Products"],
"summary": "Get Products",
"description": "User will get all products",
"security": [{ "JWT": {} }],
"responses": {
"200": {
"description": "OK"
},
"401": {
"description": "Unauthorized"
}
}
}
},
"/api/users/signin": {
"post": {
"tags": ["Users"],
"summary": "Login",
"description": "User login to get token",
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"email": {
"type": "string"
},
"password": {
"type": "string"
}
}
}
}
}
},
"responses": {
"200": {
"description": "OK"
},
"400": {
"description": "Incorrect Credentials"
}
}
}
}
}
}
In this example, the swagger.json
file defines an OpenAPI 3.0 specification for an E-Commerce API.
The index.js
file imports and serves this specification using Swagger UI.
Adjust the paths, tags, and other details in swagger.json
to match your API's structure and documentation needs.