lexmin0412 / swagger2ts

A tool for transfer swagger to ts, available both for API and CLI

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@lexmin0412/swagger2ts

NPM version NPM downloads

A tool for transfer swagger to ts, available both for API and CLI.

Install

# install in current project, better for API usage
npm install @lexmin0412/swagger2ts
# install global, better for CLI usage
npm install @lexmin0412/swagger2ts -g

Usage

API Usage

import path from 'path'
import { gen } from '@lexmin0412/swagger2ts'

const rootDir = path.resolve(__dirname)
const requestInstancePath = path.resolve(__dirname, 'src', 'request.ts')

gen({
  rootDir,
  requestInstancePath,
  excludeDirs: [
    'assets',
  ],
  transformConfig: {
    var2TypeMap: {
      'string': [
        'id',
        "parent_id"
      ],
      'string[]': [
        'ids'
      ]
    },
  }
})

CLI Usage

npx s2c ./

Options

interface GenOptions {
	/**
	 * root dir for excuting tasks to generate files
	 */
	rootDir: string
	/**
	 * path for your custom request instance
	 */
	requestInstancePath: string
	/**
	 * excluded paths when scanning *.swagger.json files
	 */
	excludeDirs: string[]
	/**
	 * type transforming configuration
	 */
	transformConfig: {
		/**
		 * variable type transforming map
		 */
		var2TypeMap: {
			[key: string]: string[]
		}
		/**
		 * variable list that should be transformed to string
		 */
		toStringVars?: string[]
	}
}

About

A tool for transfer swagger to ts, available both for API and CLI


Languages

Language:TypeScript 98.3%Language:JavaScript 1.7%