Added rlly basic lib generation

ref #31
This commit is contained in:
Nicolai Ort 2020-12-12 22:21:45 +01:00
parent c3aa88c212
commit 421ddc50ed
3 changed files with 68 additions and 3 deletions

1
lib/openapi.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -38,11 +38,11 @@
"reflect-metadata": "^0.1.13",
"routing-controllers": "^0.9.0-alpha.6",
"routing-controllers-openapi": "^2.1.0",
"sqlite3": "^5.0.0",
"swagger-ui-express": "^4.1.5",
"typeorm": "^0.2.29",
"typeorm-routing-controllers-extensions": "^0.2.0",
"typeorm-seeding": "^1.6.1",
"sqlite3": "^5.0.0",
"uuid": "^8.3.1"
},
"devDependencies": {
@ -56,6 +56,7 @@
"axios": "^0.21.0",
"jest": "^26.6.3",
"nodemon": "^2.0.6",
"rimraf": "^2.7.1",
"start-server-and-test": "^1.11.6",
"ts-jest": "^26.4.4",
"ts-node": "^9.0.0",
@ -69,7 +70,10 @@
"test": "jest",
"test:watch": "jest --watchAll",
"test:ci": "start-server-and-test dev http://localhost:4010/api/openapi.json test",
"seed": "ts-node ./node_modules/typeorm/cli.js schema:sync && ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"
"seed": "ts-node ./node_modules/typeorm/cli.js schema:sync && ts-node ./node_modules/typeorm-seeding/dist/cli.js seed",
"openapi:export": "ts-node src/openapi_export.ts",
"lib": "rimraf ./lib && npm run openapi:export && npm run lib:generate",
"lib:generate": "npx openapi-typescript-codegen --input ./lib/openapi.json --output ./lib/dist"
},
"nodemonConfig": {
"ignore": [
@ -77,4 +81,4 @@
"docs/*"
]
}
}
}

60
src/openapi_export.ts Normal file
View File

@ -0,0 +1,60 @@
import { validationMetadatasToSchemas } from 'class-validator-jsonschema';
import consola from "consola";
import fs from "fs";
import "reflect-metadata";
import { createExpressServer, getMetadataArgsStorage } from "routing-controllers";
import { routingControllersToSpec } from 'routing-controllers-openapi';
import authchecker from "./authchecker";
import { config } from './config';
import { ErrorHandler } from './middlewares/ErrorHandler';
const CONTROLLERS_FILE_EXTENSION = process.env.NODE_ENV === 'production' ? 'js' : 'ts';
createExpressServer({
authorizationChecker: authchecker,
middlewares: [ErrorHandler],
development: config.development,
cors: true,
routePrefix: "/api",
controllers: [`${__dirname}/controllers/*.${CONTROLLERS_FILE_EXTENSION}`],
});
const storage = getMetadataArgsStorage();
const schemas = validationMetadatasToSchemas({
refPointerPrefix: "#/components/schemas/",
});
//Spec creation based on the previously created schemas
const spec = routingControllersToSpec(
storage,
{
routePrefix: "/api"
},
{
components: {
schemas,
"securitySchemes": {
"AuthToken": {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
},
info: {
description: "The the backend API for the LfK! runner system.",
title: "LfK! Backend API",
version: "1.0.0",
},
}
);
if (!fs.existsSync("./lib")) {
fs.mkdirSync("./lib");
}
try {
fs.writeFileSync("./lib/openapi.json", JSON.stringify(spec), { encoding: "utf-8" });
consola.success("Exported openapi spec to lib/openapi.json");
} catch (error) {
consola.error("Couldn't export the openapi spec");
}