fastify / fastify/fastify-autoload

fileNameRoutePrefix

Open
#363 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
373
Forks
77
PR merge metrics
No merged PRs in 30d

Description

### 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
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.