@1szx1/vite-plugin-swagger2ts 1.3.20 → 1.3.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +95 -95
- package/dist/adapters.d.cts +19 -18
- package/dist/adapters.d.mts +19 -18
- package/dist/adapters.d.ts +19 -18
- package/dist/bin.d.cts +1 -1
- package/dist/bin.d.mts +1 -1
- package/dist/bin.d.ts +1 -1
- package/dist/index.cjs +38 -6
- package/dist/index.d.cts +60 -59
- package/dist/index.d.mts +60 -59
- package/dist/index.d.ts +60 -59
- package/dist/index.mjs +38 -6
- package/package.json +3 -2
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021 hannoeru
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 hannoeru
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
# vite-plugin-swagger2ts
|
|
2
|
-
|
|
3
|
-
[](https://www.npmjs.com/package/vite-plugin-swagger2ts)
|
|
4
|
-
[](https://www.npmjs.com/package/vite-plugin-swagger2ts)
|
|
5
|
-
[](https://github.com/hannoeru/vite-plugin-swagger2ts/blob/main/src/types.ts)
|
|
6
|
-
[](https://github.com/hannoeru/vite-plugin-swagger2ts/blob/main/LICENSE)
|
|
7
|
-
|
|
8
|
-
## 使用流程
|
|
9
|
-
|
|
10
|
-
1. 安装
|
|
11
|
-
|
|
12
|
-
```sh
|
|
13
|
-
# npm i vite-plugin-swagger2ts -D
|
|
14
|
-
# or
|
|
15
|
-
# yarn add vite-plugin-swagger2ts -D
|
|
16
|
-
# or
|
|
17
|
-
pnpm add vite-plugin-swagger2ts -D
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
2. 配置到 `vite.config.ts`
|
|
21
|
-
|
|
22
|
-
```ts
|
|
23
|
-
// vite.config.ts
|
|
24
|
-
import ViteSwagger2ts from "vite-plugin-swagger2ts";
|
|
25
|
-
|
|
26
|
-
export default {
|
|
27
|
-
plugins: [
|
|
28
|
-
ViteSwagger2ts({
|
|
29
|
-
swaggerUrl: "url", // swagger-resources URL
|
|
30
|
-
output: "pathToSave", // not require, default './src/swagger.ts'
|
|
31
|
-
formatSchema: (schema) => {
|
|
32
|
-
// not require, deal with schema. such as unwrapper common response
|
|
33
|
-
if ("properties" in schema) {
|
|
34
|
-
const properties = schema["properties"];
|
|
35
|
-
if ("code" in properties && ("msg" in properties || "message" in properties) && "data" in properties) {
|
|
36
|
-
return properties["data"];
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return schema;
|
|
40
|
-
}
|
|
41
|
-
})
|
|
42
|
-
]
|
|
43
|
-
};
|
|
44
|
-
```
|
|
45
|
-
|
|
46
|
-
## 插件流程
|
|
47
|
-
|
|
48
|
-
1. 通过配置的 url 请求 `${baseUrl}/swagger-resources` 地址,获取微服务的 resources 地址;
|
|
49
|
-
2. 通过 resources 地址请求 swagger 具体文档;
|
|
50
|
-
3. 将 swagger 接口生成具体的 typescript 类型:
|
|
51
|
-
|
|
52
|
-
```ts
|
|
53
|
-
interface SwaggerInterface {
|
|
54
|
-
// SwaggerInterface start
|
|
55
|
-
url: {
|
|
56
|
-
get: {
|
|
57
|
-
param: { path: { id: string }; query: { status: number }; body: { name: string } };
|
|
58
|
-
reponse: { id: string; name: string; status: number };
|
|
59
|
-
};
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
```
|
|
63
|
-
|
|
64
|
-
4. 附加 typescrip 转换模版内容:
|
|
65
|
-
|
|
66
|
-
```ts
|
|
67
|
-
import { PathsSwaggerInterface } from "./swagger.ts";
|
|
68
|
-
|
|
69
|
-
export type UrlKey = keyof PathsSwaggerInterface;
|
|
70
|
-
export type MethodKey<U extends UrlKey> = string & keyof PathsSwaggerInterface[U];
|
|
71
|
-
|
|
72
|
-
type SwaggerInterfaceSingle<U extends UrlKey, M extends MethodKey<U>> = PathsSwaggerInterface[U][M];
|
|
73
|
-
type SwaggerField<U extends UrlKey, M extends MethodKey<U>> = keyof SwaggerInterfaceSingle<U, M>;
|
|
74
|
-
type SwaggerFieldType<U extends UrlKey, M extends MethodKey<U>, F extends SwaggerField<U, M>> = SwaggerInterfaceSingle<U, M>[F];
|
|
75
|
-
|
|
76
|
-
export type Param<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "param" & SwaggerField<U, M>>;
|
|
77
|
-
export type Response<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "response" & SwaggerField<U, M>>;
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
5. 将生成的文件保存到项目指定位置,项目通过以下代码进行使用类型提示;
|
|
81
|
-
|
|
82
|
-
```ts
|
|
83
|
-
import type { PathKey, MethodKey, Param, Respone } from "configfile";
|
|
84
|
-
|
|
85
|
-
export function customFetch<P extends PathKey, M extends MethodKey<P>>(path: P, method: M, params: Param<P, M>): Promise<Respone<P, M>> {
|
|
86
|
-
// custome code
|
|
87
|
-
return "" as any;
|
|
88
|
-
}
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
6. 业务代码通过引用 `customFetch` 进行使用
|
|
92
|
-
|
|
93
|
-
## License
|
|
94
|
-
|
|
95
|
-
MIT License © 2021 [morelearn1990](https://github.com/morelearn1990)
|
|
1
|
+
# vite-plugin-swagger2ts
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/vite-plugin-swagger2ts)
|
|
4
|
+
[](https://www.npmjs.com/package/vite-plugin-swagger2ts)
|
|
5
|
+
[](https://github.com/hannoeru/vite-plugin-swagger2ts/blob/main/src/types.ts)
|
|
6
|
+
[](https://github.com/hannoeru/vite-plugin-swagger2ts/blob/main/LICENSE)
|
|
7
|
+
|
|
8
|
+
## 使用流程
|
|
9
|
+
|
|
10
|
+
1. 安装
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
# npm i vite-plugin-swagger2ts -D
|
|
14
|
+
# or
|
|
15
|
+
# yarn add vite-plugin-swagger2ts -D
|
|
16
|
+
# or
|
|
17
|
+
pnpm add vite-plugin-swagger2ts -D
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
2. 配置到 `vite.config.ts`
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// vite.config.ts
|
|
24
|
+
import ViteSwagger2ts from "vite-plugin-swagger2ts";
|
|
25
|
+
|
|
26
|
+
export default {
|
|
27
|
+
plugins: [
|
|
28
|
+
ViteSwagger2ts({
|
|
29
|
+
swaggerUrl: "url", // swagger-resources URL
|
|
30
|
+
output: "pathToSave", // not require, default './src/swagger.ts'
|
|
31
|
+
formatSchema: (schema) => {
|
|
32
|
+
// not require, deal with schema. such as unwrapper common response
|
|
33
|
+
if ("properties" in schema) {
|
|
34
|
+
const properties = schema["properties"];
|
|
35
|
+
if ("code" in properties && ("msg" in properties || "message" in properties) && "data" in properties) {
|
|
36
|
+
return properties["data"];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return schema;
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
]
|
|
43
|
+
};
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## 插件流程
|
|
47
|
+
|
|
48
|
+
1. 通过配置的 url 请求 `${baseUrl}/swagger-resources` 地址,获取微服务的 resources 地址;
|
|
49
|
+
2. 通过 resources 地址请求 swagger 具体文档;
|
|
50
|
+
3. 将 swagger 接口生成具体的 typescript 类型:
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
interface SwaggerInterface {
|
|
54
|
+
// SwaggerInterface start
|
|
55
|
+
url: {
|
|
56
|
+
get: {
|
|
57
|
+
param: { path: { id: string }; query: { status: number }; body: { name: string } };
|
|
58
|
+
reponse: { id: string; name: string; status: number };
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
4. 附加 typescrip 转换模版内容:
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { PathsSwaggerInterface } from "./swagger.ts";
|
|
68
|
+
|
|
69
|
+
export type UrlKey = keyof PathsSwaggerInterface;
|
|
70
|
+
export type MethodKey<U extends UrlKey> = string & keyof PathsSwaggerInterface[U];
|
|
71
|
+
|
|
72
|
+
type SwaggerInterfaceSingle<U extends UrlKey, M extends MethodKey<U>> = PathsSwaggerInterface[U][M];
|
|
73
|
+
type SwaggerField<U extends UrlKey, M extends MethodKey<U>> = keyof SwaggerInterfaceSingle<U, M>;
|
|
74
|
+
type SwaggerFieldType<U extends UrlKey, M extends MethodKey<U>, F extends SwaggerField<U, M>> = SwaggerInterfaceSingle<U, M>[F];
|
|
75
|
+
|
|
76
|
+
export type Param<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "param" & SwaggerField<U, M>>;
|
|
77
|
+
export type Response<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "response" & SwaggerField<U, M>>;
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
5. 将生成的文件保存到项目指定位置,项目通过以下代码进行使用类型提示;
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import type { PathKey, MethodKey, Param, Respone } from "configfile";
|
|
84
|
+
|
|
85
|
+
export function customFetch<P extends PathKey, M extends MethodKey<P>>(path: P, method: M, params: Param<P, M>): Promise<Respone<P, M>> {
|
|
86
|
+
// custome code
|
|
87
|
+
return "" as any;
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
6. 业务代码通过引用 `customFetch` 进行使用
|
|
92
|
+
|
|
93
|
+
## License
|
|
94
|
+
|
|
95
|
+
MIT License © 2021 [morelearn1990](https://github.com/morelearn1990)
|
package/dist/adapters.d.cts
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
+
import * as axios from 'axios';
|
|
1
2
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
3
|
import * as luch_request from 'luch-request';
|
|
3
4
|
import luch_request__default, { HttpRequestConfig, HttpResponse } from 'luch-request';
|
|
4
5
|
|
|
5
6
|
type Get<T, K> = T[K & keyof T];
|
|
6
7
|
|
|
7
|
-
declare function axiosAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: AxiosInstance): {
|
|
8
|
-
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (AxiosRequestConfig<any> & {
|
|
9
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
10
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
11
|
-
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
12
|
-
}) | undefined) => Promise<AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any> : T[U][M]["response" & keyof T[U][M]]>;
|
|
8
|
+
declare function axiosAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: AxiosInstance): {
|
|
9
|
+
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (AxiosRequestConfig<any, any> & {
|
|
10
|
+
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
11
|
+
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
12
|
+
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
13
|
+
}) | undefined) => Promise<(AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any, {}, any> : T[U][M]["response" & keyof T[U][M]]) extends infer T_1 ? T_1 extends (AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any, {}, any> : T[U][M]["response" & keyof T[U][M]]) ? T_1 extends axios.AxiosResponseDefault ? AxiosResponse<any, any, {}, any> : T_1 : never : never>;
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
declare function LuchAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: luch_request__default): {
|
|
16
|
-
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (HttpRequestConfig<luch_request.HttpTask> & {
|
|
17
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
18
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
19
|
-
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
20
|
-
}) | undefined) => Promise<AxiosRes extends true ? HttpResponse<T[U][M]["response" & keyof T[U][M]], luch_request.HttpTask> : T[U][M]["response" & keyof T[U][M]]>;
|
|
21
|
-
upload: <U_1 extends { [K in keyof T]: T[K] extends {
|
|
22
|
-
post: any;
|
|
23
|
-
} ? K : never; }[keyof T], P extends Get<T[U_1]["post"]["param" & keyof T[U_1]["post"]], "body" | "query">, N extends keyof P>(url: U_1 & string, config: HttpRequestConfig<UniApp.UploadTask> & {
|
|
24
|
-
name?: N | undefined;
|
|
25
|
-
formData?: Omit<P, N> | undefined;
|
|
26
|
-
}) => Promise<luch_request.HttpUploadResponse<T[U_1]["post"]["response" & keyof T[U_1]["post"]], luch_request.HttpTask>>;
|
|
16
|
+
declare function LuchAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: luch_request__default): {
|
|
17
|
+
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (HttpRequestConfig<luch_request.HttpTask> & {
|
|
18
|
+
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
19
|
+
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
20
|
+
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
21
|
+
}) | undefined) => Promise<AxiosRes extends true ? HttpResponse<T[U][M]["response" & keyof T[U][M]], luch_request.HttpTask> : T[U][M]["response" & keyof T[U][M]]>;
|
|
22
|
+
upload: <U_1 extends { [K in keyof T]: T[K] extends {
|
|
23
|
+
post: any;
|
|
24
|
+
} ? K : never; }[keyof T], P extends Get<T[U_1]["post"]["param" & keyof T[U_1]["post"]], "body" | "query">, N extends keyof P>(url: U_1 & string, config: HttpRequestConfig<UniApp.UploadTask> & {
|
|
25
|
+
name?: N | undefined;
|
|
26
|
+
formData?: Omit<P, N> | undefined;
|
|
27
|
+
}) => Promise<luch_request.HttpUploadResponse<T[U_1]["post"]["response" & keyof T[U_1]["post"]], luch_request.HttpTask>>;
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
export { LuchAdapter, axiosAdapter };
|
package/dist/adapters.d.mts
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
+
import * as axios from 'axios';
|
|
1
2
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
3
|
import * as luch_request from 'luch-request';
|
|
3
4
|
import luch_request__default, { HttpRequestConfig, HttpResponse } from 'luch-request';
|
|
4
5
|
|
|
5
6
|
type Get<T, K> = T[K & keyof T];
|
|
6
7
|
|
|
7
|
-
declare function axiosAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: AxiosInstance): {
|
|
8
|
-
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (AxiosRequestConfig<any> & {
|
|
9
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
10
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
11
|
-
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
12
|
-
}) | undefined) => Promise<AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any> : T[U][M]["response" & keyof T[U][M]]>;
|
|
8
|
+
declare function axiosAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: AxiosInstance): {
|
|
9
|
+
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (AxiosRequestConfig<any, any> & {
|
|
10
|
+
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
11
|
+
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
12
|
+
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
13
|
+
}) | undefined) => Promise<(AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any, {}, any> : T[U][M]["response" & keyof T[U][M]]) extends infer T_1 ? T_1 extends (AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any, {}, any> : T[U][M]["response" & keyof T[U][M]]) ? T_1 extends axios.AxiosResponseDefault ? AxiosResponse<any, any, {}, any> : T_1 : never : never>;
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
declare function LuchAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: luch_request__default): {
|
|
16
|
-
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (HttpRequestConfig<luch_request.HttpTask> & {
|
|
17
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
18
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
19
|
-
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
20
|
-
}) | undefined) => Promise<AxiosRes extends true ? HttpResponse<T[U][M]["response" & keyof T[U][M]], luch_request.HttpTask> : T[U][M]["response" & keyof T[U][M]]>;
|
|
21
|
-
upload: <U_1 extends { [K in keyof T]: T[K] extends {
|
|
22
|
-
post: any;
|
|
23
|
-
} ? K : never; }[keyof T], P extends Get<T[U_1]["post"]["param" & keyof T[U_1]["post"]], "body" | "query">, N extends keyof P>(url: U_1 & string, config: HttpRequestConfig<UniApp.UploadTask> & {
|
|
24
|
-
name?: N | undefined;
|
|
25
|
-
formData?: Omit<P, N> | undefined;
|
|
26
|
-
}) => Promise<luch_request.HttpUploadResponse<T[U_1]["post"]["response" & keyof T[U_1]["post"]], luch_request.HttpTask>>;
|
|
16
|
+
declare function LuchAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: luch_request__default): {
|
|
17
|
+
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (HttpRequestConfig<luch_request.HttpTask> & {
|
|
18
|
+
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
19
|
+
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
20
|
+
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
21
|
+
}) | undefined) => Promise<AxiosRes extends true ? HttpResponse<T[U][M]["response" & keyof T[U][M]], luch_request.HttpTask> : T[U][M]["response" & keyof T[U][M]]>;
|
|
22
|
+
upload: <U_1 extends { [K in keyof T]: T[K] extends {
|
|
23
|
+
post: any;
|
|
24
|
+
} ? K : never; }[keyof T], P extends Get<T[U_1]["post"]["param" & keyof T[U_1]["post"]], "body" | "query">, N extends keyof P>(url: U_1 & string, config: HttpRequestConfig<UniApp.UploadTask> & {
|
|
25
|
+
name?: N | undefined;
|
|
26
|
+
formData?: Omit<P, N> | undefined;
|
|
27
|
+
}) => Promise<luch_request.HttpUploadResponse<T[U_1]["post"]["response" & keyof T[U_1]["post"]], luch_request.HttpTask>>;
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
export { LuchAdapter, axiosAdapter };
|
package/dist/adapters.d.ts
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
|
+
import * as axios from 'axios';
|
|
1
2
|
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
2
3
|
import * as luch_request from 'luch-request';
|
|
3
4
|
import luch_request__default, { HttpRequestConfig, HttpResponse } from 'luch-request';
|
|
4
5
|
|
|
5
6
|
type Get<T, K> = T[K & keyof T];
|
|
6
7
|
|
|
7
|
-
declare function axiosAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: AxiosInstance): {
|
|
8
|
-
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (AxiosRequestConfig<any> & {
|
|
9
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
10
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
11
|
-
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
12
|
-
}) | undefined) => Promise<AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any> : T[U][M]["response" & keyof T[U][M]]>;
|
|
8
|
+
declare function axiosAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: AxiosInstance): {
|
|
9
|
+
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (AxiosRequestConfig<any, any> & {
|
|
10
|
+
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
11
|
+
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
12
|
+
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
13
|
+
}) | undefined) => Promise<(AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any, {}, any> : T[U][M]["response" & keyof T[U][M]]) extends infer T_1 ? T_1 extends (AxiosRes extends true ? AxiosResponse<T[U][M]["response" & keyof T[U][M]], any, {}, any> : T[U][M]["response" & keyof T[U][M]]) ? T_1 extends axios.AxiosResponseDefault ? AxiosResponse<any, any, {}, any> : T_1 : never : never>;
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
declare function LuchAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: luch_request__default): {
|
|
16
|
-
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (HttpRequestConfig<luch_request.HttpTask> & {
|
|
17
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
18
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
19
|
-
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
20
|
-
}) | undefined) => Promise<AxiosRes extends true ? HttpResponse<T[U][M]["response" & keyof T[U][M]], luch_request.HttpTask> : T[U][M]["response" & keyof T[U][M]]>;
|
|
21
|
-
upload: <U_1 extends { [K in keyof T]: T[K] extends {
|
|
22
|
-
post: any;
|
|
23
|
-
} ? K : never; }[keyof T], P extends Get<T[U_1]["post"]["param" & keyof T[U_1]["post"]], "body" | "query">, N extends keyof P>(url: U_1 & string, config: HttpRequestConfig<UniApp.UploadTask> & {
|
|
24
|
-
name?: N | undefined;
|
|
25
|
-
formData?: Omit<P, N> | undefined;
|
|
26
|
-
}) => Promise<luch_request.HttpUploadResponse<T[U_1]["post"]["response" & keyof T[U_1]["post"]], luch_request.HttpTask>>;
|
|
16
|
+
declare function LuchAdapter<T extends Record<string, any> /**类型集合 */, AxiosRes = true /**返回类型是否包含Response */>(httpInstance: luch_request__default): {
|
|
17
|
+
request: <U extends keyof T, M extends string & keyof T[U]>(url: U & string, method: M, param?: Get<T[U][M]["param" & keyof T[U][M]], M extends "get" ? "query" : "body"> | undefined, config?: (HttpRequestConfig<luch_request.HttpTask> & {
|
|
18
|
+
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
19
|
+
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
20
|
+
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
21
|
+
}) | undefined) => Promise<AxiosRes extends true ? HttpResponse<T[U][M]["response" & keyof T[U][M]], luch_request.HttpTask> : T[U][M]["response" & keyof T[U][M]]>;
|
|
22
|
+
upload: <U_1 extends { [K in keyof T]: T[K] extends {
|
|
23
|
+
post: any;
|
|
24
|
+
} ? K : never; }[keyof T], P extends Get<T[U_1]["post"]["param" & keyof T[U_1]["post"]], "body" | "query">, N extends keyof P>(url: U_1 & string, config: HttpRequestConfig<UniApp.UploadTask> & {
|
|
25
|
+
name?: N | undefined;
|
|
26
|
+
formData?: Omit<P, N> | undefined;
|
|
27
|
+
}) => Promise<luch_request.HttpUploadResponse<T[U_1]["post"]["response" & keyof T[U_1]["post"]], luch_request.HttpTask>>;
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
export { LuchAdapter, axiosAdapter };
|
package/dist/bin.d.cts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
export {
|
|
2
|
+
export { };
|
package/dist/bin.d.mts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
export {
|
|
2
|
+
export { };
|
package/dist/bin.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
export {
|
|
2
|
+
export { };
|
package/dist/index.cjs
CHANGED
|
@@ -148,27 +148,46 @@ const TYPES = {
|
|
|
148
148
|
string: "string",
|
|
149
149
|
array: "array"
|
|
150
150
|
};
|
|
151
|
+
function isNullSchema(s) {
|
|
152
|
+
if (!s || typeof s !== "object")
|
|
153
|
+
return false;
|
|
154
|
+
if (s.type === "null")
|
|
155
|
+
return true;
|
|
156
|
+
if (Array.isArray(s.type) && s.type.length === 1 && s.type[0] === "null")
|
|
157
|
+
return true;
|
|
158
|
+
if (Array.isArray(s.type) && s.type.includes("null") && s.enum && s.enum.length === 1 && s.enum[0] === null)
|
|
159
|
+
return true;
|
|
160
|
+
if (s.enum && s.enum.length === 1 && s.enum[0] === null && (!s.type || s.type === "null" || Array.isArray(s.type) && s.type.includes("null")))
|
|
161
|
+
return true;
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
151
164
|
function generatorSchemaType(key, schema, { isRoot = false, isRequired, isExport = false, docsName = "" } = {}) {
|
|
152
165
|
if (isTypeAny(schema)) {
|
|
153
166
|
return `${key && `
|
|
154
167
|
${key}${isRequired === false ? "?" : ""}: `}any${key && `;`}`;
|
|
155
168
|
}
|
|
156
|
-
const { type, $ref, enum: Enum, items, properties, oneOf, additionalProperties, required, allOf } = schema;
|
|
169
|
+
const { type, $ref, enum: Enum, items, properties, oneOf, anyOf, additionalProperties, required, allOf } = schema;
|
|
157
170
|
let result = "";
|
|
158
171
|
if ($ref) {
|
|
159
172
|
result = getRefName($ref, docsName);
|
|
160
173
|
} else if (Enum) {
|
|
161
|
-
result = `${Enum.map((t) => `"${t}"`).join(" | ")}`;
|
|
174
|
+
result = `${Enum.map((t) => typeof t === "string" ? `"${t}"` : `${t}`).join(" | ")}`;
|
|
162
175
|
} else if (items) {
|
|
163
176
|
result = `${generatorSchemaType("", items, { docsName })}[]`;
|
|
164
177
|
} else if (properties && !isTypeAny(properties)) {
|
|
165
178
|
result += `{${Object.entries(properties).map(([pName, _schema]) => generatorSchemaType(pName, _schema, { isRequired: !required || required.includes(pName) ? true : false, docsName })).join("")}
|
|
166
179
|
}`;
|
|
167
180
|
} else if (oneOf) {
|
|
168
|
-
|
|
181
|
+
const validOneOf = oneOf.filter((_schema) => !isNullSchema(_schema));
|
|
182
|
+
const list = validOneOf.length > 0 ? validOneOf : oneOf;
|
|
183
|
+
result = `${list.map((_schema) => `${generatorSchemaType("", _schema, { docsName })}`).join(" | ")}`;
|
|
184
|
+
} else if (anyOf) {
|
|
185
|
+
const validAnyOf = anyOf.filter((_schema) => !isNullSchema(_schema));
|
|
186
|
+
const list = validAnyOf.length > 0 ? validAnyOf : anyOf;
|
|
187
|
+
result = `${list.map((_schema) => `${generatorSchemaType("", _schema, { docsName })}`).join(" | ")}`;
|
|
169
188
|
} else if (allOf) {
|
|
170
189
|
result = `${allOf.map((_schema) => generatorSchemaType("", _schema, { docsName })).join(" & ")}`;
|
|
171
|
-
} else if (type === "object" && !result) {
|
|
190
|
+
} else if ((type === "object" || Array.isArray(type) && type.includes("object")) && !result) {
|
|
172
191
|
if (additionalProperties) {
|
|
173
192
|
result = `{${generatorSchemaType("[x: string | number]", additionalProperties, { docsName })}
|
|
174
193
|
}`;
|
|
@@ -176,7 +195,18 @@ ${key}${isRequired === false ? "?" : ""}: `}any${key && `;`}`;
|
|
|
176
195
|
result = "{[x: string | number]: any}";
|
|
177
196
|
}
|
|
178
197
|
} else {
|
|
179
|
-
|
|
198
|
+
if (Array.isArray(type)) {
|
|
199
|
+
const types = type.filter((t) => t !== "null");
|
|
200
|
+
if (types.length > 0) {
|
|
201
|
+
result = types.map((t) => TYPES[t] || "string").join(" | ");
|
|
202
|
+
} else if (type.includes("null")) {
|
|
203
|
+
result = "null";
|
|
204
|
+
} else {
|
|
205
|
+
result = "string";
|
|
206
|
+
}
|
|
207
|
+
} else {
|
|
208
|
+
result = TYPES[type] || "string";
|
|
209
|
+
}
|
|
180
210
|
}
|
|
181
211
|
if (!key) {
|
|
182
212
|
return result;
|
|
@@ -190,9 +220,11 @@ function generatorParameterType(schemas, docsName) {
|
|
|
190
220
|
}
|
|
191
221
|
function generatorRequestBodyType(schemas, docsName) {
|
|
192
222
|
const { description, content, required } = schemas;
|
|
193
|
-
|
|
223
|
+
let schema = content && Object.values(content)[0]?.["schema"];
|
|
194
224
|
if (!schema)
|
|
195
225
|
return "";
|
|
226
|
+
if ("properties" in schema)
|
|
227
|
+
schema = { ...schema, required: schema.required || [] };
|
|
196
228
|
const result = generatorSchemaType("", schema, { docsName });
|
|
197
229
|
return jsDoc({ description }, { key: "body", result, isRequired: required });
|
|
198
230
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,66 +1,67 @@
|
|
|
1
1
|
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
|
|
2
2
|
import { Plugin } from 'vite';
|
|
3
3
|
|
|
4
|
-
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
-
interface Options {
|
|
6
|
-
/**
|
|
7
|
-
* 文档域名地址
|
|
8
|
-
*/
|
|
9
|
-
swaggerUrl?: string;
|
|
10
|
-
/**
|
|
11
|
-
* 文档json地址
|
|
12
|
-
*/
|
|
13
|
-
jsonUrl?: string;
|
|
14
|
-
/**
|
|
15
|
-
* openApi 本地json
|
|
16
|
-
*/
|
|
17
|
-
jsonPath?: string;
|
|
18
|
-
/**
|
|
19
|
-
* 加载json的方法
|
|
20
|
-
*/
|
|
21
|
-
jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
|
|
22
|
-
output?: string;
|
|
23
|
-
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
24
|
-
formatSchema?: FormatSchema;
|
|
25
|
-
glob?: {
|
|
26
|
-
namespace: string;
|
|
27
|
-
output?: string;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
type UserOptions = Options | {
|
|
31
|
-
sources: Options[];
|
|
32
|
-
};
|
|
33
|
-
interface ResolvedOptions extends Options {
|
|
34
|
-
output: string;
|
|
35
|
-
}
|
|
36
|
-
type ExportPlugin = Plugin;
|
|
37
|
-
interface SwaggerSource {
|
|
38
|
-
name: string;
|
|
39
|
-
url: string;
|
|
40
|
-
swaggerVersion: string;
|
|
41
|
-
location: string;
|
|
42
|
-
}
|
|
43
|
-
interface SwaggerDoc {
|
|
44
|
-
basePath: string;
|
|
45
|
-
swagger: string;
|
|
46
|
-
paths: {
|
|
47
|
-
[key: string]: any;
|
|
48
|
-
};
|
|
49
|
-
definitions: {
|
|
50
|
-
[key: string]: any;
|
|
51
|
-
};
|
|
52
|
-
host: string;
|
|
53
|
-
info: any;
|
|
54
|
-
tags: any[];
|
|
55
|
-
securityDefinitions: any;
|
|
56
|
-
}
|
|
57
|
-
interface GenerateDocOption {
|
|
58
|
-
docsName: string;
|
|
59
|
-
baseUrl: string;
|
|
60
|
-
formatSchema?: FormatSchema;
|
|
4
|
+
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* 文档域名地址
|
|
8
|
+
*/
|
|
9
|
+
swaggerUrl?: string;
|
|
10
|
+
/**
|
|
11
|
+
* 文档json地址
|
|
12
|
+
*/
|
|
13
|
+
jsonUrl?: string;
|
|
14
|
+
/**
|
|
15
|
+
* openApi 本地json
|
|
16
|
+
*/
|
|
17
|
+
jsonPath?: string;
|
|
18
|
+
/**
|
|
19
|
+
* 加载json的方法
|
|
20
|
+
*/
|
|
21
|
+
jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
|
|
22
|
+
output?: string;
|
|
23
|
+
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
24
|
+
formatSchema?: FormatSchema;
|
|
25
|
+
glob?: {
|
|
26
|
+
namespace: string;
|
|
27
|
+
output?: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
type UserOptions = Options | {
|
|
31
|
+
sources: Options[];
|
|
32
|
+
};
|
|
33
|
+
interface ResolvedOptions extends Options {
|
|
34
|
+
output: string;
|
|
35
|
+
}
|
|
36
|
+
type ExportPlugin = Plugin;
|
|
37
|
+
interface SwaggerSource {
|
|
38
|
+
name: string;
|
|
39
|
+
url: string;
|
|
40
|
+
swaggerVersion: string;
|
|
41
|
+
location: string;
|
|
42
|
+
}
|
|
43
|
+
interface SwaggerDoc {
|
|
44
|
+
basePath: string;
|
|
45
|
+
swagger: string;
|
|
46
|
+
paths: {
|
|
47
|
+
[key: string]: any;
|
|
48
|
+
};
|
|
49
|
+
definitions: {
|
|
50
|
+
[key: string]: any;
|
|
51
|
+
};
|
|
52
|
+
host: string;
|
|
53
|
+
info: any;
|
|
54
|
+
tags: any[];
|
|
55
|
+
securityDefinitions: any;
|
|
56
|
+
}
|
|
57
|
+
interface GenerateDocOption {
|
|
58
|
+
docsName: string;
|
|
59
|
+
baseUrl: string;
|
|
60
|
+
formatSchema?: FormatSchema;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
63
|
+
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
64
64
|
declare function defineConfig(userOptions?: UserOptions): UserOptions | undefined;
|
|
65
65
|
|
|
66
|
-
export {
|
|
66
|
+
export { swagger2TsPlugin as default, defineConfig };
|
|
67
|
+
export type { ExportPlugin, GenerateDocOption, Options, ResolvedOptions, SwaggerDoc, SwaggerSource, UserOptions };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,66 +1,67 @@
|
|
|
1
1
|
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
|
|
2
2
|
import { Plugin } from 'vite';
|
|
3
3
|
|
|
4
|
-
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
-
interface Options {
|
|
6
|
-
/**
|
|
7
|
-
* 文档域名地址
|
|
8
|
-
*/
|
|
9
|
-
swaggerUrl?: string;
|
|
10
|
-
/**
|
|
11
|
-
* 文档json地址
|
|
12
|
-
*/
|
|
13
|
-
jsonUrl?: string;
|
|
14
|
-
/**
|
|
15
|
-
* openApi 本地json
|
|
16
|
-
*/
|
|
17
|
-
jsonPath?: string;
|
|
18
|
-
/**
|
|
19
|
-
* 加载json的方法
|
|
20
|
-
*/
|
|
21
|
-
jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
|
|
22
|
-
output?: string;
|
|
23
|
-
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
24
|
-
formatSchema?: FormatSchema;
|
|
25
|
-
glob?: {
|
|
26
|
-
namespace: string;
|
|
27
|
-
output?: string;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
type UserOptions = Options | {
|
|
31
|
-
sources: Options[];
|
|
32
|
-
};
|
|
33
|
-
interface ResolvedOptions extends Options {
|
|
34
|
-
output: string;
|
|
35
|
-
}
|
|
36
|
-
type ExportPlugin = Plugin;
|
|
37
|
-
interface SwaggerSource {
|
|
38
|
-
name: string;
|
|
39
|
-
url: string;
|
|
40
|
-
swaggerVersion: string;
|
|
41
|
-
location: string;
|
|
42
|
-
}
|
|
43
|
-
interface SwaggerDoc {
|
|
44
|
-
basePath: string;
|
|
45
|
-
swagger: string;
|
|
46
|
-
paths: {
|
|
47
|
-
[key: string]: any;
|
|
48
|
-
};
|
|
49
|
-
definitions: {
|
|
50
|
-
[key: string]: any;
|
|
51
|
-
};
|
|
52
|
-
host: string;
|
|
53
|
-
info: any;
|
|
54
|
-
tags: any[];
|
|
55
|
-
securityDefinitions: any;
|
|
56
|
-
}
|
|
57
|
-
interface GenerateDocOption {
|
|
58
|
-
docsName: string;
|
|
59
|
-
baseUrl: string;
|
|
60
|
-
formatSchema?: FormatSchema;
|
|
4
|
+
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* 文档域名地址
|
|
8
|
+
*/
|
|
9
|
+
swaggerUrl?: string;
|
|
10
|
+
/**
|
|
11
|
+
* 文档json地址
|
|
12
|
+
*/
|
|
13
|
+
jsonUrl?: string;
|
|
14
|
+
/**
|
|
15
|
+
* openApi 本地json
|
|
16
|
+
*/
|
|
17
|
+
jsonPath?: string;
|
|
18
|
+
/**
|
|
19
|
+
* 加载json的方法
|
|
20
|
+
*/
|
|
21
|
+
jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
|
|
22
|
+
output?: string;
|
|
23
|
+
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
24
|
+
formatSchema?: FormatSchema;
|
|
25
|
+
glob?: {
|
|
26
|
+
namespace: string;
|
|
27
|
+
output?: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
type UserOptions = Options | {
|
|
31
|
+
sources: Options[];
|
|
32
|
+
};
|
|
33
|
+
interface ResolvedOptions extends Options {
|
|
34
|
+
output: string;
|
|
35
|
+
}
|
|
36
|
+
type ExportPlugin = Plugin;
|
|
37
|
+
interface SwaggerSource {
|
|
38
|
+
name: string;
|
|
39
|
+
url: string;
|
|
40
|
+
swaggerVersion: string;
|
|
41
|
+
location: string;
|
|
42
|
+
}
|
|
43
|
+
interface SwaggerDoc {
|
|
44
|
+
basePath: string;
|
|
45
|
+
swagger: string;
|
|
46
|
+
paths: {
|
|
47
|
+
[key: string]: any;
|
|
48
|
+
};
|
|
49
|
+
definitions: {
|
|
50
|
+
[key: string]: any;
|
|
51
|
+
};
|
|
52
|
+
host: string;
|
|
53
|
+
info: any;
|
|
54
|
+
tags: any[];
|
|
55
|
+
securityDefinitions: any;
|
|
56
|
+
}
|
|
57
|
+
interface GenerateDocOption {
|
|
58
|
+
docsName: string;
|
|
59
|
+
baseUrl: string;
|
|
60
|
+
formatSchema?: FormatSchema;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
63
|
+
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
64
64
|
declare function defineConfig(userOptions?: UserOptions): UserOptions | undefined;
|
|
65
65
|
|
|
66
|
-
export {
|
|
66
|
+
export { swagger2TsPlugin as default, defineConfig };
|
|
67
|
+
export type { ExportPlugin, GenerateDocOption, Options, ResolvedOptions, SwaggerDoc, SwaggerSource, UserOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,66 +1,67 @@
|
|
|
1
1
|
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
|
|
2
2
|
import { Plugin } from 'vite';
|
|
3
3
|
|
|
4
|
-
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
-
interface Options {
|
|
6
|
-
/**
|
|
7
|
-
* 文档域名地址
|
|
8
|
-
*/
|
|
9
|
-
swaggerUrl?: string;
|
|
10
|
-
/**
|
|
11
|
-
* 文档json地址
|
|
12
|
-
*/
|
|
13
|
-
jsonUrl?: string;
|
|
14
|
-
/**
|
|
15
|
-
* openApi 本地json
|
|
16
|
-
*/
|
|
17
|
-
jsonPath?: string;
|
|
18
|
-
/**
|
|
19
|
-
* 加载json的方法
|
|
20
|
-
*/
|
|
21
|
-
jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
|
|
22
|
-
output?: string;
|
|
23
|
-
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
24
|
-
formatSchema?: FormatSchema;
|
|
25
|
-
glob?: {
|
|
26
|
-
namespace: string;
|
|
27
|
-
output?: string;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
type UserOptions = Options | {
|
|
31
|
-
sources: Options[];
|
|
32
|
-
};
|
|
33
|
-
interface ResolvedOptions extends Options {
|
|
34
|
-
output: string;
|
|
35
|
-
}
|
|
36
|
-
type ExportPlugin = Plugin;
|
|
37
|
-
interface SwaggerSource {
|
|
38
|
-
name: string;
|
|
39
|
-
url: string;
|
|
40
|
-
swaggerVersion: string;
|
|
41
|
-
location: string;
|
|
42
|
-
}
|
|
43
|
-
interface SwaggerDoc {
|
|
44
|
-
basePath: string;
|
|
45
|
-
swagger: string;
|
|
46
|
-
paths: {
|
|
47
|
-
[key: string]: any;
|
|
48
|
-
};
|
|
49
|
-
definitions: {
|
|
50
|
-
[key: string]: any;
|
|
51
|
-
};
|
|
52
|
-
host: string;
|
|
53
|
-
info: any;
|
|
54
|
-
tags: any[];
|
|
55
|
-
securityDefinitions: any;
|
|
56
|
-
}
|
|
57
|
-
interface GenerateDocOption {
|
|
58
|
-
docsName: string;
|
|
59
|
-
baseUrl: string;
|
|
60
|
-
formatSchema?: FormatSchema;
|
|
4
|
+
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* 文档域名地址
|
|
8
|
+
*/
|
|
9
|
+
swaggerUrl?: string;
|
|
10
|
+
/**
|
|
11
|
+
* 文档json地址
|
|
12
|
+
*/
|
|
13
|
+
jsonUrl?: string;
|
|
14
|
+
/**
|
|
15
|
+
* openApi 本地json
|
|
16
|
+
*/
|
|
17
|
+
jsonPath?: string;
|
|
18
|
+
/**
|
|
19
|
+
* 加载json的方法
|
|
20
|
+
*/
|
|
21
|
+
jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
|
|
22
|
+
output?: string;
|
|
23
|
+
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
24
|
+
formatSchema?: FormatSchema;
|
|
25
|
+
glob?: {
|
|
26
|
+
namespace: string;
|
|
27
|
+
output?: string;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
type UserOptions = Options | {
|
|
31
|
+
sources: Options[];
|
|
32
|
+
};
|
|
33
|
+
interface ResolvedOptions extends Options {
|
|
34
|
+
output: string;
|
|
35
|
+
}
|
|
36
|
+
type ExportPlugin = Plugin;
|
|
37
|
+
interface SwaggerSource {
|
|
38
|
+
name: string;
|
|
39
|
+
url: string;
|
|
40
|
+
swaggerVersion: string;
|
|
41
|
+
location: string;
|
|
42
|
+
}
|
|
43
|
+
interface SwaggerDoc {
|
|
44
|
+
basePath: string;
|
|
45
|
+
swagger: string;
|
|
46
|
+
paths: {
|
|
47
|
+
[key: string]: any;
|
|
48
|
+
};
|
|
49
|
+
definitions: {
|
|
50
|
+
[key: string]: any;
|
|
51
|
+
};
|
|
52
|
+
host: string;
|
|
53
|
+
info: any;
|
|
54
|
+
tags: any[];
|
|
55
|
+
securityDefinitions: any;
|
|
56
|
+
}
|
|
57
|
+
interface GenerateDocOption {
|
|
58
|
+
docsName: string;
|
|
59
|
+
baseUrl: string;
|
|
60
|
+
formatSchema?: FormatSchema;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
63
|
+
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
64
64
|
declare function defineConfig(userOptions?: UserOptions): UserOptions | undefined;
|
|
65
65
|
|
|
66
|
-
export {
|
|
66
|
+
export { swagger2TsPlugin as default, defineConfig };
|
|
67
|
+
export type { ExportPlugin, GenerateDocOption, Options, ResolvedOptions, SwaggerDoc, SwaggerSource, UserOptions };
|
package/dist/index.mjs
CHANGED
|
@@ -139,27 +139,46 @@ const TYPES = {
|
|
|
139
139
|
string: "string",
|
|
140
140
|
array: "array"
|
|
141
141
|
};
|
|
142
|
+
function isNullSchema(s) {
|
|
143
|
+
if (!s || typeof s !== "object")
|
|
144
|
+
return false;
|
|
145
|
+
if (s.type === "null")
|
|
146
|
+
return true;
|
|
147
|
+
if (Array.isArray(s.type) && s.type.length === 1 && s.type[0] === "null")
|
|
148
|
+
return true;
|
|
149
|
+
if (Array.isArray(s.type) && s.type.includes("null") && s.enum && s.enum.length === 1 && s.enum[0] === null)
|
|
150
|
+
return true;
|
|
151
|
+
if (s.enum && s.enum.length === 1 && s.enum[0] === null && (!s.type || s.type === "null" || Array.isArray(s.type) && s.type.includes("null")))
|
|
152
|
+
return true;
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
142
155
|
function generatorSchemaType(key, schema, { isRoot = false, isRequired, isExport = false, docsName = "" } = {}) {
|
|
143
156
|
if (isTypeAny(schema)) {
|
|
144
157
|
return `${key && `
|
|
145
158
|
${key}${isRequired === false ? "?" : ""}: `}any${key && `;`}`;
|
|
146
159
|
}
|
|
147
|
-
const { type, $ref, enum: Enum, items, properties, oneOf, additionalProperties, required, allOf } = schema;
|
|
160
|
+
const { type, $ref, enum: Enum, items, properties, oneOf, anyOf, additionalProperties, required, allOf } = schema;
|
|
148
161
|
let result = "";
|
|
149
162
|
if ($ref) {
|
|
150
163
|
result = getRefName($ref, docsName);
|
|
151
164
|
} else if (Enum) {
|
|
152
|
-
result = `${Enum.map((t) => `"${t}"`).join(" | ")}`;
|
|
165
|
+
result = `${Enum.map((t) => typeof t === "string" ? `"${t}"` : `${t}`).join(" | ")}`;
|
|
153
166
|
} else if (items) {
|
|
154
167
|
result = `${generatorSchemaType("", items, { docsName })}[]`;
|
|
155
168
|
} else if (properties && !isTypeAny(properties)) {
|
|
156
169
|
result += `{${Object.entries(properties).map(([pName, _schema]) => generatorSchemaType(pName, _schema, { isRequired: !required || required.includes(pName) ? true : false, docsName })).join("")}
|
|
157
170
|
}`;
|
|
158
171
|
} else if (oneOf) {
|
|
159
|
-
|
|
172
|
+
const validOneOf = oneOf.filter((_schema) => !isNullSchema(_schema));
|
|
173
|
+
const list = validOneOf.length > 0 ? validOneOf : oneOf;
|
|
174
|
+
result = `${list.map((_schema) => `${generatorSchemaType("", _schema, { docsName })}`).join(" | ")}`;
|
|
175
|
+
} else if (anyOf) {
|
|
176
|
+
const validAnyOf = anyOf.filter((_schema) => !isNullSchema(_schema));
|
|
177
|
+
const list = validAnyOf.length > 0 ? validAnyOf : anyOf;
|
|
178
|
+
result = `${list.map((_schema) => `${generatorSchemaType("", _schema, { docsName })}`).join(" | ")}`;
|
|
160
179
|
} else if (allOf) {
|
|
161
180
|
result = `${allOf.map((_schema) => generatorSchemaType("", _schema, { docsName })).join(" & ")}`;
|
|
162
|
-
} else if (type === "object" && !result) {
|
|
181
|
+
} else if ((type === "object" || Array.isArray(type) && type.includes("object")) && !result) {
|
|
163
182
|
if (additionalProperties) {
|
|
164
183
|
result = `{${generatorSchemaType("[x: string | number]", additionalProperties, { docsName })}
|
|
165
184
|
}`;
|
|
@@ -167,7 +186,18 @@ ${key}${isRequired === false ? "?" : ""}: `}any${key && `;`}`;
|
|
|
167
186
|
result = "{[x: string | number]: any}";
|
|
168
187
|
}
|
|
169
188
|
} else {
|
|
170
|
-
|
|
189
|
+
if (Array.isArray(type)) {
|
|
190
|
+
const types = type.filter((t) => t !== "null");
|
|
191
|
+
if (types.length > 0) {
|
|
192
|
+
result = types.map((t) => TYPES[t] || "string").join(" | ");
|
|
193
|
+
} else if (type.includes("null")) {
|
|
194
|
+
result = "null";
|
|
195
|
+
} else {
|
|
196
|
+
result = "string";
|
|
197
|
+
}
|
|
198
|
+
} else {
|
|
199
|
+
result = TYPES[type] || "string";
|
|
200
|
+
}
|
|
171
201
|
}
|
|
172
202
|
if (!key) {
|
|
173
203
|
return result;
|
|
@@ -181,9 +211,11 @@ function generatorParameterType(schemas, docsName) {
|
|
|
181
211
|
}
|
|
182
212
|
function generatorRequestBodyType(schemas, docsName) {
|
|
183
213
|
const { description, content, required } = schemas;
|
|
184
|
-
|
|
214
|
+
let schema = content && Object.values(content)[0]?.["schema"];
|
|
185
215
|
if (!schema)
|
|
186
216
|
return "";
|
|
217
|
+
if ("properties" in schema)
|
|
218
|
+
schema = { ...schema, required: schema.required || [] };
|
|
187
219
|
const result = generatorSchemaType("", schema, { docsName });
|
|
188
220
|
return jsDoc({ description }, { key: "body", result, isRequired: required });
|
|
189
221
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1szx1/vite-plugin-swagger2ts",
|
|
3
3
|
"description": "forked from https://github.com/morelearn1990/vite-plugin-swagger2ts",
|
|
4
|
-
"version": "1.3.
|
|
4
|
+
"version": "1.3.22",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "1SZX1 <2414126198@qq.com>",
|
|
7
7
|
"repository": {
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
"homepage": "https://github.com/1SZX1/vite-plugin-swagger2ts",
|
|
12
12
|
"bugs": "https://github.com/1SZX1/vite-plugin-swagger2ts/issues",
|
|
13
13
|
"publishConfig": {
|
|
14
|
-
"registry": "https://registry.npmjs.org"
|
|
14
|
+
"registry": "https://registry.npmjs.org",
|
|
15
|
+
"access": "public"
|
|
15
16
|
},
|
|
16
17
|
"files": [
|
|
17
18
|
"dist"
|