0.1.0 • Published 4 years ago

generator-swagger-tap v0.1.0

Weekly downloads
2
License
ISC
Repository
-
Last release
4 years ago

Swagger-tap

Swagger-tap is a tool used to generate code from swagger API definition with fine-grained customization support. The following features are provided:

  • Auto model type definitions generation
  • Auto fetch methods generation
  • Customize the methods generation with plugins
  • Auto mock data generation
  • Customize mock data generation for a specific path

Usage

swagger-tap generate -i /path/to/swagger.json -o /destination/path -c /path/to/swagger-tap-config.js

The above command will read data from the provided path/url and generate the related code to the destination path. The generated output should have the following structures:

api
├── types
│   ├── service.ts
|   └── configuration.ts
├── callers
│   ├── service.ts
|   └── configuration.ts
└── mock
    ├── service.mock.json
    └── configuration.mock.json

Auto model type definitions generation

Swagger-tap will read the definitions section of the specified swagger.json file and generate corresponding type definitions under the api/types folders.

Auto fetch methods generation

Swagger-tap will read the paths section of the specified swagger.json file and generate corresponding fetch methods under the api/callers method. Name of the file are the tag specified in each group, and the paths with the same tag are grouped into the exact same file.

Customize the methods generaton with plugins

Developers can specify a bunch of plugins used for fetch methods generation. When swagger-tap start to generate code for a specific path, it will walk down the plugins list, if a plugin has a match(regex or exact string equation), this plugin will receive the context object (path) and return a fetch method definition.

export default {
  plugins: [
    {
      match: '/services/{id}',
      method: 'GET',
      generator: (ctx) => {
        return ```
          const fetchMethod => () => {
            ....
          }
        ```
      }
    }
  ]
}

Auto mock data generation

Developers and specify mock data for a specific route or let swagger-tap auto-generate for them.

export default {
  plugins: [],
  mocks: [
    {
      match: '/services/{id}',
      auto: true
    },
    {
      match: '/configurations/{id}',
      generator: (params) => {
        // return mock data for this route according to the parameters
      }
    }
  ]
}