@1szx1/vite-plugin-swagger2ts 1.3.10 → 1.3.12
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/index.cjs +80 -28
- 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 +81 -29
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -14,6 +14,13 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'defau
|
|
|
14
14
|
const fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
|
|
15
15
|
const chalk__default = /*#__PURE__*/_interopDefaultCompat(chalk);
|
|
16
16
|
|
|
17
|
+
const pluginName = "vite-plugin-swagger2ts";
|
|
18
|
+
const CODE_PREFIX = `/* eslint-disable */
|
|
19
|
+
/* prettier-ignore */
|
|
20
|
+
// @ts-nocheck
|
|
21
|
+
// noinspection JSUnusedGlobalSymbols
|
|
22
|
+
// Generated by ${pluginName}
|
|
23
|
+
`;
|
|
17
24
|
function loadConfig() {
|
|
18
25
|
return unconfig.loadConfig({
|
|
19
26
|
sources: [
|
|
@@ -35,7 +42,7 @@ async function resolveOptions(userOptions) {
|
|
|
35
42
|
const { output } = item;
|
|
36
43
|
const root = process.cwd();
|
|
37
44
|
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
38
|
-
return
|
|
45
|
+
return { ...item, output: path.resolve(root, outputFile) };
|
|
39
46
|
});
|
|
40
47
|
}
|
|
41
48
|
async function fetchUrl(url) {
|
|
@@ -75,6 +82,7 @@ function getRefName($ref, prefix = "") {
|
|
|
75
82
|
return getSchemaName(prefix) + getSchemaName($ref || "");
|
|
76
83
|
}
|
|
77
84
|
|
|
85
|
+
const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
78
86
|
function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
79
87
|
function getRefObject(ref) {
|
|
80
88
|
return ref.split("/").slice(1).reduce((obj, item) => {
|
|
@@ -85,7 +93,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
|
85
93
|
const pathValue = value;
|
|
86
94
|
const parametersExtended = pathValue.parameters;
|
|
87
95
|
const result = Object.entries(pathValue).map(([method, options]) => {
|
|
88
|
-
if (!
|
|
96
|
+
if (!METHODS.includes(method)) {
|
|
89
97
|
return;
|
|
90
98
|
}
|
|
91
99
|
let allParameters = [...parametersExtended || [], ...options.parameters || []].map((item) => {
|
|
@@ -214,6 +222,21 @@ ${title ? ` * @title ${title}
|
|
|
214
222
|
` : `${key}${isRequired === false ? "?" : ""}: ${result};
|
|
215
223
|
`}`;
|
|
216
224
|
}
|
|
225
|
+
function generatorMethodType(intersection) {
|
|
226
|
+
const paramKeys = ["body", "query", "path"];
|
|
227
|
+
const genTypeCode = (method, key) => {
|
|
228
|
+
const paths = `keyof FilterOptional<${intersection}, { ${method}: { param: { ${key}: any } } }`;
|
|
229
|
+
return `export type ${toPascalCase(method)}${toPascalCase(key)}<T extends ${paths}>> = ${intersection}[T]['${method}']['param']['${key}'];`;
|
|
230
|
+
};
|
|
231
|
+
return METHODS.reduce((total, method) => {
|
|
232
|
+
const responseCode = `export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
|
|
233
|
+
const code = paramKeys.reduce((total1, key) => `${total1}
|
|
234
|
+
${genTypeCode(method, key)}`, responseCode);
|
|
235
|
+
return `${total}${total && `
|
|
236
|
+
|
|
237
|
+
`}${code}`;
|
|
238
|
+
}, "");
|
|
239
|
+
}
|
|
217
240
|
function getPathsName(docsName) {
|
|
218
241
|
return `Paths${getRefName(docsName)}`;
|
|
219
242
|
}
|
|
@@ -231,7 +254,9 @@ async function loadSwaggerSource(options) {
|
|
|
231
254
|
else
|
|
232
255
|
throw new Error(JSON.stringify(docs));
|
|
233
256
|
}
|
|
234
|
-
|
|
257
|
+
const res = generateDocs(docs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
258
|
+
console.log(chalk__default.green(`[${pluginName}]: ${docsName} success`));
|
|
259
|
+
return res;
|
|
235
260
|
}
|
|
236
261
|
async function genResourcesCode(source) {
|
|
237
262
|
const { url, name } = source;
|
|
@@ -240,60 +265,87 @@ async function loadSwaggerSource(options) {
|
|
|
240
265
|
const code2 = await genInterface(docs, name);
|
|
241
266
|
return code2;
|
|
242
267
|
} catch (error) {
|
|
243
|
-
console.log(chalk__default.yellow(`[
|
|
268
|
+
console.log(chalk__default.yellow(`[${pluginName}]: ${name} error;`) + error.message);
|
|
244
269
|
return "";
|
|
245
270
|
}
|
|
246
271
|
}
|
|
247
272
|
async function getSources() {
|
|
248
|
-
|
|
249
|
-
|
|
273
|
+
const sources2 = [];
|
|
274
|
+
const interfaces2 = [];
|
|
250
275
|
if (typeof swaggerUrl === "string") {
|
|
251
276
|
const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
|
|
252
277
|
if (Array.isArray(resources)) {
|
|
253
|
-
sources2
|
|
254
|
-
interfaces2.push(...await Promise.all(
|
|
278
|
+
sources2.push(...resources);
|
|
279
|
+
interfaces2.push(...await Promise.all(resources.map(genResourcesCode)));
|
|
255
280
|
} else {
|
|
256
|
-
console.log(chalk__default.yellow(`[
|
|
281
|
+
console.log(chalk__default.yellow(`[${pluginName}]: resources error`) + JSON.stringify(resources));
|
|
257
282
|
}
|
|
258
283
|
}
|
|
259
284
|
if (typeof jsonUrl === "string") {
|
|
260
285
|
const swaggerJson = await fetchUrl(jsonUrl);
|
|
261
|
-
if (
|
|
262
|
-
|
|
263
|
-
|
|
286
|
+
if (swaggerJson.info) {
|
|
287
|
+
const name = getApiName(swaggerJson);
|
|
288
|
+
interfaces2.push(await genInterface(swaggerJson, name));
|
|
289
|
+
sources2.push({ name });
|
|
290
|
+
} else {
|
|
291
|
+
console.log(chalk__default.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
|
|
292
|
+
}
|
|
264
293
|
}
|
|
265
294
|
if (typeof jsonPath === "string") {
|
|
266
295
|
const _jsonPath = path.resolve(process.cwd(), jsonPath);
|
|
267
296
|
const swaggerJson = JSON.parse(fs.readFileSync(_jsonPath, { encoding: "utf-8" }));
|
|
268
|
-
|
|
297
|
+
const name = getApiName(swaggerJson);
|
|
298
|
+
interfaces2.push(await genInterface(swaggerJson, name));
|
|
299
|
+
sources2.push({ name });
|
|
269
300
|
}
|
|
270
301
|
return { sources: sources2, interfaces: interfaces2 };
|
|
271
302
|
}
|
|
272
|
-
const { sources, interfaces } = await getSources()
|
|
273
|
-
if (!interfaces
|
|
303
|
+
const { sources, interfaces } = await getSources();
|
|
304
|
+
if (!interfaces.length)
|
|
274
305
|
return;
|
|
275
|
-
const prefix = `/* eslint-disable */
|
|
276
|
-
/* prettier-ignore */
|
|
277
|
-
// @ts-nocheck
|
|
278
|
-
// noinspection JSUnusedGlobalSymbols
|
|
279
|
-
// Generated by vite-plugin-swagger2ts`;
|
|
280
306
|
const code = interfaces.reduce((a, b) => a + b, "");
|
|
281
|
-
const suffix =
|
|
307
|
+
const suffix = interfaces.reduce((a, b, index) => {
|
|
282
308
|
if (b) {
|
|
283
309
|
a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
|
|
284
|
-
|
|
310
|
+
const { name } = sources[index];
|
|
311
|
+
a += name ? getPathsName(name) : "any";
|
|
285
312
|
}
|
|
286
313
|
return a;
|
|
287
|
-
}, "")
|
|
314
|
+
}, "");
|
|
288
315
|
const outputFile = path.resolve(process.cwd(), output);
|
|
289
|
-
fs.writeFileSync(outputFile,
|
|
316
|
+
fs.writeFileSync(outputFile, CODE_PREFIX + code + suffix);
|
|
317
|
+
return { sources, interfaces };
|
|
318
|
+
}
|
|
319
|
+
function loadGlobTypes({ glob, output }) {
|
|
320
|
+
if (!glob)
|
|
321
|
+
return;
|
|
322
|
+
const outputFile = path.resolve(process.cwd(), glob.output || "auto-types.d.ts");
|
|
323
|
+
const importPath = path.relative(outputFile, output).replaceAll("\\", "/").replace(/^\.\.\//, "./");
|
|
324
|
+
const intersection = getPathsName("_Intersection");
|
|
325
|
+
const code = `${CODE_PREFIX}import { ${intersection} } from '${importPath}'
|
|
326
|
+
|
|
327
|
+
type FilterOptional<Source, Condition> = Pick<
|
|
328
|
+
Source,
|
|
329
|
+
{
|
|
330
|
+
[K in keyof Source]: Source[K] extends Condition ? K : never;
|
|
331
|
+
}[keyof Source]
|
|
332
|
+
>
|
|
333
|
+
|
|
334
|
+
declare global {
|
|
335
|
+
export namespace ${glob.namespace} {
|
|
336
|
+
${generatorMethodType(intersection)}
|
|
337
|
+
}
|
|
338
|
+
}`;
|
|
339
|
+
fs.writeFileSync(outputFile, code);
|
|
290
340
|
}
|
|
291
341
|
function swagger2TsPlugin(userOptions) {
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
342
|
+
(async () => {
|
|
343
|
+
const options = await resolveOptions(userOptions);
|
|
344
|
+
await Promise.all(options.map(loadSwaggerSource));
|
|
345
|
+
options.forEach(loadGlobTypes);
|
|
346
|
+
})();
|
|
295
347
|
return {
|
|
296
|
-
name:
|
|
348
|
+
name: `${pluginName}`,
|
|
297
349
|
enforce: "pre",
|
|
298
350
|
apply: "serve"
|
|
299
351
|
};
|
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,10 +1,17 @@
|
|
|
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
4
|
import fetch from 'cross-fetch';
|
|
5
5
|
import { loadConfig as loadConfig$1 } from 'unconfig';
|
|
6
6
|
import chalk from 'chalk';
|
|
7
7
|
|
|
8
|
+
const pluginName = "vite-plugin-swagger2ts";
|
|
9
|
+
const CODE_PREFIX = `/* eslint-disable */
|
|
10
|
+
/* prettier-ignore */
|
|
11
|
+
// @ts-nocheck
|
|
12
|
+
// noinspection JSUnusedGlobalSymbols
|
|
13
|
+
// Generated by ${pluginName}
|
|
14
|
+
`;
|
|
8
15
|
function loadConfig() {
|
|
9
16
|
return loadConfig$1({
|
|
10
17
|
sources: [
|
|
@@ -26,7 +33,7 @@ async function resolveOptions(userOptions) {
|
|
|
26
33
|
const { output } = item;
|
|
27
34
|
const root = process.cwd();
|
|
28
35
|
const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
|
|
29
|
-
return
|
|
36
|
+
return { ...item, output: resolve(root, outputFile) };
|
|
30
37
|
});
|
|
31
38
|
}
|
|
32
39
|
async function fetchUrl(url) {
|
|
@@ -66,6 +73,7 @@ function getRefName($ref, prefix = "") {
|
|
|
66
73
|
return getSchemaName(prefix) + getSchemaName($ref || "");
|
|
67
74
|
}
|
|
68
75
|
|
|
76
|
+
const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
69
77
|
function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
70
78
|
function getRefObject(ref) {
|
|
71
79
|
return ref.split("/").slice(1).reduce((obj, item) => {
|
|
@@ -76,7 +84,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
|
|
|
76
84
|
const pathValue = value;
|
|
77
85
|
const parametersExtended = pathValue.parameters;
|
|
78
86
|
const result = Object.entries(pathValue).map(([method, options]) => {
|
|
79
|
-
if (!
|
|
87
|
+
if (!METHODS.includes(method)) {
|
|
80
88
|
return;
|
|
81
89
|
}
|
|
82
90
|
let allParameters = [...parametersExtended || [], ...options.parameters || []].map((item) => {
|
|
@@ -205,6 +213,21 @@ ${title ? ` * @title ${title}
|
|
|
205
213
|
` : `${key}${isRequired === false ? "?" : ""}: ${result};
|
|
206
214
|
`}`;
|
|
207
215
|
}
|
|
216
|
+
function generatorMethodType(intersection) {
|
|
217
|
+
const paramKeys = ["body", "query", "path"];
|
|
218
|
+
const genTypeCode = (method, key) => {
|
|
219
|
+
const paths = `keyof FilterOptional<${intersection}, { ${method}: { param: { ${key}: any } } }`;
|
|
220
|
+
return `export type ${toPascalCase(method)}${toPascalCase(key)}<T extends ${paths}>> = ${intersection}[T]['${method}']['param']['${key}'];`;
|
|
221
|
+
};
|
|
222
|
+
return METHODS.reduce((total, method) => {
|
|
223
|
+
const responseCode = `export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
|
|
224
|
+
const code = paramKeys.reduce((total1, key) => `${total1}
|
|
225
|
+
${genTypeCode(method, key)}`, responseCode);
|
|
226
|
+
return `${total}${total && `
|
|
227
|
+
|
|
228
|
+
`}${code}`;
|
|
229
|
+
}, "");
|
|
230
|
+
}
|
|
208
231
|
function getPathsName(docsName) {
|
|
209
232
|
return `Paths${getRefName(docsName)}`;
|
|
210
233
|
}
|
|
@@ -222,7 +245,9 @@ async function loadSwaggerSource(options) {
|
|
|
222
245
|
else
|
|
223
246
|
throw new Error(JSON.stringify(docs));
|
|
224
247
|
}
|
|
225
|
-
|
|
248
|
+
const res = generateDocs(docs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
|
|
249
|
+
console.log(chalk.green(`[${pluginName}]: ${docsName} success`));
|
|
250
|
+
return res;
|
|
226
251
|
}
|
|
227
252
|
async function genResourcesCode(source) {
|
|
228
253
|
const { url, name } = source;
|
|
@@ -231,60 +256,87 @@ async function loadSwaggerSource(options) {
|
|
|
231
256
|
const code2 = await genInterface(docs, name);
|
|
232
257
|
return code2;
|
|
233
258
|
} catch (error) {
|
|
234
|
-
console.log(chalk.yellow(`[
|
|
259
|
+
console.log(chalk.yellow(`[${pluginName}]: ${name} error;`) + error.message);
|
|
235
260
|
return "";
|
|
236
261
|
}
|
|
237
262
|
}
|
|
238
263
|
async function getSources() {
|
|
239
|
-
|
|
240
|
-
|
|
264
|
+
const sources2 = [];
|
|
265
|
+
const interfaces2 = [];
|
|
241
266
|
if (typeof swaggerUrl === "string") {
|
|
242
267
|
const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
|
|
243
268
|
if (Array.isArray(resources)) {
|
|
244
|
-
sources2
|
|
245
|
-
interfaces2.push(...await Promise.all(
|
|
269
|
+
sources2.push(...resources);
|
|
270
|
+
interfaces2.push(...await Promise.all(resources.map(genResourcesCode)));
|
|
246
271
|
} else {
|
|
247
|
-
console.log(chalk.yellow(`[
|
|
272
|
+
console.log(chalk.yellow(`[${pluginName}]: resources error`) + JSON.stringify(resources));
|
|
248
273
|
}
|
|
249
274
|
}
|
|
250
275
|
if (typeof jsonUrl === "string") {
|
|
251
276
|
const swaggerJson = await fetchUrl(jsonUrl);
|
|
252
|
-
if (
|
|
253
|
-
|
|
254
|
-
|
|
277
|
+
if (swaggerJson.info) {
|
|
278
|
+
const name = getApiName(swaggerJson);
|
|
279
|
+
interfaces2.push(await genInterface(swaggerJson, name));
|
|
280
|
+
sources2.push({ name });
|
|
281
|
+
} else {
|
|
282
|
+
console.log(chalk.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
|
|
283
|
+
}
|
|
255
284
|
}
|
|
256
285
|
if (typeof jsonPath === "string") {
|
|
257
286
|
const _jsonPath = resolve(process.cwd(), jsonPath);
|
|
258
287
|
const swaggerJson = JSON.parse(readFileSync(_jsonPath, { encoding: "utf-8" }));
|
|
259
|
-
|
|
288
|
+
const name = getApiName(swaggerJson);
|
|
289
|
+
interfaces2.push(await genInterface(swaggerJson, name));
|
|
290
|
+
sources2.push({ name });
|
|
260
291
|
}
|
|
261
292
|
return { sources: sources2, interfaces: interfaces2 };
|
|
262
293
|
}
|
|
263
|
-
const { sources, interfaces } = await getSources()
|
|
264
|
-
if (!interfaces
|
|
294
|
+
const { sources, interfaces } = await getSources();
|
|
295
|
+
if (!interfaces.length)
|
|
265
296
|
return;
|
|
266
|
-
const prefix = `/* eslint-disable */
|
|
267
|
-
/* prettier-ignore */
|
|
268
|
-
// @ts-nocheck
|
|
269
|
-
// noinspection JSUnusedGlobalSymbols
|
|
270
|
-
// Generated by vite-plugin-swagger2ts`;
|
|
271
297
|
const code = interfaces.reduce((a, b) => a + b, "");
|
|
272
|
-
const suffix =
|
|
298
|
+
const suffix = interfaces.reduce((a, b, index) => {
|
|
273
299
|
if (b) {
|
|
274
300
|
a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
|
|
275
|
-
|
|
301
|
+
const { name } = sources[index];
|
|
302
|
+
a += name ? getPathsName(name) : "any";
|
|
276
303
|
}
|
|
277
304
|
return a;
|
|
278
|
-
}, "")
|
|
305
|
+
}, "");
|
|
279
306
|
const outputFile = resolve(process.cwd(), output);
|
|
280
|
-
writeFileSync(outputFile,
|
|
307
|
+
writeFileSync(outputFile, CODE_PREFIX + code + suffix);
|
|
308
|
+
return { sources, interfaces };
|
|
309
|
+
}
|
|
310
|
+
function loadGlobTypes({ glob, output }) {
|
|
311
|
+
if (!glob)
|
|
312
|
+
return;
|
|
313
|
+
const outputFile = resolve(process.cwd(), glob.output || "auto-types.d.ts");
|
|
314
|
+
const importPath = relative(outputFile, output).replaceAll("\\", "/").replace(/^\.\.\//, "./");
|
|
315
|
+
const intersection = getPathsName("_Intersection");
|
|
316
|
+
const code = `${CODE_PREFIX}import { ${intersection} } from '${importPath}'
|
|
317
|
+
|
|
318
|
+
type FilterOptional<Source, Condition> = Pick<
|
|
319
|
+
Source,
|
|
320
|
+
{
|
|
321
|
+
[K in keyof Source]: Source[K] extends Condition ? K : never;
|
|
322
|
+
}[keyof Source]
|
|
323
|
+
>
|
|
324
|
+
|
|
325
|
+
declare global {
|
|
326
|
+
export namespace ${glob.namespace} {
|
|
327
|
+
${generatorMethodType(intersection)}
|
|
328
|
+
}
|
|
329
|
+
}`;
|
|
330
|
+
writeFileSync(outputFile, code);
|
|
281
331
|
}
|
|
282
332
|
function swagger2TsPlugin(userOptions) {
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
333
|
+
(async () => {
|
|
334
|
+
const options = await resolveOptions(userOptions);
|
|
335
|
+
await Promise.all(options.map(loadSwaggerSource));
|
|
336
|
+
options.forEach(loadGlobTypes);
|
|
337
|
+
})();
|
|
286
338
|
return {
|
|
287
|
-
name:
|
|
339
|
+
name: `${pluginName}`,
|
|
288
340
|
enforce: "pre",
|
|
289
341
|
apply: "serve"
|
|
290
342
|
};
|
package/package.json
CHANGED