swaggerjsontoapidocs 1.9.0 → 1.10.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/README.md +143 -131
- package/bin/index.js +1 -1
- package/bin/jsonToApiDocs.js +26 -15
- package/package.json +17 -2
package/README.md
CHANGED
|
@@ -46,153 +46,158 @@ MSYS_NO_PATHCONV=1 npx swaggerjsontoapidocs [options]
|
|
|
46
46
|
### Example Usage
|
|
47
47
|
|
|
48
48
|
```bash
|
|
49
|
-
npx swaggerjsontoapidocs -s http://localhost:5033/swagger/v1/swagger.json --bp /api/
|
|
49
|
+
npx swaggerjsontoapidocs -s http://localhost:5033/swagger/v1/swagger.json --bp /api/v1/
|
|
50
50
|
```
|
|
51
51
|
|
|
52
52
|
In this example:
|
|
53
53
|
|
|
54
54
|
- `-s` points to the URL of the Swagger JSON file.
|
|
55
|
-
- `--bp` defines the base path `/api/` to be removed from the endpoints.
|
|
55
|
+
- `--bp` defines the base path `/api/v1/` to be removed from the endpoints.
|
|
56
56
|
|
|
57
57
|
<details>
|
|
58
58
|
<summary>Swagger.json (Click to expand)</summary>
|
|
59
59
|
|
|
60
60
|
```json
|
|
61
61
|
{
|
|
62
|
-
"
|
|
62
|
+
"swagger": "2.0",
|
|
63
63
|
"info": {
|
|
64
|
-
"
|
|
65
|
-
"
|
|
64
|
+
"version": "1.2.0",
|
|
65
|
+
"title": "Extended Sample API with Multiple Path Parameters",
|
|
66
|
+
"description": "Test Swagger specification including endpoints with multiple path parameters."
|
|
66
67
|
},
|
|
67
68
|
"paths": {
|
|
68
|
-
"/api/
|
|
69
|
+
"/api/v1/users/{userId}/orders/{orderId}": {
|
|
69
70
|
"get": {
|
|
70
|
-
"tags": ["
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
"
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
"schema": {
|
|
85
|
-
"type": "array",
|
|
86
|
-
"items": {
|
|
87
|
-
"$ref": "#/components/schemas/Usuario"
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
},
|
|
91
|
-
"text/json": {
|
|
92
|
-
"schema": {
|
|
93
|
-
"type": "array",
|
|
94
|
-
"items": {
|
|
95
|
-
"$ref": "#/components/schemas/Usuario"
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
71
|
+
"tags": ["Order Processing"],
|
|
72
|
+
"summary": "Get a specific order for a user",
|
|
73
|
+
"parameters": [
|
|
74
|
+
{
|
|
75
|
+
"name": "userId",
|
|
76
|
+
"in": "path",
|
|
77
|
+
"required": true,
|
|
78
|
+
"type": "string"
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"name": "orderId",
|
|
82
|
+
"in": "path",
|
|
83
|
+
"required": true,
|
|
84
|
+
"type": "string"
|
|
100
85
|
}
|
|
86
|
+
],
|
|
87
|
+
"responses": {
|
|
88
|
+
"200": { "description": "Order details" },
|
|
89
|
+
"404": { "description": "Order not found" }
|
|
101
90
|
}
|
|
91
|
+
},
|
|
92
|
+
"put": {
|
|
93
|
+
"tags": ["Order Processing"],
|
|
94
|
+
"summary": "Update a specific order for a user",
|
|
95
|
+
"parameters": [
|
|
96
|
+
{
|
|
97
|
+
"name": "userId",
|
|
98
|
+
"in": "path",
|
|
99
|
+
"required": true,
|
|
100
|
+
"type": "string"
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
"name": "orderId",
|
|
104
|
+
"in": "path",
|
|
105
|
+
"required": true,
|
|
106
|
+
"type": "string"
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"name": "body",
|
|
110
|
+
"in": "body",
|
|
111
|
+
"required": true,
|
|
112
|
+
"schema": { "$ref": "#/definitions/Order" }
|
|
113
|
+
}
|
|
114
|
+
],
|
|
115
|
+
"responses": { "200": { "description": "Order updated" } }
|
|
102
116
|
}
|
|
103
117
|
},
|
|
104
|
-
"/api/
|
|
105
|
-
"
|
|
106
|
-
"tags": ["
|
|
118
|
+
"/api/v1/Products/{productId}/Reviews/{reviewId}/Comments/{commentId}": {
|
|
119
|
+
"delete": {
|
|
120
|
+
"tags": ["Reviews"],
|
|
121
|
+
"summary": "Delete a specific comment on a review",
|
|
107
122
|
"parameters": [
|
|
108
123
|
{
|
|
109
|
-
"name": "
|
|
124
|
+
"name": "productId",
|
|
110
125
|
"in": "path",
|
|
111
126
|
"required": true,
|
|
112
|
-
"
|
|
113
|
-
|
|
114
|
-
|
|
127
|
+
"type": "string"
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
"name": "reviewId",
|
|
131
|
+
"in": "path",
|
|
132
|
+
"required": true,
|
|
133
|
+
"type": "string"
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"name": "commentId",
|
|
137
|
+
"in": "path",
|
|
138
|
+
"required": true,
|
|
139
|
+
"type": "string"
|
|
115
140
|
}
|
|
116
141
|
],
|
|
117
142
|
"responses": {
|
|
118
|
-
"200": {
|
|
119
|
-
|
|
120
|
-
"content": {
|
|
121
|
-
"text/plain": {
|
|
122
|
-
"schema": {
|
|
123
|
-
"$ref": "#/components/schemas/Usuario"
|
|
124
|
-
}
|
|
125
|
-
},
|
|
126
|
-
"application/json": {
|
|
127
|
-
"schema": {
|
|
128
|
-
"$ref": "#/components/schemas/Usuario"
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
"text/json": {
|
|
132
|
-
"schema": {
|
|
133
|
-
"$ref": "#/components/schemas/Usuario"
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
143
|
+
"200": { "description": "Comment deleted" },
|
|
144
|
+
"404": { "description": "Comment not found" }
|
|
138
145
|
}
|
|
139
|
-
}
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
"/api/v1/admin/{section}/{entityId}/actions/{actionId}": {
|
|
140
149
|
"post": {
|
|
141
|
-
"tags": ["
|
|
150
|
+
"tags": ["Administration"],
|
|
151
|
+
"summary": "Perform an admin action on an entity",
|
|
142
152
|
"parameters": [
|
|
143
153
|
{
|
|
144
|
-
"name": "
|
|
154
|
+
"name": "section",
|
|
155
|
+
"in": "path",
|
|
156
|
+
"required": true,
|
|
157
|
+
"type": "string"
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"name": "entityId",
|
|
145
161
|
"in": "path",
|
|
146
162
|
"required": true,
|
|
163
|
+
"type": "string"
|
|
164
|
+
},
|
|
165
|
+
{
|
|
166
|
+
"name": "actionId",
|
|
167
|
+
"in": "path",
|
|
168
|
+
"required": true,
|
|
169
|
+
"type": "string"
|
|
170
|
+
},
|
|
171
|
+
{
|
|
172
|
+
"name": "body",
|
|
173
|
+
"in": "body",
|
|
174
|
+
"required": false,
|
|
147
175
|
"schema": {
|
|
148
|
-
"type": "
|
|
176
|
+
"type": "object",
|
|
177
|
+
"properties": {
|
|
178
|
+
"reason": { "type": "string" },
|
|
179
|
+
"timestamp": { "type": "string", "format": "date-time" }
|
|
180
|
+
}
|
|
149
181
|
}
|
|
150
182
|
}
|
|
151
183
|
],
|
|
152
184
|
"responses": {
|
|
153
|
-
"200": {
|
|
154
|
-
|
|
155
|
-
"content": {
|
|
156
|
-
"text/plain": {
|
|
157
|
-
"schema": {
|
|
158
|
-
"$ref": "#/components/schemas/Usuario"
|
|
159
|
-
}
|
|
160
|
-
},
|
|
161
|
-
"application/json": {
|
|
162
|
-
"schema": {
|
|
163
|
-
"$ref": "#/components/schemas/Usuario"
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
"text/json": {
|
|
167
|
-
"schema": {
|
|
168
|
-
"$ref": "#/components/schemas/Usuario"
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
185
|
+
"200": { "description": "Action executed successfully" },
|
|
186
|
+
"400": { "description": "Invalid action" }
|
|
173
187
|
}
|
|
174
188
|
}
|
|
175
189
|
}
|
|
176
190
|
},
|
|
177
|
-
"
|
|
178
|
-
"
|
|
179
|
-
"
|
|
180
|
-
|
|
181
|
-
"
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
"type": "string",
|
|
188
|
-
"nullable": true
|
|
189
|
-
},
|
|
190
|
-
"email": {
|
|
191
|
-
"type": "string",
|
|
192
|
-
"nullable": true
|
|
193
|
-
}
|
|
194
|
-
},
|
|
195
|
-
"additionalProperties": false
|
|
191
|
+
"definitions": {
|
|
192
|
+
"Order": {
|
|
193
|
+
"type": "object",
|
|
194
|
+
"properties": {
|
|
195
|
+
"id": { "type": "string" },
|
|
196
|
+
"status": { "type": "string" },
|
|
197
|
+
"items": {
|
|
198
|
+
"type": "array",
|
|
199
|
+
"items": { "type": "string" }
|
|
200
|
+
}
|
|
196
201
|
}
|
|
197
202
|
}
|
|
198
203
|
}
|
|
@@ -204,46 +209,52 @@ In this example:
|
|
|
204
209
|
### Result
|
|
205
210
|
|
|
206
211
|
```bash
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
│
|
|
212
|
+
api_docs/
|
|
213
|
+
├── admin
|
|
214
|
+
│ └── admin.ts
|
|
215
|
+
├── products
|
|
216
|
+
│ └── products.ts
|
|
217
|
+
└── users
|
|
218
|
+
└── users.ts
|
|
210
219
|
```
|
|
211
220
|
|
|
212
221
|
```typescript
|
|
213
222
|
// products.ts
|
|
214
223
|
/**
|
|
215
|
-
*
|
|
216
|
-
*
|
|
224
|
+
* ##### METHODS
|
|
225
|
+
* **DELETE**: Delete a specific comment on a review
|
|
226
|
+
*
|
|
227
|
+
* ---
|
|
228
|
+
* **Endpoint**: `/api/v1/Products/{productId}/Reviews/{reviewId}/Comments/{commentId}`
|
|
229
|
+
*
|
|
230
|
+
* ---
|
|
231
|
+
* ##### PATH PARAMETERS
|
|
232
|
+
* @param productId - any
|
|
233
|
+
* @param reviewId - any
|
|
234
|
+
* @param commentId - any
|
|
217
235
|
*/
|
|
218
|
-
export const
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
* @param categoryId
|
|
224
|
-
*/
|
|
225
|
-
export const products_id_category_categoryId = (id: any, categoryId: any) =>
|
|
226
|
-
`products/${id}/category/${categoryId}`;
|
|
236
|
+
export const Products_productId_Reviews_reviewId_Comments_commentId = (
|
|
237
|
+
productId: any,
|
|
238
|
+
reviewId: any,
|
|
239
|
+
commentId: any,
|
|
240
|
+
) => `Products/${productId}/Reviews/${reviewId}/Comments/${commentId}`;
|
|
227
241
|
```
|
|
228
242
|
|
|
229
243
|
### Result --function-name-lowercase
|
|
230
244
|
|
|
231
245
|
```typescript
|
|
232
246
|
// products.ts
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
*/
|
|
239
|
-
export const products_id_category_categoryid = (id: any, categoryId: any) =>
|
|
240
|
-
`products/${id}/category/${categoryId}`;
|
|
247
|
+
export const products_productId_reviews_reviewId_comments_commentId = (
|
|
248
|
+
productId: any,
|
|
249
|
+
reviewId: any,
|
|
250
|
+
commentId: any,
|
|
251
|
+
) => `Products/${productId}/Reviews/${reviewId}/Comments/${commentId}`;
|
|
241
252
|
```
|
|
242
253
|
|
|
243
254
|
### Advanced Usage
|
|
244
255
|
|
|
245
256
|
```bash
|
|
246
|
-
npx swaggerjsontoapidocs -s http://localhost:5033/swagger/v1/swagger.json --bp /api/ -o ./docs/ --skip-folder
|
|
257
|
+
npx swaggerjsontoapidocs -s http://localhost:5033/swagger/v1/swagger.json --bp /api/v1/ -o ./docs/ --skip-folder
|
|
247
258
|
```
|
|
248
259
|
|
|
249
260
|
In this example:
|
|
@@ -254,10 +265,11 @@ In this example:
|
|
|
254
265
|
### Result --skip-folder
|
|
255
266
|
|
|
256
267
|
```bash
|
|
257
|
-
docs
|
|
268
|
+
docs/
|
|
258
269
|
└── api_docs
|
|
270
|
+
├── admin.ts
|
|
259
271
|
├── products.ts
|
|
260
|
-
└──
|
|
272
|
+
└── users.ts
|
|
261
273
|
```
|
|
262
274
|
|
|
263
275
|
## License
|
package/bin/index.js
CHANGED
|
@@ -52,7 +52,7 @@ const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
|
52
52
|
.alias('help', 'h')
|
|
53
53
|
.strict()
|
|
54
54
|
.parseSync();
|
|
55
|
-
|
|
55
|
+
function main() {
|
|
56
56
|
console.log(chalk_1.default.green('CONFIGURING THE SCRIPT WITH THE PROVIDED ARGUMENTS...'));
|
|
57
57
|
const swaggerPath = argv.swagger;
|
|
58
58
|
const basePath = argv.bp;
|
package/bin/jsonToApiDocs.js
CHANGED
|
@@ -49,7 +49,7 @@ async function initScript(params) {
|
|
|
49
49
|
chalk_1.default.red('Could not connect: The server is off or the URL is incorrect.'));
|
|
50
50
|
}
|
|
51
51
|
else {
|
|
52
|
-
console.log(chalk_1.default.red(
|
|
52
|
+
console.log(chalk_1.default.red(`unknown error: ${error}`));
|
|
53
53
|
}
|
|
54
54
|
await cleanFileAndConfig();
|
|
55
55
|
return null;
|
|
@@ -82,7 +82,7 @@ async function filterPathsObject() {
|
|
|
82
82
|
async function cleanFileAndConfig() {
|
|
83
83
|
await cleanFile();
|
|
84
84
|
await cleanConfig();
|
|
85
|
-
console.log('
|
|
85
|
+
console.log('Cleaned.');
|
|
86
86
|
}
|
|
87
87
|
async function cleanFile() {
|
|
88
88
|
await (0, promises_1.rm)((0, node_path_1.join)(__dirname, 'paths.json'), { force: true });
|
|
@@ -106,7 +106,8 @@ const getFilePath = (folder) => {
|
|
|
106
106
|
: `${mainFolderOutPut}/${folder}/${folder}${ext}`;
|
|
107
107
|
};
|
|
108
108
|
function normalizeEndpoint(endpoint, toLowercase) {
|
|
109
|
-
const
|
|
109
|
+
const endpointCleaned = endpoint.replace(/[^a-zA-Z0-9_$/]/g, '');
|
|
110
|
+
const name = endpointCleaned
|
|
110
111
|
.split('/')
|
|
111
112
|
.map((value) => {
|
|
112
113
|
if (value.startsWith('{')) {
|
|
@@ -134,21 +135,31 @@ const generateDocumentation = (endpoint, methods, apiEndpoint) => {
|
|
|
134
135
|
})
|
|
135
136
|
.join(', ');
|
|
136
137
|
const paramsDoc = paramsMatch
|
|
137
|
-
.map((p) => `* @param ${p.replace(/[{}]/g, '')}`)
|
|
138
|
+
.map((p) => `* @param ${p.replace(/[{}]/g, '')} - any`)
|
|
138
139
|
.join('\n');
|
|
139
|
-
const endpointLine = apiEndpoint
|
|
140
|
+
const endpointLine = apiEndpoint
|
|
141
|
+
? `*\n* ---\n* **Endpoint**: \`${apiEndpoint}\``
|
|
142
|
+
: '*';
|
|
140
143
|
const methodsDoc = methods
|
|
141
|
-
.map((method) => {
|
|
142
|
-
let doc = '*
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
.map((method, index) => {
|
|
145
|
+
let doc = '* **' + method.verb.toUpperCase() + '**: ';
|
|
146
|
+
doc += method.summary ? `${method.summary}` : `without summary`;
|
|
147
|
+
if (index + 1 !== methods.length) {
|
|
148
|
+
doc += '\n*';
|
|
145
149
|
}
|
|
146
150
|
return doc;
|
|
147
151
|
})
|
|
148
152
|
.join('\n');
|
|
149
|
-
const methodsLine = methodsDoc ?
|
|
150
|
-
const paramsLine = paramsDoc
|
|
151
|
-
|
|
153
|
+
const methodsLine = methodsDoc ? `* ##### METHODS\n${methodsDoc}` : '*';
|
|
154
|
+
const paramsLine = paramsDoc
|
|
155
|
+
? `*\n* ---\n* ##### PATH PARAMETERS\n${paramsDoc}`
|
|
156
|
+
: '*';
|
|
157
|
+
const jsDoc = `
|
|
158
|
+
/**
|
|
159
|
+
${methodsLine}
|
|
160
|
+
${endpointLine}
|
|
161
|
+
${paramsLine}
|
|
162
|
+
*/\n`;
|
|
152
163
|
return {
|
|
153
164
|
args,
|
|
154
165
|
jsDoc,
|
|
@@ -167,15 +178,15 @@ async function makeFileContainer(apiEndpoints, foldersName) {
|
|
|
167
178
|
try {
|
|
168
179
|
await (0, promises_1.appendFile)(filePath, line);
|
|
169
180
|
await formatWithPrettier(filePath);
|
|
170
|
-
console.log(
|
|
181
|
+
console.log(`Generated: ${name}`);
|
|
171
182
|
}
|
|
172
183
|
catch (error) {
|
|
173
|
-
console.error(
|
|
184
|
+
console.error(`Error occurred while writing to ${filePath}:`, error);
|
|
174
185
|
}
|
|
175
186
|
}
|
|
176
187
|
}
|
|
177
188
|
async function destinationPath(fullPath) {
|
|
178
|
-
console.log('
|
|
189
|
+
console.log('show result --->', fullPath);
|
|
179
190
|
}
|
|
180
191
|
async function moveFolderToChoosePath() {
|
|
181
192
|
await (0, fs_extra_1.move)(mainFolderOutPut, `${paramsConfig.output}${folderName}`, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "swaggerjsontoapidocs",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.1",
|
|
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
|
"keywords": [
|
|
6
6
|
"swagger",
|
|
@@ -31,7 +31,18 @@
|
|
|
31
31
|
"api": "ts-node ./src/index.ts",
|
|
32
32
|
"dist": "node ./bin/index.js",
|
|
33
33
|
"lint": "eslint .",
|
|
34
|
-
"prepublishOnly": "npm run build"
|
|
34
|
+
"prepublishOnly": "npm run build",
|
|
35
|
+
"prepare": "simple-git-hooks"
|
|
36
|
+
},
|
|
37
|
+
"simple-git-hooks": {
|
|
38
|
+
"pre-commit": "npx lint-staged",
|
|
39
|
+
"commit-msg": "npx commitlint --edit $1"
|
|
40
|
+
},
|
|
41
|
+
"lint-staged": {
|
|
42
|
+
"*.{js,jsx,ts,tsx}": [
|
|
43
|
+
"prettier --write",
|
|
44
|
+
"eslint --quiet"
|
|
45
|
+
]
|
|
35
46
|
},
|
|
36
47
|
"bin": {
|
|
37
48
|
"swaggerjsontoapidocs": "bin/index.js"
|
|
@@ -43,6 +54,8 @@
|
|
|
43
54
|
"yargs": "18.0.0"
|
|
44
55
|
},
|
|
45
56
|
"devDependencies": {
|
|
57
|
+
"@commitlint/cli": "^20.4.1",
|
|
58
|
+
"@commitlint/config-conventional": "^20.4.1",
|
|
46
59
|
"@eslint/js": "9.39.2",
|
|
47
60
|
"@semantic-release/changelog": "^6.0.3",
|
|
48
61
|
"@semantic-release/git": "^10.0.1",
|
|
@@ -54,7 +67,9 @@
|
|
|
54
67
|
"eslint": "9.39.2",
|
|
55
68
|
"globals": "17.0.0",
|
|
56
69
|
"jiti": "2.6.1",
|
|
70
|
+
"lint-staged": "^16.2.7",
|
|
57
71
|
"semantic-release": "^25.0.2",
|
|
72
|
+
"simple-git-hooks": "^2.13.1",
|
|
58
73
|
"ts-node": "10.9.2",
|
|
59
74
|
"typescript": "5.9.3",
|
|
60
75
|
"typescript-eslint": "8.52.0"
|