Route.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. Copyright 2018 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import { assert } from 'workbox-core/_private/assert.js';
  8. import { defaultMethod, validMethods } from './utils/constants.js';
  9. import { normalizeHandler } from './utils/normalizeHandler.js';
  10. import './_version.js';
  11. /**
  12. * A `Route` consists of a pair of callback functions, "match" and "handler".
  13. * The "match" callback determine if a route should be used to "handle" a
  14. * request by returning a non-falsy value if it can. The "handler" callback
  15. * is called when there is a match and should return a Promise that resolves
  16. * to a `Response`.
  17. *
  18. * @memberof module:workbox-routing
  19. */
  20. class Route {
  21. /**
  22. * Constructor for Route class.
  23. *
  24. * @param {module:workbox-routing~matchCallback} match
  25. * A callback function that determines whether the route matches a given
  26. * `fetch` event by returning a non-falsy value.
  27. * @param {module:workbox-routing~handlerCallback} handler A callback
  28. * function that returns a Promise resolving to a Response.
  29. * @param {string} [method='GET'] The HTTP method to match the Route
  30. * against.
  31. */
  32. constructor(match, handler, method = defaultMethod) {
  33. if (process.env.NODE_ENV !== 'production') {
  34. assert.isType(match, 'function', {
  35. moduleName: 'workbox-routing',
  36. className: 'Route',
  37. funcName: 'constructor',
  38. paramName: 'match',
  39. });
  40. if (method) {
  41. assert.isOneOf(method, validMethods, { paramName: 'method' });
  42. }
  43. }
  44. // These values are referenced directly by Router so cannot be
  45. // altered by minificaton.
  46. this.handler = normalizeHandler(handler);
  47. this.match = match;
  48. this.method = method;
  49. }
  50. }
  51. export { Route };