1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- "use strict";
- Object.defineProperty(exports, "__esModule", { value: true });
- exports.createConfig = void 0;
- const isPlainObj = require("is-plain-obj");
- const url = require("url");
- const errors_1 = require("./errors");
- const logger_1 = require("./logger");
- const logger = (0, logger_1.getInstance)();
- function createConfig(context, opts) {
-
- const config = {
- context: undefined,
- options: {},
- };
-
- if (isContextless(context, opts)) {
- config.context = '/';
- config.options = Object.assign(config.options, context);
-
-
- }
- else if (isStringShortHand(context)) {
- const oUrl = url.parse(context);
- const target = [oUrl.protocol, '//', oUrl.host].join('');
- config.context = oUrl.pathname || '/';
- config.options = Object.assign(config.options, { target }, opts);
- if (oUrl.protocol === 'ws:' || oUrl.protocol === 'wss:') {
- config.options.ws = true;
- }
-
- }
- else {
- config.context = context;
- config.options = Object.assign(config.options, opts);
- }
- configureLogger(config.options);
- if (!config.options.target && !config.options.router) {
- throw new Error(errors_1.ERRORS.ERR_CONFIG_FACTORY_TARGET_MISSING);
- }
- return config;
- }
- exports.createConfig = createConfig;
- function isStringShortHand(context) {
- if (typeof context === 'string') {
- return !!url.parse(context).host;
- }
- }
- function isContextless(context, opts) {
- return isPlainObj(context) && (opts == null || Object.keys(opts).length === 0);
- }
- function configureLogger(options) {
- if (options.logLevel) {
- logger.setLevel(options.logLevel);
- }
- if (options.logProvider) {
- logger.setProvider(options.logProvider);
- }
- }
|