Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
createExpressServer,
getMetadataArgsStorage
} from 'routing-controllers'
import { routingControllersToSpec } from 'routing-controllers-openapi'
import { UsersController } from './UsersController'
const routingControllersOptions = {
controllers: [UsersController],
routePrefix: '/api'
}
const app: Express = createExpressServer(routingControllersOptions)
// Parse class-validator classes into JSON Schema:
const metadatas = (getFromContainer(MetadataStorage) as any).validationMetadatas
const schemas = validationMetadatasToSchemas(metadatas, {
refPointerPrefix: '#/components/schemas/'
})
// Parse routing-controllers classes into OpenAPI spec:
const storage = getMetadataArgsStorage()
const spec = routingControllersToSpec(storage, routingControllersOptions, {
components: {
schemas,
securitySchemes: {
basicAuth: {
scheme: 'basic',
type: 'http'
}
}
},
info: {
const GetSchemaFromType = (Type: new () => any) => {
const { name } = Type;
const cached = SCHEMA_CACHE[name];
if (cached) {
return cached;
}
const metadatas = (getFromContainer(MetadataStorage) as any)
.validationMetadatas;
const allSchemas = validationMetadatasToSchemas(metadatas);
const schema = allSchemas[name];
if (!schema) {
console.error(`No schema found for ${name}`);
} else {
SCHEMA_CACHE[name] = schema;
}
return schema;
};
public static async generateDoc(options?: IKiwiOptions) {
var fs = require('fs');
const swagger: any = {
schemes: ["https", "http"],
paths: {},
openapi: "3.0.0",
components: {}
};
const metadatas = (getFromContainer(MetadataStorage) as any).validationMetadatas;
const schemas = validationMetadatasToSchemas(metadatas, {
refPointerPrefix: '#/components/schemas'
})
swagger['components']['schemas'] = schemas;
const routes = KiwiMetadataStorage.routes;
forEach(Object.keys(routes), (url: string) => {
swagger.paths[url] = {};
forEach(Object.keys(routes[url]), (method) => {
let path = replace(url, options.prefix, '');
const tags = split(path, '/');
swagger.paths[url][method] = {
consumes: ["application/json"],
produces: ["application/json"],
parameters: this.getParameters(routes[url][method].params),
tags: [tags[1]]
}