Unified the openapi generation

ref #76
This commit is contained in:
Nicolai Ort 2021-01-10 17:10:25 +01:00
parent 9355138a8c
commit 172159414b
3 changed files with 55 additions and 83 deletions

View File

@ -3,7 +3,7 @@ import consola from "consola";
import fs from "fs"; import fs from "fs";
import "reflect-metadata"; import "reflect-metadata";
import { createExpressServer, getMetadataArgsStorage } from "routing-controllers"; import { createExpressServer, getMetadataArgsStorage } from "routing-controllers";
import { routingControllersToSpec } from 'routing-controllers-openapi'; import { generateSpec } from '../src/apispec';
import { config } from '../src/config'; import { config } from '../src/config';
import authchecker from "../src/middlewares/authchecker"; import authchecker from "../src/middlewares/authchecker";
import { ErrorHandler } from '../src/middlewares/ErrorHandler'; import { ErrorHandler } from '../src/middlewares/ErrorHandler';
@ -24,46 +24,7 @@ const schemas = validationMetadatasToSchemas({
}); });
//Spec creation based on the previously created schemas //Spec creation based on the previously created schemas
const spec = routingControllersToSpec( const spec = generateSpec(storage, schemas);
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",
},
}
);
try { try {
fs.writeFileSync("./openapi.json", JSON.stringify(spec), { encoding: "utf-8" }); fs.writeFileSync("./openapi.json", JSON.stringify(spec), { encoding: "utf-8" });

51
src/apispec.ts Normal file
View File

@ -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,
},
}
);
}

View File

@ -2,8 +2,7 @@ import { validationMetadatasToSchemas } from "class-validator-jsonschema";
import express, { Application } from "express"; import express, { Application } from "express";
import path from 'path'; import path from 'path';
import { getMetadataArgsStorage } from "routing-controllers"; import { getMetadataArgsStorage } from "routing-controllers";
import { routingControllersToSpec } from "routing-controllers-openapi"; import { generateSpec } from '../apispec';
import { config } from '../config';
/** /**
* Loader for everything openapi related - from creating the schema to serving it via a static route and swaggerUiExpress. * 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 //Spec creation based on the previously created schemas
const spec = routingControllersToSpec( const spec = generateSpec(storage, schemas);
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,
},
}
);
app.get(["/api/docs/openapi.json", "/api/docs/swagger.json"], (req, res) => { app.get(["/api/docs/openapi.json", "/api/docs/swagger.json"], (req, res) => {
res.json(spec); res.json(spec);
}); });