@1szx1/vite-plugin-swagger2ts 1.0.4

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 ADDED
@@ -0,0 +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.
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # vite-plugin-swagger2ts
2
+
3
+ [![npm version](https://badgen.net/npm/v/vite-plugin-swagger2ts)](https://www.npmjs.com/package/vite-plugin-swagger2ts)
4
+ [![monthly downloads](https://badgen.net/npm/dm/vite-plugin-swagger2ts)](https://www.npmjs.com/package/vite-plugin-swagger2ts)
5
+ [![types](https://badgen.net/npm/types/vite-plugin-swagger2ts)](https://github.com/hannoeru/vite-plugin-swagger2ts/blob/main/src/types.ts)
6
+ [![license](https://badgen.net/npm/license/vite-plugin-swagger2ts)](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
+ prettierPath: "prettierPath", // not require, default '.prettierrc' or 'prettier.json'
32
+ formatSchema: (schema) => {
33
+ // not require, deal with schema. such as unwrapper common response
34
+ if ("properties" in schema) {
35
+ const properties = schema["properties"];
36
+ if ("code" in properties && ("msg" in properties || "message" in properties) && "data" in properties) {
37
+ return properties["data"];
38
+ }
39
+ }
40
+ return schema;
41
+ }
42
+ })
43
+ ]
44
+ };
45
+ ```
46
+
47
+ ## 插件流程
48
+
49
+ 1. 通过配置的 url 请求 `${baseUrl}/swagger-resources` 地址,获取微服务的 resources 地址;
50
+ 2. 通过 resources 地址请求 swagger 具体文档;
51
+ 3. 将 swagger 接口生成具体的 typescript 类型:
52
+
53
+ ```ts
54
+ interface SwaggerInterface {
55
+ // SwaggerInterface start
56
+ url: {
57
+ get: {
58
+ param: { path: { id: string }; query: { status: number }; body: { name: string } };
59
+ reponse: { id: string; name: string; status: number };
60
+ };
61
+ };
62
+ }
63
+ ```
64
+
65
+ 4. 附加 typescrip 转换模版内容:
66
+
67
+ ```ts
68
+ import { PathsSwaggerInterface } from "./swagger.ts";
69
+
70
+ export type UrlKey = keyof PathsSwaggerInterface;
71
+ export type MethodKey<U extends UrlKey> = string & keyof PathsSwaggerInterface[U];
72
+
73
+ type SwaggerInterfaceSingle<U extends UrlKey, M extends MethodKey<U>> = PathsSwaggerInterface[U][M];
74
+ type SwaggerField<U extends UrlKey, M extends MethodKey<U>> = keyof SwaggerInterfaceSingle<U, M>;
75
+ type SwaggerFieldType<U extends UrlKey, M extends MethodKey<U>, F extends SwaggerField<U, M>> = SwaggerInterfaceSingle<U, M>[F];
76
+
77
+ export type Param<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "param" & SwaggerField<U, M>>;
78
+ export type Response<U extends UrlKey, M extends MethodKey<U>> = SwaggerFieldType<U, M, "response" & SwaggerField<U, M>>;
79
+ ```
80
+
81
+ 5. 将生成的文件保存到项目指定位置,项目通过以下代码进行使用类型提示;
82
+
83
+ ```ts
84
+ import type { PathKey, MethodKey, Param, Respone } from "configfile";
85
+
86
+ export function customFetch<P extends PathKey, M extends MethodKey<P>>(path: P, method: M, params: Param<P, M>): Promise<Respone<P, M>> {
87
+ // custome code
88
+ return "" as any;
89
+ }
90
+ ```
91
+
92
+ 6. 业务代码通过引用 `customFetch` 进行使用
93
+
94
+ ## License
95
+
96
+ MIT License © 2021 [morelearn1990](https://github.com/morelearn1990)
@@ -0,0 +1,45 @@
1
+ import { SchemaObject, OpenAPIObject } from 'openapi3-ts';
2
+ import { Plugin } from 'vite';
3
+
4
+ declare type FormatSchema = (schema: SchemaObject) => SchemaObject;
5
+ interface Options {
6
+ swaggerUrl: string;
7
+ output?: string;
8
+ prettierPath?: string;
9
+ formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
10
+ formatSchema?: FormatSchema;
11
+ }
12
+ declare type UserOptions = Options;
13
+ interface ResolvedOptions extends Options {
14
+ output: string;
15
+ }
16
+ declare type ExportPlugin = Required<Pick<Plugin, "name" | "enforce" | "apply">>;
17
+ interface SwaggerSource {
18
+ name: string;
19
+ url: string;
20
+ swaggerVersion: string;
21
+ location: string;
22
+ }
23
+ interface SwaggerDoc {
24
+ basePath: string;
25
+ swagger: string;
26
+ paths: {
27
+ [key: string]: any;
28
+ };
29
+ definitions: {
30
+ [key: string]: any;
31
+ };
32
+ host: string;
33
+ info: any;
34
+ tags: any[];
35
+ securityDefinitions: any;
36
+ }
37
+ interface GenerateDocOption {
38
+ docsName: string;
39
+ baseUrl: string;
40
+ formatSchema?: FormatSchema;
41
+ }
42
+
43
+ declare function swagger2TsPlugin(userOptions: UserOptions): ExportPlugin;
44
+
45
+ export { ExportPlugin, GenerateDocOption, ResolvedOptions, SwaggerDoc, SwaggerSource, UserOptions, swagger2TsPlugin as default };