fastify-autoload
fastify-autoload copied to clipboard
fileNameRoutePrefix
Prerequisites
- [X] I have written a descriptive issue title
- [X] 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
I don't exactly understand this, but go ahead and send a PR?
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
I'm currently finishing my thesis, but I'll have try to prepare a PR when I get it done.
Solved it like that. it takes filename as prefix and also parent directory. Index file routes are not prefixed. Only thing you'll have to change is that part where your routes files are placed:
.replace(/^\/(?:app|system)\/routes\//, '')
// index.js
fastify.register(autoload, {
dir: path.join(process.cwd(), 'system', 'routes'),
dirNameRoutePrefix: false,
autoHooks: true,
cascadeHooks: false,
overwriteHooks: false,
indexPattern: /^[_.]?(auto)?routes(\.js|\.cjs|\.mjs)$/i,
autoHooksPattern: /^[_.]?(auto)?hooks(\.js|\.cjs|\.mjs)$/i
});
// suff.js
export default async (fastify) => {
fastify.get('/', async (req, res) => {
return res.send('OK');
});
};
export const prefixOverride =
'/' +
import.meta.url
.replace(/^file:\/\//, '')
.replace(/^\/(?=[A-Za-z]:\/)/, '')
.replace(/\\/g, '/')
.replace(process.cwd().replace(/\\/g, '/'), '')
.replace(/^\/(?:app|system)\/routes\//, '')
.replace(/\.(?:mjs|cjs|js)$/, '')
.replace(/(^|\/)index$/, '')
.replace(/^\/+/, '');