WorkerRouter
import { WorkerRouter } from "https://raw.githubusercontent.com/worker-tools/router/master/index.ts";§Type Parameters
§Properties
Callback compatible with Cloudflare Worker's fetch module export.
E.g. export default router.
Implements the (ancient) event listener object interface to allow passing to fetch event directly,
e.g. self.addEventListener('fetch', router).
Callback that is compatible with Deno's serve function.
E.g. serve(router.serveCallback).
§Methods
Alias for for the more appropriately named any method
Add a route that matches any HTTP method.
Add a route that matches the DELETE method.
Add a route that matches any method with the provided pattern.
Note that the pattern here is interpreted as a URLPatternInit which has important implication for matching.
Mostly, this is for use in Service Workers to intercept requests to external resources.
The name external is a bit of a misnomer. It simply forwards init to the URLPattern constructor,
instead of being limited to the pathname property in the general case.
Might change name/API
Like .external, but only matches DELETE
Might change name/API
Like .external, but only matches GET
Might change name/API
Like .external, but only matches HEAD
Might change name/API
Like .external, but only matches OPTIONS
Might change name/API
Like .external, but only matches PATCH
Might change name/API
Like .external, but only matches POST
Might change name/API
Like .external, but only matches PUT
Might change name/API
Add a route that matches the GET method.
Add a route that matches the HEAD method.
Add a route that matches the OPTIONS method.
Add a route that matches the PATCH method.
Add a route that matches the POST method.
Add a route that matches the PUT method.
Register a special route to recover from an error during execution of a regular route.
In addition to the usual context properties, the provided handler receives a response and error property.
In case of a well-known error (typically caused by middleware),
the response contains a Fetch API Response object with matching status and status text set.
In case of an unknown error, the response is a generic "internal server error" and the error property
contains the value caught by the catch block.
Recover routes don't execute the router-level middleware (which might have caused the error), but can have middleware specifically for this route. Note that if another error occurs during the execution of this middleware, there are no more safety nets and an internal server error response is returned.
If a global DEBUG variable is set (or process.env.NODE_ENV is set to development in case of webpack)
the router will throw on an unhandled error. This is to make it easier to spot problems during development.
Otherwise, the router will not throw but instead dispatch a error event on itself before returning an empty
internal server error response.
Use a different WorkerRouter for the provided pattern. Keep in mind that:
- The pattern must end in a wildcard
* - The corresponding match is the only part used for matching in the
subRouter - Forwards all HTTP methods
- Does not apply any middleware
Why does it not apply middleware?
There are 2 reasons: First, it interferes with type inference of middleware. As a developer you'd have to provide the correct types at the point of defining the sub router, which is at least as cumbersome as providing the middleware itself.
Second, without this there would be no way to opt a route out of the router-level middleware.
For example you might want to opt out all /public* urls from cookie parsing, authentication, etc.
but add a different caching policy instead.
A pattern ending in a wildcard, e.g. /items*
A WorkerRouter that handles the remaining part of the URL, e.g. /:category/:id
The name of this method might change to avoid confusion with use method known from other routers.
See .external and .use.
Might change name/API