@1szx1/vite-plugin-swagger2ts 1.2.3 → 1.3.1

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
@@ -6,5 +6,6 @@ require('path');
6
6
  require('swagger2openapi');
7
7
  require('cross-fetch');
8
8
  require('unconfig');
9
+ require('chalk');
9
10
 
10
11
  index.default();
package/dist/bin.mjs CHANGED
@@ -4,5 +4,6 @@ import 'path';
4
4
  import 'swagger2openapi';
5
5
  import 'cross-fetch';
6
6
  import 'unconfig';
7
+ import 'chalk';
7
8
 
8
9
  swagger2TsPlugin();
package/dist/index.cjs CHANGED
@@ -7,10 +7,12 @@ const path = require('path');
7
7
  const swagger2openapi = require('swagger2openapi');
8
8
  const fetch = require('cross-fetch');
9
9
  const unconfig = require('unconfig');
10
+ const chalk = require('chalk');
10
11
 
11
12
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
12
13
 
13
14
  const fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
15
+ const chalk__default = /*#__PURE__*/_interopDefaultCompat(chalk);
14
16
 
15
17
  function loadConfig() {
16
18
  return unconfig.loadConfig({
@@ -97,7 +99,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
97
99
  const pathParameters = allParameters.filter((item) => item.in === "path");
98
100
  const bodyParameters = allParameters.filter((item) => !item.in);
99
101
  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];
102
+ let responses = Object.entries(options.responses).filter((items) => items[0].startsWith("20") || items[0] == "default").sort()?.[0]?.[1];
101
103
  if ("$ref" in responses) {
102
104
  responses = getRefObject(responses.$ref);
103
105
  }
@@ -215,6 +217,9 @@ ${title ? ` * @title ${title}
215
217
  function getPathsName(docsName) {
216
218
  return `Paths${getRefName(docsName)}`;
217
219
  }
220
+ function getApiName(json) {
221
+ return json.info.title.replace(" ", "_");
222
+ }
218
223
 
219
224
  function convertObjPromise(docs) {
220
225
  return new Promise((resolve2, reject) => {
@@ -226,7 +231,7 @@ function convertObjPromise(docs) {
226
231
  });
227
232
  }
228
233
  async function loadSwaggerSource(options) {
229
- const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
234
+ const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
230
235
  async function genInterface(docs, docsName) {
231
236
  let openapiDocs = formatDocs ? formatDocs(docs) : docs;
232
237
  if (!("openapi" in openapiDocs)) {
@@ -237,14 +242,14 @@ async function loadSwaggerSource(options) {
237
242
  }
238
243
  return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
239
244
  }
240
- async function genCode(source) {
245
+ async function genResourcesCode(source) {
241
246
  const { url, name } = source;
242
247
  try {
243
248
  const docs = await fetchUrl(`${swaggerUrl}${url}`);
244
249
  const code2 = await genInterface(docs, name);
245
250
  return code2;
246
251
  } catch (error) {
247
- console.log(`%c[vite-plugin-swagger2ts]: ${name} error`, "color: #ff0000;", error.message);
252
+ console.log(chalk__default.yellow(`[vite-plugin-swagger2ts]: ${name} error;`) + error.message);
248
253
  return "";
249
254
  }
250
255
  }
@@ -252,19 +257,24 @@ async function loadSwaggerSource(options) {
252
257
  let sources2 = void 0;
253
258
  let interfaces2 = [];
254
259
  if (typeof swaggerUrl === "string") {
255
- const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
260
+ const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
256
261
  if (Array.isArray(resources)) {
257
262
  sources2 = resources;
258
- interfaces2.push(...await Promise.all(sources2.map(genCode)));
263
+ interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
259
264
  } else {
260
- console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
265
+ console.log(chalk__default.yellow(`[vite-plugin-swagger2ts]: resources error`) + JSON.stringify(resources));
261
266
  }
262
267
  }
263
268
  if (typeof jsonUrl === "string") {
264
269
  const swaggerJson = await fetchUrl(jsonUrl);
265
270
  if (!swaggerJson.info)
266
- return console.log(`%c[vite-plugin-swagger2ts]: jsonUrl error`, "color: #ff0000;", JSON.stringify(swaggerJson));
267
- interfaces2.push(await genInterface(swaggerJson, swaggerJson.info.title.replace(" ", "_")));
271
+ return console.log(chalk__default.yellow(`[vite-plugin-swagger2ts]: jsonUrl error;`) + JSON.stringify(swaggerJson));
272
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
273
+ }
274
+ if (typeof jsonPath === "string") {
275
+ const _jsonPath = path.resolve(process.cwd(), jsonPath);
276
+ const swaggerJson = JSON.parse(fs.readFileSync(_jsonPath, { encoding: "utf-8" }));
277
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
268
278
  }
269
279
  return { sources: sources2, interfaces: interfaces2 };
270
280
  }
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,8 +1,9 @@
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';
5
5
  import { loadConfig as loadConfig$1 } from 'unconfig';
6
+ import chalk from 'chalk';
6
7
 
7
8
  function loadConfig() {
8
9
  return loadConfig$1({
@@ -89,7 +90,7 @@ function generateDocs(input, { docsName, baseUrl, formatSchema }) {
89
90
  const pathParameters = allParameters.filter((item) => item.in === "path");
90
91
  const bodyParameters = allParameters.filter((item) => !item.in);
91
92
  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];
93
+ let responses = Object.entries(options.responses).filter((items) => items[0].startsWith("20") || items[0] == "default").sort()?.[0]?.[1];
93
94
  if ("$ref" in responses) {
94
95
  responses = getRefObject(responses.$ref);
95
96
  }
@@ -207,6 +208,9 @@ ${title ? ` * @title ${title}
207
208
  function getPathsName(docsName) {
208
209
  return `Paths${getRefName(docsName)}`;
209
210
  }
211
+ function getApiName(json) {
212
+ return json.info.title.replace(" ", "_");
213
+ }
210
214
 
211
215
  function convertObjPromise(docs) {
212
216
  return new Promise((resolve2, reject) => {
@@ -218,7 +222,7 @@ function convertObjPromise(docs) {
218
222
  });
219
223
  }
220
224
  async function loadSwaggerSource(options) {
221
- const { swaggerUrl, jsonUrl, output, formatDocs, formatSchema } = options;
225
+ const { swaggerUrl, jsonUrl, jsonPath, output, formatDocs, formatSchema } = options;
222
226
  async function genInterface(docs, docsName) {
223
227
  let openapiDocs = formatDocs ? formatDocs(docs) : docs;
224
228
  if (!("openapi" in openapiDocs)) {
@@ -229,14 +233,14 @@ async function loadSwaggerSource(options) {
229
233
  }
230
234
  return generateDocs(openapiDocs, { docsName, baseUrl: docs.basePath ?? "", formatSchema });
231
235
  }
232
- async function genCode(source) {
236
+ async function genResourcesCode(source) {
233
237
  const { url, name } = source;
234
238
  try {
235
239
  const docs = await fetchUrl(`${swaggerUrl}${url}`);
236
240
  const code2 = await genInterface(docs, name);
237
241
  return code2;
238
242
  } catch (error) {
239
- console.log(`%c[vite-plugin-swagger2ts]: ${name} error`, "color: #ff0000;", error.message);
243
+ console.log(chalk.yellow(`[vite-plugin-swagger2ts]: ${name} error;`) + error.message);
240
244
  return "";
241
245
  }
242
246
  }
@@ -244,19 +248,24 @@ async function loadSwaggerSource(options) {
244
248
  let sources2 = void 0;
245
249
  let interfaces2 = [];
246
250
  if (typeof swaggerUrl === "string") {
247
- const resources = await fetchUrl(`${swaggerUrl}/swagger-resources`).catch((error) => error);
251
+ const resources = await fetchUrl(`${swaggerUrl}/swagger-resources;`).catch((error) => error);
248
252
  if (Array.isArray(resources)) {
249
253
  sources2 = resources;
250
- interfaces2.push(...await Promise.all(sources2.map(genCode)));
254
+ interfaces2.push(...await Promise.all(sources2.map(genResourcesCode)));
251
255
  } else {
252
- console.log(`%c[vite-plugin-swagger2ts]: resources error`, "color: #ff0000;", JSON.stringify(resources));
256
+ console.log(chalk.yellow(`[vite-plugin-swagger2ts]: resources error`) + JSON.stringify(resources));
253
257
  }
254
258
  }
255
259
  if (typeof jsonUrl === "string") {
256
260
  const swaggerJson = await fetchUrl(jsonUrl);
257
261
  if (!swaggerJson.info)
258
- 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
+ return console.log(chalk.yellow(`[vite-plugin-swagger2ts]: jsonUrl error;`) + JSON.stringify(swaggerJson));
263
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
264
+ }
265
+ if (typeof jsonPath === "string") {
266
+ const _jsonPath = resolve(process.cwd(), jsonPath);
267
+ const swaggerJson = JSON.parse(readFileSync(_jsonPath, { encoding: "utf-8" }));
268
+ interfaces2.push(await genInterface(swaggerJson, getApiName(swaggerJson)));
260
269
  }
261
270
  return { sources: sources2, interfaces: interfaces2 };
262
271
  }
package/package.json CHANGED
@@ -1,108 +1,109 @@
1
- {
2
- "name": "@1szx1/vite-plugin-swagger2ts",
3
- "description": "forked from https://github.com/morelearn1990/vite-plugin-swagger2ts",
4
- "version": "1.2.3",
5
- "license": "MIT",
6
- "author": "1SZX1 <2414126198@qq.com>",
7
- "repository": {
8
- "type": "git",
9
- "url": "https://github.com/1SZX1/vite-plugin-swagger2ts"
10
- },
11
- "homepage": "https://github.com/1SZX1/vite-plugin-swagger2ts",
12
- "bugs": "https://github.com/1SZX1/vite-plugin-swagger2ts/issues",
13
- "files": [
14
- "dist"
15
- ],
16
- "exports": {
17
- ".": {
18
- "require": "./dist/index.cjs",
19
- "import": "./dist/index.mjs",
20
- "types": "./dist/index.d.ts"
21
- },
22
- "./adapters": {
23
- "require": "./dist/adapters.cjs",
24
- "import": "./dist/adapters.mjs",
25
- "types": "./dist/adapters.d.ts"
26
- }
27
- },
28
- "main": "dist/index.cjs",
29
- "module": "dist/index.mjs",
30
- "types": "dist/index.d.ts",
31
- "typesVersions": {
32
- "*": {
33
- "adapters": [
34
- "./dist/adapters"
35
- ]
36
- }
37
- },
38
- "bin": {
39
- "swagger2ts": "./dist/bin.mjs"
40
- },
41
- "scripts": {
42
- "build": "unbuild",
43
- "stub": "unbuild --stub",
44
- "prepublishOnly": "pnpm run build",
45
- "release": "pnpx git-ensure -a && pnpx bumpp --commit --tag --push",
46
- "demo:dev": "pnpm -C examples/demo run dev",
47
- "demo:build": "pnpm -C examples/demo run build",
48
- "demo:preview": "pnpm -C examples/demo run preview",
49
- "test": "jest",
50
- "lint": "eslint --fix --ext .ts ./",
51
- "version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
52
- "preinstall": "npx only-allow pnpm"
53
- },
54
- "dependencies": {
55
- "axios": "^1.4.0",
56
- "cross-fetch": "^3.1.5",
57
- "luch-request": "^3.1.0",
58
- "openapi3-ts": "^2.0.2",
59
- "swagger2openapi": "^7.0.8",
60
- "unconfig": "^0.3.9"
61
- },
62
- "peerDependencies": {
63
- "axios": "^1.4.0",
64
- "luch-request": "^3.1.0",
65
- "vite": "^2.0.0"
66
- },
67
- "devDependencies": {
68
- "@antfu/eslint-config": "^0.6.6",
69
- "@commitlint/cli": "^12.1.4",
70
- "@commitlint/config-conventional": "^12.1.4",
71
- "@dcloudio/types": "^3.3.3",
72
- "@types/eslint": "^8.44.0",
73
- "@types/jest": "^26.0.24",
74
- "@types/node": "^14.18.12",
75
- "@typescript-eslint/eslint-plugin": "^4.33.0",
76
- "@typescript-eslint/parser": "^5.14.0",
77
- "conventional-changelog": "^3.1.25",
78
- "cz-conventional-changelog": "^3.3.0",
79
- "eslint": "^8.44.0",
80
- "husky": "^8.0.3",
81
- "jest": "^26.6.3",
82
- "jest-esbuild": "^0.1.6",
83
- "lint-staged": "^10.5.4",
84
- "typescript": "^4.7.0",
85
- "unbuild": "^2.0.0",
86
- "vite": "^2.8.6"
87
- },
88
- "lint-staged": {
89
- "*.{ts}": "eslint --fix --ext",
90
- "*.{ts,md}": "prettier --write"
91
- },
92
- "husky": {
93
- "hooks": {
94
- "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
95
- "pre-commit": "lint-staged"
96
- }
97
- },
98
- "commitlint": {
99
- "extends": [
100
- "@commitlint/config-conventional"
101
- ]
102
- },
103
- "config": {
104
- "commitizen": {
105
- "path": "./node_modules/cz-conventional-changelog"
106
- }
107
- }
1
+ {
2
+ "name": "@1szx1/vite-plugin-swagger2ts",
3
+ "description": "forked from https://github.com/morelearn1990/vite-plugin-swagger2ts",
4
+ "version": "1.3.1",
5
+ "license": "MIT",
6
+ "author": "1SZX1 <2414126198@qq.com>",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/1SZX1/vite-plugin-swagger2ts"
10
+ },
11
+ "homepage": "https://github.com/1SZX1/vite-plugin-swagger2ts",
12
+ "bugs": "https://github.com/1SZX1/vite-plugin-swagger2ts/issues",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "require": "./dist/index.cjs",
19
+ "import": "./dist/index.mjs",
20
+ "types": "./dist/index.d.ts"
21
+ },
22
+ "./adapters": {
23
+ "require": "./dist/adapters.cjs",
24
+ "import": "./dist/adapters.mjs",
25
+ "types": "./dist/adapters.d.ts"
26
+ }
27
+ },
28
+ "main": "dist/index.cjs",
29
+ "module": "dist/index.mjs",
30
+ "types": "dist/index.d.ts",
31
+ "typesVersions": {
32
+ "*": {
33
+ "adapters": [
34
+ "./dist/adapters"
35
+ ]
36
+ }
37
+ },
38
+ "bin": {
39
+ "swagger2ts": "./dist/bin.mjs"
40
+ },
41
+ "scripts": {
42
+ "build": "unbuild",
43
+ "stub": "unbuild --stub",
44
+ "prepublishOnly": "pnpm run build",
45
+ "release": "pnpx git-ensure -a && pnpx bumpp --commit --tag --push",
46
+ "demo:dev": "pnpm -C examples/demo run dev",
47
+ "demo:build": "pnpm -C examples/demo run build",
48
+ "demo:preview": "pnpm -C examples/demo run preview",
49
+ "test": "jest",
50
+ "lint": "eslint --fix --ext .ts ./",
51
+ "version": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md",
52
+ "preinstall": "npx only-allow pnpm"
53
+ },
54
+ "dependencies": {
55
+ "axios": "^1.4.0",
56
+ "cross-fetch": "^3.1.5",
57
+ "luch-request": "^3.1.0",
58
+ "openapi3-ts": "^2.0.2",
59
+ "swagger2openapi": "^7.0.8",
60
+ "unconfig": "^0.3.9"
61
+ },
62
+ "peerDependencies": {
63
+ "axios": "^1.4.0",
64
+ "luch-request": "^3.1.0",
65
+ "chalk": "^5.3.0",
66
+ "vite": "^2.0.0"
67
+ },
68
+ "devDependencies": {
69
+ "@antfu/eslint-config": "^0.6.6",
70
+ "@commitlint/cli": "^12.1.4",
71
+ "@commitlint/config-conventional": "^12.1.4",
72
+ "@dcloudio/types": "^3.3.3",
73
+ "@types/eslint": "^8.44.0",
74
+ "@types/jest": "^26.0.24",
75
+ "@types/node": "^14.18.12",
76
+ "@typescript-eslint/eslint-plugin": "^4.33.0",
77
+ "@typescript-eslint/parser": "^5.14.0",
78
+ "conventional-changelog": "^3.1.25",
79
+ "cz-conventional-changelog": "^3.3.0",
80
+ "eslint": "^8.44.0",
81
+ "husky": "^8.0.3",
82
+ "jest": "^26.6.3",
83
+ "jest-esbuild": "^0.1.6",
84
+ "lint-staged": "^10.5.4",
85
+ "typescript": "^4.7.0",
86
+ "unbuild": "^2.0.0",
87
+ "vite": "^2.8.6"
88
+ },
89
+ "lint-staged": {
90
+ "*.{ts}": "eslint --fix --ext",
91
+ "*.{ts,md}": "prettier --write"
92
+ },
93
+ "husky": {
94
+ "hooks": {
95
+ "commit-msg": "commitlint -E HUSKY_GIT_PARAMS",
96
+ "pre-commit": "lint-staged"
97
+ }
98
+ },
99
+ "commitlint": {
100
+ "extends": [
101
+ "@commitlint/config-conventional"
102
+ ]
103
+ },
104
+ "config": {
105
+ "commitizen": {
106
+ "path": "./node_modules/cz-conventional-changelog"
107
+ }
108
+ }
108
109
  }