Goal
- TypeScript controllers and models as the single source of truth for your API
- A valid swagger spec is generated from your controllers and models, including:
- Paths (e.g. GET /Users)
- Definitions based on TypeScript interfaces (models)
- Parameters/model properties marked as required or optional based on TypeScript (e.g. myProperty?: string is optional in the Swagger spec)
- jsDoc supported for object descriptions (most other metadata can be inferred from TypeScript types)
- Routes are generated for middleware of choice
- Express currently included, other middleware can be supported using a simple handlebars template
How it works
Create Controllers
// controllers/usersController.ts ;;;
Create Models
// models/user.ts
Generate!
From command line/npm script:
swagger-ts-generate --entryFile=./src/server.ts --swaggerDir=./dist --routesDir=./src
- entryFile: Entry point for your application
- swaggerDir: Where you want swagger.json to be dropped
- routesDir: Where you want routes.ts to be dropped
Consume generated routes
;;;; // controllers need to be referenced in order to get crawled by the generator; app: express.Express = express;app.usebodyParser.urlencoded;app.usebodyParser.json;app.usemethodOverride; RegisterRoutesapp; app.listen3000;
Use awesome Swagger tools
Now that you have a swagger spec (swagger.json), you can use all kinds of amazing tools that generate documentation, client SDKs, and more.
Installation
npm install swagger-ts --save