swaggerjsontoapidocs 1.1.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/dist/index.js +86 -0
- package/dist/jsonToApiDocs.js +156 -0
- package/package.json +38 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
|
+
};
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
const fs_1 = require("fs");
|
|
41
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
42
|
+
const readline = __importStar(require("node:readline/promises"));
|
|
43
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
44
|
+
const jsonToApiDocs_1 = require("./jsonToApiDocs");
|
|
45
|
+
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
|
+
})();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
main();
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.initScript = initScript;
|
|
40
|
+
const fs_1 = require("fs");
|
|
41
|
+
const promises_1 = require("fs/promises");
|
|
42
|
+
const path = __importStar(require("path"));
|
|
43
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
44
|
+
const prettier_1 = require("prettier");
|
|
45
|
+
const mainFolderOutPut = path.join(__dirname, "api_docs");
|
|
46
|
+
let urlSwaggerJson = "";
|
|
47
|
+
let basepath = "";
|
|
48
|
+
async function cleanFolderOutPut() {
|
|
49
|
+
await (0, promises_1.rm)(mainFolderOutPut, {
|
|
50
|
+
recursive: true,
|
|
51
|
+
force: true,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
async function initScript() {
|
|
55
|
+
await cleanFolderOutPut();
|
|
56
|
+
const raw = await (0, promises_1.readFile)(path.join(__dirname, "config.json"), "utf8");
|
|
57
|
+
const config = await JSON.parse(raw);
|
|
58
|
+
urlSwaggerJson = config.PATH;
|
|
59
|
+
basepath = config.BASEPATH;
|
|
60
|
+
try {
|
|
61
|
+
const response = await fetch(urlSwaggerJson);
|
|
62
|
+
const data = await response.json();
|
|
63
|
+
(0, fs_1.writeFile)(path.join(__dirname, "paths.json"), `${JSON.stringify(data, null, 2)}`, "utf8", async (err) => {
|
|
64
|
+
if (err) {
|
|
65
|
+
console.error(err);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
await filterPathsObject();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error instanceof Error && error.message.includes("ECONNREFUSED")) {
|
|
74
|
+
console.log(chalk_1.default.bgRed.white(" ERROR ") +
|
|
75
|
+
chalk_1.default.red("Could not connect: The server is off or the URL is incorrect."));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.log(chalk_1.default.red(`โ unknown error: ${error}`));
|
|
79
|
+
}
|
|
80
|
+
await cleanFileAndConfig();
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async function filterPathsObject() {
|
|
85
|
+
const raw = await (0, promises_1.readFile)(path.join(__dirname, "paths.json"), "utf8");
|
|
86
|
+
const pathsObj = await JSON.parse(raw);
|
|
87
|
+
const endpoints = Object.keys(pathsObj.paths).map((endpoint) => endpoint.split(basepath)[1]);
|
|
88
|
+
const foldersName = endpoints.map((endpoint) => endpoint.split("/")[0]);
|
|
89
|
+
await makeFolders(foldersName);
|
|
90
|
+
await makeFileContainer(endpoints, foldersName);
|
|
91
|
+
await cleanFileAndConfig();
|
|
92
|
+
}
|
|
93
|
+
async function cleanFileAndConfig() {
|
|
94
|
+
await cleanFile();
|
|
95
|
+
await cleanConfig();
|
|
96
|
+
console.log("๐งน Cleaned.");
|
|
97
|
+
}
|
|
98
|
+
async function cleanFile() {
|
|
99
|
+
await (0, promises_1.rm)(path.join(__dirname, "paths.json"), { force: true });
|
|
100
|
+
}
|
|
101
|
+
async function cleanConfig() {
|
|
102
|
+
await (0, promises_1.rm)(path.join(__dirname, "config.json"), { force: true });
|
|
103
|
+
}
|
|
104
|
+
async function makeFolders(foldersName) {
|
|
105
|
+
await (0, promises_1.mkdir)(mainFolderOutPut, { recursive: true });
|
|
106
|
+
for (const folder of [...new Set(foldersName)]) {
|
|
107
|
+
const folderPath = path.join(mainFolderOutPut, folder);
|
|
108
|
+
await (0, promises_1.mkdir)(folderPath, { recursive: true });
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function makeFileContainer(endpoints, foldersName) {
|
|
112
|
+
for (const folder of [...new Set(foldersName)]) {
|
|
113
|
+
await (0, promises_1.appendFile)(`${mainFolderOutPut}/${folder}/${folder}.ts`, "");
|
|
114
|
+
}
|
|
115
|
+
for (const [index, folder] of foldersName.entries()) {
|
|
116
|
+
const endpoint = endpoints[index];
|
|
117
|
+
const filePath = `${mainFolderOutPut}/${folder}/${folder}.ts`;
|
|
118
|
+
const paramsMatch = endpoint.match(/\{([^}]+)\}/g);
|
|
119
|
+
const args = paramsMatch
|
|
120
|
+
? paramsMatch.map((p) => p.replace(/[{}]/g, "").concat(":any")).join(", ")
|
|
121
|
+
: "";
|
|
122
|
+
let jsDoc = "";
|
|
123
|
+
if (paramsMatch?.length) {
|
|
124
|
+
const paramsDoc = paramsMatch
|
|
125
|
+
.map((p) => ` * @param ${p.replace(/[{}]/g, "")}`)
|
|
126
|
+
.join("\n");
|
|
127
|
+
jsDoc = `/**\n${paramsDoc}\n */\n`;
|
|
128
|
+
}
|
|
129
|
+
const name = endpoint
|
|
130
|
+
.replace(/[/|{}]/g, "_")
|
|
131
|
+
.replace(/_+/g, "_")
|
|
132
|
+
.replace(/^_|_$/g, "");
|
|
133
|
+
const templatePath = endpoint.replace(/\{/g, "${");
|
|
134
|
+
const line = `${jsDoc} export const ${name} = (${args}) => \`${templatePath}\`;\n`;
|
|
135
|
+
try {
|
|
136
|
+
await (0, promises_1.appendFile)(filePath, line);
|
|
137
|
+
await formatWhitPrettier(filePath);
|
|
138
|
+
console.log(`โ
Generated: ${name}`);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
console.error(`โ Error occurred while writing to ${filePath}:`, error);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
console.log(`๐พ show result ---> ${mainFolderOutPut}`);
|
|
145
|
+
}
|
|
146
|
+
async function formatWhitPrettier(filePath) {
|
|
147
|
+
const content = await (0, promises_1.readFile)(filePath, "utf8");
|
|
148
|
+
const formatted = await (0, prettier_1.format)(content, {
|
|
149
|
+
parser: "typescript",
|
|
150
|
+
filepath: filePath,
|
|
151
|
+
semi: true,
|
|
152
|
+
singleQuote: true,
|
|
153
|
+
trailingComma: "all",
|
|
154
|
+
});
|
|
155
|
+
(0, fs_1.writeFile)(filePath, formatted, () => { });
|
|
156
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "swaggerjsontoapidocs",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "script to convert swagger json to api docs, this help us to consume functions and center all endpoints in one place",
|
|
5
|
+
"main": "index.ts",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"api": "ts-node ./src/",
|
|
9
|
+
"dist": "node ./dist/",
|
|
10
|
+
"lint": "eslint ."
|
|
11
|
+
},
|
|
12
|
+
"author": "Esteban Fernandez",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"chalk": "^5.6.2",
|
|
16
|
+
"prettier": "^3.7.4"
|
|
17
|
+
},
|
|
18
|
+
"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"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"package.json"
|
|
31
|
+
],
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=15.0.0"
|
|
34
|
+
},
|
|
35
|
+
"bin": {
|
|
36
|
+
"swaggerjsontoapidocs": "dist/index.js"
|
|
37
|
+
}
|
|
38
|
+
}
|