feat_req(http/routes): add a accept filter
- Dominant language
- TypeScript
- Stars
- 3.6k
- Forks
- 681
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
Current implementation is supporting http method discriminant probably covers most use-case, but there's also one more use-case which I feel like is pretty common is Accept header discriminant.
Sometimes, same route path may serve different content based on Accept headers (for example a nice page for users when `text/html` is present, and a json object for developers when `application/json` is passed.
**Describe the solution you'd like**
Example: sending different format based on requested accept types:
```ts
const routes: Route[] = [
{
pattern: new URLPattern({ pathname: "/api/foo" }),
accept: ["application/xml"]
handler: () => new Response("bar"),
},
{
pattern: new URLPattern({ pathname: "/api/foo" }),
accept: ["application/json"]
handler: () => new Response('{"foo":{"bar"}}'),
}
];
```
Behaviour could be:
- If `accept` is not specified, keep current behaviour, ie: execute handler regardless of client header
- If `accept` is specified, only execute handler if one of the requested types is matching the specified array
Which also means that it's possible to specify the same route multiple time with different accept headers, and possible specify one without if you want a default handler for this route
**Describe alternatives you've considered**
Not using `route` or handle in default handler but less elegant
Contributor guide
Assessment
This issue has not been assessed yet.