Skip to content

Instantly share code, notes, and snippets.

@brianfoody
Last active August 23, 2020 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianfoody/2dcaa51c52b7dd16e6a15f66b3445983 to your computer and use it in GitHub Desktop.
Save brianfoody/2dcaa51c52b7dd16e6a15f66b3445983 to your computer and use it in GitHub Desktop.
TypeScript to OpenAPI 3
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