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:
const routes: Route[] = [
{
pattern: new URLPattern({ pathname: "/api/foo" }),
accept: ["application/xml"]
handler: () => new Response("<foo>bar</foo>"),
},
{
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
AFAIK, quite a lot of other routers in the JS ecosystem just match the URL and method. accept header matching can be easily done within the matched route handler.
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 whenapplication/json
is passed.Describe the solution you'd like
Example: sending different format based on requested accept types:
Behaviour could be:
accept
is not specified, keep current behaviour, ie: execute handler regardless of client headeraccept
is specified, only execute handler if one of the requested types is matching the specified arrayWhich 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