Feat/preflight add logLevel option to silence CORS preflight logs#375
Feat/preflight add logLevel option to silence CORS preflight logs#375mcollina merged 6 commits intofastify:mainfrom
logLevel option to silence CORS preflight logs#375Conversation
There was a problem hiding this comment.
I’m not a user of logLevel, but based on the Fastify docs about it, it looks like logLevel, when passed in plugin options, applies to the entire plugin.
In your implementation, however, it only affects the OPTIONS route used for preflight requests.
Given that, a more appropriate name for this option might be preflightLogLevel or optionsRouteLogLevel.
|
|
The change below should be enough and there is no need on types change. diff --git a/index.js b/index.js
index 94c1dd2..7ec92a2 100644
--- a/index.js
+++ b/index.js
@@ -46,6 +46,7 @@ function fastifyCors (fastify, opts, next) {
fastify.decorateRequest('corsPreflightEnabled', false)
let hideOptionsRoute = true
+ let logLevel
if (typeof opts === 'function') {
handleCorsOptionsDelegator(opts, fastify, { hook: defaultOptions.hook }, next)
} else if (opts.delegator) {
@@ -53,6 +54,7 @@ function fastifyCors (fastify, opts, next) {
handleCorsOptionsDelegator(delegator, fastify, options, next)
} else {
if (opts.hideOptionsRoute !== undefined) hideOptionsRoute = opts.hideOptionsRoute
+ if (opts.logLevel !== undefined) logLevel = opts.logLevel
const corsOptions = normalizeCorsOptions(opts)
validateHook(corsOptions.hook, next)
if (hookWithPayload.indexOf(corsOptions.hook) !== -1) {
@@ -72,7 +74,7 @@ function fastifyCors (fastify, opts, next) {
// remove most headers (such as the Authentication header).
//
// This route simply enables fastify to accept preflight requests.
- fastify.options('*', { schema: { hide: hideOptionsRoute } }, (req, reply) => {
+ fastify.options('*', { schema: { hide: hideOptionsRoute }, logLevel }, (req, reply) => {
if (!req.corsPreflightEnabled) {
// Do not handle preflight requests if the origin option disabled CORS
reply.callNotFound() |
|
@climba03003 |
Same argument apply to function delegator() {}
delegator.logLevel = 'info'
delegator.hideOptionsRoute = true
fastify.register(CORS, (instance) => delegator) |
I have refactored the code as you suggested (logLevel, hideOptionsRoute). I also added tests that cover delegator usage for both. |
climba03003
left a comment
There was a problem hiding this comment.
Should be the last change before land.
This PR introduces an optional logLevel field to FastifyCorsOptions, letting users override the Fastify logger level for the internal OPTIONS * pre-flight route.
fixes: #320
Checklist
npm run testandnpm run benchmarkand the Code of conduct