@1szx1/vite-plugin-swagger2ts 1.2.3 → 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
  }
@@ -215,6 +215,9 @@ ${title ? ` * @title ${title}
215
215
  function getPathsName(docsName) {
216
216
  return `Paths${getRefName(docsName)}`;
217
217
  }
218
+ function getApiName(json) {
219
+ return json.info.title.replace(" ", "_");
220
+ }
218
221
 
219
222
  function convertObjPromise(docs) {
220
223
  return new Promise((resolve2, reject) => {
@@ -226,7 +229,7 @@ function convertObjPromise(docs) {
226
229
  });
227
230
  }
228
231
  async function loadSwaggerSource(options) {
229
- const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
232
+ const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
230
233
  async function genInterface(docs, docsName) {
231
234
  let openapiDocs = formatDocs ? formatDocs(docs) : docs;
232
235
  if (!("openapi" in openapiDocs)) {
@@ -237,7 +240,7 @@ async function loadSwaggerSource(options) {
237
240
  }
238
241
  return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
239
242
  }
240
- async function genCode(source) {
243
+ async function genResourcesCode(source) {
241
244
  const { url, name } = source;
242
245
  try {
243
246
  const docs = await fetchUrl(`${swaggerUrl}${url}`);
@@ -255,7 +258,7 @@ async function loadSwaggerSource(options) {
255
258
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
256
259
  if (Array.isArray(resources)) {
257
260
  sources2 = resources;
258
- interfaces2.push(...await Promise.all(sources2.map(genCode)));
261
+ interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
259
262
  } else {
260
263
  console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
261
264
  }
@@ -264,7 +267,12 @@ async function loadSwaggerSource(options) {
264
267
  const swaggerJson = await fetchUrl(jsonUrl);
265
268
  if (!swaggerJson.info)
266
269
  return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
267
- 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)));
268
276
  }
269
277
  return { sources: sources2, interfaces: interfaces2 };
270
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
  }
@@ -207,6 +207,9 @@ ${title ? ` * @title ${title}
207
207
  function getPathsName(docsName) {
208
208
  return `Paths${getRefName(docsName)}`;
209
209
  }
210
+ function getApiName(json) {
211
+ return json.info.title.replace(" ", "_");
212
+ }
210
213
 
211
214
  function convertObjPromise(docs) {
212
215
  return new Promise((resolve2, reject) => {
@@ -218,7 +221,7 @@ function convertObjPromise(docs) {
218
221
  });
219
222
  }
220
223
  async function loadSwaggerSource(options) {
221
- const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
224
+ const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
222
225
  async function genInterface(docs, docsName) {
223
226
  let openapiDocs = formatDocs ? formatDocs(docs) : docs;
224
227
  if (!("openapi" in openapiDocs)) {
@@ -229,7 +232,7 @@ async function loadSwaggerSource(options) {
229
232
  }
230
233
  return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
231
234
  }
232
- async function genCode(source) {
235
+ async function genResourcesCode(source) {
233
236
  const { url, name } = source;
234
237
  try {
235
238
  const docs = await fetchUrl(`${swaggerUrl}${url}`);
@@ -247,7 +250,7 @@ async function loadSwaggerSource(options) {
247
250
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
248
251
  if (Array.isArray(resources)) {
249
252
  sources2 = resources;
250
- interfaces2.push(...await Promise.all(sources2.map(genCode)));
253
+ interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
251
254
  } else {
252
255
  console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
253
256
  }
@@ -256,7 +259,12 @@ async function loadSwaggerSource(options) {
256
259
  const swaggerJson = await fetchUrl(jsonUrl);
257
260
  if (!swaggerJson.info)
258
261
  return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
259
- 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)));
260
268
  }
261
269
  return { sources: sources2, interfaces: interfaces2 };
262
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.3",
4
+ "version": "1.3.0",
5
5
  "license": "MIT",
6
6
  "author": "1SZX1 <2414126198@qq.com>",
7
7
  "repository": {