You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

swagger-to-interfaces

Package Overview
Dependencies
Maintainers
1
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-to-interfaces

Super *lightweight* typescript interface generator. > ✨Generate Typescript interfaces, enums and api endpoints straight from the swagger

1.0.261
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

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:

  • DESTINATION_FOLDER_NAME=./src/swagger2Ts

  • SWAGGER_FILE_PATH=./swagger.json or SWAGGER_URL=https://petstore.swagger.io/v2/swagger.json

⭐File generation

Three files will be created:

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

  • endpoints.ts endpoints.ts

    [operationId: string]: {
       method: 'post' | 'get' | 'put' | 'delete';
       url?: string;
       getUrl?: (...params: any) => string};
       contentType?: string;
    }
    
  • 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

Keywords

typescript

FAQs

Package last updated on 05 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts