Add h.directory() toolkit decoration
- Dominant language
- JavaScript
- Stars
- 236
- Forks
- 48
- PR merge metrics
- No merged PRs in 30d
Description
#### Support plan
* *is this issue currently blocking your project?* (yes/no): no
* *is this issue affecting a production system?* (yes/no): no
#### Context
* *node version*: v12+
* *module version*: v6
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi application
* *any other relevant information*: see conversation in #154 for context.
#### What problem are you trying to solve?
We would like to serve a directory (a la the `directory` handler-type) which is determined asynchronously. The current solution is to define a synchronous `path` config for the `directory` handler-type, but compute the `path` in a pre-handler. This creates some indirection that we don't see with the `file` handler-type due to the existence of `h.file()`.
#### Do you have a new or modified API suggestion to solve the problem?
The recommendation coming from conversation in #154 is to introduce a new toolkit decoration `h.directory()` that can be called in a standard handler, with arbitrary logic preceding it within the handler. The input to `h.directory()` would mirror the input to the `directory` handler-type, analogously to `h.file()` and the `file` handler-type.
```js
server.route({
method: 'get',
path: '/dir/{file*}',
handler : async (request, h) => {
const myDependencyFile = await import.meta.resolve('my-dependency');
return h.directory(path.dirname(myDependencyFile), {
listing: true,
redirectToSlash: true
});
}
});
```
Contributor guide
Assessment
This issue has not been assessed yet.