Route.mjs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.mjs';
  8. import {defaultMethod, validMethods} from './utils/constants.mjs';
  9. import {normalizeHandler} from './utils/normalizeHandler.mjs';
  10. import './_version.mjs';
  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 workbox.routing
  19. */
  20. class Route {
  21. /**
  22. * Constructor for Route class.
  23. *
  24. * @param {workbox.routing.Route~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 {workbox.routing.Route~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) {
  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 minifification.
  46. this.handler = normalizeHandler(handler);
  47. this.match = match;
  48. this.method = method || defaultMethod;
  49. }
  50. }
  51. export {Route};