@1szx1/vite-plugin-swagger2ts 1.3.12 → 1.3.14

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 CHANGED
@@ -4,8 +4,9 @@ const index = require('./index.cjs');
4
4
  require('fs');
5
5
  require('path');
6
6
  require('swagger2openapi');
7
+ require('chalk');
8
+ require('prettier');
7
9
  require('cross-fetch');
8
10
  require('unconfig');
9
- require('chalk');
10
11
 
11
12
  index.default();
package/dist/bin.mjs CHANGED
@@ -2,8 +2,9 @@ import swagger2TsPlugin from './index.mjs';
2
2
  import 'fs';
3
3
  import 'path';
4
4
  import 'swagger2openapi';
5
+ import 'chalk';
6
+ import 'prettier';
5
7
  import 'cross-fetch';
6
8
  import 'unconfig';
7
- import 'chalk';
8
9
 
9
10
  swagger2TsPlugin();
package/dist/index.cjs CHANGED
@@ -5,14 +5,15 @@ 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";
18
19
  const CODE_PREFIX = `/* eslint-disable */
@@ -123,7 +124,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
123
124
  return jsDoc({}, { key: method, result: `{${paramString ? `
124
125
  param:{${paramString}}` : ""}${responseString}}` });
125
126
  }).join("");
126
- return jsDoc({}, { key: `'${baseUrl}${endPoint}'`, result: `{${result}}` });
127
+ return jsDoc({}, { key: `${baseUrl}${endPoint}`, result: `{${result}}` });
127
128
  }).join("");
128
129
  const typeString = input.components?.schemas ? Object.entries(input.components.schemas).map(([name, schema]) => {
129
130
  if (typeof formatSchema == "function") {
@@ -207,7 +208,7 @@ function generatorResponseBodyType(schemas, docsName) {
207
208
  return jsDoc({ description }, { key: "response", result });
208
209
  }
209
210
  function jsDoc({ description, title, format, example, default: default1 }, { key, result, isRoot, isExport, isRequired }) {
210
- if (!/(^\[.+\]$)|(^[^\[\]]*$)/.test(key))
211
+ if (!(isRoot || isExport) && !/^[^'"]*$/.test(key))
211
212
  key = `'${key}'`;
212
213
  const jsDoc2 = `
213
214
  /**
@@ -229,7 +230,8 @@ function generatorMethodType(intersection) {
229
230
  return `export type ${toPascalCase(method)}${toPascalCase(key)}<T extends ${paths}>> = ${intersection}[T]['${method}']['param']['${key}'];`;
230
231
  };
231
232
  return METHODS.reduce((total, method) => {
232
- const responseCode = `export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
233
+ const responseCode = `// ${method}
234
+ export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
233
235
  const code = paramKeys.reduce((total1, key) => `${total1}
234
236
  ${genTypeCode(method, key)}`, responseCode);
235
237
  return `${total}${total && `
@@ -313,10 +315,10 @@ async function loadSwaggerSource(options) {
313
315
  return a;
314
316
  }, "");
315
317
  const outputFile = path.resolve(process.cwd(), output);
316
- fs.writeFileSync(outputFile, CODE_PREFIX + code + suffix);
318
+ fs.writeFileSync(outputFile, await prettier.format(CODE_PREFIX + code + suffix, { parser: "typescript" }));
317
319
  return { sources, interfaces };
318
320
  }
319
- function loadGlobTypes({ glob, output }) {
321
+ async function loadGlobTypes({ glob, output }) {
320
322
  if (!glob)
321
323
  return;
322
324
  const outputFile = path.resolve(process.cwd(), glob.output || "auto-types.d.ts");
@@ -336,13 +338,13 @@ declare global {
336
338
  ${generatorMethodType(intersection)}
337
339
  }
338
340
  }`;
339
- fs.writeFileSync(outputFile, code);
341
+ fs.writeFileSync(outputFile, await prettier.format(code, { parser: "typescript" }));
340
342
  }
341
343
  function swagger2TsPlugin(userOptions) {
342
344
  (async () => {
343
345
  const options = await resolveOptions(userOptions);
344
346
  await Promise.all(options.map(loadSwaggerSource));
345
- options.forEach(loadGlobTypes);
347
+ await Promise.all(options.map(loadGlobTypes));
346
348
  })();
347
349
  return {
348
350
  name: `${pluginName}`,
package/dist/index.mjs CHANGED
@@ -1,9 +1,10 @@
1
1
  import { writeFileSync, readFileSync } from 'fs';
2
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";
9
10
  const CODE_PREFIX = `/* eslint-disable */
@@ -114,7 +115,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
114
115
  return jsDoc({}, { key: method, result: `{${paramString ? `
115
116
  param:{${paramString}}` : ""}${responseString}}` });
116
117
  }).join("");
117
- return jsDoc({}, { key: `'${baseUrl}${endPoint}'`, result: `{${result}}` });
118
+ return jsDoc({}, { key: `${baseUrl}${endPoint}`, result: `{${result}}` });
118
119
  }).join("");
119
120
  const typeString = input.components?.schemas ? Object.entries(input.components.schemas).map(([name, schema]) => {
120
121
  if (typeof formatSchema == "function") {
@@ -198,7 +199,7 @@ function generatorResponseBodyType(schemas, docsName) {
198
199
  return jsDoc({ description }, { key: "response", result });
199
200
  }
200
201
  function jsDoc({ description, title, format, example, default: default1 }, { key, result, isRoot, isExport, isRequired }) {
201
- if (!/(^\[.+\]$)|(^[^\[\]]*$)/.test(key))
202
+ if (!(isRoot || isExport) && !/^[^'"]*$/.test(key))
202
203
  key = `'${key}'`;
203
204
  const jsDoc2 = `
204
205
  /**
@@ -220,7 +221,8 @@ function generatorMethodType(intersection) {
220
221
  return `export type ${toPascalCase(method)}${toPascalCase(key)}<T extends ${paths}>> = ${intersection}[T]['${method}']['param']['${key}'];`;
221
222
  };
222
223
  return METHODS.reduce((total, method) => {
223
- const responseCode = `export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
224
+ const responseCode = `// ${method}
225
+ export type ${toPascalCase(method)}${toPascalCase("response")}<T extends keyof ${intersection}> = ${intersection}[T]['${method}']['response'];`;
224
226
  const code = paramKeys.reduce((total1, key) => `${total1}
225
227
  ${genTypeCode(method, key)}`, responseCode);
226
228
  return `${total}${total && `
@@ -304,10 +306,10 @@ async function loadSwaggerSource(options) {
304
306
  return a;
305
307
  }, "");
306
308
  const outputFile = resolve(process.cwd(), output);
307
- writeFileSync(outputFile, CODE_PREFIX + code + suffix);
309
+ writeFileSync(outputFile, await format(CODE_PREFIX + code + suffix, { parser: "typescript" }));
308
310
  return { sources, interfaces };
309
311
  }
310
- function loadGlobTypes({ glob, output }) {
312
+ async function loadGlobTypes({ glob, output }) {
311
313
  if (!glob)
312
314
  return;
313
315
  const outputFile = resolve(process.cwd(), glob.output || "auto-types.d.ts");
@@ -327,13 +329,13 @@ declare global {
327
329
  ${generatorMethodType(intersection)}
328
330
  }
329
331
  }`;
330
- writeFileSync(outputFile, code);
332
+ writeFileSync(outputFile, await format(code, { parser: "typescript" }));
331
333
  }
332
334
  function swagger2TsPlugin(userOptions) {
333
335
  (async () => {
334
336
  const options = await resolveOptions(userOptions);
335
337
  await Promise.all(options.map(loadSwaggerSource));
336
- options.forEach(loadGlobTypes);
338
+ await Promise.all(options.map(loadGlobTypes));
337
339
  })();
338
340
  return {
339
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.12",
4
+ "version": "1.3.14",
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
  },