npm
Spec TypeScript code generation is packaged in form of npm package. Add following dependency to your project:
% npm install --dev specgen.ioThe has specgen command line tool is built inside of the npm package.
Add the following script to package.json:
"specgen": "specgen <command> <args>"In this specgen script <command> stands for specific specgen tool command. Here are commands that generate TypeScript code: service-ts, client-ts, models-ts.
To run code generation execute specgen script:
% npm run specgenThis page provides some examples of specgen scripts configuration.
Express Service
The example below generates Express server with Superstruct models to ./src/spec and OpenAPI specification to ./docs/swagger.yaml. The command also scaffolds services (only if they don't exist yet) that should be implemented by developers and puts the code into ./src/services path.
"specgen": "specgen service-ts --server express --validation superstruct --spec-file ./spec.yaml --generate-path ./src/spec --swagger-path ./docs/swagger.yaml --services-path ./src/services"Read service-ts documentation for command details.
Koa Service
The example below generates koa server with io-ts models to ./src/spec and OpenAPI specification to ./docs/swagger.yaml. The command also scaffolds services (only if they don't exist yet) that should be implemented by developers and puts the code into ./src/services path.
"specgen": "specgen service-ts --server koa --validation io-ts --spec-file ./spec.yaml --generate-path ./src/spec --swagger-path ./docs/swagger.yaml --services-path ./src/services"Read service-ts documentation for command details.
Axios Client
The example below generates Axios HTTP client with Superstruct models from ./spec.yaml speicification and puts generated code to ./src/spec.
"specgen": "specgen client-ts --client axios --validation superstruct --spec-file ./spec.yaml --generate-path ./src/spec"Read client-ts documentation for command details.
Superstruct Models
The example below generates Superstruct models from ./spec.yaml speicification and puts generated code to ./src/spec.
"specgen": "specgen models-ts --validation superstruct --spec-file ./spec.yaml --generate-path ./src/spec"Read models-ts documentation for command details.
io-ts Models
The example below generates io-ts models from ./spec.yaml speicification and puts generated code to ./src/spec.
"specgen": "specgen models-ts --validation io-ts --spec-file ./spec.yaml --generate-path ./src/spec"Read models-ts documentation for command details.
Last updated