@trojs/openapi-server 3.6.0 → 3.7.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": "3.6.0",
4
+ "version": "3.7.0",
5
5
  "author": {
6
6
  "name": "Pieter Wigboldus",
7
7
  "url": "https://trojs.org/"
@@ -40,7 +40,7 @@
40
40
  "@trojs/lint": "^0.4.0",
41
41
  "@types/node": "^24.0.0",
42
42
  "@types/express-serve-static-core": "^5.0.0",
43
- "eslint": "^9.15.0",
43
+ "eslint": "^10.0.0",
44
44
  "globals": "^17.0.0",
45
45
  "jscpd": "^4.0.5",
46
46
  "prettier": "^3.3.3",
@@ -1,5 +1,7 @@
1
1
  export const notFound = (_context, request, response) => {
2
- response.status(404)
2
+ if (!response.headersSent) {
3
+ response.status(404)
4
+ }
3
5
  return {
4
6
  status: 404,
5
7
  timestamp: new Date(),
@@ -1,5 +1,7 @@
1
1
  export const requestValidation = (context, request, response) => {
2
- response.status(400)
2
+ if (!response.headersSent) {
3
+ response.status(400)
4
+ }
3
5
  return {
4
6
  errors: context.validation.errors,
5
7
  status: 400,
@@ -1,13 +1,22 @@
1
- export default (logger) => (context, request, response) => {
1
+ export default (logger, validateResponse) => (context, request, response) => {
2
+ // Prevent sending response if headers are already sent (e.g., after redirect)
3
+ if (response.headersSent) {
4
+ return undefined
5
+ }
6
+
2
7
  const responseDoesntNeedValidation = response.statusCode >= 400
3
8
  if (responseDoesntNeedValidation) {
4
9
  return response.json(context.response)
5
10
  }
6
11
 
7
- const valid = context.api.validateResponse(
8
- context.response,
9
- context.operation
10
- )
12
+ const valid = validateResponse
13
+ ? (
14
+ context.api.validateResponse(
15
+ context.response,
16
+ context.operation
17
+ )
18
+ )
19
+ : null
11
20
  if (valid?.errors) {
12
21
  if (logger) {
13
22
  logger.error({
@@ -18,12 +27,15 @@ export default (logger) => (context, request, response) => {
18
27
  response: context.response
19
28
  })
20
29
  }
21
- return response.status(502).json({
22
- errors: valid.errors,
23
- status: 502,
24
- timestamp: new Date(),
25
- message: 'Bad response'
26
- })
30
+ if (!response.headersSent) {
31
+ return response.status(502).json({
32
+ errors: valid.errors,
33
+ status: 502,
34
+ timestamp: new Date(),
35
+ message: 'Bad response'
36
+ })
37
+ }
38
+ return undefined
27
39
  }
28
40
 
29
41
  if (!context.response) {
@@ -1,5 +1,7 @@
1
1
  export const unauthorized = async (context, request, response) => {
2
- response.status(401)
2
+ if (!response.headersSent) {
3
+ response.status(401)
4
+ }
3
5
  return {
4
6
  status: 401,
5
7
  timestamp: new Date(),
package/src/router.js CHANGED
@@ -66,9 +66,7 @@ export const setupRouter = ({
66
66
  notFound
67
67
  }
68
68
 
69
- if (validateResponse) {
70
- handlers.postResponseHandler = makeResponseValidation(logger)
71
- }
69
+ handlers.postResponseHandler = makeResponseValidation(logger, validateResponse)
72
70
 
73
71
  api.register(handlers)
74
72
 
@@ -1 +1 @@
1
- {"version":3,"file":"not-found.d.ts","sourceRoot":"","sources":["../../src/handlers/not-found.js"],"names":[],"mappings":"AAAO;;;;EAON"}
1
+ {"version":3,"file":"not-found.d.ts","sourceRoot":"","sources":["../../src/handlers/not-found.js"],"names":[],"mappings":"AAAO;;;;EASN"}
@@ -1 +1 @@
1
- {"version":3,"file":"request-validation.d.ts","sourceRoot":"","sources":["../../src/handlers/request-validation.js"],"names":[],"mappings":"AAAO;;;;;EAQN"}
1
+ {"version":3,"file":"request-validation.d.ts","sourceRoot":"","sources":["../../src/handlers/request-validation.js"],"names":[],"mappings":"AAAO;;;;;EAUN"}
@@ -1,3 +1,3 @@
1
- declare function _default(logger: any): (context: any, request: any, response: any) => any;
1
+ declare function _default(logger: any, validateResponse: any): (context: any, request: any, response: any) => any;
2
2
  export default _default;
3
3
  //# sourceMappingURL=response-validation.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"response-validation.d.ts","sourceRoot":"","sources":["../../src/handlers/response-validation.js"],"names":[],"mappings":"AAAe,yCAAa,YAAO,EAAE,YAAO,EAAE,aAAQ,SAsCrD"}
1
+ {"version":3,"file":"response-validation.d.ts","sourceRoot":"","sources":["../../src/handlers/response-validation.js"],"names":[],"mappings":"AAAe,gEAA+B,YAAO,EAAE,YAAO,EAAE,aAAQ,SAkDvE"}
@@ -1 +1 @@
1
- {"version":3,"file":"unauthorized.d.ts","sourceRoot":"","sources":["../../src/handlers/unauthorized.js"],"names":[],"mappings":"AAAO;;;;GAON"}
1
+ {"version":3,"file":"unauthorized.d.ts","sourceRoot":"","sources":["../../src/handlers/unauthorized.js"],"names":[],"mappings":"AAAO;;;;GASN"}
@@ -1 +1 @@
1
- {"version":3,"file":"router.d.ts","sourceRoot":"","sources":["../src/router.js"],"names":[],"mappings":"AAmCO,sNAfJ;IAAuB,oBAAoB,EAAnC,MAAM;IACS,WAAW,EAA1B,MAAM;IACU,OAAO,GAAvB,MAAM,YAAC;IACU,mBAAmB,GAApC,OAAO,YAAC;IACS,YAAY,GAA7B,OAAO,YAAC;IACQ,MAAM,GAAtB,MAAM,YAAC;IACS,IAAI,GAApB,MAAM,YAAC;IACoB,gBAAgB,GAA3C,eAAe,EAAE,YAAC;IACD,mBAAmB,GAApC,OAAO,YAAC;IACS,gBAAgB,GAAjC,OAAO,YAAC;IACS,UAAU,GAA3B,OAAO,YAAC;IACe,YAAY,GAAnC,aAAa,YAAC;IACG,IAAI,GAArB,OAAO,YAAC;CAChB,GAAU;IAAE,GAAG,EAAE,cAAc,CAAC,GAAG,CAAC,CAAC;IAAC,oBAAoB,EAAE,MAAM,CAAA;CAAE,CA0EtE;qBAjGY,OAAO,UAAU,EAAE,MAAM;8BACzB,OAAO,UAAU,EAAE,eAAe;sBAClC,OAAO,UAAU,EAAE,OAAO;sBAC1B,OAAO,KAAK,EAAE,OAAO;4BACrB,OAAO,iBAAiB,EAAE,aAAa;+BAdrB,iBAAiB"}
1
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