Last active
August 23, 2020 01:19
-
-
Save brianfoody/2dcaa51c52b7dd16e6a15f66b3445983 to your computer and use it in GitHub Desktop.
TypeScript to OpenAPI 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as tsj from "ts-json-schema-generator"; | |
const toOpenApi = require("json-schema-to-openapi-schema"); | |
const defaultConfig: SchemaConfig = { | |
tsconfig: "./tsconfig-types.json", | |
type: "*", | |
}; | |
export const generateOpenApiSchema = ( | |
schemaName: string, | |
version = "1.0.0", | |
config = defaultConfig | |
) => { | |
const schema = tsj.createGenerator(config).createSchema(config.type); | |
const convertedSchema = toOpenApi(schema); | |
const openAPI3Schema = { | |
openapi: "3.0.0", | |
info: { | |
version: version, | |
title: schemaName, | |
}, | |
paths: {}, | |
components: { | |
schemas: { | |
...convertedSchema.definitions, | |
}, | |
}, | |
}; | |
return openAPI3Schema; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment