normalizeHandler.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 '../_version.mjs';
  9. /**
  10. * @param {function()|Object} handler Either a function, or an object with a
  11. * 'handle' method.
  12. * @return {Object} An object with a handle method.
  13. *
  14. * @private
  15. */
  16. export const normalizeHandler = (handler) => {
  17. if (handler && typeof handler === 'object') {
  18. if (process.env.NODE_ENV !== 'production') {
  19. assert.hasMethod(handler, 'handle', {
  20. moduleName: 'workbox-routing',
  21. className: 'Route',
  22. funcName: 'constructor',
  23. paramName: 'handler',
  24. });
  25. }
  26. return handler;
  27. } else {
  28. if (process.env.NODE_ENV !== 'production') {
  29. assert.isType(handler, 'function', {
  30. moduleName: 'workbox-routing',
  31. className: 'Route',
  32. funcName: 'constructor',
  33. paramName: 'handler',
  34. });
  35. }
  36. return {handle: handler};
  37. }
  38. };