Router.d.ts
4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Route } from './Route.js';
import { HTTPMethod } from './utils/constants.js';
import { Handler, HandlerCallbackOptions } from './_types.js';
import './_version.js';
/**
* The Router can be used to process a FetchEvent through one or more
* [Routes]{@link module:workbox-routing.Route} responding with a Request if
* a matching route exists.
*
* If no route matches a given a request, the Router will use a "default"
* handler if one is defined.
*
* Should the matching Route throw an error, the Router will use a "catch"
* handler if one is defined to gracefully deal with issues and respond with a
* Request.
*
* If a request matches multiple routes, the **earliest** registered route will
* be used to respond to the request.
*
* @memberof module:workbox-routing
*/
declare class Router {
private readonly _routes;
private _defaultHandler?;
private _catchHandler?;
/**
* Initializes a new Router.
*/
constructor();
/**
* @return {Map<string, Array<module:workbox-routing.Route>>} routes A `Map` of HTTP
* method name ('GET', etc.) to an array of all the corresponding `Route`
* instances that are registered.
*/
get routes(): Map<HTTPMethod, Route[]>;
/**
* Adds a fetch event listener to respond to events when a route matches
* the event's request.
*/
addFetchListener(): void;
/**
* Adds a message event listener for URLs to cache from the window.
* This is useful to cache resources loaded on the page prior to when the
* service worker started controlling it.
*
* The format of the message data sent from the window should be as follows.
* Where the `urlsToCache` array may consist of URL strings or an array of
* URL string + `requestInit` object (the same as you'd pass to `fetch()`).
*
* ```
* {
* type: 'CACHE_URLS',
* payload: {
* urlsToCache: [
* './script1.js',
* './script2.js',
* ['./script3.js', {mode: 'no-cors'}],
* ],
* },
* }
* ```
*/
addCacheListener(): void;
/**
* Apply the routing rules to a FetchEvent object to get a Response from an
* appropriate Route's handler.
*
* @param {Object} options
* @param {Request} options.request The request to handle (this is usually
* from a fetch event, but it does not have to be).
* @param {FetchEvent} [options.event] The event that triggered the request,
* if applicable.
* @return {Promise<Response>|undefined} A promise is returned if a
* registered route can handle the request. If there is no matching
* route and there's no `defaultHandler`, `undefined` is returned.
*/
handleRequest({ request, event }: {
request: Request;
event?: ExtendableEvent;
}): Promise<Response> | undefined;
/**
* Checks a request and URL (and optionally an event) against the list of
* registered routes, and if there's a match, returns the corresponding
* route along with any params generated by the match.
*
* @param {Object} options
* @param {URL} options.url
* @param {Request} options.request The request to match.
* @param {Event} [options.event] The corresponding event (unless N/A).
* @return {Object} An object with `route` and `params` properties.
* They are populated if a matching route was found or `undefined`
* otherwise.
*/
findMatchingRoute({ url, request, event }: {
url: URL;
request: Request;
event?: ExtendableEvent;
}): {
route?: Route;
params?: HandlerCallbackOptions['params'];
};
/**
* Define a default `handler` that's called when no routes explicitly
* match the incoming request.
*
* Without a default handler, unmatched requests will go against the
* network as if there were no service worker present.
*
* @param {module:workbox-routing~handlerCallback} handler A callback
* function that returns a Promise resulting in a Response.
*/
setDefaultHandler(handler: Handler): void;
/**
* If a Route throws an error while handling a request, this `handler`
* will be called and given a chance to provide a response.
*
* @param {module:workbox-routing~handlerCallback} handler A callback
* function that returns a Promise resulting in a Response.
*/
setCatchHandler(handler: Handler): void;
/**
* Registers a route with the router.
*
* @param {module:workbox-routing.Route} route The route to register.
*/
registerRoute(route: Route): void;
/**
* Unregisters a route with the router.
*
* @param {module:workbox-routing.Route} route The route to unregister.
*/
unregisterRoute(route: Route): void;
}
export { Router };