@1szx1/vite-plugin-swagger2ts 1.0.18 → 1.1.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.cjs +42 -0
- package/dist/adapters.d.ts +19 -19
- package/dist/adapters.mjs +2 -9
- package/dist/bin.cjs +10 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.mjs +8 -0
- package/dist/{index.js → index.cjs} +51 -61
- package/dist/index.d.ts +40 -38
- package/dist/index.mjs +39 -25
- package/package.json +109 -102
- package/dist/adapters.js +0 -74
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function mergeUrlParam(url, paths) {
|
|
4
|
+
return url.replace(/{(.*?)}/g, (_, key) => paths[key]);
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function axiosAdapter(httpInstance) {
|
|
8
|
+
async function request(url, method, param, config) {
|
|
9
|
+
if (config?.path)
|
|
10
|
+
url = mergeUrlParam(url, config.path);
|
|
11
|
+
const response = await httpInstance.request({
|
|
12
|
+
url,
|
|
13
|
+
method,
|
|
14
|
+
params: method === "get" ? param : config?.params,
|
|
15
|
+
data: method !== "get" ? param : config?.data,
|
|
16
|
+
...config
|
|
17
|
+
});
|
|
18
|
+
return response.data;
|
|
19
|
+
}
|
|
20
|
+
return { request };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function LuchAdapter(httpInstance) {
|
|
24
|
+
async function request(url, method, param, config) {
|
|
25
|
+
const response = await httpInstance.middleware({
|
|
26
|
+
url,
|
|
27
|
+
method: method.toUpperCase(),
|
|
28
|
+
params: method === "get" ? param : config?.params,
|
|
29
|
+
data: method !== "get" ? param : config?.data,
|
|
30
|
+
...config
|
|
31
|
+
});
|
|
32
|
+
return response.data;
|
|
33
|
+
}
|
|
34
|
+
async function upload(url, config) {
|
|
35
|
+
const response = await httpInstance.upload(url, config);
|
|
36
|
+
return response.data;
|
|
37
|
+
}
|
|
38
|
+
return { request, upload };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
exports.LuchAdapter = LuchAdapter;
|
|
42
|
+
exports.axiosAdapter = axiosAdapter;
|
package/dist/adapters.d.ts
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
import { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
-
import
|
|
2
|
+
import * as Request from 'luch-request';
|
|
3
|
+
import Request__default, { HttpRequestConfig } from 'luch-request';
|
|
3
4
|
|
|
4
5
|
type Get<T, K> = T[K & keyof T];
|
|
5
6
|
|
|
6
|
-
declare function axiosAdapter<T>(httpInstance: AxiosInstance): {
|
|
7
|
-
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> & {
|
|
8
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
9
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
10
|
-
path?: Get<T[U][M]["param" & keyof T[U][M]], "path"> | undefined;
|
|
11
|
-
}) | 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]]>;
|
|
12
13
|
};
|
|
13
14
|
|
|
14
|
-
declare function LuchAdapter<T extends Record<string, any>>(httpInstance:
|
|
15
|
-
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<
|
|
16
|
-
data?: Get<T[U][M]["param" & keyof T[U][M]], "body"> | undefined;
|
|
17
|
-
params?: Get<T[U][M]["param" & keyof T[U][M]], "query"> | undefined;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}) => Promise<T[U_1]["post"]["response" & keyof T[U_1]["post"]]>;
|
|
15
|
+
declare function LuchAdapter<T extends Record<string, any>>(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> & {
|
|
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
|
+
}) | undefined) => Promise<T[U][M]["response" & keyof T[U][M]]>;
|
|
20
|
+
upload: <U_1 extends { [K in keyof T]: T[K] extends {
|
|
21
|
+
post: any;
|
|
22
|
+
} ? 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> & {
|
|
23
|
+
name?: N | undefined;
|
|
24
|
+
formData?: Omit<P, N> | undefined;
|
|
25
|
+
}) => Promise<T[U_1]["post"]["response" & keyof T[U_1]["post"]]>;
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
export { LuchAdapter, axiosAdapter };
|
package/dist/adapters.mjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
// src/adapters/utils.ts
|
|
2
1
|
function mergeUrlParam(url, paths) {
|
|
3
2
|
return url.replace(/{(.*?)}/g, (_, key) => paths[key]);
|
|
4
3
|
}
|
|
5
4
|
|
|
6
|
-
// src/adapters/axios.ts
|
|
7
5
|
function axiosAdapter(httpInstance) {
|
|
8
6
|
async function request(url, method, param, config) {
|
|
9
7
|
if (config?.path)
|
|
@@ -20,11 +18,8 @@ function axiosAdapter(httpInstance) {
|
|
|
20
18
|
return { request };
|
|
21
19
|
}
|
|
22
20
|
|
|
23
|
-
// src/adapters/luchRequest.ts
|
|
24
21
|
function LuchAdapter(httpInstance) {
|
|
25
22
|
async function request(url, method, param, config) {
|
|
26
|
-
if (config?.path)
|
|
27
|
-
url = mergeUrlParam(url, config.path);
|
|
28
23
|
const response = await httpInstance.middleware({
|
|
29
24
|
url,
|
|
30
25
|
method: method.toUpperCase(),
|
|
@@ -40,7 +35,5 @@ function LuchAdapter(httpInstance) {
|
|
|
40
35
|
}
|
|
41
36
|
return { request, upload };
|
|
42
37
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
axiosAdapter
|
|
46
|
-
};
|
|
38
|
+
|
|
39
|
+
export { LuchAdapter, axiosAdapter };
|
package/dist/bin.cjs
ADDED
package/dist/bin.d.ts
ADDED
package/dist/bin.mjs
ADDED
|
@@ -1,53 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const swagger2openapi = require('swagger2openapi');
|
|
8
|
+
const fetch = require('cross-fetch');
|
|
9
|
+
const unconfig = require('unconfig');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
12
|
+
|
|
13
|
+
const fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
|
|
29
14
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
15
|
+
function loadConfig() {
|
|
16
|
+
return unconfig.loadConfig({
|
|
17
|
+
sources: [
|
|
18
|
+
{
|
|
19
|
+
files: `swagger2ts.config`
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
// if false, the only the first matched will be loaded
|
|
23
|
+
// if true, all matched will be loaded and deep merged
|
|
24
|
+
merge: false
|
|
25
|
+
});
|
|
26
|
+
}
|
|
39
27
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
function resolveOptions(userOptions) {
|
|
28
|
+
async function resolveOptions(userOptions) {
|
|
29
|
+
const { config } = await loadConfig();
|
|
30
|
+
userOptions = userOptions || config || {};
|
|
44
31
|
const { output } = userOptions;
|
|
45
32
|
const root = process.cwd();
|
|
46
33
|
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
47
|
-
return Object.assign({ output:
|
|
34
|
+
return Object.assign({ output: path.resolve(root, outputFile) }, userOptions);
|
|
48
35
|
}
|
|
49
36
|
async function fetchUrl(url) {
|
|
50
|
-
return await (
|
|
37
|
+
return await fetch__default(url).then((res) => res.json());
|
|
51
38
|
}
|
|
52
39
|
function isTypeAny(type) {
|
|
53
40
|
if (type === true) {
|
|
@@ -83,7 +70,6 @@ function getRefName($ref, prefix = "") {
|
|
|
83
70
|
return getSchemaName(prefix) + getSchemaName($ref || "");
|
|
84
71
|
}
|
|
85
72
|
|
|
86
|
-
// src/generator.ts
|
|
87
73
|
function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
88
74
|
function getRefObject(ref) {
|
|
89
75
|
return ref.split("/").slice(1).reduce((obj, item) => {
|
|
@@ -140,7 +126,7 @@ export interface Paths${getRefName(docsName)}{${pathsTypeString}}
|
|
|
140
126
|
`;
|
|
141
127
|
return apiString.replaceAll(/\n\n/g, "\n").replaceAll(/\n\n/g, "\n");
|
|
142
128
|
}
|
|
143
|
-
|
|
129
|
+
const TYPES = {
|
|
144
130
|
integer: "number",
|
|
145
131
|
number: "number",
|
|
146
132
|
boolean: "boolean",
|
|
@@ -223,23 +209,20 @@ ${title ? ` * @title ${title}
|
|
|
223
209
|
`}`;
|
|
224
210
|
}
|
|
225
211
|
|
|
226
|
-
// src/index.ts
|
|
227
212
|
function convertObjPromise(docs) {
|
|
228
|
-
return new Promise((
|
|
229
|
-
|
|
213
|
+
return new Promise((resolve2, reject) => {
|
|
214
|
+
swagger2openapi.convertObj(docs, { patch: true }, function(err, option) {
|
|
230
215
|
if (err || !option.openapi)
|
|
231
216
|
return reject(err);
|
|
232
|
-
|
|
217
|
+
resolve2(option.openapi);
|
|
233
218
|
});
|
|
234
219
|
});
|
|
235
220
|
}
|
|
236
221
|
function swagger2TsPlugin(userOptions) {
|
|
237
|
-
const { swaggerUrl, output, formatDocs, formatSchema } = resolveOptions(userOptions);
|
|
238
222
|
async function loadSwaggerSource() {
|
|
239
|
-
const
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const { url, name: docsName } = sources[i];
|
|
223
|
+
const { swaggerUrl, output, formatDocs, formatSchema } = await resolveOptions(userOptions);
|
|
224
|
+
async function genCode(source) {
|
|
225
|
+
const { url, name: docsName } = source;
|
|
243
226
|
try {
|
|
244
227
|
const docs = await fetchUrl(`${swaggerUrl}${url}`);
|
|
245
228
|
let openapiDocs = formatDocs ? formatDocs(docs) : docs;
|
|
@@ -247,15 +230,19 @@ function swagger2TsPlugin(userOptions) {
|
|
|
247
230
|
if (docs.swagger)
|
|
248
231
|
openapiDocs = await convertObjPromise(openapiDocs);
|
|
249
232
|
else
|
|
250
|
-
|
|
233
|
+
return "";
|
|
251
234
|
}
|
|
252
235
|
const apistrings = generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
253
|
-
|
|
236
|
+
return apistrings;
|
|
254
237
|
} catch (error) {
|
|
238
|
+
console.log("vite-plugin-swagger2ts convert error", docsName, error);
|
|
239
|
+
return "";
|
|
255
240
|
}
|
|
256
241
|
}
|
|
257
|
-
const
|
|
258
|
-
(
|
|
242
|
+
const sources = await fetchUrl(`${swaggerUrl}/swagger-resources`);
|
|
243
|
+
const code = (await Promise.all(sources.map(genCode))).reduce((a, b) => a + b, "");
|
|
244
|
+
const outputFile = path.resolve(process.cwd(), output);
|
|
245
|
+
fs.writeFileSync(outputFile, code);
|
|
259
246
|
}
|
|
260
247
|
loadSwaggerSource();
|
|
261
248
|
return {
|
|
@@ -264,6 +251,9 @@ function swagger2TsPlugin(userOptions) {
|
|
|
264
251
|
apply: "serve"
|
|
265
252
|
};
|
|
266
253
|
}
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
254
|
+
function defineConfig(userOptions) {
|
|
255
|
+
return userOptions;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
exports.default = swagger2TsPlugin;
|
|
259
|
+
exports.defineConfig = defineConfig;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,46 @@
|
|
|
1
1
|
import { SchemaObject, OpenAPIObject } 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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
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;
|
|
40
41
|
}
|
|
41
42
|
|
|
42
|
-
declare function swagger2TsPlugin(userOptions
|
|
43
|
+
declare function swagger2TsPlugin(userOptions?: UserOptions): ExportPlugin;
|
|
44
|
+
declare function defineConfig(userOptions?: UserOptions): UserOptions | undefined;
|
|
43
45
|
|
|
44
|
-
export { ExportPlugin, GenerateDocOption, ResolvedOptions, SwaggerDoc, SwaggerSource, UserOptions, swagger2TsPlugin as default };
|
|
46
|
+
export { ExportPlugin, GenerateDocOption, ResolvedOptions, SwaggerDoc, SwaggerSource, UserOptions, swagger2TsPlugin as default, defineConfig };
|
package/dist/index.mjs
CHANGED
|
@@ -1,12 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
1
|
+
import { writeFileSync } from 'fs';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
import { convertObj } from 'swagger2openapi';
|
|
4
|
+
import fetch from 'cross-fetch';
|
|
5
|
+
import { loadConfig as loadConfig$1 } from 'unconfig';
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
function loadConfig() {
|
|
8
|
+
return loadConfig$1({
|
|
9
|
+
sources: [
|
|
10
|
+
{
|
|
11
|
+
files: `swagger2ts.config`
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
// if false, the only the first matched will be loaded
|
|
15
|
+
// if true, all matched will be loaded and deep merged
|
|
16
|
+
merge: false
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function resolveOptions(userOptions) {
|
|
21
|
+
const { config } = await loadConfig();
|
|
22
|
+
userOptions = userOptions || config || {};
|
|
10
23
|
const { output } = userOptions;
|
|
11
24
|
const root = process.cwd();
|
|
12
25
|
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
@@ -49,7 +62,6 @@ function getRefName($ref, prefix = "") {
|
|
|
49
62
|
return getSchemaName(prefix) + getSchemaName($ref || "");
|
|
50
63
|
}
|
|
51
64
|
|
|
52
|
-
// src/generator.ts
|
|
53
65
|
function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
54
66
|
function getRefObject(ref) {
|
|
55
67
|
return ref.split("/").slice(1).reduce((obj, item) => {
|
|
@@ -106,7 +118,7 @@ export interface Paths${getRefName(docsName)}{${pathsTypeString}}
|
|
|
106
118
|
`;
|
|
107
119
|
return apiString.replaceAll(/\n\n/g, "\n").replaceAll(/\n\n/g, "\n");
|
|
108
120
|
}
|
|
109
|
-
|
|
121
|
+
const TYPES = {
|
|
110
122
|
integer: "number",
|
|
111
123
|
number: "number",
|
|
112
124
|
boolean: "boolean",
|
|
@@ -189,23 +201,20 @@ ${title ? ` * @title ${title}
|
|
|
189
201
|
`}`;
|
|
190
202
|
}
|
|
191
203
|
|
|
192
|
-
// src/index.ts
|
|
193
204
|
function convertObjPromise(docs) {
|
|
194
|
-
return new Promise((
|
|
205
|
+
return new Promise((resolve2, reject) => {
|
|
195
206
|
convertObj(docs, { patch: true }, function(err, option) {
|
|
196
207
|
if (err || !option.openapi)
|
|
197
208
|
return reject(err);
|
|
198
|
-
|
|
209
|
+
resolve2(option.openapi);
|
|
199
210
|
});
|
|
200
211
|
});
|
|
201
212
|
}
|
|
202
213
|
function swagger2TsPlugin(userOptions) {
|
|
203
|
-
const { swaggerUrl, output, formatDocs, formatSchema } = resolveOptions(userOptions);
|
|
204
214
|
async function loadSwaggerSource() {
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const { url, name: docsName } = sources[i];
|
|
215
|
+
const { swaggerUrl, output, formatDocs, formatSchema } = await resolveOptions(userOptions);
|
|
216
|
+
async function genCode(source) {
|
|
217
|
+
const { url, name: docsName } = source;
|
|
209
218
|
try {
|
|
210
219
|
const docs = await fetchUrl(`${swaggerUrl}${url}`);
|
|
211
220
|
let openapiDocs = formatDocs ? formatDocs(docs) : docs;
|
|
@@ -213,14 +222,18 @@ function swagger2TsPlugin(userOptions) {
|
|
|
213
222
|
if (docs.swagger)
|
|
214
223
|
openapiDocs = await convertObjPromise(openapiDocs);
|
|
215
224
|
else
|
|
216
|
-
|
|
225
|
+
return "";
|
|
217
226
|
}
|
|
218
227
|
const apistrings = generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
219
|
-
|
|
228
|
+
return apistrings;
|
|
220
229
|
} catch (error) {
|
|
230
|
+
console.log("vite-plugin-swagger2ts convert error", docsName, error);
|
|
231
|
+
return "";
|
|
221
232
|
}
|
|
222
233
|
}
|
|
223
|
-
const
|
|
234
|
+
const sources = await fetchUrl(`${swaggerUrl}/swagger-resources`);
|
|
235
|
+
const code = (await Promise.all(sources.map(genCode))).reduce((a, b) => a + b, "");
|
|
236
|
+
const outputFile = resolve(process.cwd(), output);
|
|
224
237
|
writeFileSync(outputFile, code);
|
|
225
238
|
}
|
|
226
239
|
loadSwaggerSource();
|
|
@@ -230,7 +243,8 @@ function swagger2TsPlugin(userOptions) {
|
|
|
230
243
|
apply: "serve"
|
|
231
244
|
};
|
|
232
245
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
246
|
+
function defineConfig(userOptions) {
|
|
247
|
+
return userOptions;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export { swagger2TsPlugin as default, defineConfig };
|
package/package.json
CHANGED
|
@@ -1,103 +1,110 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@1szx1/vite-plugin-swagger2ts",
|
|
3
|
-
"description": "fork for https://github.com/1SZX1/vite-plugin-swagger2ts",
|
|
4
|
-
"version": "1.0
|
|
5
|
-
"license": "MIT",
|
|
6
|
-
"author": "1SZX1 <2414126198@qq.com>",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "https://github.com/1SZX1/vite-plugin-swagger2ts"
|
|
10
|
-
},
|
|
11
|
-
"homepage": "https://github.com/1SZX1/vite-plugin-swagger2ts",
|
|
12
|
-
"bugs": "https://github.com/1SZX1/vite-plugin-swagger2ts/issues",
|
|
13
|
-
"files": [
|
|
14
|
-
"dist"
|
|
15
|
-
],
|
|
16
|
-
"exports": {
|
|
17
|
-
".": {
|
|
18
|
-
"require": "./dist/index.
|
|
19
|
-
"import": "./dist/index.mjs",
|
|
20
|
-
"types": "./dist/index.d.ts"
|
|
21
|
-
},
|
|
22
|
-
"./adapters": {
|
|
23
|
-
"require": "./dist/adapters.
|
|
24
|
-
"import": "./dist/adapters.mjs",
|
|
25
|
-
"types": "./dist/adapters.d.ts"
|
|
26
|
-
}
|
|
27
|
-
},
|
|
28
|
-
"main": "dist/index.
|
|
29
|
-
"module": "dist/index.mjs",
|
|
30
|
-
"types": "index.d.ts",
|
|
31
|
-
"typesVersions": {
|
|
32
|
-
"*": {
|
|
33
|
-
"
|
|
34
|
-
"./dist
|
|
35
|
-
]
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
"
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"axios": "^1.
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"eslint": "^
|
|
72
|
-
"
|
|
73
|
-
"
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
"
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@1szx1/vite-plugin-swagger2ts",
|
|
3
|
+
"description": "fork for https://github.com/1SZX1/vite-plugin-swagger2ts",
|
|
4
|
+
"version": "1.1.0",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "1SZX1 <2414126198@qq.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/1SZX1/vite-plugin-swagger2ts"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/1SZX1/vite-plugin-swagger2ts",
|
|
12
|
+
"bugs": "https://github.com/1SZX1/vite-plugin-swagger2ts/issues",
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"require": "./dist/index.cjs",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"types": "./dist/index.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./adapters": {
|
|
23
|
+
"require": "./dist/adapters.cjs",
|
|
24
|
+
"import": "./dist/adapters.mjs",
|
|
25
|
+
"types": "./dist/adapters.d.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"main": "dist/index.cjs",
|
|
29
|
+
"module": "dist/index.mjs",
|
|
30
|
+
"types": "dist/index.d.ts",
|
|
31
|
+
"typesVersions": {
|
|
32
|
+
"*": {
|
|
33
|
+
"/": [
|
|
34
|
+
"./dist"
|
|
35
|
+
],
|
|
36
|
+
"adapters": [
|
|
37
|
+
"./dist/adapters"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"bin": {
|
|
42
|
+
"swagger2ts": "./dist/bin.mjs"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"build": "unbuild",
|
|
46
|
+
"stub": "unbuild --stub",
|
|
47
|
+
"prepublishOnly": "pnpm run build",
|
|
48
|
+
"release": "pnpx git-ensure -a && pnpx bumpp --commit --tag --push",
|
|
49
|
+
"demo:dev": "pnpm -C examples/demo run dev",
|
|
50
|
+
"demo:build": "pnpm -C examples/demo run build",
|
|
51
|
+
"demo:preview": "pnpm -C examples/demo run preview",
|
|
52
|
+
"test": "jest",
|
|
53
|
+
"lint": "eslint --fix --ext .ts ./",
|
|
54
|
+
"version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
|
|
55
|
+
"preinstall": "npx only-allow pnpm"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"cross-fetch": "^3.1.5",
|
|
59
|
+
"openapi3-ts": "^2.0.2",
|
|
60
|
+
"swagger2openapi": "^7.0.8",
|
|
61
|
+
"luch-request": "^3.1.0",
|
|
62
|
+
"axios": "^1.4.0",
|
|
63
|
+
"unconfig": "^0.3.9"
|
|
64
|
+
},
|
|
65
|
+
"peerDependencies": {
|
|
66
|
+
"vite": "^2.0.0",
|
|
67
|
+
"luch-request": "^3.1.0",
|
|
68
|
+
"axios": "^1.4.0"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@antfu/eslint-config": "^0.6.6",
|
|
72
|
+
"@commitlint/cli": "^12.1.4",
|
|
73
|
+
"@commitlint/config-conventional": "^12.1.4",
|
|
74
|
+
"@dcloudio/types": "^3.3.3",
|
|
75
|
+
"@types/jest": "^26.0.24",
|
|
76
|
+
"@types/node": "^14.18.12",
|
|
77
|
+
"@typescript-eslint/eslint-plugin": "^4.33.0",
|
|
78
|
+
"@typescript-eslint/parser": "^5.14.0",
|
|
79
|
+
"conventional-changelog": "^3.1.25",
|
|
80
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
81
|
+
"eslint": "^7.32.0",
|
|
82
|
+
"husky": "^8.0.3",
|
|
83
|
+
"jest": "^26.6.3",
|
|
84
|
+
"jest-esbuild": "^0.1.6",
|
|
85
|
+
"lint-staged": "^10.5.4",
|
|
86
|
+
"typescript": "^4.7.0",
|
|
87
|
+
"unbuild": "^1.2.1",
|
|
88
|
+
"vite": "^2.8.6"
|
|
89
|
+
},
|
|
90
|
+
"lint-staged": {
|
|
91
|
+
"*.{ts}": "eslint --fix --ext",
|
|
92
|
+
"*.{ts,md}": "prettier --write"
|
|
93
|
+
},
|
|
94
|
+
"husky": {
|
|
95
|
+
"hooks": {
|
|
96
|
+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
|
|
97
|
+
"pre-commit": "lint-staged"
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
"commitlint": {
|
|
101
|
+
"extends": [
|
|
102
|
+
"@commitlint/config-conventional"
|
|
103
|
+
]
|
|
104
|
+
},
|
|
105
|
+
"config": {
|
|
106
|
+
"commitizen": {
|
|
107
|
+
"path": "./node_modules/cz-conventional-changelog"
|
|
108
|
+
}
|
|
109
|
+
}
|
|
103
110
|
}
|
package/dist/adapters.js
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/adapters.ts
|
|
21
|
-
var adapters_exports = {};
|
|
22
|
-
__export(adapters_exports, {
|
|
23
|
-
LuchAdapter: () => LuchAdapter,
|
|
24
|
-
axiosAdapter: () => axiosAdapter
|
|
25
|
-
});
|
|
26
|
-
module.exports = __toCommonJS(adapters_exports);
|
|
27
|
-
|
|
28
|
-
// src/adapters/utils.ts
|
|
29
|
-
function mergeUrlParam(url, paths) {
|
|
30
|
-
return url.replace(/{(.*?)}/g, (_, key) => paths[key]);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// src/adapters/axios.ts
|
|
34
|
-
function axiosAdapter(httpInstance) {
|
|
35
|
-
async function request(url, method, param, config) {
|
|
36
|
-
if (config?.path)
|
|
37
|
-
url = mergeUrlParam(url, config.path);
|
|
38
|
-
const response = await httpInstance.request({
|
|
39
|
-
url,
|
|
40
|
-
method,
|
|
41
|
-
params: method === "get" ? param : config?.params,
|
|
42
|
-
data: method !== "get" ? param : config?.data,
|
|
43
|
-
...config
|
|
44
|
-
});
|
|
45
|
-
return response.data;
|
|
46
|
-
}
|
|
47
|
-
return { request };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// src/adapters/luchRequest.ts
|
|
51
|
-
function LuchAdapter(httpInstance) {
|
|
52
|
-
async function request(url, method, param, config) {
|
|
53
|
-
if (config?.path)
|
|
54
|
-
url = mergeUrlParam(url, config.path);
|
|
55
|
-
const response = await httpInstance.middleware({
|
|
56
|
-
url,
|
|
57
|
-
method: method.toUpperCase(),
|
|
58
|
-
params: method === "get" ? param : config?.params,
|
|
59
|
-
data: method !== "get" ? param : config?.data,
|
|
60
|
-
...config
|
|
61
|
-
});
|
|
62
|
-
return response.data;
|
|
63
|
-
}
|
|
64
|
-
async function upload(url, config) {
|
|
65
|
-
const response = await httpInstance.upload(url, config);
|
|
66
|
-
return response.data;
|
|
67
|
-
}
|
|
68
|
-
return { request, upload };
|
|
69
|
-
}
|
|
70
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
-
0 && (module.exports = {
|
|
72
|
-
LuchAdapter,
|
|
73
|
-
axiosAdapter
|
|
74
|
-
});
|