0.5.9 • Published 6 months ago

sw2ts-generate v0.5.9

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

sw2ts-generate


Install

$ npm install -g sw2ts-generate

or, install with --save-dev option to use in npm scripts.

Usage

Usage: sw2ts [options] [--] [filename]

Options:
  -o, --out <file>                output filename.
  -d, --dir <path>                output dirname.
  --url <url>                     input json schema from the url.
  -r, --required                  set property is required.
  -c, --config <file>             set configuration file path.
  -p, --proxy <path>              set proxy path.
  -n, --namespace <items...>      set namespace separated list.
  -m, --mode <mode>               set compilation mode (typescript | javascript).
  • filename should be swagger.json file.

Example

# 解析在线 swagger.json
$ sw2ts -o output --url https://petstore.swagger.io/v2/swagger.json -r

# 解析本地 swagger.json
$ sw2ts -o output -d schema swagger.json

# 通过 -r, 强制指定属性全部必传
$ sw2ts -o output -d schema -r swagger.json

# 通过 -n, 指定命名空间,生成部分API
$ sw2ts -o output --url https://petstore.swagger.io/v2/swagger.json -r -n /pet /user/login

Usage Tips

Use sw2ts in your script.

Code Sample:

import * as ts from 'typescript';

import * as sw2dts from '../index';

// @ts-ignore
const callback  = (data: any) => sw2dts.default()(data);

// @ts-ignore
const compilerOptions = { module: ts.ModuleKind.ES2015 };

// @ts-ignore
function fecth (url: string, code: string) {
	sw2dts.setConfSw2ts({
		proxy: 'api',
		file: '',
		namespace: []
	});

	if (code === 'typescript') {
		// generate typescript code
		sw2dts.readSchemaFromUrl(url).then(r => sw2dts.Generate2API(r, 'mock'));
	} else {
		sw2dts.readSchemaFromUrl(url).then(callback).then(r => {
			// generate javascript code
			sw2dts.createTransfer({ compilerOptions })(r.paths, 'release/index.js');
		});
	}
}

// ts-node --files  .\src\tests\index.ts
fecth('https://petstore.swagger.io/v2/swagger.json', 'typescript');

Format

prettierprettier-eslint

// 可以通过重写 .eslintrc.js  .prettierrc.js 实现自定义格式化效果
module.exports = {
	'singleQuote': true,
	'printWidth': 120,
	'prettier.tabWidth': 2,
	'prettier.semi': true,
	'proseWrap': 'never',
	'prettier.useTabs': false,
	'parser': 'babel-ts',
	'prettier.bracketSpacing': true,
}

Notes

/**
 * http://10.4.196.168:31621/v2/api-docs
 * path: /pet/{state.petId}
 * 本插件,示例如下:
 * 即将 body 中的参数,置于 payload 字段下,便于使用;
 * 由此可能导致请求参数命名不能出现 payload 名称,请注意;
 */
export function postPetPetId<T = any> (
	state: {
		payload?: FormData;
		petId: number;
	},
	config?: RequestConfig
) {
	return API.post<T>(`/api/pet/${state.petId}`, state.payload, config);
}

/**
 * 键名包含 id 的属性,通常是 number 类型,
 * 不好设置有效的默认值,故本插件统一重置为可选属性
 * 插件生成示例代码如下
 */
declare interface Order {
	id?: number; // int64
	petId?: number; // int64
	quantity: number; // int32
	shipDate: string; // date-time
	/* Order Status */
	status: OrderStatus;
	complete: boolean;
}

How to build

npm run build
0.5.8

6 months ago

0.5.7

6 months ago

0.5.9

6 months ago

0.5.6

10 months ago

0.5.5

10 months ago

0.5.4

10 months ago

0.5.3

1 year ago

0.4.9

1 year ago

0.4.8

1 year ago

0.5.0

1 year ago

0.5.2

1 year ago

0.5.1

1 year ago

0.4.5

1 year ago

0.4.4

1 year ago

0.4.7

1 year ago

0.4.6

1 year ago

0.4.1

1 year ago

0.4.0

1 year ago

0.4.3

1 year ago

0.4.2

1 year ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.7

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.3

3 years ago

0.2.17

3 years ago

0.2.9-alpha.1

3 years ago

0.2.9-alpha.0

3 years ago

0.2.9-alpha.3

3 years ago

0.2.9-alpha.2

3 years ago

0.2.9-alpha.5

3 years ago

0.2.9-alpha.4

3 years ago

0.2.16

3 years ago

0.2.9-alpha.7

3 years ago

0.2.15

3 years ago

0.2.9-alpha.6

3 years ago

0.2.14

3 years ago

0.2.13

3 years ago

0.2.9-alpha.8

3 years ago

0.2.12

3 years ago

0.2.11

3 years ago

0.2.10

3 years ago

0.2.7

3 years ago

0.2.6

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.2.5

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.8

3 years ago

0.2.3

3 years ago

0.1.4

3 years ago

0.2.2

3 years ago

0.1.6

3 years ago

0.2.4

3 years ago

0.1.5

3 years ago

0.1.0

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.1.3

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago