From 293273cec029ec2f6396611765d5a3b875ca589b Mon Sep 17 00:00:00 2001 From: abdulla-ashurov Date: Mon, 10 Jul 2023 16:54:21 +0500 Subject: [PATCH] Add command to package.json for auto generating swagger. --- package.json | 1 + src/app.ts | 33 ++++++--------------------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/package.json b/package.json index e77d39c0..620ce534 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ } }, "scripts": { + "prebuild": "swagger-jsdoc -d src/swagger.cjs -o src/static/swagger.json ./src/controllers/*.ts", "build": "tsc", "start": "node dist/index.js", "format": "prettier --write '*.{json,js}' 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'", diff --git a/src/app.ts b/src/app.ts index 5ff2eca9..0f7ec9d0 100644 --- a/src/app.ts +++ b/src/app.ts @@ -14,6 +14,8 @@ import { Connection } from './database/connection/connection.js' import { RevocationController } from './controllers/revocation.js' import { CORS_ERROR_MSG } from './types/constants.js' +import swaggerJSONDoc from './static/swagger.json' assert { type: "json" } + import * as dotenv from 'dotenv' dotenv.config() @@ -21,31 +23,6 @@ import path from 'path' import { LogToWebHook } from './middleware/hook.js' import { Middleware } from './middleware/middleware.js' -import swaggerJsdoc from 'swagger-jsdoc'; - -const options = { - swaggerDefinition: { - openapi: '3.0.0', - info: { - title: 'Credential Service for cheqd network', - version: '2.0.0', - description: 'API service to create and manage DIDs and credentials on cheqd network.' - }, - tags: [ - { - name: 'Credential', - externalDocs: { - url: 'https://github.com/cheqd/credential-service#readme' - } - } - ], - components: {} - }, - apis: ['./src/controllers/*.ts'], -}; - -const openApiSpecification = swaggerJsdoc(options); - let swagger_options = {} if (process.env.ENABLE_AUTHENTICATION === 'true') { swagger_options = { @@ -96,8 +73,10 @@ class App { this.express.use( '/swagger', - swaggerUi.serve, - swaggerUi.setup(openApiSpecification, swagger_options) + swaggerUi.serve, + async (_req: express.Request, res: express.Response) => { + return res.send(swaggerUi.generateHTML(swaggerJSONDoc, swagger_options)) + } ) this.express.use(auth.handleError) this.express.use(async (req, res, next) => await auth.accessControl(req, res, next))