I'm using swagger-typescript-api to generate interfaces from swagger schema
npx swagger-typescript-api generate -p PATH_TO_YOUR_SCHEMA -o ./
Answer from Sergey Volkov on Stack Overflow
» npm install swagger-typescript-api
Videos
I'm using swagger-typescript-api to generate interfaces from swagger schema
npx swagger-typescript-api generate -p PATH_TO_YOUR_SCHEMA -o ./
Not sure if that's a sane way to do this, it's the first time I'm playing around with Swagger.
I bumped into the following link and pasted the schema from the project I integrate with. From the top 'Generate Client' menu I chose one of the TypeScript presets and it generated a minimal project where I could extract the bits I needed, interface and classes, etc.
http://editor.swagger.io/#/
I tried running your schema. Here's a small excerpt of the generated code:
export interface Bar {
"a"?: string;
"b": number;
"c": Date;
"baz"?: Baz;
}
export interface Baz {
"d": number;
"color": Color;
}
/**
*
*/
export type Color = "0" | "1" | "2";
Maybe with a bit more tweaking it can make exactly what you're looking for.
Further reading may be about tools like https://github.com/swagger-api/swagger-codegen but the online web editor is a quick and dirty way to do this.
How do you guys generate swagger docs from Typescript definitions?
Another option to tsoa is routing-controllers + routing-controllers-openapi. The main difference between the two (AFAIK) is that tsoa relies on code generation whereas routing-controllers operates wholly on runtime. Both methods have their upsides: tsoa is able to e.g. utilize richer metadata (such as code comments) whereas with routing-controllers we can skip the generation step. My recommendation is to check both out!
One more option is typescript-json-schema, which generates JSON Schema out of Typescript interfaces; after you have defined your models in JSON Schema you're not terribly far off from an OpenAPI spec.
Yes, you can easily generate Swagger and OpenAPI documents from your TypeScript types by using tsoa. The readme contains all of the setup information that you would need to start using it. It's compatible with express, hapi, koa, and more:
https://github.com/lukeautry/tsoa
(Full Transparency: I ~am~ was one of the maintainers of tsoa. But I was first a consumer of tsoa and I find it to be a great product... that's why I asked to help maintain it! :) ) I’ve since stepped back, but the new maintainers are awesome!
» npm install typescript-rest-swagger