swagger-to-interfaces
TypeScript icon, indicating that this package has built-in type declarations

1.0.261 • Public • Published

Swagger to Interfaces

Super lightweight typescript interface generator.

✨Generate Typescript interfaces, enums and api endpoints straight from the swagger

WHY ?!

Swagger codegen allows you to generate a whole heavyweight full-blown client.
Most of us frontend developers do not use it.
We want to use our regular flow and loose the hassle of duplicating the server's schema.
swagger-to-interfaces is a lightweight library designed to auto generate only data which is useful.


⭐Installation

  npm install swagger-to-interfaces

⭐Usage


  swagger-to-interfaces --pathToFile=/path/to/json/swagger.json --destinationFolderName=./src/swagger2Ts

⭐Environment

Arguments: Include the following args on activation:

  • --destinationFolderName

  • --pathToFile OR --swaggerUrl

OR

.env file: create a .env file at the root directory of your project with the following variables:

⭐File generation


Three files will be created:

  1. interfaces.ts interfaces.ts all model definitions in Typescript interfaces.

  2. endpoints.ts endpoints.ts

    [operationId: string]: {
       method: 'post' | 'get' | 'put' | 'delete';
       url?: string;
       getUrl?: (...params: any) => string};
       contentType?: string;
    }
  3. enums.ts enums.ts all enum definitions.


Common flow

♙ Get the swagger json from the server.

♘ Add it to the UI project (for example: /path/to/json/swagger).

♗ Add a hook to your package.json scripts section:

"build-swagger": "swagger-to-typescript --pathToFile=/path/to/json/swagger"

♕ Add this hook to your build process:

"prebuild": "npm run build-swagger"

♔ Make sure to commit your swagger.json, so the UI build is not dependent on external resources.

view in npmjs.org

Readme

Keywords

Package Sidebar

Install

npm i swagger-to-interfaces

Weekly Downloads

37

Version

1.0.261

License

ISC

Unpacked Size

54.1 kB

Total Files

33

Last publish

Collaborators

  • galbh