Skip to content
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

setNotFoundHandler is not encapsulated when called inside autohooks.js #326

Open
2 tasks done
felixmosh opened this issue Sep 12, 2023 · 4 comments · Fixed by #368
Open
2 tasks done

setNotFoundHandler is not encapsulated when called inside autohooks.js #326

felixmosh opened this issue Sep 12, 2023 · 4 comments · Fixed by #368
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@felixmosh
Copy link

felixmosh commented Sep 12, 2023

Prerequisites

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

Fastify version

4.22.2

Plugin version

4.5.1

Node.js version

18.7

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

20.04

Description

Using autoload & autohooks.js inside a folder, when defining setNotFoundHandler inside autohooks.js, & defining prefix on the autoload options, the defined setNotFoundHandler leaks outside of the prefix.

Steps to Reproduce

Assume the following folder structure.

├── index.ts
├── plugins
│   ├── renderer.ts
│   └── routes.ts
├── routes
│   ├── api
│   │   ├── alive-route.ts
│   │   └── autohooks.ts
│   └── renderer.ts
// plugins/routes.ts
export const routesPlugin: FastifyPluginCallback = async function routesPlugin(instance) {
  instance.route(rendererRoute);

  instance.register(autoload, {
    dir: path.join(__dirname, '../routes/api'),
    forceESM: true,
    autoHooks: true,
    cascadeHooks: true,
    options: { prefix: env.API_PATH }, // this is the autoload prefix its value is `/api`
  });
};
// routes/api/autohooks.ts
const autoHooks: FastifyPluginAsync = async function autoHooks(instance) {
  instance.setNotFoundHandler(() => {
    throw new ActionNotFound();
  });
}

export default autoHooks;

When calling to /api/not-exists it shows the ActionNotFound error (as expected)
but, when calling /not-exists it also shows the ActionNotFound error, evenough, the not found handler is defined inside the prefix /api.

I've tried even to add encapsulate: true to autoload options, without luck.

Expected Behavior

From Fastify docs, it specifies that if there is a prefix defined, setNotFoundHandler should be encapsulated to this prefix.

AutoLoad should enforce the same thing.

@Eomm
Copy link
Member

Eomm commented Nov 12, 2023

Thanks for reporting!
Would you like to send a Pull Request to address this issue? Remember to add unit tests.

@Eomm Eomm added bug Something isn't working good first issue Good for newcomers labels Nov 12, 2023
@jean-michelet
Copy link
Contributor

Are you still interested in fixing this @Eomm?

@felixmosh
Copy link
Author

Thank you team🙏🏼

@jean-michelet
Copy link
Contributor

jean-michelet commented Jun 17, 2024

Sorry, we had to revert this change.

See #380 and #383

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants