@1szx1/vite-plugin-swagger2ts 1.3.11 → 1.3.13
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/bin.cjs +2 -1
- package/dist/bin.mjs +2 -1
- package/dist/index.cjs +57 -14
- package/dist/index.d.cts +4 -0
- package/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.mjs +57 -14
- package/package.json +2 -1
package/dist/bin.cjs
CHANGED
package/dist/bin.mjs
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -5,16 +5,23 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
const fs = require('fs');
|
|
6
6
|
const path = require('path');
|
|
7
7
|
const swagger2openapi = require('swagger2openapi');
|
|
8
|
+
const chalk = require('chalk');
|
|
9
|
+
const prettier = require('prettier');
|
|
8
10
|
const fetch = require('cross-fetch');
|
|
9
11
|
const unconfig = require('unconfig');
|
|
10
|
-
const chalk = require('chalk');
|
|
11
12
|
|
|
12
13
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
13
14
|
|
|
14
|
-
const fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
|
|
15
15
|
const chalk__default = /*#__PURE__*/_interopDefaultCompat(chalk);
|
|
16
|
+
const fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
|
|
16
17
|
|
|
17
18
|
const pluginName = "vite-plugin-swagger2ts";
|
|
19
|
+
const CODE_PREFIX = `/* eslint-disable */
|
|
20
|
+
/* prettier-ignore */
|
|
21
|
+
// @ts-nocheck
|
|
22
|
+
// noinspection JSUnusedGlobalSymbols
|
|
23
|
+
// Generated by ${pluginName}
|
|
24
|
+
`;
|
|
18
25
|
function loadConfig() {
|
|
19
26
|
return unconfig.loadConfig({
|
|
20
27
|
sources: [
|
|
@@ -36,7 +43,7 @@ async function resolveOptions(userOptions) {
|
|
|
36
43
|
const { output } = item;
|
|
37
44
|
const root = process.cwd();
|
|
38
45
|
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
39
|
-
return
|
|
46
|
+
return { ...item, output: path.resolve(root, outputFile) };
|
|
40
47
|
});
|
|
41
48
|
}
|
|
42
49
|
async function fetchUrl(url) {
|
|
@@ -76,6 +83,7 @@ function getRefName($ref, prefix = "") {
|
|
|
76
83
|
return getSchemaName(prefix) + getSchemaName($ref || "");
|
|
77
84
|
}
|
|
78
85
|
|
|
86
|
+
const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
79
87
|
function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
80
88
|
function getRefObject(ref) {
|
|
81
89
|
return ref.split("/").slice(1).reduce((obj, item) => {
|
|
@@ -86,7 +94,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
|
86
94
|
const pathValue = value;
|
|
87
95
|
const parametersExtended = pathValue.parameters;
|
|
88
96
|
const result = Object.entries(pathValue).map(([method, options]) => {
|
|
89
|
-
if (!
|
|
97
|
+
if (!METHODS.includes(method)) {
|
|
90
98
|
return;
|
|
91
99
|
}
|
|
92
100
|
let allParameters = [...parametersExtended || [], ...options.parameters || []].map((item) => {
|
|
@@ -215,6 +223,22 @@ ${title ? ` * @title ${title}
|
|
|
215
223
|
` : `${key}${isRequired === false ? "?" : ""}: ${result};
|
|
216
224
|
`}`;
|
|
217
225
|
}
|
|
226
|
+
function generatorMethodType(intersection) {
|
|
227
|
+
const paramKeys = ["body", "query", "path"];
|
|
228
|
+
const genTypeCode = (method, key) => {
|
|
229
|
+
const paths = `keyof FilterOptional<${intersection}, { ${method}: { param: { ${key}: any } } }`;
|
|
230
|
+
return `export type ${toPascalCase(method)}${toPascalCase(key)}<T extends ${paths}>> = ${intersection}[T]['${method}']['param']['${key}'];`;
|
|
231
|
+
};
|
|
232
|
+
return METHODS.reduce((total, method) => {
|
|
233
|
+
const responseCode = `// ${method}
|
|
234
|
+
export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
|
|
235
|
+
const code = paramKeys.reduce((total1, key) => `${total1}
|
|
236
|
+
${genTypeCode(method, key)}`, responseCode);
|
|
237
|
+
return `${total}${total && `
|
|
238
|
+
|
|
239
|
+
`}${code}`;
|
|
240
|
+
}, "");
|
|
241
|
+
}
|
|
218
242
|
function getPathsName(docsName) {
|
|
219
243
|
return `Paths${getRefName(docsName)}`;
|
|
220
244
|
}
|
|
@@ -281,27 +305,46 @@ async function loadSwaggerSource(options) {
|
|
|
281
305
|
const { sources, interfaces } = await getSources();
|
|
282
306
|
if (!interfaces.length)
|
|
283
307
|
return;
|
|
284
|
-
const prefix = `/* eslint-disable */
|
|
285
|
-
/* prettier-ignore */
|
|
286
|
-
// @ts-nocheck
|
|
287
|
-
// noinspection JSUnusedGlobalSymbols
|
|
288
|
-
// Generated by ${pluginName}
|
|
289
|
-
`;
|
|
290
308
|
const code = interfaces.reduce((a, b) => a + b, "");
|
|
291
|
-
const suffix =
|
|
309
|
+
const suffix = interfaces.reduce((a, b, index) => {
|
|
292
310
|
if (b) {
|
|
293
311
|
a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
|
|
294
|
-
|
|
312
|
+
const { name } = sources[index];
|
|
313
|
+
a += name ? getPathsName(name) : "any";
|
|
295
314
|
}
|
|
296
315
|
return a;
|
|
297
|
-
}, "")
|
|
316
|
+
}, "");
|
|
298
317
|
const outputFile = path.resolve(process.cwd(), output);
|
|
299
|
-
fs.writeFileSync(outputFile,
|
|
318
|
+
fs.writeFileSync(outputFile, await prettier.format(CODE_PREFIX + code + suffix, { parser: "typescript" }));
|
|
319
|
+
return { sources, interfaces };
|
|
320
|
+
}
|
|
321
|
+
async function loadGlobTypes({ glob, output }) {
|
|
322
|
+
if (!glob)
|
|
323
|
+
return;
|
|
324
|
+
const outputFile = path.resolve(process.cwd(), glob.output || "auto-types.d.ts");
|
|
325
|
+
const importPath = path.relative(outputFile, output).replaceAll("\\", "/").replace(/^\.\.\//, "./");
|
|
326
|
+
const intersection = getPathsName("_Intersection");
|
|
327
|
+
const code = `${CODE_PREFIX}import { ${intersection} } from '${importPath}'
|
|
328
|
+
|
|
329
|
+
type FilterOptional<Source, Condition> = Pick<
|
|
330
|
+
Source,
|
|
331
|
+
{
|
|
332
|
+
[K in keyof Source]: Source[K] extends Condition ? K : never;
|
|
333
|
+
}[keyof Source]
|
|
334
|
+
>
|
|
335
|
+
|
|
336
|
+
declare global {
|
|
337
|
+
export namespace ${glob.namespace} {
|
|
338
|
+
${generatorMethodType(intersection)}
|
|
339
|
+
}
|
|
340
|
+
}`;
|
|
341
|
+
fs.writeFileSync(outputFile, await prettier.format(code, { parser: "typescript" }));
|
|
300
342
|
}
|
|
301
343
|
function swagger2TsPlugin(userOptions) {
|
|
302
344
|
(async () => {
|
|
303
345
|
const options = await resolveOptions(userOptions);
|
|
304
346
|
await Promise.all(options.map(loadSwaggerSource));
|
|
347
|
+
await Promise.all(options.map(loadGlobTypes));
|
|
305
348
|
})();
|
|
306
349
|
return {
|
|
307
350
|
name: `${pluginName}`,
|
package/dist/index.d.cts
CHANGED
|
@@ -18,6 +18,10 @@ interface Options {
|
|
|
18
18
|
output?: string;
|
|
19
19
|
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
20
20
|
formatSchema?: FormatSchema;
|
|
21
|
+
glob?: {
|
|
22
|
+
namespace: string;
|
|
23
|
+
output?: string;
|
|
24
|
+
};
|
|
21
25
|
}
|
|
22
26
|
type UserOptions = Options | {
|
|
23
27
|
sources: Options[];
|
package/dist/index.d.mts
CHANGED
|
@@ -18,6 +18,10 @@ interface Options {
|
|
|
18
18
|
output?: string;
|
|
19
19
|
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
20
20
|
formatSchema?: FormatSchema;
|
|
21
|
+
glob?: {
|
|
22
|
+
namespace: string;
|
|
23
|
+
output?: string;
|
|
24
|
+
};
|
|
21
25
|
}
|
|
22
26
|
type UserOptions = Options | {
|
|
23
27
|
sources: Options[];
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,10 @@ interface Options {
|
|
|
18
18
|
output?: string;
|
|
19
19
|
formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
|
|
20
20
|
formatSchema?: FormatSchema;
|
|
21
|
+
glob?: {
|
|
22
|
+
namespace: string;
|
|
23
|
+
output?: string;
|
|
24
|
+
};
|
|
21
25
|
}
|
|
22
26
|
type UserOptions = Options | {
|
|
23
27
|
sources: Options[];
|
package/dist/index.mjs
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { writeFileSync, readFileSync } from 'fs';
|
|
2
|
-
import { resolve } from 'path';
|
|
2
|
+
import { resolve, relative } from 'path';
|
|
3
3
|
import { convertObj } from 'swagger2openapi';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import { format } from 'prettier';
|
|
4
6
|
import fetch from 'cross-fetch';
|
|
5
7
|
import { loadConfig as loadConfig$1 } from 'unconfig';
|
|
6
|
-
import chalk from 'chalk';
|
|
7
8
|
|
|
8
9
|
const pluginName = "vite-plugin-swagger2ts";
|
|
10
|
+
const CODE_PREFIX = `/* eslint-disable */
|
|
11
|
+
/* prettier-ignore */
|
|
12
|
+
// @ts-nocheck
|
|
13
|
+
// noinspection JSUnusedGlobalSymbols
|
|
14
|
+
// Generated by ${pluginName}
|
|
15
|
+
`;
|
|
9
16
|
function loadConfig() {
|
|
10
17
|
return loadConfig$1({
|
|
11
18
|
sources: [
|
|
@@ -27,7 +34,7 @@ async function resolveOptions(userOptions) {
|
|
|
27
34
|
const { output } = item;
|
|
28
35
|
const root = process.cwd();
|
|
29
36
|
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
30
|
-
return
|
|
37
|
+
return { ...item, output: resolve(root, outputFile) };
|
|
31
38
|
});
|
|
32
39
|
}
|
|
33
40
|
async function fetchUrl(url) {
|
|
@@ -67,6 +74,7 @@ function getRefName($ref, prefix = "") {
|
|
|
67
74
|
return getSchemaName(prefix) + getSchemaName($ref || "");
|
|
68
75
|
}
|
|
69
76
|
|
|
77
|
+
const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
70
78
|
function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
71
79
|
function getRefObject(ref) {
|
|
72
80
|
return ref.split("/").slice(1).reduce((obj, item) => {
|
|
@@ -77,7 +85,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
|
77
85
|
const pathValue = value;
|
|
78
86
|
const parametersExtended = pathValue.parameters;
|
|
79
87
|
const result = Object.entries(pathValue).map(([method, options]) => {
|
|
80
|
-
if (!
|
|
88
|
+
if (!METHODS.includes(method)) {
|
|
81
89
|
return;
|
|
82
90
|
}
|
|
83
91
|
let allParameters = [...parametersExtended || [], ...options.parameters || []].map((item) => {
|
|
@@ -206,6 +214,22 @@ ${title ? ` * @title ${title}
|
|
|
206
214
|
` : `${key}${isRequired === false ? "?" : ""}: ${result};
|
|
207
215
|
`}`;
|
|
208
216
|
}
|
|
217
|
+
function generatorMethodType(intersection) {
|
|
218
|
+
const paramKeys = ["body", "query", "path"];
|
|
219
|
+
const genTypeCode = (method, key) => {
|
|
220
|
+
const paths = `keyof FilterOptional<${intersection}, { ${method}: { param: { ${key}: any } } }`;
|
|
221
|
+
return `export type ${toPascalCase(method)}${toPascalCase(key)}<T extends ${paths}>> = ${intersection}[T]['${method}']['param']['${key}'];`;
|
|
222
|
+
};
|
|
223
|
+
return METHODS.reduce((total, method) => {
|
|
224
|
+
const responseCode = `// ${method}
|
|
225
|
+
export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
|
|
226
|
+
const code = paramKeys.reduce((total1, key) => `${total1}
|
|
227
|
+
${genTypeCode(method, key)}`, responseCode);
|
|
228
|
+
return `${total}${total && `
|
|
229
|
+
|
|
230
|
+
`}${code}`;
|
|
231
|
+
}, "");
|
|
232
|
+
}
|
|
209
233
|
function getPathsName(docsName) {
|
|
210
234
|
return `Paths${getRefName(docsName)}`;
|
|
211
235
|
}
|
|
@@ -272,27 +296,46 @@ async function loadSwaggerSource(options) {
|
|
|
272
296
|
const { sources, interfaces } = await getSources();
|
|
273
297
|
if (!interfaces.length)
|
|
274
298
|
return;
|
|
275
|
-
const prefix = `/* eslint-disable */
|
|
276
|
-
/* prettier-ignore */
|
|
277
|
-
// @ts-nocheck
|
|
278
|
-
// noinspection JSUnusedGlobalSymbols
|
|
279
|
-
// Generated by ${pluginName}
|
|
280
|
-
`;
|
|
281
299
|
const code = interfaces.reduce((a, b) => a + b, "");
|
|
282
|
-
const suffix =
|
|
300
|
+
const suffix = interfaces.reduce((a, b, index) => {
|
|
283
301
|
if (b) {
|
|
284
302
|
a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
|
|
285
|
-
|
|
303
|
+
const { name } = sources[index];
|
|
304
|
+
a += name ? getPathsName(name) : "any";
|
|
286
305
|
}
|
|
287
306
|
return a;
|
|
288
|
-
}, "")
|
|
307
|
+
}, "");
|
|
289
308
|
const outputFile = resolve(process.cwd(), output);
|
|
290
|
-
writeFileSync(outputFile,
|
|
309
|
+
writeFileSync(outputFile, await format(CODE_PREFIX + code + suffix, { parser: "typescript" }));
|
|
310
|
+
return { sources, interfaces };
|
|
311
|
+
}
|
|
312
|
+
async function loadGlobTypes({ glob, output }) {
|
|
313
|
+
if (!glob)
|
|
314
|
+
return;
|
|
315
|
+
const outputFile = resolve(process.cwd(), glob.output || "auto-types.d.ts");
|
|
316
|
+
const importPath = relative(outputFile, output).replaceAll("\\", "/").replace(/^\.\.\//, "./");
|
|
317
|
+
const intersection = getPathsName("_Intersection");
|
|
318
|
+
const code = `${CODE_PREFIX}import { ${intersection} } from '${importPath}'
|
|
319
|
+
|
|
320
|
+
type FilterOptional<Source, Condition> = Pick<
|
|
321
|
+
Source,
|
|
322
|
+
{
|
|
323
|
+
[K in keyof Source]: Source[K] extends Condition ? K : never;
|
|
324
|
+
}[keyof Source]
|
|
325
|
+
>
|
|
326
|
+
|
|
327
|
+
declare global {
|
|
328
|
+
export namespace ${glob.namespace} {
|
|
329
|
+
${generatorMethodType(intersection)}
|
|
330
|
+
}
|
|
331
|
+
}`;
|
|
332
|
+
writeFileSync(outputFile, await format(code, { parser: "typescript" }));
|
|
291
333
|
}
|
|
292
334
|
function swagger2TsPlugin(userOptions) {
|
|
293
335
|
(async () => {
|
|
294
336
|
const options = await resolveOptions(userOptions);
|
|
295
337
|
await Promise.all(options.map(loadSwaggerSource));
|
|
338
|
+
await Promise.all(options.map(loadGlobTypes));
|
|
296
339
|
})();
|
|
297
340
|
return {
|
|
298
341
|
name: `${pluginName}`,
|
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.13",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "1SZX1 <2414126198@qq.com>",
|
|
7
7
|
"repository": {
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
"cross-fetch": "^3.1.5",
|
|
61
61
|
"luch-request": "^3.1.0",
|
|
62
62
|
"openapi3-ts": "^2.0.2",
|
|
63
|
+
"prettier": "^3.2.5",
|
|
63
64
|
"swagger2openapi": "^7.0.8",
|
|
64
65
|
"unconfig": "^0.3.9"
|
|
65
66
|
},
|