feat: support baseUrl and remove port :443 and :80 when http or https (#103)

master
Ricardo Gobbo de Souza 2020-06-03 17:36:17 -03:00 committed by GitHub
parent 4e00babc6d
commit fc3e78e130
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 2 deletions

View File

@ -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://')