From 172159414b1d4964ee862ad1edc66a7c4c94cccb Mon Sep 17 00:00:00 2001 From: Nicolai Ort Date: Sun, 10 Jan 2021 17:10:25 +0100 Subject: [PATCH] Unified the openapi generation ref #76 --- scripts/openapi_export.ts | 43 ++------------------------------- src/apispec.ts | 51 +++++++++++++++++++++++++++++++++++++++ src/loaders/openapi.ts | 44 ++------------------------------- 3 files changed, 55 insertions(+), 83 deletions(-) create mode 100644 src/apispec.ts diff --git a/scripts/openapi_export.ts b/scripts/openapi_export.ts index b72a369..83fd879 100644 --- a/scripts/openapi_export.ts +++ b/scripts/openapi_export.ts @@ -3,7 +3,7 @@ import consola from "consola"; import fs from "fs"; import "reflect-metadata"; import { createExpressServer, getMetadataArgsStorage } from "routing-controllers"; -import { routingControllersToSpec } from 'routing-controllers-openapi'; +import { generateSpec } from '../src/apispec'; import { config } from '../src/config'; import authchecker from "../src/middlewares/authchecker"; import { ErrorHandler } from '../src/middlewares/ErrorHandler'; @@ -24,46 +24,7 @@ const schemas = validationMetadatasToSchemas({ }); //Spec creation based on the previously created schemas -const spec = routingControllersToSpec( - storage, - { - routePrefix: "/api" - }, - { - components: { - schemas, - "securitySchemes": { - "AuthToken": { - "type": "http", - "scheme": "bearer", - "bearerFormat": "JWT", - description: "A JWT based access token. Use /api/auth/login or /api/auth/refresh to get one." - }, - "RefreshTokenCookie": { - "type": "apiKey", - "in": "cookie", - "name": "lfk_backend__refresh_token", - description: "A cookie containing a JWT based refreh token. Attention: Doesn't work in swagger-ui. Use /api/auth/login or /api/auth/refresh to get one." - }, - "StatsApiToken": { - "type": "http", - "scheme": "bearer", - description: "Api token that can be obtained by creating a new stats client (post to /api/statsclients). Only valid for obtaining stats." - }, - "StationApiToken": { - "type": "http", - "scheme": "bearer", - description: "Api token that can be obtained by creating a new scan station (post to /api/stations). Only valid for creating scans." - } - } - }, - info: { - description: "The the backend API for the LfK! runner system.", - title: "LfK! Backend API", - version: "0.0.8", - }, - } -); +const spec = generateSpec(storage, schemas); try { fs.writeFileSync("./openapi.json", JSON.stringify(spec), { encoding: "utf-8" }); diff --git a/src/apispec.ts b/src/apispec.ts new file mode 100644 index 0000000..6beb511 --- /dev/null +++ b/src/apispec.ts @@ -0,0 +1,51 @@ +import { MetadataArgsStorage } from 'routing-controllers'; +import { routingControllersToSpec } from 'routing-controllers-openapi'; +import { config } from './config'; + +/** + * This function generates a the openapi spec from route metadata and type schemas. + * @param storage MetadataArgsStorage object generated by routing-controllers. + * @param schemas MetadataArgsStorage object generated by class-validator-jsonschema. + */ +export function generateSpec(storage: MetadataArgsStorage, schemas) { + return routingControllersToSpec( + storage, + { + routePrefix: "/api" + }, + { + components: { + schemas, + "securitySchemes": { + "AuthToken": { + "type": "http", + "scheme": "bearer", + "bearerFormat": "JWT", + description: "A JWT based access token. Use /api/auth/login or /api/auth/refresh to get one." + }, + "RefreshTokenCookie": { + "type": "apiKey", + "in": "cookie", + "name": "lfk_backend__refresh_token", + description: "A cookie containing a JWT based refreh token. Attention: Doesn't work in swagger-ui. Use /api/auth/login or /api/auth/refresh to get one." + }, + "StatsApiToken": { + "type": "http", + "scheme": "bearer", + description: "Api token that can be obtained by creating a new stats client (post to /api/statsclients). Only valid for obtaining stats." + }, + "StationApiToken": { + "type": "http", + "scheme": "bearer", + description: "Api token that can be obtained by creating a new scan station (post to /api/stations). Only valid for creating scans." + } + } + }, + info: { + description: "The the backend API for the LfK! runner system.", + title: "LfK! Backend API", + version: config.package_version, + }, + } + ); +} \ No newline at end of file diff --git a/src/loaders/openapi.ts b/src/loaders/openapi.ts index 6f2056c..0f3a49d 100644 --- a/src/loaders/openapi.ts +++ b/src/loaders/openapi.ts @@ -2,8 +2,7 @@ import { validationMetadatasToSchemas } from "class-validator-jsonschema"; import express, { Application } from "express"; import path from 'path'; import { getMetadataArgsStorage } from "routing-controllers"; -import { routingControllersToSpec } from "routing-controllers-openapi"; -import { config } from '../config'; +import { generateSpec } from '../apispec'; /** * Loader for everything openapi related - from creating the schema to serving it via a static route and swaggerUiExpress. @@ -16,46 +15,7 @@ export default async (app: Application) => { }); //Spec creation based on the previously created schemas - const spec = routingControllersToSpec( - storage, - { - routePrefix: "/api" - }, - { - components: { - schemas, - "securitySchemes": { - "AuthToken": { - "type": "http", - "scheme": "bearer", - "bearerFormat": "JWT", - description: "A JWT based access token. Use /api/auth/login or /api/auth/refresh to get one." - }, - "RefreshTokenCookie": { - "type": "apiKey", - "in": "cookie", - "name": "lfk_backend__refresh_token", - description: "A cookie containing a JWT based refreh token. Attention: Doesn't work in swagger-ui. Use /api/auth/login or /api/auth/refresh to get one." - }, - "StatsApiToken": { - "type": "http", - "scheme": "bearer", - description: "Api token that can be obtained by creating a new stats client (post to /api/statsclients). Only valid for obtaining stats." - }, - "StationApiToken": { - "type": "http", - "scheme": "bearer", - description: "Api token that can be obtained by creating a new scan station (post to /api/stations). Only valid for creating scans." - } - } - }, - info: { - description: "The the backend API for the LfK! runner system.", - title: "LfK! Backend API", - version: config.package_version, - }, - } - ); + const spec = generateSpec(storage, schemas); app.get(["/api/docs/openapi.json", "/api/docs/swagger.json"], (req, res) => { res.json(spec); });