@trojs/openapi-server 3.2.0 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/api.js +18 -2
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@trojs/openapi-server",
3
3
  "description": "OpenAPI Server",
4
- "version": "3.2.0",
4
+ "version": "3.3.0",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
package/src/api.js CHANGED
@@ -80,10 +80,26 @@ export class Api {
80
80
  swaggerUi.setup(this.specification)
81
81
  )
82
82
  }
83
+
83
84
  if (this.apiDocs) {
84
- router.get('/api-docs', (_request, response) =>
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)
85
101
  response.json(this.specification)
86
- )
102
+ })
87
103
  }
88
104
 
89
105
  const { api } = setupRouter({