@1szx1/vite-plugin-swagger2ts 1.1.7 → 1.2.0
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/dist/adapters.d.cts +29 -0
- package/dist/adapters.d.mts +29 -0
- package/dist/adapters.d.ts +18 -18
- package/dist/bin.d.cts +2 -0
- package/dist/bin.d.mts +2 -0
- package/dist/bin.d.ts +1 -1
- package/dist/index.cjs +64 -39
- package/dist/index.d.cts +54 -0
- package/dist/index.d.mts +54 -0
- package/dist/index.d.ts +48 -40
- package/dist/index.mjs +64 -39
- package/package.json +2 -2
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import * as Request from 'luch-request';
|
|
3
|
+
import Request__default, { HttpRequestConfig } from 'luch-request';
|
|
4
|
+
|
|
5
|
+
type Get<T, K> = T[K & keyof T];
|
|
6
|
+
|
|
7
|
+
declare function axiosAdapter<T>(httpInstance: AxiosInstance): {
|
|
8
|
+
request: <U extends string & keyof T, M extends string & keyof T[U]>(url: U, 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<T[U][M]["response" & keyof T[U][M]]>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare function LuchAdapter<T extends Record<string, any>, C extends HttpRequestConfig = HttpRequestConfig>(httpInstance: 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<Request.HttpTask> & C & {
|
|
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<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<T[U_1]["post"]["response" & keyof T[U_1]["post"]]>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { LuchAdapter, axiosAdapter };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import * as Request from 'luch-request';
|
|
3
|
+
import Request__default, { HttpRequestConfig } from 'luch-request';
|
|
4
|
+
|
|
5
|
+
type Get<T, K> = T[K & keyof T];
|
|
6
|
+
|
|
7
|
+
declare function axiosAdapter<T>(httpInstance: AxiosInstance): {
|
|
8
|
+
request: <U extends string & keyof T, M extends string & keyof T[U]>(url: U, 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<T[U][M]["response" & keyof T[U][M]]>;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
declare function LuchAdapter<T extends Record<string, any>, C extends HttpRequestConfig = HttpRequestConfig>(httpInstance: 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<Request.HttpTask> & C & {
|
|
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<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<T[U_1]["post"]["response" & keyof T[U_1]["post"]]>;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { LuchAdapter, axiosAdapter };
|
package/dist/adapters.d.ts
CHANGED
|
@@ -4,26 +4,26 @@ import Request__default, { HttpRequestConfig } from 'luch-request';
|
|
|
4
4
|
|
|
5
5
|
type Get<T, K> = T[K & keyof T];
|
|
6
6
|
|
|
7
|
-
declare function axiosAdapter<T>(httpInstance: AxiosInstance): {
|
|
8
|
-
request: <U extends string & keyof T, M extends string & keyof T[U]>(url: U, 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<T[U][M]["response" & keyof T[U][M]]>;
|
|
7
|
+
declare function axiosAdapter<T>(httpInstance: AxiosInstance): {
|
|
8
|
+
request: <U extends string & keyof T, M extends string & keyof T[U]>(url: U, 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<T[U][M]["response" & keyof T[U][M]]>;
|
|
13
13
|
};
|
|
14
14
|
|
|
15
|
-
declare function LuchAdapter<T extends Record<string, any>, C extends HttpRequestConfig = HttpRequestConfig>(httpInstance: 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<Request.HttpTask> & C & {
|
|
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<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<T[U_1]["post"]["response" & keyof T[U_1]["post"]]>;
|
|
15
|
+
declare function LuchAdapter<T extends Record<string, any>, C extends HttpRequestConfig = HttpRequestConfig>(httpInstance: 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<Request.HttpTask> & C & {
|
|
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<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<T[U_1]["post"]["response" & keyof T[U_1]["post"]]>;
|
|
27
27
|
};
|
|
28
28
|
|
|
29
29
|
export { LuchAdapter, axiosAdapter };
|
package/dist/bin.d.cts
ADDED
package/dist/bin.d.mts
ADDED
package/dist/bin.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
|
|
2
|
-
export {
|
|
2
|
+
export { }
|
package/dist/index.cjs
CHANGED
|
@@ -28,10 +28,13 @@ function loadConfig() {
|
|
|
28
28
|
async function resolveOptions(userOptions) {
|
|
29
29
|
const { config } = await loadConfig();
|
|
30
30
|
userOptions = userOptions || config || {};
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
const options = "sources" in userOptions ? userOptions.sources : [userOptions];
|
|
32
|
+
return options.map((item) => {
|
|
33
|
+
const { output } = item;
|
|
34
|
+
const root = process.cwd();
|
|
35
|
+
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
36
|
+
return Object.assign({ output: path.resolve(root, outputFile) }, item);
|
|
37
|
+
});
|
|
35
38
|
}
|
|
36
39
|
async function fetchUrl(url) {
|
|
37
40
|
return await fetch__default(url).then((res) => res.json());
|
|
@@ -221,44 +224,66 @@ function convertObjPromise(docs) {
|
|
|
221
224
|
});
|
|
222
225
|
});
|
|
223
226
|
}
|
|
224
|
-
function
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (docs.swagger)
|
|
234
|
-
openapiDocs = await convertObjPromise(openapiDocs);
|
|
235
|
-
else
|
|
236
|
-
throw new Error(JSON.stringify(docs));
|
|
237
|
-
}
|
|
238
|
-
const apistrings = generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
239
|
-
return apistrings;
|
|
240
|
-
} catch (error) {
|
|
241
|
-
console.log(`%c[vite-plugin-swagger2ts]: ${docsName} error`, "color: #ff0000;", error);
|
|
242
|
-
return "";
|
|
243
|
-
}
|
|
227
|
+
async function loadSwaggerSource(options) {
|
|
228
|
+
const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
|
|
229
|
+
async function genInterface(docs, docsName) {
|
|
230
|
+
let openapiDocs = formatDocs ? formatDocs(docs) : docs;
|
|
231
|
+
if (!("openapi" in openapiDocs)) {
|
|
232
|
+
if (docs.swagger)
|
|
233
|
+
openapiDocs = await convertObjPromise(openapiDocs);
|
|
234
|
+
else
|
|
235
|
+
throw new Error(JSON.stringify(docs));
|
|
244
236
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
237
|
+
return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
238
|
+
}
|
|
239
|
+
async function genCode(source) {
|
|
240
|
+
const { url, name } = source;
|
|
241
|
+
try {
|
|
242
|
+
const docs = await fetchUrl(`${swaggerUrl}${url}`);
|
|
243
|
+
return genInterface(docs, name);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
console.log(`%c[vite-plugin-swagger2ts]: ${name} error`, "color: #ff0000;", error);
|
|
246
|
+
return "";
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async function getSources() {
|
|
250
|
+
let sources2 = void 0;
|
|
251
|
+
let interfaces2 = [];
|
|
252
|
+
if (typeof swaggerUrl === "string") {
|
|
253
|
+
const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
|
|
254
|
+
if (Array.isArray(resources)) {
|
|
255
|
+
sources2 = resources;
|
|
256
|
+
interfaces2.push(...await Promise.all(sources2.map(genCode)));
|
|
257
|
+
} else {
|
|
258
|
+
console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
|
|
255
259
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
+
}
|
|
261
|
+
if (typeof jsonUrl === "string") {
|
|
262
|
+
const swaggerJson = await fetchUrl(jsonUrl);
|
|
263
|
+
if (!swaggerJson.info)
|
|
264
|
+
return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
|
|
265
|
+
interfaces2.push(await genInterface(swaggerJson, swaggerJson.info.title.replace(" ", "_")));
|
|
266
|
+
}
|
|
267
|
+
return { sources: sources2, interfaces: interfaces2 };
|
|
260
268
|
}
|
|
261
|
-
|
|
269
|
+
const { sources, interfaces } = await getSources() || {};
|
|
270
|
+
if (!interfaces?.length)
|
|
271
|
+
return;
|
|
272
|
+
const code = interfaces.reduce((a, b) => a + b, "");
|
|
273
|
+
const suffix = sources ? interfaces.reduce((a, b, index) => {
|
|
274
|
+
if (b) {
|
|
275
|
+
a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
|
|
276
|
+
a += `${getPathsName(sources[index].name)}`;
|
|
277
|
+
}
|
|
278
|
+
return a;
|
|
279
|
+
}, "") : "";
|
|
280
|
+
const outputFile = path.resolve(process.cwd(), output);
|
|
281
|
+
fs.writeFileSync(outputFile, code + suffix);
|
|
282
|
+
}
|
|
283
|
+
function swagger2TsPlugin(userOptions) {
|
|
284
|
+
resolveOptions(userOptions).then((options) => {
|
|
285
|
+
Promise.all(options.map(loadSwaggerSource));
|
|
286
|
+
});
|
|
262
287
|
return {
|
|
263
288
|
name: "vite-plugin-swagger2ts",
|
|
264
289
|
enforce: "pre",
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
|
|
2
|
+
import { Plugin } from 'vite';
|
|
3
|
+
|
|
4
|
+
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* 文档域名地址
|
|
8
|
+
*/
|
|
9
|
+
swaggerUrl?: string;
|
|
10
|
+
/**
|
|
11
|
+
* 文档json地址
|
|
12
|
+
*/
|
|
13
|
+
jsonUrl?: string;
|
|
14
|
+
output?: string;
|
|
15
|
+
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
16
|
+
formatSchema?: FormatSchema;
|
|
17
|
+
}
|
|
18
|
+
type UserOptions = Options | {
|
|
19
|
+
sources: Options[];
|
|
20
|
+
};
|
|
21
|
+
interface ResolvedOptions extends Options {
|
|
22
|
+
output: string;
|
|
23
|
+
}
|
|
24
|
+
type ExportPlugin = Plugin;
|
|
25
|
+
interface SwaggerSource {
|
|
26
|
+
name: string;
|
|
27
|
+
url: string;
|
|
28
|
+
swaggerVersion: string;
|
|
29
|
+
location: string;
|
|
30
|
+
}
|
|
31
|
+
interface SwaggerDoc {
|
|
32
|
+
basePath: string;
|
|
33
|
+
swagger: string;
|
|
34
|
+
paths: {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
};
|
|
37
|
+
definitions: {
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
};
|
|
40
|
+
host: string;
|
|
41
|
+
info: any;
|
|
42
|
+
tags: any[];
|
|
43
|
+
securityDefinitions: any;
|
|
44
|
+
}
|
|
45
|
+
interface GenerateDocOption {
|
|
46
|
+
docsName: string;
|
|
47
|
+
baseUrl: string;
|
|
48
|
+
formatSchema?: FormatSchema;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
52
|
+
declare function defineConfig(userOptions?: UserOptions): UserOptions | undefined;
|
|
53
|
+
|
|
54
|
+
export { type ExportPlugin, type GenerateDocOption, type Options, type ResolvedOptions, type SwaggerDoc, type SwaggerSource, type UserOptions, swagger2TsPlugin as default, defineConfig };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { OpenAPIObject, SchemaObject } from 'openapi3-ts';
|
|
2
|
+
import { Plugin } from 'vite';
|
|
3
|
+
|
|
4
|
+
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* 文档域名地址
|
|
8
|
+
*/
|
|
9
|
+
swaggerUrl?: string;
|
|
10
|
+
/**
|
|
11
|
+
* 文档json地址
|
|
12
|
+
*/
|
|
13
|
+
jsonUrl?: string;
|
|
14
|
+
output?: string;
|
|
15
|
+
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
16
|
+
formatSchema?: FormatSchema;
|
|
17
|
+
}
|
|
18
|
+
type UserOptions = Options | {
|
|
19
|
+
sources: Options[];
|
|
20
|
+
};
|
|
21
|
+
interface ResolvedOptions extends Options {
|
|
22
|
+
output: string;
|
|
23
|
+
}
|
|
24
|
+
type ExportPlugin = Plugin;
|
|
25
|
+
interface SwaggerSource {
|
|
26
|
+
name: string;
|
|
27
|
+
url: string;
|
|
28
|
+
swaggerVersion: string;
|
|
29
|
+
location: string;
|
|
30
|
+
}
|
|
31
|
+
interface SwaggerDoc {
|
|
32
|
+
basePath: string;
|
|
33
|
+
swagger: string;
|
|
34
|
+
paths: {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
};
|
|
37
|
+
definitions: {
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
};
|
|
40
|
+
host: string;
|
|
41
|
+
info: any;
|
|
42
|
+
tags: any[];
|
|
43
|
+
securityDefinitions: any;
|
|
44
|
+
}
|
|
45
|
+
interface GenerateDocOption {
|
|
46
|
+
docsName: string;
|
|
47
|
+
baseUrl: string;
|
|
48
|
+
formatSchema?: FormatSchema;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
52
|
+
declare function defineConfig(userOptions?: UserOptions): UserOptions | undefined;
|
|
53
|
+
|
|
54
|
+
export { type ExportPlugin, type GenerateDocOption, type Options, type ResolvedOptions, type SwaggerDoc, type SwaggerSource, type UserOptions, swagger2TsPlugin as default, defineConfig };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,46 +1,54 @@
|
|
|
1
|
-
import {
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
output
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
4
|
+
type FormatSchema = (schema: SchemaObject) => SchemaObject;
|
|
5
|
+
interface Options {
|
|
6
|
+
/**
|
|
7
|
+
* 文档域名地址
|
|
8
|
+
*/
|
|
9
|
+
swaggerUrl?: string;
|
|
10
|
+
/**
|
|
11
|
+
* 文档json地址
|
|
12
|
+
*/
|
|
13
|
+
jsonUrl?: string;
|
|
14
|
+
output?: string;
|
|
15
|
+
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
16
|
+
formatSchema?: FormatSchema;
|
|
17
|
+
}
|
|
18
|
+
type UserOptions = Options | {
|
|
19
|
+
sources: Options[];
|
|
20
|
+
};
|
|
21
|
+
interface ResolvedOptions extends Options {
|
|
22
|
+
output: string;
|
|
23
|
+
}
|
|
24
|
+
type ExportPlugin = Plugin;
|
|
25
|
+
interface SwaggerSource {
|
|
26
|
+
name: string;
|
|
27
|
+
url: string;
|
|
28
|
+
swaggerVersion: string;
|
|
29
|
+
location: string;
|
|
30
|
+
}
|
|
31
|
+
interface SwaggerDoc {
|
|
32
|
+
basePath: string;
|
|
33
|
+
swagger: string;
|
|
34
|
+
paths: {
|
|
35
|
+
[key: string]: any;
|
|
36
|
+
};
|
|
37
|
+
definitions: {
|
|
38
|
+
[key: string]: any;
|
|
39
|
+
};
|
|
40
|
+
host: string;
|
|
41
|
+
info: any;
|
|
42
|
+
tags: any[];
|
|
43
|
+
securityDefinitions: any;
|
|
44
|
+
}
|
|
45
|
+
interface GenerateDocOption {
|
|
46
|
+
docsName: string;
|
|
47
|
+
baseUrl: string;
|
|
48
|
+
formatSchema?: FormatSchema;
|
|
41
49
|
}
|
|
42
50
|
|
|
43
|
-
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
51
|
+
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
44
52
|
declare function defineConfig(userOptions?: UserOptions): UserOptions | undefined;
|
|
45
53
|
|
|
46
|
-
export { ExportPlugin, GenerateDocOption, ResolvedOptions, SwaggerDoc, SwaggerSource, UserOptions, swagger2TsPlugin as default, defineConfig };
|
|
54
|
+
export { type ExportPlugin, type GenerateDocOption, type Options, type ResolvedOptions, type SwaggerDoc, type SwaggerSource, type UserOptions, swagger2TsPlugin as default, defineConfig };
|
package/dist/index.mjs
CHANGED
|
@@ -20,10 +20,13 @@ function loadConfig() {
|
|
|
20
20
|
async function resolveOptions(userOptions) {
|
|
21
21
|
const { config } = await loadConfig();
|
|
22
22
|
userOptions = userOptions || config || {};
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
const options = "sources" in userOptions ? userOptions.sources : [userOptions];
|
|
24
|
+
return options.map((item) => {
|
|
25
|
+
const { output } = item;
|
|
26
|
+
const root = process.cwd();
|
|
27
|
+
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
28
|
+
return Object.assign({ output: resolve(root, outputFile) }, item);
|
|
29
|
+
});
|
|
27
30
|
}
|
|
28
31
|
async function fetchUrl(url) {
|
|
29
32
|
return await fetch(url).then((res) => res.json());
|
|
@@ -213,44 +216,66 @@ function convertObjPromise(docs) {
|
|
|
213
216
|
});
|
|
214
217
|
});
|
|
215
218
|
}
|
|
216
|
-
function
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (docs.swagger)
|
|
226
|
-
openapiDocs = await convertObjPromise(openapiDocs);
|
|
227
|
-
else
|
|
228
|
-
throw new Error(JSON.stringify(docs));
|
|
229
|
-
}
|
|
230
|
-
const apistrings = generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
231
|
-
return apistrings;
|
|
232
|
-
} catch (error) {
|
|
233
|
-
console.log(`%c[vite-plugin-swagger2ts]: ${docsName} error`, "color: #ff0000;", error);
|
|
234
|
-
return "";
|
|
235
|
-
}
|
|
219
|
+
async function loadSwaggerSource(options) {
|
|
220
|
+
const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
|
|
221
|
+
async function genInterface(docs, docsName) {
|
|
222
|
+
let openapiDocs = formatDocs ? formatDocs(docs) : docs;
|
|
223
|
+
if (!("openapi" in openapiDocs)) {
|
|
224
|
+
if (docs.swagger)
|
|
225
|
+
openapiDocs = await convertObjPromise(openapiDocs);
|
|
226
|
+
else
|
|
227
|
+
throw new Error(JSON.stringify(docs));
|
|
236
228
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
229
|
+
return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
230
|
+
}
|
|
231
|
+
async function genCode(source) {
|
|
232
|
+
const { url, name } = source;
|
|
233
|
+
try {
|
|
234
|
+
const docs = await fetchUrl(`${swaggerUrl}${url}`);
|
|
235
|
+
return genInterface(docs, name);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
console.log(`%c[vite-plugin-swagger2ts]: ${name} error`, "color: #ff0000;", error);
|
|
238
|
+
return "";
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
async function getSources() {
|
|
242
|
+
let sources2 = void 0;
|
|
243
|
+
let interfaces2 = [];
|
|
244
|
+
if (typeof swaggerUrl === "string") {
|
|
245
|
+
const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
|
|
246
|
+
if (Array.isArray(resources)) {
|
|
247
|
+
sources2 = resources;
|
|
248
|
+
interfaces2.push(...await Promise.all(sources2.map(genCode)));
|
|
249
|
+
} else {
|
|
250
|
+
console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
|
|
247
251
|
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
+
}
|
|
253
|
+
if (typeof jsonUrl === "string") {
|
|
254
|
+
const swaggerJson = await fetchUrl(jsonUrl);
|
|
255
|
+
if (!swaggerJson.info)
|
|
256
|
+
return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
|
|
257
|
+
interfaces2.push(await genInterface(swaggerJson, swaggerJson.info.title.replace(" ", "_")));
|
|
258
|
+
}
|
|
259
|
+
return { sources: sources2, interfaces: interfaces2 };
|
|
252
260
|
}
|
|
253
|
-
|
|
261
|
+
const { sources, interfaces } = await getSources() || {};
|
|
262
|
+
if (!interfaces?.length)
|
|
263
|
+
return;
|
|
264
|
+
const code = interfaces.reduce((a, b) => a + b, "");
|
|
265
|
+
const suffix = sources ? interfaces.reduce((a, b, index) => {
|
|
266
|
+
if (b) {
|
|
267
|
+
a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
|
|
268
|
+
a += `${getPathsName(sources[index].name)}`;
|
|
269
|
+
}
|
|
270
|
+
return a;
|
|
271
|
+
}, "") : "";
|
|
272
|
+
const outputFile = resolve(process.cwd(), output);
|
|
273
|
+
writeFileSync(outputFile, code + suffix);
|
|
274
|
+
}
|
|
275
|
+
function swagger2TsPlugin(userOptions) {
|
|
276
|
+
resolveOptions(userOptions).then((options) => {
|
|
277
|
+
Promise.all(options.map(loadSwaggerSource));
|
|
278
|
+
});
|
|
254
279
|
return {
|
|
255
280
|
name: "vite-plugin-swagger2ts",
|
|
256
281
|
enforce: "pre",
|
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.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "1SZX1 <2414126198@qq.com>",
|
|
7
7
|
"repository": {
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"jest-esbuild": "^0.1.6",
|
|
83
83
|
"lint-staged": "^10.5.4",
|
|
84
84
|
"typescript": "^4.7.0",
|
|
85
|
-
"unbuild": "^
|
|
85
|
+
"unbuild": "^2.0.0",
|
|
86
86
|
"vite": "^2.8.6"
|
|
87
87
|
},
|
|
88
88
|
"lint-staged": {
|