@1szx1/vite-plugin-swagger2ts 1.2.2 → 1.3.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/index.cjs CHANGED
@@ -97,7 +97,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
97
97
  const pathParameters = allParameters.filter((item) => item.in === "path");
98
98
  const bodyParameters = allParameters.filter((item) => !item.in);
99
99
  const requestBody = options.requestBody ? "$ref" in options.requestBody ? getRefObject(options.requestBody.$ref) : options.requestBody : void 0;
100
- let responses = Object.entries(options.responses).filter((items) => items[0] == "200" || items[0] == "default").sort()?.[0]?.[1];
100
+ let responses = Object.entries(options.responses).filter((items) => items[0].startsWith("20") || items[0] == "default").sort()?.[0]?.[1];
101
101
  if ("$ref" in responses) {
102
102
  responses = getRefObject(responses.$ref);
103
103
  }
@@ -194,8 +194,6 @@ function generatorResponseBodyType(schemas, docsName) {
194
194
  } else {
195
195
  result = generatorSchemaType("", schema, { docsName });
196
196
  }
197
- if (result.includes("any;"))
198
- console.log(jsDoc({ description }, { key: "response", result }));
199
197
  return jsDoc({ description }, { key: "response", result });
200
198
  }
201
199
  function jsDoc({ description, title, format, example, default: default1 }, { key, result, isRoot, isExport, isRequired }) {
@@ -217,6 +215,9 @@ ${title ? ` * @title ${title}
217
215
  function getPathsName(docsName) {
218
216
  return `Paths${getRefName(docsName)}`;
219
217
  }
218
+ function getApiName(json) {
219
+ return json.info.title.replace(" ", "_");
220
+ }
220
221
 
221
222
  function convertObjPromise(docs) {
222
223
  return new Promise((resolve2, reject) => {
@@ -228,7 +229,7 @@ function convertObjPromise(docs) {
228
229
  });
229
230
  }
230
231
  async function loadSwaggerSource(options) {
231
- const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
232
+ const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
232
233
  async function genInterface(docs, docsName) {
233
234
  let openapiDocs = formatDocs ? formatDocs(docs) : docs;
234
235
  if (!("openapi" in openapiDocs)) {
@@ -239,7 +240,7 @@ async function loadSwaggerSource(options) {
239
240
  }
240
241
  return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
241
242
  }
242
- async function genCode(source) {
243
+ async function genResourcesCode(source) {
243
244
  const { url, name } = source;
244
245
  try {
245
246
  const docs = await fetchUrl(`${swaggerUrl}${url}`);
@@ -257,7 +258,7 @@ async function loadSwaggerSource(options) {
257
258
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
258
259
  if (Array.isArray(resources)) {
259
260
  sources2 = resources;
260
- interfaces2.push(...await Promise.all(sources2.map(genCode)));
261
+ interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
261
262
  } else {
262
263
  console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
263
264
  }
@@ -266,7 +267,12 @@ async function loadSwaggerSource(options) {
266
267
  const swaggerJson = await fetchUrl(jsonUrl);
267
268
  if (!swaggerJson.info)
268
269
  return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
269
- interfaces2.push(await genInterface(swaggerJson, swaggerJson.info.title.replace(" ", "_")));
270
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
271
+ }
272
+ if (typeof jsonPath === "string") {
273
+ const _jsonPath = path.resolve(process.cwd(), jsonPath);
274
+ const swaggerJson = JSON.parse(fs.readFileSync(_jsonPath, { encoding: "utf-8" }));
275
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
270
276
  }
271
277
  return { sources: sources2, interfaces: interfaces2 };
272
278
  }
package/dist/index.d.cts CHANGED
@@ -11,6 +11,10 @@ interface Options {
11
11
  * 文档json地址
12
12
  */
13
13
  jsonUrl?: string;
14
+ /**
15
+ * openApi 本地json
16
+ */
17
+ jsonPath?: string;
14
18
  output?: string;
15
19
  formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
16
20
  formatSchema?: FormatSchema;
package/dist/index.d.mts CHANGED
@@ -11,6 +11,10 @@ interface Options {
11
11
  * 文档json地址
12
12
  */
13
13
  jsonUrl?: string;
14
+ /**
15
+ * openApi 本地json
16
+ */
17
+ jsonPath?: string;
14
18
  output?: string;
15
19
  formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
16
20
  formatSchema?: FormatSchema;
package/dist/index.d.ts CHANGED
@@ -11,6 +11,10 @@ interface Options {
11
11
  * 文档json地址
12
12
  */
13
13
  jsonUrl?: string;
14
+ /**
15
+ * openApi 本地json
16
+ */
17
+ jsonPath?: string;
14
18
  output?: string;
15
19
  formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
16
20
  formatSchema?: FormatSchema;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { writeFileSync } from 'fs';
1
+ import { writeFileSync, readFileSync } from 'fs';
2
2
  import { resolve } from 'path';
3
3
  import { convertObj } from 'swagger2openapi';
4
4
  import fetch from 'cross-fetch';
@@ -89,7 +89,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
89
89
  const pathParameters = allParameters.filter((item) => item.in === "path");
90
90
  const bodyParameters = allParameters.filter((item) => !item.in);
91
91
  const requestBody = options.requestBody ? "$ref" in options.requestBody ? getRefObject(options.requestBody.$ref) : options.requestBody : void 0;
92
- let responses = Object.entries(options.responses).filter((items) => items[0] == "200" || items[0] == "default").sort()?.[0]?.[1];
92
+ let responses = Object.entries(options.responses).filter((items) => items[0].startsWith("20") || items[0] == "default").sort()?.[0]?.[1];
93
93
  if ("$ref" in responses) {
94
94
  responses = getRefObject(responses.$ref);
95
95
  }
@@ -186,8 +186,6 @@ function generatorResponseBodyType(schemas, docsName) {
186
186
  } else {
187
187
  result = generatorSchemaType("", schema, { docsName });
188
188
  }
189
- if (result.includes("any;"))
190
- console.log(jsDoc({ description }, { key: "response", result }));
191
189
  return jsDoc({ description }, { key: "response", result });
192
190
  }
193
191
  function jsDoc({ description, title, format, example, default: default1 }, { key, result, isRoot, isExport, isRequired }) {
@@ -209,6 +207,9 @@ ${title ? ` * @title ${title}
209
207
  function getPathsName(docsName) {
210
208
  return `Paths${getRefName(docsName)}`;
211
209
  }
210
+ function getApiName(json) {
211
+ return json.info.title.replace(" ", "_");
212
+ }
212
213
 
213
214
  function convertObjPromise(docs) {
214
215
  return new Promise((resolve2, reject) => {
@@ -220,7 +221,7 @@ function convertObjPromise(docs) {
220
221
  });
221
222
  }
222
223
  async function loadSwaggerSource(options) {
223
- const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
224
+ const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
224
225
  async function genInterface(docs, docsName) {
225
226
  let openapiDocs = formatDocs ? formatDocs(docs) : docs;
226
227
  if (!("openapi" in openapiDocs)) {
@@ -231,7 +232,7 @@ async function loadSwaggerSource(options) {
231
232
  }
232
233
  return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
233
234
  }
234
- async function genCode(source) {
235
+ async function genResourcesCode(source) {
235
236
  const { url, name } = source;
236
237
  try {
237
238
  const docs = await fetchUrl(`${swaggerUrl}${url}`);
@@ -249,7 +250,7 @@ async function loadSwaggerSource(options) {
249
250
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
250
251
  if (Array.isArray(resources)) {
251
252
  sources2 = resources;
252
- interfaces2.push(...await Promise.all(sources2.map(genCode)));
253
+ interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
253
254
  } else {
254
255
  console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
255
256
  }
@@ -258,7 +259,12 @@ async function loadSwaggerSource(options) {
258
259
  const swaggerJson = await fetchUrl(jsonUrl);
259
260
  if (!swaggerJson.info)
260
261
  return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
261
- interfaces2.push(await genInterface(swaggerJson, swaggerJson.info.title.replace(" ", "_")));
262
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
263
+ }
264
+ if (typeof jsonPath === "string") {
265
+ const _jsonPath = resolve(process.cwd(), jsonPath);
266
+ const swaggerJson = JSON.parse(readFileSync(_jsonPath, { encoding: "utf-8" }));
267
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
262
268
  }
263
269
  return { sources: sources2, interfaces: interfaces2 };
264
270
  }
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.2.2",
4
+ "version": "1.3.0",
5
5
  "license": "MIT",
6
6
  "author": "1SZX1 <2414126198@qq.com>",
7
7
  "repository": {