@1szx1/vite-plugin-swagger2ts 1.3.10 → 1.3.11

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
@@ -14,6 +14,7 @@ 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";
17
18
  function loadConfig() {
18
19
  return unconfig.loadConfig({
19
20
  sources: [
@@ -231,7 +232,9 @@ async function loadSwaggerSource(options) {
231
232
  else
232
233
  throw new Error(JSON.stringify(docs));
233
234
  }
234
- return generateDocs(docs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
235
+ const res = generateDocs(docs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
236
+ console.log(chalk__default.green(`[${pluginName}]: ${docsName} success`));
237
+ return res;
235
238
  }
236
239
  async function genResourcesCode(source) {
237
240
  const { url, name } = source;
@@ -240,45 +243,52 @@ async function loadSwaggerSource(options) {
240
243
  const code2 = await genInterface(docs, name);
241
244
  return code2;
242
245
  } catch (error) {
243
- console.log(chalk__default.yellow(`[vite-plugin-swagger2ts]: ${name} error;`) + error.message);
246
+ console.log(chalk__default.yellow(`[${pluginName}]: ${name} error;`) + error.message);
244
247
  return "";
245
248
  }
246
249
  }
247
250
  async function getSources() {
248
- let sources2 = void 0;
249
- let interfaces2 = [];
251
+ const sources2 = [];
252
+ const interfaces2 = [];
250
253
  if (typeof swaggerUrl === "string") {
251
254
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
252
255
  if (Array.isArray(resources)) {
253
- sources2 = resources;
254
- interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
256
+ sources2.push(...resources);
257
+ interfaces2.push(...await Promise.all(resources.map(genResourcesCode)));
255
258
  } else {
256
- console.log(chalk__default.yellow(`[vite-plugin-swagger2ts]: resources error`) + JSON.stringify(resources));
259
+ console.log(chalk__default.yellow(`[${pluginName}]: resources error`) + JSON.stringify(resources));
257
260
  }
258
261
  }
259
262
  if (typeof jsonUrl === "string") {
260
263
  const swaggerJson = await fetchUrl(jsonUrl);
261
- if (!swaggerJson.info)
262
- return console.log(chalk__default.yellow(`[vite-plugin-swagger2ts]: jsonUrl error;`) + JSON.stringify(swaggerJson));
263
- interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
264
+ if (swaggerJson.info) {
265
+ const name = getApiName(swaggerJson);
266
+ interfaces2.push(await genInterface(swaggerJson, name));
267
+ sources2.push({ name });
268
+ } else {
269
+ console.log(chalk__default.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
270
+ }
264
271
  }
265
272
  if (typeof jsonPath === "string") {
266
273
  const _jsonPath = path.resolve(process.cwd(), jsonPath);
267
274
  const swaggerJson = JSON.parse(fs.readFileSync(_jsonPath, { encoding: "utf-8" }));
268
- interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
275
+ const name = getApiName(swaggerJson);
276
+ interfaces2.push(await genInterface(swaggerJson, name));
277
+ sources2.push({ name });
269
278
  }
270
279
  return { sources: sources2, interfaces: interfaces2 };
271
280
  }
272
- const { sources, interfaces } = await getSources() || {};
273
- if (!interfaces?.length)
281
+ const { sources, interfaces } = await getSources();
282
+ if (!interfaces.length)
274
283
  return;
275
284
  const prefix = `/* eslint-disable */
276
285
  /* prettier-ignore */
277
286
  // @ts-nocheck
278
287
  // noinspection JSUnusedGlobalSymbols
279
- // Generated by vite-plugin-swagger2ts`;
288
+ // Generated by ${pluginName}
289
+ `;
280
290
  const code = interfaces.reduce((a, b) => a + b, "");
281
- const suffix = sources ? interfaces.reduce((a, b, index) => {
291
+ const suffix = sources.length ? interfaces.reduce((a, b, index) => {
282
292
  if (b) {
283
293
  a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
284
294
  a += `${getPathsName(sources[index].name)}`;
@@ -289,11 +299,12 @@ async function loadSwaggerSource(options) {
289
299
  fs.writeFileSync(outputFile, prefix + code + suffix);
290
300
  }
291
301
  function swagger2TsPlugin(userOptions) {
292
- resolveOptions(userOptions).then((options) => {
293
- Promise.all(options.map(loadSwaggerSource));
294
- });
302
+ (async () => {
303
+ const options = await resolveOptions(userOptions);
304
+ await Promise.all(options.map(loadSwaggerSource));
305
+ })();
295
306
  return {
296
- name: "vite-plugin-swagger2ts",
307
+ name: `${pluginName}`,
297
308
  enforce: "pre",
298
309
  apply: "serve"
299
310
  };
package/dist/index.mjs CHANGED
@@ -5,6 +5,7 @@ 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";
8
9
  function loadConfig() {
9
10
  return loadConfig$1({
10
11
  sources: [
@@ -222,7 +223,9 @@ async function loadSwaggerSource(options) {
222
223
  else
223
224
  throw new Error(JSON.stringify(docs));
224
225
  }
225
- return generateDocs(docs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
226
+ const res = generateDocs(docs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
227
+ console.log(chalk.green(`[${pluginName}]: ${docsName} success`));
228
+ return res;
226
229
  }
227
230
  async function genResourcesCode(source) {
228
231
  const { url, name } = source;
@@ -231,45 +234,52 @@ async function loadSwaggerSource(options) {
231
234
  const code2 = await genInterface(docs, name);
232
235
  return code2;
233
236
  } catch (error) {
234
- console.log(chalk.yellow(`[vite-plugin-swagger2ts]: ${name} error;`) + error.message);
237
+ console.log(chalk.yellow(`[${pluginName}]: ${name} error;`) + error.message);
235
238
  return "";
236
239
  }
237
240
  }
238
241
  async function getSources() {
239
- let sources2 = void 0;
240
- let interfaces2 = [];
242
+ const sources2 = [];
243
+ const interfaces2 = [];
241
244
  if (typeof swaggerUrl === "string") {
242
245
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
243
246
  if (Array.isArray(resources)) {
244
- sources2 = resources;
245
- interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
247
+ sources2.push(...resources);
248
+ interfaces2.push(...await Promise.all(resources.map(genResourcesCode)));
246
249
  } else {
247
- console.log(chalk.yellow(`[vite-plugin-swagger2ts]: resources error`) + JSON.stringify(resources));
250
+ console.log(chalk.yellow(`[${pluginName}]: resources error`) + JSON.stringify(resources));
248
251
  }
249
252
  }
250
253
  if (typeof jsonUrl === "string") {
251
254
  const swaggerJson = await fetchUrl(jsonUrl);
252
- if (!swaggerJson.info)
253
- return console.log(chalk.yellow(`[vite-plugin-swagger2ts]: jsonUrl error;`) + JSON.stringify(swaggerJson));
254
- interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
255
+ if (swaggerJson.info) {
256
+ const name = getApiName(swaggerJson);
257
+ interfaces2.push(await genInterface(swaggerJson, name));
258
+ sources2.push({ name });
259
+ } else {
260
+ console.log(chalk.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
261
+ }
255
262
  }
256
263
  if (typeof jsonPath === "string") {
257
264
  const _jsonPath = resolve(process.cwd(), jsonPath);
258
265
  const swaggerJson = JSON.parse(readFileSync(_jsonPath, { encoding: "utf-8" }));
259
- interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
266
+ const name = getApiName(swaggerJson);
267
+ interfaces2.push(await genInterface(swaggerJson, name));
268
+ sources2.push({ name });
260
269
  }
261
270
  return { sources: sources2, interfaces: interfaces2 };
262
271
  }
263
- const { sources, interfaces } = await getSources() || {};
264
- if (!interfaces?.length)
272
+ const { sources, interfaces } = await getSources();
273
+ if (!interfaces.length)
265
274
  return;
266
275
  const prefix = `/* eslint-disable */
267
276
  /* prettier-ignore */
268
277
  // @ts-nocheck
269
278
  // noinspection JSUnusedGlobalSymbols
270
- // Generated by vite-plugin-swagger2ts`;
279
+ // Generated by ${pluginName}
280
+ `;
271
281
  const code = interfaces.reduce((a, b) => a + b, "");
272
- const suffix = sources ? interfaces.reduce((a, b, index) => {
282
+ const suffix = sources.length ? interfaces.reduce((a, b, index) => {
273
283
  if (b) {
274
284
  a += !a ? `export type ${getPathsName("_Intersection")} = ` : `&`;
275
285
  a += `${getPathsName(sources[index].name)}`;
@@ -280,11 +290,12 @@ async function loadSwaggerSource(options) {
280
290
  writeFileSync(outputFile, prefix + code + suffix);
281
291
  }
282
292
  function swagger2TsPlugin(userOptions) {
283
- resolveOptions(userOptions).then((options) => {
284
- Promise.all(options.map(loadSwaggerSource));
285
- });
293
+ (async () => {
294
+ const options = await resolveOptions(userOptions);
295
+ await Promise.all(options.map(loadSwaggerSource));
296
+ })();
286
297
  return {
287
- name: "vite-plugin-swagger2ts",
298
+ name: `${pluginName}`,
288
299
  enforce: "pre",
289
300
  apply: "serve"
290
301
  };
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.10",
4
+ "version": "1.3.11",
5
5
  "license": "MIT",
6
6
  "author": "1SZX1 <2414126198@qq.com>",
7
7
  "repository": {