AdapterLoader.js 6.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. exports.loadAdapter = loadAdapter;
  7. exports.loadModule = loadModule;
  8. /**
  9. * @module AdapterLoader
  10. */
  11. /**
  12. * @static
  13. * Attempt to load an adapter or fallback to the default.
  14. * @param {Adapter} adapter an adapter
  15. * @param {Adapter} defaultAdapter the default adapter to load
  16. * @param {any} options options to pass to the contstructor
  17. * @returns {Object} the loaded adapter
  18. */
  19. function loadAdapter(adapter, defaultAdapter, options) {
  20. if (!adapter) {
  21. if (!defaultAdapter) {
  22. return options;
  23. }
  24. // Load from the default adapter when no adapter is set
  25. return loadAdapter(defaultAdapter, undefined, options);
  26. } else if (typeof adapter === 'function') {
  27. try {
  28. return adapter(options);
  29. } catch (e) {
  30. if (e.name === 'TypeError') {
  31. var Adapter = adapter;
  32. return new Adapter(options);
  33. } else {
  34. throw e;
  35. }
  36. }
  37. } else if (typeof adapter === 'string') {
  38. /* eslint-disable */
  39. adapter = require(adapter);
  40. // If it's define as a module, get the default
  41. if (adapter.default) {
  42. adapter = adapter.default;
  43. }
  44. return loadAdapter(adapter, undefined, options);
  45. } else if (adapter.module) {
  46. return loadAdapter(adapter.module, undefined, adapter.options);
  47. } else if (adapter.class) {
  48. return loadAdapter(adapter.class, undefined, adapter.options);
  49. } else if (adapter.adapter) {
  50. return loadAdapter(adapter.adapter, undefined, adapter.options);
  51. }
  52. // return the adapter as provided
  53. return adapter;
  54. }
  55. async function loadModule(modulePath) {
  56. let module;
  57. try {
  58. module = require(modulePath);
  59. } catch (err) {
  60. if (err.code === 'ERR_REQUIRE_ESM') {
  61. module = await import(modulePath);
  62. if (module.default) {
  63. module = module.default;
  64. }
  65. } else {
  66. throw err;
  67. }
  68. }
  69. return module;
  70. }
  71. var _default = exports.default = loadAdapter;
  72. //# sourceMappingURL=data:application/json;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