Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.
This repository was 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

@rluvaton

Description

@rluvaton

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions