@1szx1/vite-plugin-swagger2ts 1.1.7 → 1.2.1

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.
@@ -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 };
@@ -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
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/dist/bin.d.mts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { }
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 { output } = userOptions;
32
- const root = process.cwd();
33
- const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
34
- return Object.assign({ output: path.resolve(root, outputFile) }, userOptions);
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,67 @@ function convertObjPromise(docs) {
221
224
  });
222
225
  });
223
226
  }
224
- function swagger2TsPlugin(userOptions) {
225
- async function loadSwaggerSource() {
226
- const { swaggerUrl, output, formatDocs, formatSchema } = await resolveOptions(userOptions);
227
- async function genCode(source) {
228
- const { url, name: docsName } = source;
229
- try {
230
- const docs = await fetchUrl(`${swaggerUrl}${url}`);
231
- let openapiDocs = formatDocs ? formatDocs(docs) : docs;
232
- if (!("openapi" in openapiDocs)) {
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
- const sources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
246
- if (!Array.isArray(sources))
247
- return console.log(`%c[vite-plugin-swagger2ts]: doc error`, "color: #ff0000;", JSON.stringify(sources));
248
- const interfaceList = await Promise.all(sources.map(genCode));
249
- const code = interfaceList.reduce((a, b) => a + b, "");
250
- const suffix = interfaceList.reduce((a, b, index) => {
251
- if (b) {
252
- a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&
253
- `;
254
- a += `${getPathsName(sources[index].name)}`;
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
+ const code2 = await genInterface(docs, name);
244
+ return code2;
245
+ } catch (error) {
246
+ console.log(`%c[vite-plugin-swagger2ts]: ${name} error`, "color: #ff0000;", error.message);
247
+ return "";
248
+ }
249
+ }
250
+ async function getSources() {
251
+ let sources2 = void 0;
252
+ let interfaces2 = [];
253
+ if (typeof swaggerUrl === "string") {
254
+ const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
255
+ if (Array.isArray(resources)) {
256
+ sources2 = resources;
257
+ interfaces2.push(...await Promise.all(sources2.map(genCode)));
258
+ } else {
259
+ console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
255
260
  }
256
- return a;
257
- }, "");
258
- const outputFile = path.resolve(process.cwd(), output);
259
- fs.writeFileSync(outputFile, code + suffix);
261
+ }
262
+ if (typeof jsonUrl === "string") {
263
+ const swaggerJson = await fetchUrl(jsonUrl);
264
+ if (!swaggerJson.info)
265
+ return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
266
+ interfaces2.push(await genInterface(swaggerJson, swaggerJson.info.title.replace(" ", "_")));
267
+ }
268
+ return { sources: sources2, interfaces: interfaces2 };
260
269
  }
261
- loadSwaggerSource();
270
+ const { sources, interfaces } = await getSources() || {};
271
+ if (!interfaces?.length)
272
+ return;
273
+ const code = interfaces.reduce((a, b) => a + b, "");
274
+ const suffix = sources ? interfaces.reduce((a, b, index) => {
275
+ if (b) {
276
+ a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
277
+ a += `${getPathsName(sources[index].name)}`;
278
+ }
279
+ return a;
280
+ }, "") : "";
281
+ const outputFile = path.resolve(process.cwd(), output);
282
+ fs.writeFileSync(outputFile, code + suffix);
283
+ }
284
+ function swagger2TsPlugin(userOptions) {
285
+ resolveOptions(userOptions).then((options) => {
286
+ Promise.all(options.map(loadSwaggerSource));
287
+ });
262
288
  return {
263
289
  name: "vite-plugin-swagger2ts",
264
290
  enforce: "pre",
@@ -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 };
@@ -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 { SchemaObject, OpenAPIObject } from 'openapi3-ts';
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
- swaggerUrl: string;
7
- output?: string;
8
- formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
9
- formatSchema?: FormatSchema;
10
- }
11
- interface UserOptions extends Options {
12
- }
13
- interface ResolvedOptions extends Options {
14
- output: string;
15
- }
16
- type ExportPlugin = Plugin;
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;
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 { output } = userOptions;
24
- const root = process.cwd();
25
- const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
26
- return Object.assign({ output: resolve(root, outputFile) }, userOptions);
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,67 @@ function convertObjPromise(docs) {
213
216
  });
214
217
  });
215
218
  }
216
- function swagger2TsPlugin(userOptions) {
217
- async function loadSwaggerSource() {
218
- const { swaggerUrl, output, formatDocs, formatSchema } = await resolveOptions(userOptions);
219
- async function genCode(source) {
220
- const { url, name: docsName } = source;
221
- try {
222
- const docs = await fetchUrl(`${swaggerUrl}${url}`);
223
- let openapiDocs = formatDocs ? formatDocs(docs) : docs;
224
- if (!("openapi" in openapiDocs)) {
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
- const sources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
238
- if (!Array.isArray(sources))
239
- return console.log(`%c[vite-plugin-swagger2ts]: doc error`, "color: #ff0000;", JSON.stringify(sources));
240
- const interfaceList = await Promise.all(sources.map(genCode));
241
- const code = interfaceList.reduce((a, b) => a + b, "");
242
- const suffix = interfaceList.reduce((a, b, index) => {
243
- if (b) {
244
- a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&
245
- `;
246
- a += `${getPathsName(sources[index].name)}`;
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
+ const code2 = await genInterface(docs, name);
236
+ return code2;
237
+ } catch (error) {
238
+ console.log(`%c[vite-plugin-swagger2ts]: ${name} error`, "color: #ff0000;", error.message);
239
+ return "";
240
+ }
241
+ }
242
+ async function getSources() {
243
+ let sources2 = void 0;
244
+ let interfaces2 = [];
245
+ if (typeof swaggerUrl === "string") {
246
+ const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
247
+ if (Array.isArray(resources)) {
248
+ sources2 = resources;
249
+ interfaces2.push(...await Promise.all(sources2.map(genCode)));
250
+ } else {
251
+ console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
247
252
  }
248
- return a;
249
- }, "");
250
- const outputFile = resolve(process.cwd(), output);
251
- writeFileSync(outputFile, code + suffix);
253
+ }
254
+ if (typeof jsonUrl === "string") {
255
+ const swaggerJson = await fetchUrl(jsonUrl);
256
+ if (!swaggerJson.info)
257
+ return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
258
+ interfaces2.push(await genInterface(swaggerJson, swaggerJson.info.title.replace(" ", "_")));
259
+ }
260
+ return { sources: sources2, interfaces: interfaces2 };
252
261
  }
253
- loadSwaggerSource();
262
+ const { sources, interfaces } = await getSources() || {};
263
+ if (!interfaces?.length)
264
+ return;
265
+ const code = interfaces.reduce((a, b) => a + b, "");
266
+ const suffix = sources ? interfaces.reduce((a, b, index) => {
267
+ if (b) {
268
+ a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
269
+ a += `${getPathsName(sources[index].name)}`;
270
+ }
271
+ return a;
272
+ }, "") : "";
273
+ const outputFile = resolve(process.cwd(), output);
274
+ writeFileSync(outputFile, code + suffix);
275
+ }
276
+ function swagger2TsPlugin(userOptions) {
277
+ resolveOptions(userOptions).then((options) => {
278
+ Promise.all(options.map(loadSwaggerSource));
279
+ });
254
280
  return {
255
281
  name: "vite-plugin-swagger2ts",
256
282
  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.1.7",
4
+ "version": "1.2.1",
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": "^1.2.1",
85
+ "unbuild": "^2.0.0",
86
86
  "vite": "^2.8.6"
87
87
  },
88
88
  "lint-staged": {