@1szx1/vite-plugin-swagger2ts 1.3.11 → 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 CHANGED
@@ -15,6 +15,12 @@ const fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
15
15
  const chalk__default = /*#__PURE__*/_interopDefaultCompat(chalk);
16
16
 
17
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
+ `;
18
24
  function loadConfig() {
19
25
  return unconfig.loadConfig({
20
26
  sources: [
@@ -36,7 +42,7 @@ async function resolveOptions(userOptions) {
36
42
  const { output } = item;
37
43
  const root = process.cwd();
38
44
  const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
39
- return Object.assign({ output: path.resolve(root, outputFile) }, item);
45
+ return { ...item, output: path.resolve(root, outputFile) };
40
46
  });
41
47
  }
42
48
  async function fetchUrl(url) {
@@ -76,6 +82,7 @@ function getRefName($ref, prefix = "") {
76
82
  return getSchemaName(prefix) + getSchemaName($ref || "");
77
83
  }
78
84
 
85
+ const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
79
86
  function generateDocs(input, { docsName, baseUrl, formatSchema }) {
80
87
  function getRefObject(ref) {
81
88
  return ref.split("/").slice(1).reduce((obj, item) => {
@@ -86,7 +93,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
86
93
  const pathValue = value;
87
94
  const parametersExtended = pathValue.parameters;
88
95
  const result = Object.entries(pathValue).map(([method, options]) => {
89
- if (!["get", "put", "post", "delete", "options", "head", "patch", "trace"].includes(method)) {
96
+ if (!METHODS.includes(method)) {
90
97
  return;
91
98
  }
92
99
  let allParameters = [...parametersExtended || [], ...options.parameters || []].map((item) => {
@@ -215,6 +222,21 @@ ${title ? ` * @title ${title}
215
222
  ` : `${key}${isRequired === false ? "?" : ""}: ${result};
216
223
  `}`;
217
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
+ }
218
240
  function getPathsName(docsName) {
219
241
  return `Paths${getRefName(docsName)}`;
220
242
  }
@@ -281,27 +303,46 @@ async function loadSwaggerSource(options) {
281
303
  const { sources, interfaces } = await getSources();
282
304
  if (!interfaces.length)
283
305
  return;
284
- const prefix = `/* eslint-disable */
285
- /* prettier-ignore */
286
- // @ts-nocheck
287
- // noinspection JSUnusedGlobalSymbols
288
- // Generated by ${pluginName}
289
- `;
290
306
  const code = interfaces.reduce((a, b) => a + b, "");
291
- const suffix = sources.length ? interfaces.reduce((a, b, index) => {
307
+ const suffix = interfaces.reduce((a, b, index) => {
292
308
  if (b) {
293
309
  a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
294
- a += `${getPathsName(sources[index].name)}`;
310
+ const { name } = sources[index];
311
+ a += name ? getPathsName(name) : "any";
295
312
  }
296
313
  return a;
297
- }, "") : "";
314
+ }, "");
298
315
  const outputFile = path.resolve(process.cwd(), output);
299
- fs.writeFileSync(outputFile, prefix + code + suffix);
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);
300
340
  }
301
341
  function swagger2TsPlugin(userOptions) {
302
342
  (async () => {
303
343
  const options = await resolveOptions(userOptions);
304
344
  await Promise.all(options.map(loadSwaggerSource));
345
+ options.forEach(loadGlobTypes);
305
346
  })();
306
347
  return {
307
348
  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,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
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
+ `;
9
15
  function loadConfig() {
10
16
  return loadConfig$1({
11
17
  sources: [
@@ -27,7 +33,7 @@ async function resolveOptions(userOptions) {
27
33
  const { output } = item;
28
34
  const root = process.cwd();
29
35
  const outputFile = output ? output?.endsWith(".ts") ? output : output + ".ts" : "./src/swagger.ts";
30
- return Object.assign({ output: resolve(root, outputFile) }, item);
36
+ return { ...item, output: resolve(root, outputFile) };
31
37
  });
32
38
  }
33
39
  async function fetchUrl(url) {
@@ -67,6 +73,7 @@ function getRefName($ref, prefix = "") {
67
73
  return getSchemaName(prefix) + getSchemaName($ref || "");
68
74
  }
69
75
 
76
+ const METHODS = ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
70
77
  function generateDocs(input, { docsName, baseUrl, formatSchema }) {
71
78
  function getRefObject(ref) {
72
79
  return ref.split("/").slice(1).reduce((obj, item) => {
@@ -77,7 +84,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
77
84
  const pathValue = value;
78
85
  const parametersExtended = pathValue.parameters;
79
86
  const result = Object.entries(pathValue).map(([method, options]) => {
80
- if (!["get", "put", "post", "delete", "options", "head", "patch", "trace"].includes(method)) {
87
+ if (!METHODS.includes(method)) {
81
88
  return;
82
89
  }
83
90
  let allParameters = [...parametersExtended || [], ...options.parameters || []].map((item) => {
@@ -206,6 +213,21 @@ ${title ? ` * @title ${title}
206
213
  ` : `${key}${isRequired === false ? "?" : ""}: ${result};
207
214
  `}`;
208
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
+ }
209
231
  function getPathsName(docsName) {
210
232
  return `Paths${getRefName(docsName)}`;
211
233
  }
@@ -272,27 +294,46 @@ async function loadSwaggerSource(options) {
272
294
  const { sources, interfaces } = await getSources();
273
295
  if (!interfaces.length)
274
296
  return;
275
- const prefix = `/* eslint-disable */
276
- /* prettier-ignore */
277
- // @ts-nocheck
278
- // noinspection JSUnusedGlobalSymbols
279
- // Generated by ${pluginName}
280
- `;
281
297
  const code = interfaces.reduce((a, b) => a + b, "");
282
- const suffix = sources.length ? interfaces.reduce((a, b, index) => {
298
+ const suffix = interfaces.reduce((a, b, index) => {
283
299
  if (b) {
284
300
  a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
285
- a += `${getPathsName(sources[index].name)}`;
301
+ const { name } = sources[index];
302
+ a += name ? getPathsName(name) : "any";
286
303
  }
287
304
  return a;
288
- }, "") : "";
305
+ }, "");
289
306
  const outputFile = resolve(process.cwd(), output);
290
- writeFileSync(outputFile, prefix + code + suffix);
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);
291
331
  }
292
332
  function swagger2TsPlugin(userOptions) {
293
333
  (async () => {
294
334
  const options = await resolveOptions(userOptions);
295
335
  await Promise.all(options.map(loadSwaggerSource));
336
+ options.forEach(loadGlobTypes);
296
337
  })();
297
338
  return {
298
339
  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.11",
4
+ "version": "1.3.12",
5
5
  "license": "MIT",
6
6
  "author": "1SZX1 <2414126198@qq.com>",
7
7
  "repository": {