Skip to content
This repository has been archived by the owner on Jul 8, 2023. It is now read-only.

Mention in the hooks page that registering a global hook after adding a new endpoint will still run #329

Open
2 tasks done
rluvaton opened this issue Feb 7, 2022 · 2 comments

Comments

@rluvaton
Copy link

rluvaton commented Feb 7, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

馃殌 Feature Proposal

Add in the Hooks page that registering a global hook after adding a new endpoint, the global hook will still run

Motivation

Migrating from express to Fastify can lead to this problem:

Example code in express:

const express = require('express')
const app = express()
const router = express.Router()

router.post('/user/:id', function (req, res) {
  res.send('signing up user!')
})

// predicate the router with a check and bail out when needed
router.use(function (req, res, next) {
  if (!req.headers['x-auth']) return next('router')
  next()
})

// other routes

Example code in fastify:

const Fastify = require("fastify");
const fastifyJwt = require("fastify-jwt");

const fastify = Fastify({
  logger: false,
});
fastify.register(require("fastify-jwt"), {
  secret: "secret",
});

fastify.post("/signup", (req, reply) => {
  const token = fastify.jwt.sign({ userId: "kent-beck" });
  reply.send({ token });
});

fastify.addHook("onRequest", async (request, reply) => {
  try {
    await request.jwtVerify();
  } catch (err) {
    reply.send(err);
  }
});

// other routes

Doing the above code in fastify one could think that the hook only runs for requests that happen after the hook declaration

Example

No response


I can create a PR if interested...

@jsumners
Copy link
Member

jsumners commented Feb 7, 2022

@rluvaton rluvaton changed the title Mention in the hooks page that registering a global hook after adding a new endpoin will still run Mention in the hooks page that registering a global hook after adding a new endpoint will still run Feb 7, 2022
@rluvaton
Copy link
Author

rluvaton commented Feb 7, 2022

I think it should be mentioned, maybe a link to the Encapsulation page you provided

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants