@1szx1/vite-plugin-swagger2ts 1.3.15 → 1.3.17

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
@@ -247,7 +247,7 @@ function getApiName(json) {
247
247
  }
248
248
 
249
249
  async function loadSwaggerSource(options) {
250
- const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
250
+ const { swaggerUrl, jsonUrl, jsonPath, jsonLoader, output, formatDocs, formatSchema } = options;
251
251
  async function genInterface(docs, docsName) {
252
252
  docs = formatDocs ? formatDocs(docs) : docs;
253
253
  if (!("openapi" in docs)) {
@@ -274,6 +274,15 @@ async function loadSwaggerSource(options) {
274
274
  async function getSources() {
275
275
  const sources2 = [];
276
276
  const interfaces2 = [];
277
+ async function loadSwaggerJson(swaggerJson) {
278
+ if (swaggerJson.info) {
279
+ const name = getApiName(swaggerJson);
280
+ interfaces2.push(await genInterface(swaggerJson, name));
281
+ sources2.push({ name });
282
+ } else {
283
+ console.log(chalk__default.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
284
+ }
285
+ }
277
286
  if (typeof swaggerUrl === "string") {
278
287
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
279
288
  if (Array.isArray(resources)) {
@@ -285,20 +294,14 @@ async function loadSwaggerSource(options) {
285
294
  }
286
295
  if (typeof jsonUrl === "string") {
287
296
  const swaggerJson = await fetchUrl(jsonUrl);
288
- if (swaggerJson.info) {
289
- const name = getApiName(swaggerJson);
290
- interfaces2.push(await genInterface(swaggerJson, name));
291
- sources2.push({ name });
292
- } else {
293
- console.log(chalk__default.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
294
- }
297
+ loadSwaggerJson(swaggerJson);
295
298
  }
299
+ if (jsonLoader)
300
+ loadSwaggerJson(await jsonLoader());
296
301
  if (typeof jsonPath === "string") {
297
302
  const _jsonPath = path.resolve(process.cwd(), jsonPath);
298
303
  const swaggerJson = JSON.parse(fs.readFileSync(_jsonPath, { encoding: "utf-8" }));
299
- const name = getApiName(swaggerJson);
300
- interfaces2.push(await genInterface(swaggerJson, name));
301
- sources2.push({ name });
304
+ loadSwaggerJson(swaggerJson);
302
305
  }
303
306
  return { sources: sources2, interfaces: interfaces2 };
304
307
  }
@@ -315,7 +318,7 @@ async function loadSwaggerSource(options) {
315
318
  return a;
316
319
  }, "");
317
320
  const outputFile = path.resolve(process.cwd(), output);
318
- fs.writeFileSync(outputFile, await prettier.format(CODE_PREFIX + code + suffix, { parser: "typescript" }));
321
+ fs.writeFileSync(outputFile, CODE_PREFIX + await prettier.format(code + suffix, { parser: "typescript" }));
319
322
  return { sources, interfaces };
320
323
  }
321
324
  async function loadGlobTypes({ glob, output }) {
@@ -324,7 +327,7 @@ async function loadGlobTypes({ glob, output }) {
324
327
  const outputFile = path.resolve(process.cwd(), glob.output || "auto-types.d.ts");
325
328
  const importPath = path.relative(outputFile, output).replaceAll("\\", "/").replace(/^\.\.\//, "./");
326
329
  const intersection = getPathsName("_Intersection");
327
- const code = `${CODE_PREFIX}import { ${intersection} } from '${importPath}'
330
+ const code = `import { ${intersection} } from '${importPath}'
328
331
 
329
332
  type FilterOptional<Source, Condition> = Pick<
330
333
  Source,
@@ -338,7 +341,7 @@ declare global {
338
341
  ${generatorMethodType(intersection)}
339
342
  }
340
343
  }`;
341
- fs.writeFileSync(outputFile, await prettier.format(code, { parser: "typescript" }));
344
+ fs.writeFileSync(outputFile, await prettier.format(CODE_PREFIX + code, { parser: "typescript" }));
342
345
  }
343
346
  function swagger2TsPlugin(userOptions) {
344
347
  (async () => {
package/dist/index.d.cts CHANGED
@@ -15,6 +15,10 @@ interface Options {
15
15
  * openApi 本地json
16
16
  */
17
17
  jsonPath?: string;
18
+ /**
19
+ * 加载json的方法
20
+ */
21
+ jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
18
22
  output?: string;
19
23
  formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
20
24
  formatSchema?: FormatSchema;
package/dist/index.d.mts CHANGED
@@ -15,6 +15,10 @@ interface Options {
15
15
  * openApi 本地json
16
16
  */
17
17
  jsonPath?: string;
18
+ /**
19
+ * 加载json的方法
20
+ */
21
+ jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
18
22
  output?: string;
19
23
  formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
20
24
  formatSchema?: FormatSchema;
package/dist/index.d.ts CHANGED
@@ -15,6 +15,10 @@ interface Options {
15
15
  * openApi 本地json
16
16
  */
17
17
  jsonPath?: string;
18
+ /**
19
+ * 加载json的方法
20
+ */
21
+ jsonLoader?: () => OpenAPIObject | Promise<OpenAPIObject>;
18
22
  output?: string;
19
23
  formatDocs?: (docs: SwaggerDoc | OpenAPIObject) => typeof docs;
20
24
  formatSchema?: FormatSchema;
package/dist/index.mjs CHANGED
@@ -238,7 +238,7 @@ function getApiName(json) {
238
238
  }
239
239
 
240
240
  async function loadSwaggerSource(options) {
241
- const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
241
+ const { swaggerUrl, jsonUrl, jsonPath, jsonLoader, output, formatDocs, formatSchema } = options;
242
242
  async function genInterface(docs, docsName) {
243
243
  docs = formatDocs ? formatDocs(docs) : docs;
244
244
  if (!("openapi" in docs)) {
@@ -265,6 +265,15 @@ async function loadSwaggerSource(options) {
265
265
  async function getSources() {
266
266
  const sources2 = [];
267
267
  const interfaces2 = [];
268
+ async function loadSwaggerJson(swaggerJson) {
269
+ if (swaggerJson.info) {
270
+ const name = getApiName(swaggerJson);
271
+ interfaces2.push(await genInterface(swaggerJson, name));
272
+ sources2.push({ name });
273
+ } else {
274
+ console.log(chalk.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
275
+ }
276
+ }
268
277
  if (typeof swaggerUrl === "string") {
269
278
  const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
270
279
  if (Array.isArray(resources)) {
@@ -276,20 +285,14 @@ async function loadSwaggerSource(options) {
276
285
  }
277
286
  if (typeof jsonUrl === "string") {
278
287
  const swaggerJson = await fetchUrl(jsonUrl);
279
- if (swaggerJson.info) {
280
- const name = getApiName(swaggerJson);
281
- interfaces2.push(await genInterface(swaggerJson, name));
282
- sources2.push({ name });
283
- } else {
284
- console.log(chalk.yellow(`[${pluginName}]: jsonUrl error;`) + JSON.stringify(swaggerJson));
285
- }
288
+ loadSwaggerJson(swaggerJson);
286
289
  }
290
+ if (jsonLoader)
291
+ loadSwaggerJson(await jsonLoader());
287
292
  if (typeof jsonPath === "string") {
288
293
  const _jsonPath = resolve(process.cwd(), jsonPath);
289
294
  const swaggerJson = JSON.parse(readFileSync(_jsonPath, { encoding: "utf-8" }));
290
- const name = getApiName(swaggerJson);
291
- interfaces2.push(await genInterface(swaggerJson, name));
292
- sources2.push({ name });
295
+ loadSwaggerJson(swaggerJson);
293
296
  }
294
297
  return { sources: sources2, interfaces: interfaces2 };
295
298
  }
@@ -306,7 +309,7 @@ async function loadSwaggerSource(options) {
306
309
  return a;
307
310
  }, "");
308
311
  const outputFile = resolve(process.cwd(), output);
309
- writeFileSync(outputFile, await format(CODE_PREFIX + code + suffix, { parser: "typescript" }));
312
+ writeFileSync(outputFile, CODE_PREFIX + await format(code + suffix, { parser: "typescript" }));
310
313
  return { sources, interfaces };
311
314
  }
312
315
  async function loadGlobTypes({ glob, output }) {
@@ -315,7 +318,7 @@ async function loadGlobTypes({ glob, output }) {
315
318
  const outputFile = resolve(process.cwd(), glob.output || "auto-types.d.ts");
316
319
  const importPath = relative(outputFile, output).replaceAll("\\", "/").replace(/^\.\.\//, "./");
317
320
  const intersection = getPathsName("_Intersection");
318
- const code = `${CODE_PREFIX}import { ${intersection} } from '${importPath}'
321
+ const code = `import { ${intersection} } from '${importPath}'
319
322
 
320
323
  type FilterOptional<Source, Condition> = Pick<
321
324
  Source,
@@ -329,7 +332,7 @@ declare global {
329
332
  ${generatorMethodType(intersection)}
330
333
  }
331
334
  }`;
332
- writeFileSync(outputFile, await format(code, { parser: "typescript" }));
335
+ writeFileSync(outputFile, await format(CODE_PREFIX + code, { parser: "typescript" }));
333
336
  }
334
337
  function swagger2TsPlugin(userOptions) {
335
338
  (async () => {
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.15",
4
+ "version": "1.3.17",
5
5
  "license": "MIT",
6
6
  "author": "1SZX1 <2414126198@qq.com>",
7
7
  "repository": {