@trojs/openapi-server 1.11.2 → 1.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trojs/openapi-server",
3
3
  "description": "OpenAPI Server",
4
- "version": "1.11.2",
4
+ "version": "1.13.0",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
@@ -35,7 +35,7 @@
35
35
  "main": "src/server.js",
36
36
  "devDependencies": {
37
37
  "@eslint/js": "^9.15.0",
38
- "@trojs/lint": "^0.2.9",
38
+ "@trojs/lint": "^0.3.4",
39
39
  "@types/node": "^22.0.0",
40
40
  "@types/express-serve-static-core": "^5.0.6",
41
41
  "eslint": "^9.15.0",
package/src/api.js CHANGED
@@ -93,10 +93,26 @@ export class Api {
93
93
  swaggerUi.setup(this.specification)
94
94
  )
95
95
  }
96
+
96
97
  if (this.apiDocs) {
97
- router.get('/api-docs', (_request, response) =>
98
+ // Generate an ETag for the specification (simple hash or JSON string)
99
+ const apiDocsString = JSON.stringify(this.specification)
100
+ const etag = `"${Buffer.from(apiDocsString).toString('base64')}"`
101
+
102
+ router.get('/api-docs', (request, response) => {
103
+ // Check for If-None-Match header
104
+ const ifNoneMatchHeader = request.headers['if-none-match']
105
+ if (ifNoneMatchHeader) {
106
+ const etags = ifNoneMatchHeader.split(',').map((tag) => tag.trim())
107
+ if (etags.includes('*') || etags.includes(etag)) {
108
+ response.status(304).end()
109
+ return
110
+ }
111
+ }
112
+ response.setHeader('Cache-Control', 'public, max-age=3600, must-revalidate')
113
+ response.setHeader('ETag', etag)
98
114
  response.json(this.specification)
99
- )
115
+ })
100
116
  }
101
117
 
102
118
  const { api } = setupRouter({
@@ -1,4 +1,4 @@
1
- export const responseValidation = (context, request, response) => {
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)
@@ -9,6 +9,15 @@ export const responseValidation = (context, request, response) => {
9
9
  context.operation
10
10
  )
11
11
  if (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 { responseValidation } from './handlers/response-validation.js'
7
+ import makeResponseValidation from './handlers/response-validation.js'
8
8
  import { unauthorized } from './handlers/unauthorized.js'
9
9
 
10
10
  /**
@@ -66,7 +66,7 @@ export const setupRouter = ({
66
66
  unauthorizedHandler: unauthorizedHandler || unauthorized,
67
67
  validationFail: requestValidation,
68
68
  notFound,
69
- postResponseHandler: responseValidation
69
+ postResponseHandler: makeResponseValidation(logger)
70
70
  })
71
71
 
72
72
  operationIds({ specification: openAPISpecification }).forEach(