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
Add Fastify-allow plugin reference #3242
Add Fastify-allow plugin reference #3242
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.
LGTM. Very usefull plugin!
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
Great plugin! May I ask for a change? I think by default it should not add a new not found handler but rather provide an utility that could be used to implement such behavior. All the applications I have worked with have a custom not found handler.. which will make this plugin significantly less useful. |
Interesting, I didn't know that it was common to replace the notFoundHandler. Do you have a plugin that does this well? My first thought is to expose a method |
I would not really use such behavior. The best thing I think would be to add a property on the thrown error with the allowed methods |
I don't think I understand. When would I throw an error? Is there a hook I can use that is for requests that do not match to a route where I could throw an error? |
I'm sorry I've responded too quickly. I think the best approach would be to add a decorator in the app to fetch all the allowed method of a route. |
The decorator approach is fine for known routes with handlers like GET, but I don't think it will work for the routes that have one handler but not more, where the spec expects the server to return 405 with an Allow header. For instance:
Then the client calls:
The only way I found to respond with the 405 automatically is to replace the notFoundHandler because no route exists for This may actually be better as a core behaviour of fastify? It is part of the http 1.1 spec. |
Actually you are better off implementing this as a hook and throwing an Something like (this works): import Fastify from 'fastify'
import errors from 'http-errors'
const app = Fastify({ logger: true })
app.addHook('onRequest', async function (req) {
if (req.method === 'POST') {
throw new errors.MethodNotAllowed('POST')
}
})
await app.listen(3000) Which in pseudocode would become: import Fastify from 'fastify'
import errors from 'http-errors'
const app = Fastify({ logger: true })
await app.register(import('fastify-allow'))
app.addHook('onRequest', async function (req) {
if (app.isAllowed(req)) {
throw new errors.MethodNotAllowed('POST')
}
})
await app.listen(3000) |
The 'onRequest' approach was actually my first idea, but I found that it was not called if a request had no registered handler. Find-my-way includes the method in the search for a route, so if fastify has a handler for The only hook I could find for non-registered routes was the notFoundHandler. This plugin does allow the developer to turn off the |
I think we fixed this, this will get triggered all the time. |
Cool, what version?
…On Tue, Aug 10, 2021 at 6:33 AM Matteo Collina ***@***.***> wrote:
The 'onRequest' approach was actually my first idea, but I found that it
was not called if a request had no registered handler. Find-my-way includes
the method in the search for a route, so if fastify has a handler for
app.get('/route'...), fastify will trigger onRequest for GET /route, but it
will not trigger for POST /route.
I *think* we fixed this, this will get triggered all the time.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#3242 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACY3BJCCU3XGVU4DKQYU43T4ETDZANCNFSM5BY56ZZA>
.
|
I think it was fixed since the v3 cycle but I might be mistaken. Anyway, I could not reproduce the behavior you are mentioning now. |
Great news, I was wrong and onRequest is indeed called when a path matches but not the method. I tested both fastify 3.3.0 and 3.18.1: test("onRequest for unregistered routes", (t) => {
t.plan(2)
const app = Fastify()
app.addHook("onRequest", (request: FastifyRequest, reply: FastifyReply, done: () => void) => {
t.pass(`onRequest called for ${request.method} ${request.url}`)
done()
})
const url = "/a-route"
app.get(url, (request: FastifyRequest, reply: FastifyReply) => {
reply.send()
})
app.inject({method: "GET", url})
app.inject({method: "DELETE", url})
})
So I can take out the notFoundHandler approach altogether. I'll post in this issue when I do that. |
I have refactored to not use the noFoundHandler. Thanks again for the direction. |
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
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Checklist
and the Code of conduct