Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

66 строки
2.2 KiB

  1. /*
  2. Copyright 2017 Google Inc. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. import {assert} from 'workbox-core/_private/assert.mjs';
  14. import {defaultMethod, validMethods} from './utils/constants.mjs';
  15. import normalizeHandler from './utils/normalizeHandler.mjs';
  16. import './_version.mjs';
  17. /**
  18. * A `Route` consists of a pair of callback functions, "match" and "handler".
  19. * The "match" callback determine if a route should be used to "handle" a
  20. * request by returning a non-falsy value if it can. The "handler" callback
  21. * is called when there is a match and should return a Promise that resolves
  22. * to a `Response`.
  23. *
  24. * @memberof workbox.routing
  25. */
  26. class Route {
  27. /**
  28. * Constructor for Route class.
  29. *
  30. * @param {workbox.routing.Route~matchCallback} match
  31. * A callback function that determines whether the route matches a given
  32. * `fetch` event by returning a non-falsy value.
  33. * @param {workbox.routing.Route~handlerCallback} handler A callback
  34. * function that returns a Promise resolving to a Response.
  35. * @param {string} [method='GET'] The HTTP method to match the Route
  36. * against.
  37. */
  38. constructor(match, handler, method) {
  39. if (process.env.NODE_ENV !== 'production') {
  40. assert.isType(match, 'function', {
  41. moduleName: 'workbox-routing',
  42. className: 'Route',
  43. funcName: 'constructor',
  44. paramName: 'match',
  45. });
  46. if (method) {
  47. assert.isOneOf(method, validMethods, {paramName: 'method'});
  48. }
  49. }
  50. // These values are referenced directly by Router so cannot be
  51. // altered by minifification.
  52. this.handler = normalizeHandler(handler);
  53. this.match = match;
  54. this.method = method || defaultMethod;
  55. }
  56. }
  57. export {Route};