config-factory.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createConfig = void 0;
  4. const isPlainObj = require("is-plain-obj");
  5. const url = require("url");
  6. const errors_1 = require("./errors");
  7. const logger_1 = require("./logger");
  8. const logger = logger_1.getInstance();
  9. function createConfig(context, opts) {
  10. // structure of config object to be returned
  11. const config = {
  12. context: undefined,
  13. options: {},
  14. };
  15. // app.use('/api', proxy({target:'http://localhost:9000'}));
  16. if (isContextless(context, opts)) {
  17. config.context = '/';
  18. config.options = Object.assign(config.options, context);
  19. // app.use('/api', proxy('http://localhost:9000'));
  20. // app.use(proxy('http://localhost:9000/api'));
  21. }
  22. else if (isStringShortHand(context)) {
  23. const oUrl = url.parse(context);
  24. const target = [oUrl.protocol, '//', oUrl.host].join('');
  25. config.context = oUrl.pathname || '/';
  26. config.options = Object.assign(config.options, { target }, opts);
  27. if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
  28. config.options.ws = true;
  29. }
  30. // app.use('/api', proxy({target:'http://localhost:9000'}));
  31. }
  32. else {
  33. config.context = context;
  34. config.options = Object.assign(config.options, opts);
  35. }
  36. configureLogger(config.options);
  37. if (!config.options.target && !config.options.router) {
  38. throw new Error(errors_1.ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
  39. }
  40. return config;
  41. }
  42. exports.createConfig = createConfig;
  43. /**
  44. * Checks if a String only target/config is provided.
  45. * This can be just the host or with the optional path.
  46. *
  47. * @example
  48. * app.use('/api', proxy('http://localhost:9000'));
  49. * app.use(proxy('http://localhost:9000/api'));
  50. *
  51. * @param {String} context [description]
  52. * @return {Boolean} [description]
  53. */
  54. function isStringShortHand(context) {
  55. if (typeof context === 'string') {
  56. return !!url.parse(context).host;
  57. }
  58. }
  59. /**
  60. * Checks if a Object only config is provided, without a context.
  61. * In this case the all paths will be proxied.
  62. *
  63. * @example
  64. * app.use('/api', proxy({target:'http://localhost:9000'}));
  65. *
  66. * @param {Object} context [description]
  67. * @param {*} opts [description]
  68. * @return {Boolean} [description]
  69. */
  70. function isContextless(context, opts) {
  71. return isPlainObj(context) && (opts == null || Object.keys(opts).length === 0);
  72. }
  73. function configureLogger(options) {
  74. if (options.logLevel) {
  75. logger.setLevel(options.logLevel);
  76. }
  77. if (options.logProvider) {
  78. logger.setProvider(options.logProvider);
  79. }
  80. }