normalizeHandler.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 '../_version.js';
  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. }
  28. else {
  29. if (process.env.NODE_ENV !== 'production') {
  30. assert.isType(handler, 'function', {
  31. moduleName: 'workbox-routing',
  32. className: 'Route',
  33. funcName: 'constructor',
  34. paramName: 'handler',
  35. });
  36. }
  37. return { handle: handler };
  38. }
  39. };