1.0.3 • Published 10 months ago

yunfeidog-openapi2typescript v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

介绍

GitHub Repo stars npm (scoped) GitHub tag (latest SemVer pre-release)

根据 OpenApi3 文档生成 request 请求代码。

如果你使用 umi ,你可以使用@umijs/plugin-openapi 插件。 本项目参考了:https://github.com/chenshuai2144/openapi2typescript 在此基础上进行了一些修改。 新增功能如下:

状态说明
支持用户自定义接口模版
系统内置多种请求库模版,如:axios、umi-request

使用

pnpm i --save-dev yunfeidog-openapi2typescript

在项目根目录新建 openapi.config.ts

import {generateService} from "yunfeidog-openapi2typescript";

generateService({
    schemaPath: `${__dirname}/test/openapi.json`,
    serversPath: './test/gen/umi',
    requestLibrary: 'umi',
    customServicePath: 'test/custom-service-template'
});

供公司使用:

import {generateService} from "yunfeidog-openapi2typescript";

let service = ['auth', 'baas', 'docshare', 'did', 'generator', 'governance']
for (let i = 0; i < service.length; i++) {
    generateService({
        schemaPath: `http://192.168.2.141:18001/${service[i]}/v3/api-docs`,
        serversPath: `./casll/${service[i]}`,
        requestLibrary: 'axios',
        requestImportStatement: 'import { request } from "@/utils/http"',
    });
}

package.jsonscript 中添加 api: "openapi": "ts-node openapi.config.ts",

生成api

pnpm run openapi

参数

属性必填备注类型默认值
requestLibPath自定义请求方法路径string-
requestLibrary默认支持的请求库'umi' 、'axios'、 'fetch'、 'request'、 'custom'umi
customServicePath如果requestLibrary为custom,那么需要传入自定义的Controller位置string
requestOptionsType自定义请求方法 options 参数类型string{key: string: any}
requestImportStatement自定义请求方法表达式string-
apiPrefixapi 的前缀string-
serversPath生成的文件夹的路径string-
schemaPathSwagger 2.0 或 OpenAPI 3.0 的地址string-
projectName项目名称string-
namespace命名空间名称stringAPI
mockFoldermock目录string-
enumStyle枚举样式string-literal | enumstring-literal
nullable使用null代替可选booleanfalse
dataFieldsresponse中数据字段string[]-
isCamelCase小驼峰命名文件和请求函数booleantrue
hook自定义 hookCustom Hook-

Custom Hook

属性类型说明
afterOpenApiDataInited(openAPIData: OpenAPIObject) => OpenAPIObject-
customFunctionName(data: APIDataType) => string自定义请求方法函数名称
customTypeName(data: APIDataType) => string自定义类型名称
customClassName(tagName: string) => string自定义类名
customType(schemaObject: SchemaObject | undefined,namespace: string,originGetType:(schemaObject: SchemaObject | undefined, namespace: string) => string,) => string自定义获取类型 返回非字符串将使用默认方法获取type
customFileNames(operationObject: OperationObject,apiPath: string,_apiMethod: string,) => string[]自定义生成文件名,可返回多个,表示生成多个文件. 返回为空,则使用默认的获取方法获取