-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature: allow custom hook name #234
feature: allow custom hook name #234
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not change the default. Otherwise, it is a breaking change.
a2154fe
to
790e810
Compare
index.js
Outdated
} | ||
}) | ||
} | ||
|
||
function onRequest (fastify, options, req, reply, next) { | ||
function handler (fastify, options, req, reply, next) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont name it handler. Name it addCorsHeaders or so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gonna call it addCorsHeadersHandler
since there's a function already named addCorsHeaders
, and the word handler
is good to have since it's mainly called from a hook handler (like onRequest
)
index.js
Outdated
function handleCorsOptionsDelegator (optionsResolver, fastify) { | ||
fastify.addHook('onRequest', function onRequestCors (req, reply, next) { | ||
function handleCorsOptionsDelegator (optionsResolver, fastify, { hookName } = { hookName: defaultHookName }) { | ||
fastify.addHook(hookName, function handleCors (req, reply, next) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Were do we define the variable hookName?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's being passed as parameter and defaults to defaultHookName
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont see a const hookName=
in your new code?!
README.md
Outdated
|
||
const fastify = Fastify() | ||
await fastify.register(cors, { | ||
hookName: 'preHandler', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should call it not hookName but simply hook as we do it in other plugins, like in fastify-rate-limit
790e810
to
10910d9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Something strange: If I add a test case like this:
then the unit tests break. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please dont merge. I will have to review it.
Your |
Well. I reworked it to handle custom hooks more resilient. It is easy to oversee that some hooks also have payload as an parameter. @climba03003 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
The goal with this PR is to be able to change the hook created by the plugin, for example, being able to execute the code on
preHandler
rather thanonRequest
.I currently have a case where I need to build a redux store on
preHandler
, injected into therequest
object for later consuption, and use it in this plugin to perform some checksChecklist
npm run test
andnpm run benchmark
and the Code of conduct