swaggerjsontoapidocs 1.1.0 → 1.3.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Esteban Fernandez <efernandezdev@gmail.com>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # swaggerjsontoapidocs CLI
2
+
3
+ This project is a Command Line Interface (CLI) tool that generates API documentation from a Swagger JSON file. It is developed in TypeScript and uses Node.js.
4
+
5
+ ## Installation
6
+
7
+ Make sure you have Node.js installed on your system. Then, install the project dependencies by running:
8
+
9
+ ```bash
10
+ npm install swaggerjsontoapidocs
11
+ ```
12
+
13
+ ```bash
14
+ npm install swaggerjsontoapidocs -g
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ The CLI script is executed using the following command:
20
+
21
+ ```bash
22
+ npx swaggerjsontoapidocs [options]
23
+ ```
24
+
25
+ ### Available Arguments
26
+
27
+ - `-s, --swagger <url>`: Specifies the URL of the Swagger JSON file.
28
+ - `-bp, --basePath <path>`: Defines the base path where the API documentation will be generated.
29
+
30
+ ### Example Usage
31
+
32
+ ```bash
33
+ npx swaggerjsontoapidocs -s http://localhost:5033/swagger/v1/swagger.json --bp /api/
34
+ ```
35
+
36
+ In this example:
37
+
38
+ - `-s` points to the URL of the Swagger JSON file.
39
+ - `-bp` defines the base path `/api/` where the documentation will be generated.
40
+
41
+ <details>
42
+ <summary>Swagger.json (Click to expand)</summary>
43
+
44
+ ```json
45
+ {
46
+ "openapi": "3.0.1",
47
+ "info": {
48
+ "title": "fakeApi",
49
+ "version": "1.0"
50
+ },
51
+ "paths": {
52
+ "/api/Users": {
53
+ "get": {
54
+ "tags": ["Users"],
55
+ "responses": {
56
+ "200": {
57
+ "description": "OK",
58
+ "content": {
59
+ "text/plain": {
60
+ "schema": {
61
+ "type": "array",
62
+ "items": {
63
+ "$ref": "#/components/schemas/Usuario"
64
+ }
65
+ }
66
+ },
67
+ "application/json": {
68
+ "schema": {
69
+ "type": "array",
70
+ "items": {
71
+ "$ref": "#/components/schemas/Usuario"
72
+ }
73
+ }
74
+ },
75
+ "text/json": {
76
+ "schema": {
77
+ "type": "array",
78
+ "items": {
79
+ "$ref": "#/components/schemas/Usuario"
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
87
+ },
88
+ "/api/Users/{id}": {
89
+ "get": {
90
+ "tags": ["Users"],
91
+ "parameters": [
92
+ {
93
+ "name": "id",
94
+ "in": "path",
95
+ "required": true,
96
+ "schema": {
97
+ "type": "string"
98
+ }
99
+ }
100
+ ],
101
+ "responses": {
102
+ "200": {
103
+ "description": "OK",
104
+ "content": {
105
+ "text/plain": {
106
+ "schema": {
107
+ "$ref": "#/components/schemas/Usuario"
108
+ }
109
+ },
110
+ "application/json": {
111
+ "schema": {
112
+ "$ref": "#/components/schemas/Usuario"
113
+ }
114
+ },
115
+ "text/json": {
116
+ "schema": {
117
+ "$ref": "#/components/schemas/Usuario"
118
+ }
119
+ }
120
+ }
121
+ }
122
+ }
123
+ },
124
+ "post": {
125
+ "tags": ["Users"],
126
+ "parameters": [
127
+ {
128
+ "name": "id",
129
+ "in": "path",
130
+ "required": true,
131
+ "schema": {
132
+ "type": "string"
133
+ }
134
+ }
135
+ ],
136
+ "responses": {
137
+ "200": {
138
+ "description": "OK",
139
+ "content": {
140
+ "text/plain": {
141
+ "schema": {
142
+ "$ref": "#/components/schemas/Usuario"
143
+ }
144
+ },
145
+ "application/json": {
146
+ "schema": {
147
+ "$ref": "#/components/schemas/Usuario"
148
+ }
149
+ },
150
+ "text/json": {
151
+ "schema": {
152
+ "$ref": "#/components/schemas/Usuario"
153
+ }
154
+ }
155
+ }
156
+ }
157
+ }
158
+ }
159
+ }
160
+ },
161
+ "components": {
162
+ "schemas": {
163
+ "Usuario": {
164
+ "type": "object",
165
+ "properties": {
166
+ "id": {
167
+ "type": "string",
168
+ "nullable": true
169
+ },
170
+ "nombre": {
171
+ "type": "string",
172
+ "nullable": true
173
+ },
174
+ "email": {
175
+ "type": "string",
176
+ "nullable": true
177
+ }
178
+ },
179
+ "additionalProperties": false
180
+ }
181
+ }
182
+ }
183
+ }
184
+ ```
185
+
186
+ </details>
187
+
188
+ ### Result
189
+
190
+ ```typescript
191
+ // Users.ts
192
+ export const Users = () => `Users`;
193
+ /**
194
+ * @param id
195
+ */
196
+ export const Users_id = (id: any) => `Users/${id}`;
197
+ ```
198
+
199
+ ## License
200
+
201
+ This project is licensed under the MIT License. See the LICENSE file for more details.
package/dist/index.js CHANGED
@@ -1,86 +1,48 @@
1
1
  #!/usr/bin/env node
2
2
  "use strict";
3
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
- if (k2 === undefined) k2 = k;
5
- var desc = Object.getOwnPropertyDescriptor(m, k);
6
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
- desc = { enumerable: true, get: function() { return m[k]; } };
8
- }
9
- Object.defineProperty(o, k2, desc);
10
- }) : (function(o, m, k, k2) {
11
- if (k2 === undefined) k2 = k;
12
- o[k2] = m[k];
13
- }));
14
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
- Object.defineProperty(o, "default", { enumerable: true, value: v });
16
- }) : function(o, v) {
17
- o["default"] = v;
18
- });
19
- var __importStar = (this && this.__importStar) || (function () {
20
- var ownKeys = function(o) {
21
- ownKeys = Object.getOwnPropertyNames || function (o) {
22
- var ar = [];
23
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
- return ar;
25
- };
26
- return ownKeys(o);
27
- };
28
- return function (mod) {
29
- if (mod && mod.__esModule) return mod;
30
- var result = {};
31
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
- __setModuleDefault(result, mod);
33
- return result;
34
- };
35
- })();
36
3
  var __importDefault = (this && this.__importDefault) || function (mod) {
37
4
  return (mod && mod.__esModule) ? mod : { "default": mod };
38
5
  };
39
6
  Object.defineProperty(exports, "__esModule", { value: true });
40
7
  const fs_1 = require("fs");
41
8
  const chalk_1 = __importDefault(require("chalk"));
42
- const readline = __importStar(require("node:readline/promises"));
43
9
  const node_path_1 = __importDefault(require("node:path"));
10
+ const yargs_1 = __importDefault(require("yargs"));
11
+ const helpers_1 = require("yargs/helpers");
44
12
  const jsonToApiDocs_1 = require("./jsonToApiDocs");
13
+ const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
14
+ .options({
15
+ swagger: {
16
+ alias: "s",
17
+ type: "string",
18
+ demandOption: true,
19
+ describe: "URL of the swagger.json file, e.g.: http://localhost:5033/swagger/v1/swagger.json",
20
+ },
21
+ bp: {
22
+ type: "string",
23
+ demandOption: true,
24
+ describe: "Basepath to remove from endpoints, e.g.: /api/",
25
+ },
26
+ })
27
+ .version()
28
+ .help()
29
+ .alias("help", "h")
30
+ .strict()
31
+ .parseSync();
45
32
  async function main() {
46
- console.log(chalk_1.default.green("To configure the script correctly, you must ensure that BE is running and you can view the Swagger page.".toUpperCase()));
47
- console.log(chalk_1.default.red("Have the PATH of the swagger.json and the BASEPATH which will determine how the endpoints will be returned."));
48
- await promptConfigFile();
49
- (0, jsonToApiDocs_1.initScript)();
50
- }
51
- async function promptConfigFile() {
52
- return new Promise((resolve) => {
53
- (async () => {
54
- const rl = readline.createInterface({
55
- input: process.stdin,
56
- output: process.stdout,
57
- });
58
- let pathInputValid = true;
59
- let basepathInputValid = true;
60
- let pathInput = "";
61
- let basepathInput = "";
62
- while (pathInputValid) {
63
- pathInput = await rl.question("Copy the correct path to download the swagger.json file, e.g.: http://localhost:5033/swagger/v1/swagger.json: ");
64
- if (pathInput.trim().length > 0) {
65
- pathInputValid = false;
66
- }
67
- }
68
- while (basepathInputValid) {
69
- basepathInput = await rl.question("Enter the basepath, e.g.: /api/ to deleted of path /api/Users/{id} => return export const Users_id = (id: any) => `Users/${id}`:");
70
- if (basepathInput.trim().length > 0) {
71
- basepathInputValid = false;
72
- }
73
- }
74
- rl.close();
75
- (0, fs_1.writeFile)(node_path_1.default.join(__dirname, "config.json"), `{"BASEPATH": "${basepathInput.trim()}","PATH": "${pathInput.trim()}"}`, "utf8", async (err) => {
76
- if (err) {
77
- console.error(err);
78
- }
79
- else {
80
- resolve();
81
- }
82
- });
83
- })();
33
+ console.log(chalk_1.default.green("CONFIGURING THE SCRIPT WITH THE PROVIDED ARGUMENTS..."));
34
+ const swaggerPath = argv.swagger;
35
+ const basePath = argv.bp;
36
+ console.log(chalk_1.default.blue(`Swagger Path: ${swaggerPath}`));
37
+ console.log(chalk_1.default.blue(`Basepath: ${basePath}`));
38
+ (0, fs_1.writeFile)(node_path_1.default.join(__dirname, "config.json"), `{"BASEPATH": "${basePath.trim()}","PATH": "${swaggerPath.trim()}"}`, "utf8", (err) => {
39
+ if (err) {
40
+ console.error(chalk_1.default.red("Error writing the configuration file:"), err);
41
+ }
42
+ else {
43
+ console.log(chalk_1.default.green("Configuration file created successfully."));
44
+ (0, jsonToApiDocs_1.initScript)();
45
+ }
84
46
  });
85
47
  }
86
48
  main();
@@ -42,6 +42,8 @@ const promises_1 = require("fs/promises");
42
42
  const path = __importStar(require("path"));
43
43
  const chalk_1 = __importDefault(require("chalk"));
44
44
  const prettier_1 = require("prettier");
45
+ const os_1 = __importDefault(require("os"));
46
+ const child_process_1 = require("child_process");
45
47
  const mainFolderOutPut = path.join(__dirname, "api_docs");
46
48
  let urlSwaggerJson = "";
47
49
  let basepath = "";
@@ -142,6 +144,30 @@ async function makeFileContainer(endpoints, foldersName) {
142
144
  }
143
145
  }
144
146
  console.log(`💾 show result ---> ${mainFolderOutPut}`);
147
+ openFileManager(mainFolderOutPut);
148
+ }
149
+ function openFileManager(fullPath) {
150
+ const platform = os_1.default.platform();
151
+ let command = "";
152
+ switch (platform) {
153
+ case "win32":
154
+ command = `explorer "${fullPath}"`;
155
+ break;
156
+ case "darwin":
157
+ command = `open "${fullPath}"`;
158
+ break;
159
+ case "linux":
160
+ command = `xdg-open "${fullPath}"`;
161
+ break;
162
+ default:
163
+ console.error(`Platform ${platform} is not supported.`);
164
+ return;
165
+ }
166
+ (0, child_process_1.exec)(command, (error) => {
167
+ if (error) {
168
+ console.error(`Error attempting to open folder: ${error.message}`);
169
+ }
170
+ });
145
171
  }
146
172
  async function formatWhitPrettier(filePath) {
147
173
  const content = await (0, promises_1.readFile)(filePath, "utf8");
package/package.json CHANGED
@@ -1,33 +1,37 @@
1
1
  {
2
2
  "name": "swaggerjsontoapidocs",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "script to convert swagger json to api docs, this help us to consume functions and center all endpoints in one place",
5
5
  "main": "index.ts",
6
6
  "scripts": {
7
7
  "build": "tsc",
8
- "api": "ts-node ./src/",
9
- "dist": "node ./dist/",
10
- "lint": "eslint ."
8
+ "api": "ts-node ./src/index.ts",
9
+ "dist": "node ./dist/index.js",
10
+ "lint": "eslint .",
11
+ "prepublishOnly": "npm run build"
11
12
  },
12
13
  "author": "Esteban Fernandez",
13
- "license": "ISC",
14
+ "license": "MIT",
14
15
  "dependencies": {
15
- "chalk": "^5.6.2",
16
- "prettier": "^3.7.4"
16
+ "chalk": "5.6.2",
17
+ "prettier": "3.7.4",
18
+ "yargs": "18.0.0"
17
19
  },
18
20
  "devDependencies": {
19
- "@eslint/js": "^9.39.2",
20
- "@types/prettier": "^2.7.3",
21
- "eslint": "^9.39.2",
22
- "globals": "^17.0.0",
23
- "jiti": "^2.6.1",
24
- "typescript": "^5.9.3",
25
- "typescript-eslint": "^8.52.0",
26
- "ts-node": "^10.9.2"
21
+ "@eslint/js": "9.39.2",
22
+ "@types/prettier": "2.7.3",
23
+ "@types/yargs": "17.0.35",
24
+ "eslint": "9.39.2",
25
+ "globals": "17.0.0",
26
+ "jiti": "2.6.1",
27
+ "ts-node": "10.9.2",
28
+ "typescript": "5.9.3",
29
+ "typescript-eslint": "8.52.0"
27
30
  },
28
31
  "files": [
29
32
  "dist",
30
- "package.json"
33
+ "package.json",
34
+ "!dist/api_docs"
31
35
  ],
32
36
  "engines": {
33
37
  "node": ">=15.0.0"