Skip to content

AchyuthMohan/open-api-generator-node

Repository files navigation

Open API DTO Generator (Node)

Small TypeScript utility that generates TypeScript DTOs (interfaces or classes) from an OpenAPI 3.x spec.

Requirements

  • 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 tsx

Usage

Run the generator (this project exposes an npm start script):

npm start

What the start script does:

  • Runs the generator against openapi.yaml in the repository root.
  • Uses --style=nest to produce class-based DTOs with basic class-validator decorators.
  • 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.ts

Generate 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.yaml first, then openapi.yaml in the repo root.
    • Reads info.title from the spec and converts it to a snake_cased filename suffixed with .dto.ts.
      • Example: Weather Data APIweather_data_api.dto.ts.
    • Runs the generator and writes the DTOs to that file.

You can still run the generator directly if you prefer custom filenames or styles.

Options

  • --style=interface|class|nest — Choose output style. nest adds basic class-validator imports and decorator markers.
  • --no-export — When generating interfaces, omit the export keyword.

Notes

  • The generator reads components/schemas from 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 nest style the generator emits imports for class-validator decorators, but it only adds minimal decorator calls — you may want to extend these for full validation rules.

Example

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors