Skip to content

Commit f5a964f

Browse files
committed
feat: add @response tag (#21)
1 parent 65d8188 commit f5a964f

File tree

7 files changed

+34
-9
lines changed

7 files changed

+34
-9
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ node -e 'require("express-autodoc").generateSwagger(".")'
4343
| @queryParam | (\<name\>) {type: string, required: true, default: \<defaultValue\> } \<description\> | `/** @queryParam (name) A name param */` |
4444
| @pathParam | (\<:name\>) \<description\> | `/** @pathParam (:id) song Id */` |
4545
| @produces | \<contentType1\>,\<contentTypeN\> | `/** @produces application/json */`| |
46-
| @description | \<description\> | `/** @description A description */` |
47-
| @body | \<body\> [{"example": "object"} |<reference>] | `/** @body {} */` `/** @body #definitions/Song */` |
46+
| @description | \<description\> | `/** @description A description */` |
47+
| @body, @request | \<body\> [{"example": "object"} |<reference>] | `/** @body {} */` `/** @body #definitions/Song */` |
48+
| @response | \<response\> [{"example": "object"} |<reference>] | `/** @response {} */` `/** @response #definitions/Song */` |
4849

4950
### See more examples: [simple app](examples/singleApp/), [app with router](examples/withRouter/)

examples/singleApp/app.js

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ app.head('/api/v1/song/:id/*', (_req, res) => (
9090
/**
9191
* @description Updates a new song
9292
* @body #/definitions/Song
93+
* @response #/definitions/Song
9394
*/
9495
app.post('/api/v3/song-json', (_req, res) => (
9596
res.json({

examples/singleApp/swagger_output.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@
177177
],
178178
"responses": {
179179
"200": {
180-
"description": "OK"
180+
"content": "application/json",
181+
"description": "OK",
182+
"schema": {
183+
"$ref": "#/definitions/Song"
184+
}
181185
}
182186
}
183187
}

examples/singleApp/swagger_output_with_config.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,11 @@
191191
],
192192
"responses": {
193193
"200": {
194-
"description": "OK"
194+
"content": "application/json",
195+
"description": "OK",
196+
"schema": {
197+
"$ref": "#/definitions/Song"
198+
}
195199
}
196200
}
197201
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "express-autodoc",
3-
"version": "0.0.15",
3+
"version": "0.0.16",
44
"description": "",
55
"main": "src/traverse.js",
66
"scripts": {

src/parser/EndpointDoc.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class EndpointDoc {
1515
}, {})
1616
this.queryParams = tags.filter(t => t.title === 'queryParam')
1717
.map(t => QueryParam.parse(t.description))
18-
this.body = tags.find(t => t.title === 'body')?.description
18+
this.body = tags.find(t => t.title === 'body' || t.title == 'request')?.description
19+
this.response = tags.find(t => t.title === 'response')?.description
1920
this.pathParams = this.extractPathParams(path).map(paramName => `:${paramName}`).map(paramName => new PathParam(paramName).merge(docParms[paramName]))
2021
this.produces = tags.filter(t => t.title === 'produces').map(t => new ProducesTag(t.description))
2122
}
@@ -121,6 +122,17 @@ class SwaggerBody {
121122
}
122123
}
123124

125+
class SwaggerResponse {
126+
127+
constructor(body, contentType = 'application/json') {
128+
if (body.startsWith('{')) {
129+
this.value = { content: contentType, schema: { type: 'object', example: body } }
130+
} else {
131+
this.value = { content: contentType, schema: { $ref: body } }
132+
}
133+
}
134+
}
135+
124136
class SwaggerEndpointPath {
125137

126138
constructor(path, pathParams) {
@@ -158,4 +170,5 @@ exports.SwaggerPathParam = SwaggerPathParam
158170
exports.SwaggerEndpointPath = SwaggerEndpointPath
159171
exports.SwaggerQueryParam = SwaggerQueryParam
160172
exports.ProducesTag = ProducesTag
161-
exports.SwaggerBody = SwaggerBody
173+
exports.SwaggerBody = SwaggerBody
174+
exports.SwaggerResponse = SwaggerResponse

src/swagger/ExpressProject.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { defaultConfig } = require('./config')
22
const fs = require('fs')
3-
const { SwaggerPathParam, SwaggerEndpointPath, SwaggerQueryParam, SwaggerBody } = require('../parser/EndpointDoc')
3+
const { SwaggerPathParam, SwaggerEndpointPath, SwaggerQueryParam, SwaggerBody, SwaggerResponse } = require('../parser/EndpointDoc')
44
const { parse } = require('path')
55

66
class ExpressProject {
@@ -56,10 +56,12 @@ function createPath(endpoint, rootPath, paths) {
5656
const produces = doc.produces.flatMap(p => p.produces)
5757
const body = doc.body
5858
const bodyParams = body ? [new SwaggerBody(body).value] : []
59+
const response = doc.response
60+
const responseBody = response ? new SwaggerResponse(response).value : {}
5961
path[endpoint.method] = {
6062
description: doc.description,
6163
parameters: pathParams.concat(queryParams).concat(bodyParams),
62-
responses: { '200': { description: 'OK' } },
64+
responses: { '200': { description: 'OK', ...responseBody } },
6365
}
6466
if(produces && produces.length > 0){
6567
path[endpoint.method].produces = produces

0 commit comments

Comments
 (0)