4 Star 0 Fork 0

longxiaobaiWJ / sw2ts-generate

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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
  • 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
MIT License Copyright (c) 2021 longxiaobaiWJ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

sw2ts-generate 展开 收起
TypeScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/frontend_site/sw2ts-generate.git
git@gitee.com:frontend_site/sw2ts-generate.git
frontend_site
sw2ts-generate
sw2ts-generate
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891