0.5.6 • Published 3 months ago

sw2ts-generate v0.5.6

Weekly downloads
-
License
MIT
Repository
-
Last release
3 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.6

3 months ago

0.5.5

3 months ago

0.5.4

4 months ago

0.5.3

6 months ago

0.4.9

10 months ago

0.4.8

10 months ago

0.5.0

10 months ago

0.5.2

9 months ago

0.5.1

10 months ago

0.4.5

11 months ago

0.4.4

11 months ago

0.4.7

11 months ago

0.4.6

11 months ago

0.4.1

11 months ago

0.4.0

12 months ago

0.4.3

11 months ago

0.4.2

11 months ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.7

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.3

2 years ago

0.2.17

2 years ago

0.2.9-alpha.1

2 years ago

0.2.9-alpha.0

2 years ago

0.2.9-alpha.3

2 years ago

0.2.9-alpha.2

2 years ago

0.2.9-alpha.5

2 years ago

0.2.9-alpha.4

2 years ago

0.2.16

2 years ago

0.2.9-alpha.7

2 years ago

0.2.15

2 years ago

0.2.9-alpha.6

2 years ago

0.2.14

2 years ago

0.2.13

2 years ago

0.2.9-alpha.8

2 years ago

0.2.12

2 years ago

0.2.11

2 years ago

0.2.10

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.9

2 years ago

0.2.8

2 years ago

0.2.5

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.8

2 years ago

0.2.3

2 years ago

0.1.4

2 years ago

0.2.2

2 years ago

0.1.6

2 years ago

0.2.4

2 years ago

0.1.5

2 years ago

0.1.0

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.1.3

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago