@trojs/openapi-server 3.1.4 → 3.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/package.json +1 -1
- package/src/api.js +24 -3
- package/src/handlers/response-validation.js +11 -2
- package/src/router.js +6 -7
package/package.json
CHANGED
package/src/api.js
CHANGED
|
@@ -26,6 +26,7 @@ import { setupRouter } from './router.js'
|
|
|
26
26
|
* @property {boolean=} swagger
|
|
27
27
|
* @property {boolean=} apiDocs
|
|
28
28
|
* @property {AjvOpts=} ajvOptions
|
|
29
|
+
* @property {any[]=} middleware
|
|
29
30
|
*/
|
|
30
31
|
|
|
31
32
|
/**
|
|
@@ -51,7 +52,8 @@ export class Api {
|
|
|
51
52
|
securityHandlers,
|
|
52
53
|
swagger,
|
|
53
54
|
apiDocs,
|
|
54
|
-
ajvOptions
|
|
55
|
+
ajvOptions,
|
|
56
|
+
middleware = []
|
|
55
57
|
}) {
|
|
56
58
|
this.version = version
|
|
57
59
|
this.specification = specification
|
|
@@ -65,6 +67,7 @@ export class Api {
|
|
|
65
67
|
this.swagger = swagger ?? true
|
|
66
68
|
this.apiDocs = apiDocs ?? true
|
|
67
69
|
this.ajvOptions = ajvOptions ?? { allErrors: false }
|
|
70
|
+
this.middleware = middleware
|
|
68
71
|
}
|
|
69
72
|
|
|
70
73
|
setup () {
|
|
@@ -77,10 +80,26 @@ export class Api {
|
|
|
77
80
|
swaggerUi.setup(this.specification)
|
|
78
81
|
)
|
|
79
82
|
}
|
|
83
|
+
|
|
80
84
|
if (this.apiDocs) {
|
|
81
|
-
|
|
85
|
+
// Generate an ETag for the specification (simple hash or JSON string)
|
|
86
|
+
const apiDocsString = JSON.stringify(this.specification)
|
|
87
|
+
const etag = `"${Buffer.from(apiDocsString).toString('base64')}"`
|
|
88
|
+
|
|
89
|
+
router.get('/api-docs', (request, response) => {
|
|
90
|
+
// Check for If-None-Match header
|
|
91
|
+
const ifNoneMatchHeader = request.headers['if-none-match']
|
|
92
|
+
if (ifNoneMatchHeader) {
|
|
93
|
+
const etags = ifNoneMatchHeader.split(',').map((tag) => tag.trim())
|
|
94
|
+
if (etags.includes('*') || etags.includes(etag)) {
|
|
95
|
+
response.status(304).end()
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
response.setHeader('Cache-Control', 'public, max-age=3600, must-revalidate')
|
|
100
|
+
response.setHeader('ETag', etag)
|
|
82
101
|
response.json(this.specification)
|
|
83
|
-
)
|
|
102
|
+
})
|
|
84
103
|
}
|
|
85
104
|
|
|
86
105
|
const { api } = setupRouter({
|
|
@@ -96,6 +115,8 @@ export class Api {
|
|
|
96
115
|
})
|
|
97
116
|
api.init()
|
|
98
117
|
|
|
118
|
+
this.middleware.forEach((fn) => router.use(fn))
|
|
119
|
+
|
|
99
120
|
router.use((request, response) =>
|
|
100
121
|
api.handleRequest(request, request, response)
|
|
101
122
|
)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export default (logger) => (context, request, response) => {
|
|
2
2
|
const responseDoesntNeedValidation = response.statusCode >= 400
|
|
3
3
|
if (responseDoesntNeedValidation) {
|
|
4
4
|
return response.json(context.response)
|
|
@@ -8,7 +8,16 @@ export const responseValidation = (context, request, response) => {
|
|
|
8
8
|
context.response,
|
|
9
9
|
context.operation
|
|
10
10
|
)
|
|
11
|
-
if (valid
|
|
11
|
+
if (valid && valid.errors) {
|
|
12
|
+
if (logger) {
|
|
13
|
+
logger.error({
|
|
14
|
+
message: 'Response validation failed',
|
|
15
|
+
errors: valid.errors,
|
|
16
|
+
operation: context.operation,
|
|
17
|
+
statusCode: response.statusCode,
|
|
18
|
+
response: context.response
|
|
19
|
+
})
|
|
20
|
+
}
|
|
12
21
|
return response.status(502).json({
|
|
13
22
|
errors: valid.errors,
|
|
14
23
|
status: 502,
|
package/src/router.js
CHANGED
|
@@ -4,7 +4,7 @@ import { makeExpressCallback } from './express-callback.js'
|
|
|
4
4
|
import { operationIds } from './operation-ids.js'
|
|
5
5
|
import { notFound } from './handlers/not-found.js'
|
|
6
6
|
import { requestValidation } from './handlers/request-validation.js'
|
|
7
|
-
import
|
|
7
|
+
import makeResponseValidation from './handlers/response-validation.js'
|
|
8
8
|
import { unauthorized } from './handlers/unauthorized.js'
|
|
9
9
|
|
|
10
10
|
/**
|
|
@@ -43,23 +43,22 @@ export const setupRouter = ({
|
|
|
43
43
|
customizeAjv,
|
|
44
44
|
mock
|
|
45
45
|
}) => {
|
|
46
|
-
const ajvWithExtraFormats = (originalAjv) => {
|
|
47
|
-
addFormats(originalAjv)
|
|
48
|
-
return originalAjv
|
|
49
|
-
}
|
|
50
46
|
const api = new OpenAPIBackend({
|
|
51
47
|
definition: openAPISpecification,
|
|
52
48
|
apiRoot,
|
|
53
49
|
strict: strictSpecification,
|
|
54
50
|
ajvOpts: ajvOptions,
|
|
55
|
-
customizeAjv:
|
|
51
|
+
customizeAjv: (originalAjv) => {
|
|
52
|
+
addFormats(originalAjv)
|
|
53
|
+
return originalAjv
|
|
54
|
+
}
|
|
56
55
|
})
|
|
57
56
|
|
|
58
57
|
api.register({
|
|
59
58
|
unauthorizedHandler: unauthorized,
|
|
60
59
|
validationFail: requestValidation,
|
|
61
60
|
notFound,
|
|
62
|
-
postResponseHandler:
|
|
61
|
+
postResponseHandler: makeResponseValidation(logger)
|
|
63
62
|
})
|
|
64
63
|
|
|
65
64
|
operationIds({ specification: openAPISpecification }).forEach(
|