Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
* @typedef {import('ajv').Options} AjvOpts
* @typedef {import('openapi-backend').AjvCustomizer} AjvCustomizer
* @typedef {object} Logger
* @property {Function} error

Check warning on line 11 in src/api.js

View workflow job for this annotation

GitHub Actions / build

Prefer a more specific type to `Function`
* @property {Function} warn

Check warning on line 12 in src/api.js

View workflow job for this annotation

GitHub Actions / build

Prefer a more specific type to `Function`
* @property {Function} info

Check warning on line 13 in src/api.js

View workflow job for this annotation

GitHub Actions / build

Prefer a more specific type to `Function`
* @property {Function} debug

Check warning on line 14 in src/api.js

View workflow job for this annotation

GitHub Actions / build

Prefer a more specific type to `Function`
* @typedef {object} SecurityHandler
* @property {string} name
* @property {Handler} handler
Expand All @@ -28,9 +28,10 @@
* @property {Handler=} unauthorizedHandler
* @property {boolean=} swagger
* @property {boolean=} apiDocs
* @property {boolean=} validateResponse
* @property {AjvOpts=} ajvOptions
* @property {AjvCustomizer=} customizeAjv
* @property {any[]=} middleware

Check warning on line 34 in src/api.js

View workflow job for this annotation

GitHub Actions / build

Prefer a more specific type to `any`
*/

/**
Expand All @@ -57,6 +58,7 @@
unauthorizedHandler,
swagger,
apiDocs,
validateResponse,
ajvOptions,
customizeAjv,
middleware = []
Expand All @@ -73,6 +75,7 @@
this.unauthorizedHandler = unauthorizedHandler || undefined
this.swagger = swagger ?? true
this.apiDocs = apiDocs ?? true
this.validateResponse = validateResponse ?? true
this.ajvOptions = ajvOptions ?? { allErrors: false }
this.customizeAjv = customizeAjv
this.middleware = middleware
Expand Down Expand Up @@ -120,6 +123,7 @@
meta: this.meta,
securityHandlers: this.securityHandlers,
unauthorizedHandler: this.unauthorizedHandler,
validateResponse: this.validateResponse,
ajvOptions: this.ajvOptions,
customizeAjv: this.customizeAjv
})
Expand Down
15 changes: 11 additions & 4 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
* @param {object=} params.meta
* @param {SecurityHandler[]=} params.securityHandlers
* @param {Handler=} params.unauthorizedHandler
* @param {boolean=} params.validateResponse
* @param {AjvOpts=} params.ajvOptions
* @param {AjvCustomizer=} params.customizeAjv
* @param {boolean=} params.mock
* @returns {{ api: OpenAPIBackend<any>, openAPISpecification: object }}

Check warning on line 34 in src/router.js

View workflow job for this annotation

GitHub Actions / build

Prefer a more specific type to `any`
*/
export const setupRouter = ({
openAPISpecification,
Expand All @@ -42,6 +43,7 @@
meta,
securityHandlers = [],
unauthorizedHandler,
validateResponse = true,
ajvOptions = {},
customizeAjv,
mock
Expand All @@ -58,12 +60,17 @@
customizeAjv: customizeAjv || ajvWithExtraFormats
})

api.register({
const handlers = {
unauthorizedHandler: unauthorizedHandler || unauthorized,
validationFail: requestValidation,
notFound,
postResponseHandler: makeResponseValidation(logger)
})
notFound
}

if (validateResponse) {
handlers.postResponseHandler = makeResponseValidation(logger)
}

api.register(handlers)

operationIds({ specification: openAPISpecification }).forEach(
(operationId) => {
Expand Down
64 changes: 64 additions & 0 deletions src/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,68 @@ test('Test the router', async (t) => {
)
}
)

await t.test(
'It should not register postResponseHandler when validateResponse is false',
async () => {
const controllers = {
getMessages: () => ({
test: 'ok'
})
}

const { api } = setupRouter({
secret: envExample.SECRET,
openAPISpecification,
controllers,
validateResponse: false,
logger
})

assert.strictEqual(api.handlers.postResponseHandler, undefined)
}
)

await t.test(
'It should register postResponseHandler when validateResponse is true',
async () => {
const controllers = {
getMessages: () => ({
test: 'ok'
})
}

const { api } = setupRouter({
secret: envExample.SECRET,
openAPISpecification,
controllers,
validateResponse: true,
logger
})

assert.notStrictEqual(api.handlers.postResponseHandler, undefined)
assert.strictEqual(typeof api.handlers.postResponseHandler, 'function')
}
)

await t.test(
'It should register postResponseHandler by default (when validateResponse is not specified)',
async () => {
const controllers = {
getMessages: () => ({
test: 'ok'
})
}

const { api } = setupRouter({
secret: envExample.SECRET,
openAPISpecification,
controllers,
logger
})

assert.notStrictEqual(api.handlers.postResponseHandler, undefined)
assert.strictEqual(typeof api.handlers.postResponseHandler, 'function')
}
)
})
Loading