From fc3e78e1307455df506dbc8f6ba5269f28f6e364 Mon Sep 17 00:00:00 2001 From: Ricardo Gobbo de Souza Date: Wed, 3 Jun 2020 17:36:17 -0300 Subject: [PATCH] feat: support baseUrl and remove port :443 and :80 when http or https (#103) --- lib/module.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/module.js b/lib/module.js index 384f11d..e769d62 100755 --- a/lib/module.js +++ b/lib/module.js @@ -34,10 +34,17 @@ function httpModule (_moduleOptions) { // Default prefix const prefix = process.env.API_PREFIX || moduleOptions.prefix || '/' + // Support baseUrl + if (moduleOptions.baseUrl && !moduleOptions.baseURL) { + moduleOptions.baseURL = moduleOptions.baseUrl + + delete moduleOptions.baseUrl + } + // Apply defaults const options = { baseURL: `http://${defaultHost}:${defaultPort}${prefix}`, - browserBaseURL: null, + browserBaseURL: undefined, proxyHeaders: true, proxyHeadersIgnore: ['accept', 'host', 'cf-ray', 'cf-connecting-ip', 'content-length'], proxy: false, @@ -62,7 +69,7 @@ function httpModule (_moduleOptions) { } // Default browserBaseURL - if (!options.browserBaseURL) { + if (typeof options.browserBaseURL === 'undefined') { options.browserBaseURL = options.proxy ? prefix : options.baseURL } @@ -77,6 +84,16 @@ function httpModule (_moduleOptions) { options.retry = JSON.stringify(options.retry) } + // Remove port 443 when https + if (options.baseURL.includes('https://')) { + options.baseURL = options.baseURL.replace(':443', '') + } + + // Remove port 80 when http + if (options.baseURL.includes('http://')) { + options.baseURL = options.baseURL.replace(':80', '') + } + // Convert http:// to https:// if https option is on if (options.https === true) { const https = s => s.replace('http://', 'https://')