0.2.18 • Published 11 months ago
openapi-schema-code v0.2.18
介绍
使用
npm i --save-dev openapi-schema-code
在项目根目录新建 osc.config.ts
const { generate } = require('openapi-schema-code')
generate({
schemaPath: 'http://petstore.swagger.io/v2/swagger.json',
serversPath: './servers',
requestImportStatement: 'import request from "@/utils/request"',
// apiPrefix: "'apiPrefix'",
projectName: 'proa',
single: true,
})
在 package.json
的 script
中添加 api: "osc": "ts-node osc.config.ts",
生成api
npm run openapi
参数
export type GenerateProps = {
requestLibPath?: string;
requestImportStatement?: string;
/**
* api 的前缀
*/
apiPrefix?:
| string
| ((params: {
path: string;
method: string;
namespace: string;
functionName: string;
autoExclude?: boolean;
}) => string);
/**
* 生成的文件夹的路径
*/
serversPath?: string;
/**
* Swagger 2.0 或 OpenAPI 3.0 的地址
*/
schemaPath?: string;
/**
* 项目名称
*/
projectName?: string;
hook?: {
/** 自定义函数名称 */
customFunctionName?: (data: OperationObject) => string;
/** 自定义类型名称 */
customTypeName?: (data: OperationObject) => string;
/** 自定义类名 */
customClassName?: (tagName: string) => string;
/**
* 自定义过滤器
*/
customeFilter?: (api: APIDataType) => boolean;
};
namespace?: string;
/**
* 默认为false,true时使用null代替可选
*/
nullable?: boolean;
mockFolder?: string;
/**
* 模板文件的文件路径
*/
templatesFolder?: string;
/**
* 枚举样式
*/
enumStyle?: 'string-literal' | 'enum';
/**
* 单接口 单文件
*/
single?: boolean;
/**
* 是否需要 index文件统一导出,默认不需要,方便做tree-shaking
*/
ableCombineIndex?: boolean;
}
- diff 不做删除 只做新增:
- interface 文件需要预生成一个interface.json 记录当前全量的 types array, 下次生成时 只做 重名update & 新插入
- 文件单个生成
- 模板 使用js 文件
- 增加 filter 来过滤接口
- 接口(文件)名自定义, 重名文件如何处理