UNPKG

redis-smq-rest-api

Version:
44 lines 1.48 kB
import { URL } from 'url'; import { constants } from './constants.js'; import { ConfigInvalidApiServerParamsError } from './errors/ConfigInvalidApiServerParamsError.js'; function normalizePath(path) { const s = path .split('/') .map((i) => i.trim()) .filter((i) => !!i) .join('/'); return `/${s}`; } function validateURL(apiServer) { const { hostname = constants.apiServerHostname, port = constants.apiServerPort, basePath = constants.apiServerBasePath, } = apiServer; const address = `http://${hostname}:${port}${normalizePath(basePath)}`; try { const url = new URL(address); if (!['http'].map((x) => `${x.toLowerCase()}:`).includes(url.protocol)) { return new ConfigInvalidApiServerParamsError(); } if (hostname !== url.hostname || port !== Number(url.port)) { return new ConfigInvalidApiServerParamsError(); } return [url.hostname, Number(url.port), url.pathname]; } catch { return new ConfigInvalidApiServerParamsError(); } } export function parseConfig(config = {}) { const { apiServer = {}, ...rest } = config; const reply = validateURL(apiServer); if (reply instanceof Error) throw reply; const [hostname, port, basePath] = reply; return { ...rest, apiServer: { hostname, port, basePath, }, }; } //# sourceMappingURL=parseConfig.js.map