Small TypeScript utility that generates TypeScript DTOs (interfaces or classes) from an OpenAPI 3.x spec.
- Node 18+ (or any Node that supports ES modules)
tsx(used to run the TypeScript script directly)js-yaml(already listed as a dependency)
Install tsx locally if you don't have it globally:
npm install --save-dev tsxRun the generator (this project exposes an npm start script):
npm startWhat the start script does:
- Runs the generator against
openapi.yamlin the repository root. - Uses
--style=nestto produce class-based DTOs with basicclass-validatordecorators. - Redirects stdout into
weather.dto.ts.
You can run the generator directly (with different options):
# generate interfaces (default)
tsx generator.ts openapi.yaml --style=interface > dtos.interface.ts
# generate plain classes
tsx generator.ts openapi.yaml --style=class > dtos.class.ts
# generate Nest-style classes (decorators) and export DTOs
tsx generator.ts openapi.yaml --style=nest > dtos.nest.ts
# prevent exported interfaces/classes (useful for embedding into other modules)
tsx generator.ts openapi.yaml --no-export > dtos.partial.tsGenerate script
This repository includes an automated wrapper script that detects your OpenAPI file and generates a DTO file whose name is derived from the OpenAPI info.title.
- Run:
npm run generate-glint-dtos- Behavior:
- Looks for
openapi/openapi.yamlfirst, thenopenapi.yamlin the repo root. - Reads
info.titlefrom the spec and converts it to a snake_cased filename suffixed with.dto.ts.- Example:
Weather Data API→weather_data_api.dto.ts.
- Example:
- Runs the generator and writes the DTOs to that file.
- Looks for
You can still run the generator directly if you prefer custom filenames or styles.
--style=interface|class|nest— Choose output style.nestadds basicclass-validatorimports and decorator markers.--no-export— When generating interfaces, omit theexportkeyword.
- The generator reads
components/schemasfrom the provided OpenAPI spec and will exit with an error if no schemas are found. - The generated output contains a header with the source file name and generation timestamp.
- For
neststyle the generator emits imports forclass-validatordecorators, but it only adds minimal decorator calls — you may want to extend these for full validation rules.
After running npm start you'll find weather.dto.ts (per the current start script) containing generated DTOs for the schemas defined in openapi.yaml.
Generated by the small TypeScript OpenAPI → DTO generator included in this repo.