1.3.17 • Published 21 days ago
@1szx1/vite-plugin-swagger2ts v1.3.17
vite-plugin-swagger2ts
使用流程
- 安装
# npm i vite-plugin-swagger2ts -D
# or
# yarn add vite-plugin-swagger2ts -D
# or
pnpm add vite-plugin-swagger2ts -D
- 配置到
vite.config.ts
// vite.config.ts
import ViteSwagger2ts from "vite-plugin-swagger2ts";
export default {
plugins: [
ViteSwagger2ts({
swaggerUrl: "url", // swagger-resources URL
output: "pathToSave", // not require, default './src/swagger.ts'
formatSchema: (schema) => {
// not require, deal with schema. such as unwrapper common response
if ("properties" in schema) {
const properties = schema["properties"];
if ("code" in properties && ("msg" in properties || "message" in properties) && "data" in properties) {
return properties["data"];
}
}
return schema;
}
})
]
};
插件流程
- 通过配置的 url 请求
${baseUrl}/swagger-resources
地址,获取微服务的 resources 地址; - 通过 resources 地址请求 swagger 具体文档;
- 将 swagger 接口生成具体的 typescript 类型:
interface SwaggerInterface {
// SwaggerInterface start
url: {
get: {
param: { path: { id: string }; query: { status: number }; body: { name: string } };
reponse: { id: string; name: string; status: number };
};
};
}
- 附加 typescrip 转换模版内容:
import { PathsSwaggerInterface } from "./swagger.ts";
export type UrlKey = keyof PathsSwaggerInterface;
export type MethodKey<U extends UrlKey> = string & keyof PathsSwaggerInterface[U];
type SwaggerInterfaceSingle<U extends UrlKey, M extends MethodKey<U>> = PathsSwaggerInterface[U][M];
type SwaggerField<U extends UrlKey, M extends MethodKey<U>> = keyof SwaggerInterfaceSingle<U, M>;
type SwaggerFieldType<U extends UrlKey, M extends MethodKey<U>, F extends SwaggerField<U, M>> = SwaggerInterfaceSingle<U, M>[F];
export type Param<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "param" & SwaggerField<U, M>>;
export type Response<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "response" & SwaggerField<U, M>>;
- 将生成的文件保存到项目指定位置,项目通过以下代码进行使用类型提示;
import type { PathKey, MethodKey, Param, Respone } from "configfile";
export function customFetch<P extends PathKey, M extends MethodKey<P>>(path: P, method: M, params: Param<P, M>): Promise<Respone<P, M>> {
// custome code
return "" as any;
}
- 业务代码通过引用
customFetch
进行使用
License
MIT License © 2021 morelearn1990
1.3.17
21 days ago
1.3.16
2 months ago
1.3.14
2 months ago
1.3.15
2 months ago
1.3.13
3 months ago
1.3.12
3 months ago
1.3.11
3 months ago
1.3.10
4 months ago
1.3.9
4 months ago
1.2.0
8 months ago
1.1.1
11 months ago
1.1.0
11 months ago
1.3.7
8 months ago
1.3.6
8 months ago
1.3.5
8 months ago
1.1.7
8 months ago
1.3.4
8 months ago
1.1.6
10 months ago
1.3.3
8 months ago
1.1.5
10 months ago
1.3.2
8 months ago
1.2.3
8 months ago
1.1.4
10 months ago
1.3.1
8 months ago
1.2.2
8 months ago
1.1.3
10 months ago
1.3.0
8 months ago
1.2.1
8 months ago
1.1.2
10 months ago
1.3.8
8 months ago
1.0.19
12 months ago
1.0.18
1 year ago
1.0.17
1 year ago
1.0.16
1 year ago
1.0.9
1 year ago
1.0.8
1 year ago
1.0.7
1 year ago
1.0.6
1 year ago
1.0.11
1 year ago
1.0.10
1 year ago
1.0.15
1 year ago
1.0.14
1 year ago
1.0.13
1 year ago
1.0.12
1 year ago
1.0.5
1 year ago
1.0.4
1 year ago