Route.ts
2 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
/*
Copyright 2018 Google LLC
Use of this source code is governed by an MIT-style
license that can be found in the LICENSE file or at
https://opensource.org/licenses/MIT.
*/
import {assert} from 'workbox-core/_private/assert.js';
import {HTTPMethod, defaultMethod, validMethods} from './utils/constants.js';
import {normalizeHandler} from './utils/normalizeHandler.js';
import {Handler, HandlerObject, MatchCallback} from './_types.js';
import './_version.js';
/**
* A `Route` consists of a pair of callback functions, "match" and "handler".
* The "match" callback determine if a route should be used to "handle" a
* request by returning a non-falsy value if it can. The "handler" callback
* is called when there is a match and should return a Promise that resolves
* to a `Response`.
*
* @memberof module:workbox-routing
*/
class Route {
handler: HandlerObject;
match: MatchCallback;
method: HTTPMethod;
/**
* Constructor for Route class.
*
* @param {module:workbox-routing~matchCallback} match
* A callback function that determines whether the route matches a given
* `fetch` event by returning a non-falsy value.
* @param {module:workbox-routing~handlerCallback} handler A callback
* function that returns a Promise resolving to a Response.
* @param {string} [method='GET'] The HTTP method to match the Route
* against.
*/
constructor(
match: MatchCallback,
handler: Handler,
method: HTTPMethod = defaultMethod) {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(match, 'function', {
moduleName: 'workbox-routing',
className: 'Route',
funcName: 'constructor',
paramName: 'match',
});
if (method) {
assert!.isOneOf(method, validMethods, {paramName: 'method'});
}
}
// These values are referenced directly by Router so cannot be
// altered by minificaton.
this.handler = normalizeHandler(handler);
this.match = match;
this.method = method;
}
}
export {Route};