@fxri/swagger2api 1.0.8 → 1.0.10

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.js CHANGED
@@ -2,13 +2,14 @@ import axios from "axios";
2
2
  import { execSync } from "node:child_process";
3
3
  import os from "node:os";
4
4
  import fs from "node:fs";
5
+ import url from "node:url";
5
6
  import path from "node:path";
6
7
  import prompts from "prompts";
7
- import { getParam, isUrl, saveFile } from "./utils/index.js";
8
+ import { getParam, getParamList, isUrl, saveFile } from "./utils/index.js";
8
9
  /** 配置集合 */
9
10
  let confs = {
10
11
  /** 文件 */
11
- file: path.resolve(getParam("--config") ?? ".swaggerrc"),
12
+ file: path.resolve(getParam(["--config", "-c"]) ?? ".swaggerrc"),
12
13
  /** 参数 */
13
14
  param: {}
14
15
  };
@@ -32,9 +33,7 @@ let datas = {
32
33
  /** 用户目录 */
33
34
  home: os.homedir(),
34
35
  /** 脚本 */
35
- script: path.resolve(import.meta.dirname, "../node_modules/.bin/swagger-typescript-api"),
36
- /** 参数 */
37
- param: process.argv.filter((_item, index) => index > 1),
36
+ script: path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), "../node_modules/.bin/swagger-typescript-api"),
38
37
  /** 文件 */
39
38
  file: path.resolve(`${confs.param.output}/${confs.param.name}-${new Date().getTime()}.json`),
40
39
  /** 枚举 */
@@ -48,24 +47,34 @@ function onGenerate(data) {
48
47
  // 处理数据
49
48
  Object.entries(data.paths).forEach((item) => {
50
49
  Object.keys(item[1]).forEach((item2) => {
51
- data.paths[item[0]][item2].operationId = `${item[0]}/${item2}`
52
- .replace(/([^\}]\/)get$/, "$1query")
53
- .replace(/\/\{.+?\}/, "")
54
- .replace(/^\/[^\/]*/, "");
55
- const last = data.paths[item[0]][item2].operationId.split("/");
56
- if (typeof datas.enum[last.slice(-1)] == "number") {
57
- datas.enum[last.slice(-1)]++;
50
+ // 配置操作id
51
+ let id = `${item[0]}/${item2}`;
52
+ // 转换无{.+}get为query
53
+ if (getParam(["--convert-get", "-cg"], false)) {
54
+ id = id.replace(/([^\}]\/)get$/g, "$1query");
55
+ }
56
+ // 移除参数
57
+ if (getParam(["--remove-param", "-rp"], false)) {
58
+ id = id.replace(/\/\{.+?\}/g, "");
58
59
  }
59
- else {
60
- datas.enum[last.slice(-1)] = 1;
60
+ // 移除前缀索引
61
+ if (getParam(["--remove-prefix-index", "-rpi"])) {
62
+ let ids = id.split("/");
63
+ ids = ids.slice(0, 1).concat(ids.slice(Number(getParam(["--remove-prefix-index", "-rpi"])) + 2));
64
+ id = ids.join("/");
61
65
  }
66
+ // 配置操作id
67
+ data.paths[item[0]][item2].operationId = id;
68
+ // 统计请求方式
69
+ const method = id.split("/").slice(-1)[0];
70
+ typeof datas.enum[method] == "number" ? datas.enum[method]++ : (datas.enum[method] = 1);
62
71
  });
63
72
  });
64
73
  console.log("就绪:", ...Object.entries(datas.enum).map((item) => item.join("-")));
65
74
  // 保存文档
66
75
  saveFile(datas.file, JSON.stringify(data));
67
76
  // 生成接口
68
- const param = ["generate", "-n", `${confs.param.name}.ts`, "-o", path.resolve(confs.param.output), "-p", datas.file, ...datas.param];
77
+ const param = ["generate", "-n", `${confs.param.name}.ts`, "-o", path.resolve(confs.param.output), "-p", datas.file, ...getParamList(false)];
69
78
  console.log("执行:", execSync(`${datas.script} ${param.join(" ")}`, { cwd: datas.home }).toString());
70
79
  // 删除文档
71
80
  fs.unlinkSync(datas.file);
@@ -1,4 +1,6 @@
1
1
  /** 获取参数 */
2
- export declare function getParam(name: string | string[]): string;
2
+ export declare function getParam(name: string | string[], next?: boolean, use?: boolean): string;
3
+ /** 获取参数列表 */
4
+ export declare function getParamList(used?: boolean): string[];
3
5
  /** 保存文件 */
4
6
  export declare function saveFile(file: any, data: any): void;
@@ -1,16 +1,36 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ /** 数据集合 */
4
+ let datas = {
5
+ /** 参数 */
6
+ param: process.argv.filter((_item, index) => index > 1),
7
+ /** 已使用 */
8
+ used: {}
9
+ };
3
10
  /** 获取参数 */
4
- export function getParam(name) {
5
- !Array.isArray(name) && (name = [name]);
6
- const index = process.argv.findLastIndex((item) => name.includes(item));
7
- return index >= 0 ? process.argv[index + 1] : undefined;
11
+ export function getParam(name, next = true, use = true) {
12
+ // 处理名称
13
+ if (!Array.isArray(name))
14
+ name = [name];
15
+ // 查询索引
16
+ const index = datas.param.findLastIndex((item) => name.includes(item));
17
+ // 配置已使用
18
+ use && index >= 0 && Object.assign(datas.used, { [index]: true, [index + 1]: next });
19
+ // 返回参数
20
+ return index >= 0 ? datas.param[index + Number(next)] : undefined;
21
+ }
22
+ /** 获取参数列表 */
23
+ export function getParamList(used) {
24
+ return typeof used == "boolean" ? datas.param.filter((_item, index) => (used ? datas.used[index] : !datas.used[index])) : datas.param;
8
25
  }
9
26
  /** 保存文件 */
10
27
  export function saveFile(file, data) {
28
+ // 获取目录
11
29
  const dir = path.dirname(file);
30
+ // 创建目录
12
31
  if (!fs.existsSync(dir)) {
13
32
  fs.mkdirSync(dir, { recursive: true });
14
33
  }
34
+ // 写入文件
15
35
  fs.writeFileSync(file, data);
16
36
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fxri/swagger2api",
3
- "version": "1.0.8",
4
- "description": "基于 swagger-typescript-api,将 Swagger 文档自动转为 TypeScript 格式的 API 文件",
3
+ "version": "1.0.10",
4
+ "description": "基于 swagger-typescript-api,将 Swagger 文档自动转为 TypeScript 格式的 API 文件。",
5
5
  "license": "MIT",
6
6
  "author": {
7
7
  "name": "唐启云",
@@ -46,6 +46,7 @@
46
46
  "dev": "pnpm build && node ./dist/index.js",
47
47
  "prebuild": "rimraf dist",
48
48
  "build": "tsc",
49
- "release": "pnpm build && pnpm publish"
49
+ "release": "pnpm build && pnpm publish",
50
+ "api": "pnpm dev --axios --responses"
50
51
  }
51
52
  }