fast-openapi 1.0.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.
@@ -0,0 +1,7 @@
1
+ {
2
+ "requestLibPath": "import { request } from '@umijs/request';",
3
+ "schemaPath": "http://127.0.0.1:5500/sample-api.json",
4
+ "projectName": "my-project",
5
+ "apiPrefix": "/api/v1",
6
+ "servicePath": "./services"
7
+ }
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # fast-openapi
2
+
3
+ 一个 CLI 工具,用于读取 Swagger/OpenAPI JSON 文件并生成 API 调用文件, 目前只支持windows。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install -g fast-openapi
9
+ ```
10
+
11
+ 或在项目中安装:
12
+
13
+ ```bash
14
+ npm install fast-openapi --save-dev
15
+ ```
16
+
17
+ ## 使用
18
+
19
+ 1. 在你的项目根目录下创建配置文件 `.swaggerrc.json` 或 `.swaggerrc.js`
20
+
21
+ **`.swaggerrc.json` 示例:**
22
+
23
+ ```json
24
+ {
25
+ "requestLibPath": "import { request } from '@umijs/request';",
26
+ "schemaPath": "http://localhost:3000/api-docs",
27
+ "projectName": "my-project",
28
+ "apiPrefix": "/order",
29
+ "servicePath": "project_path/src/services"
30
+ }
31
+ ```
32
+
33
+ **`.swaggerrc.js` 示例:**
34
+
35
+ ```javascript
36
+ module.exports = {
37
+ requestLibPath: "import { request } from '@umijs/request';",
38
+ schemaPath: "http://localhost:3000/api-docs",
39
+ projectName: "my-project",
40
+ apiPrefix: "/order",
41
+ servicePath: path.join(__dirname, "src", "services"),
42
+ };
43
+ ```
44
+
45
+ 2. 运行命令:
46
+
47
+ ```bash
48
+ fast-openapi
49
+ ```
50
+
51
+ ## 许可证
52
+
53
+ MIT
package/bin/index.js ADDED
@@ -0,0 +1,108 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require("fs");
4
+ const path = require("path");
5
+ const { execSync } = require("child_process");
6
+
7
+ // 获取工作目录(运行 fast-openapi 命令的目录)
8
+ const workDir = process.cwd();
9
+
10
+ // 配置文件可能的路径
11
+ const configPaths = [
12
+ path.join(workDir, ".swaggerrc.json"),
13
+ path.join(workDir, ".swaggerrc.js"),
14
+ ];
15
+
16
+ // 查找配置文件
17
+ let configPath = null;
18
+ let config = null;
19
+
20
+ for (const filePath of configPaths) {
21
+ if (fs.existsSync(filePath)) {
22
+ configPath = filePath;
23
+ try {
24
+ if (filePath.endsWith(".json")) {
25
+ config = JSON.parse(fs.readFileSync(filePath, "utf8"));
26
+ } else if (filePath.endsWith(".js")) {
27
+ // 清除 require 缓存,防止模块缓存问题
28
+ delete require.cache[path.resolve(filePath)];
29
+ config = require(path.resolve(filePath));
30
+ }
31
+ console.log(`✓ Found config file: ${filePath}`);
32
+ break;
33
+ } catch (error) {
34
+ console.error(`✗ Error reading config file ${filePath}:`, error.message);
35
+ process.exit(1);
36
+ }
37
+ }
38
+ }
39
+
40
+ if (!config) {
41
+ console.error(
42
+ "✗ No .swaggerrc.json or .swaggerrc.js file found in the current directory",
43
+ );
44
+ process.exit(1);
45
+ }
46
+
47
+ // 获取 ropenapi 可执行程序的路径
48
+ const ropenapiPath = path.join(__dirname, "..", "libs", "ropenapi.exe");
49
+
50
+ if (!fs.existsSync(ropenapiPath)) {
51
+ console.error(`✗ ropenapi executable not found at: ${ropenapiPath}`);
52
+ process.exit(1);
53
+ }
54
+
55
+ // 构建命令参数
56
+ try {
57
+ // 将配置对象转换为命令行参数
58
+ const args = buildArguments(config);
59
+ const command = `"${ropenapiPath}" ${args}`;
60
+
61
+ console.log(`\n📝 Running...\n`);
62
+
63
+ // 执行 ropenapi
64
+ const result = execSync(command, {
65
+ cwd: workDir,
66
+ stdio: "inherit",
67
+ });
68
+
69
+ console.log("\n✓ Command executed successfully");
70
+ } catch (error) {
71
+ console.error("\n✗ Error executing ropenapi:", error.message);
72
+ process.exit(1);
73
+ }
74
+
75
+ /**
76
+ * 将配置对象转换为命令行参数
77
+ * @param {Object} config - 配置对象
78
+ * @returns {string} 命令行参数字符串
79
+ */
80
+ function buildArguments(config) {
81
+ const args = [];
82
+
83
+ for (const [_key, value] of Object.entries(config)) {
84
+ if (value === null || value === undefined) {
85
+ continue;
86
+ }
87
+
88
+ const key = _key === "schemaPath" ? "swagger" : convertCamelCase(_key);
89
+
90
+ if (typeof value === "boolean") {
91
+ if (value) {
92
+ args.push(`--${key}`);
93
+ }
94
+ } else if (Array.isArray(value)) {
95
+ args.push(`--${key}="${JSON.stringify(value)}"`);
96
+ } else if (typeof value === "object") {
97
+ args.push(`--${key}="${JSON.stringify(value)}"`);
98
+ } else {
99
+ args.push(`--${key}="${value}"`);
100
+ }
101
+ }
102
+
103
+ return args.join(" ");
104
+ }
105
+
106
+ function convertCamelCase(str) {
107
+ return str.replace(/([A-Z])/g, "-$1").toLowerCase();
108
+ }
Binary file
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "fast-openapi",
3
+ "version": "1.0.0",
4
+ "description": "A CLI tool to read Swagger/OpenAPI JSON files and generate API call files",
5
+ "main": "index.js",
6
+ "bin": {
7
+ "fast-openapi": "./bin/index.js"
8
+ },
9
+ "scripts": {
10
+ "start": "node bin/index.js",
11
+ "test": "echo \"Error: no test specified\" && exit 1"
12
+ },
13
+ "keywords": [
14
+ "swagger",
15
+ "openapi",
16
+ "api",
17
+ "cli"
18
+ ],
19
+ "author": "charlie_wei",
20
+ "license": "MIT",
21
+ "engines": {
22
+ "node": ">=12.0.0"
23
+ }
24
+ }