npm

Spec TypeScript code generation is packaged in form of npm package. Add following dependency to your project:

% npm install --dev specgen.io

The has specgen command line tool is built inside of the npm package.

Add the following scriptarrow-up-right 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 specgen

This page provides some examples of specgen scripts configuration.

Express Service

The example below generates Expressarrow-up-right server with Superstructarrow-up-right 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 koaarrow-up-right server with io-tsarrow-up-right 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 Axiosarrow-up-right HTTP client with Superstructarrow-up-right models from ./spec.yaml speicification and puts generated code to ./src/spec.

Read client-ts documentation for command details.

Superstruct Models

The example below generates Superstructarrow-up-right models from ./spec.yaml speicification and puts generated code to ./src/spec.

Read models-ts documentation for command details.

io-ts Models

The example below generates io-tsarrow-up-right models from ./spec.yaml speicification and puts generated code to ./src/spec.

Read models-ts documentation for command details.

Last updated