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

fileNameRoutePrefix #363

Open
2 tasks done
tajnymag opened this issue Feb 13, 2024 · 3 comments
Open
2 tasks done

fileNameRoutePrefix #363

tajnymag opened this issue Feb 13, 2024 · 3 comments

Comments

@tajnymag
Copy link

tajnymag commented Feb 13, 2024

Prerequisites

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

🚀 Feature Proposal

Plugin could not only get their prefixes from their parent directories, but also from their filename.

The nature of the feature is very similar to the existing dirNameRoutePrefix. It could take either boolean or a function checking/parsing given filename.

Motivation

It would make routes hierarchy much easier to digest and eliminate some code duplicity and hardcoded prefixes.

If I haven't missed the feature, right now, there's no easy way to declare routes prefix without either hardcoding it into the plugin file or to have a directory with an index file for each route.

Example

Given this structure:

├── plugins
│   ├── hooked-plugin
│   │   ├── autohooks.mjs
│   │   ├── routes.js
│   │   └── children
│   │       ├── commonjs.cjs
│   │       ├── module.mjs
│   │       └── typescript.ts
│   ├── single-plugin
│   │   ├── index.js
│   │   └── utils.js
│   ├── more-plugins
│   │   ├── commonjs.cjs
│   │   ├── module.mjs
│   │   └── typescript.ts
│   └── another-plugin.js
├── package.json
└── app.js

fastify-autoload with fileNameRoutePrefix: true, autoHooks: true would generate these routes and prefix the leaf plugins accordingly:

/hooked-plugin/routes
/hooked-plugin/children/commonjs
/hooked-plugin/children/module
/hooked-plugin/children/typescript

/single-plugin
/single-plugin/utils

/more-plugins/commonjs
/more-plugins/module
/more-plugins/typescript

/another-plugin
@mcollina
Copy link
Member

I don't exactly understand this, but go ahead and send a PR?

@RyanIgo
Copy link

RyanIgo commented Mar 12, 2024

Agree this would be nice.

In my project i have a feature folder and a single file for each endpoint against that feature.

Simple Example
user

  • list
  • read
  • update
  • delete

I am using ESM for my environment so i made this little inline function to call so i dont have duplicate my the name .

let endpoint = `/${getFileName(import.meta.url)}`,

my getFile name just parses the url to only return the file name, it also supports create.$id.ts to i can extract a parameter if needed

@tajnymag
Copy link
Author

I'm currently finishing my thesis, but I'll have try to prepare a PR when I get it done.

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

No branches or pull requests

3 participants