@trojs/openapi-server 3.1.3 → 3.2.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,77 +1,78 @@
1
1
  {
2
- "name": "@trojs/openapi-server",
3
- "description": "OpenAPI Server",
4
- "version": "3.1.3",
5
- "author": {
6
- "name": "Pieter Wigboldus",
7
- "url": "https://trojs.org/"
8
- },
9
- "license": "MIT",
10
- "scripts": {
11
- "lint": "eslint",
12
- "lint:report": "eslint src/*.js -f json -o report.json",
13
- "lint:fix": "eslint --fix",
14
- "test": "node --test --experimental-test-coverage --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=./coverage/lcov.info",
15
- "cpd": "node_modules/jscpd/bin/jscpd src",
16
- "vulnerabilities": "npm audit --omit=dev"
17
- },
18
- "type": "module",
19
- "files": [
20
- "src/api.js",
21
- "src/openapi.js",
22
- "src/router.js",
23
- "src/server.js",
24
- "src/error-status.js",
25
- "src/types.js",
26
- "src/params.js",
27
- "src/express-callback.js",
28
- "src/operation-ids.js",
29
- "src/handlers/not-found.js",
30
- "src/handlers/request-validation.js",
31
- "src/handlers/response-validation.js",
32
- "src/handlers/unauthorized.js"
33
- ],
34
- "main": "src/server.js",
35
- "devDependencies": {
36
- "@trojs/lint": "^0.2.9",
37
- "@types/node": "^22.0.0",
38
- "@types/express-serve-static-core": "^5.0.0",
39
- "eslint": "^9.15.0",
40
- "globals": "^16.0.0",
41
- "jscpd": "^4.0.5",
42
- "prettier": "^3.3.3",
43
- "supertest": "^7.0.0"
44
- },
45
- "repository": {
46
- "type": "git",
47
- "url": "https://github.com/trojs/openapi-server"
48
- },
49
- "engines": {
50
- "node": ">= 20 < 21 || >= 22 < 23"
51
- },
52
- "keywords": [
53
- "openapi",
54
- "server",
55
- "express"
56
- ],
57
- "dependencies": {
58
- "@sentry/node": "^9.0.0",
59
- "ajv-formats": "^3.0.0",
60
- "body-parser": "^2.0.0",
61
- "compression": "^1.7.4",
62
- "cors": "^2.8.5",
63
- "express": "^5.0.0",
64
- "helmet": "^8.0.0",
65
- "openapi-backend": "^5.9.2",
66
- "swagger-ui-express": "^5.0.0"
67
- },
68
- "funding": {
69
- "type": "github",
70
- "url": "https://github.com/sponsors/w3nl"
71
- },
72
- "overrides": {
73
- "semver@<=7.5.3": "^7.5.3",
74
- "send@<=0.19.0": "^0.19.0",
75
- "cookie@<=0.7.0": "0.7.0"
76
- }
2
+ "name": "@trojs/openapi-server",
3
+ "description": "OpenAPI Server",
4
+ "version": "3.2.0",
5
+ "author": {
6
+ "name": "Pieter Wigboldus",
7
+ "url": "https://trojs.org/"
8
+ },
9
+ "license": "MIT",
10
+ "scripts": {
11
+ "lint": "eslint",
12
+ "lint:report": "eslint src/*.js -f json -o report.json",
13
+ "lint:fix": "eslint --fix",
14
+ "test": "node --test --experimental-test-coverage --test-reporter=spec --test-reporter=lcov --test-reporter-destination=stdout --test-reporter-destination=./coverage/lcov.info",
15
+ "cpd": "node_modules/jscpd/bin/jscpd src",
16
+ "vulnerabilities": "npm audit --omit=dev"
17
+ },
18
+ "type": "module",
19
+ "files": [
20
+ "src/api.js",
21
+ "src/openapi.js",
22
+ "src/router.js",
23
+ "src/server.js",
24
+ "src/error-status.js",
25
+ "src/types.js",
26
+ "src/params.js",
27
+ "src/express-callback.js",
28
+ "src/operation-ids.js",
29
+ "src/handlers/not-found.js",
30
+ "src/handlers/request-validation.js",
31
+ "src/handlers/response-validation.js",
32
+ "src/handlers/unauthorized.js"
33
+ ],
34
+ "main": "src/server.js",
35
+ "devDependencies": {
36
+ "@trojs/lint": "^0.3.0",
37
+ "@types/node": "^22.0.0",
38
+ "@types/express-serve-static-core": "^5.0.0",
39
+ "eslint": "^9.15.0",
40
+ "globals": "^16.0.0",
41
+ "jscpd": "^4.0.5",
42
+ "prettier": "^3.3.3",
43
+ "supertest": "^7.0.0"
44
+ },
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "https://github.com/trojs/openapi-server"
48
+ },
49
+ "engines": {
50
+ "node": ">= 20 < 21 || >= 22 < 23"
51
+ },
52
+ "keywords": [
53
+ "openapi",
54
+ "server",
55
+ "express"
56
+ ],
57
+ "dependencies": {
58
+ "@sentry/node": "^9.0.0",
59
+ "ajv-formats": "^3.0.0",
60
+ "body-parser": "^2.0.0",
61
+ "compression": "^1.7.4",
62
+ "cors": "^2.8.5",
63
+ "express": "^5.0.0",
64
+ "helmet": "^8.0.0",
65
+ "openapi-backend": "^5.9.2",
66
+ "swagger-ui-express": "^5.0.0"
67
+ },
68
+ "funding": {
69
+ "type": "github",
70
+ "url": "https://github.com/sponsors/w3nl"
71
+ },
72
+ "overrides": {
73
+ "semver@<=7.5.3": "^7.5.3",
74
+ "send@<=0.19.0": "^0.19.0",
75
+ "cookie@<=0.7.0": "0.7.0",
76
+ "@apidevtools/json-schema-ref-parser@<=14.0.0": "14.0.0"
77
+ }
77
78
  }
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 () {
@@ -96,6 +99,8 @@ export class Api {
96
99
  })
97
100
  api.init()
98
101
 
102
+ this.middleware.forEach((fn) => router.use(fn))
103
+
99
104
  router.use((request, response) =>
100
105
  api.handleRequest(request, request, response)
101
106
  )
@@ -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)
@@ -8,7 +8,16 @@ export const responseValidation = (context, request, response) => {
8
8
  context.response,
9
9
  context.operation
10
10
  )
11
- if (valid?.errors) {
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 { 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
  /**
@@ -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: customizeAjv || ajvWithExtraFormats
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: responseValidation
61
+ postResponseHandler: makeResponseValidation(logger)
63
62
  })
64
63
 
65
64
  operationIds({ specification: openAPISpecification }).forEach(