Last active
April 30, 2020 18:52
-
-
Save D7Torres/852be6c411dd5f0138f250cfd2fbbcab to your computer and use it in GitHub Desktop.
Endpoint Refactor - complete endpoint file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getProtocol = (service, isServerSide, environment) => { | |
if (isServerSide) { | |
return 'http' | |
} | |
if (service === 'webclient' || environment !== 'local') { | |
return 'https' | |
} | |
return 'http' | |
} | |
const getPath = (service, isServerSide, environment, version) => { | |
const isCore = service === 'core' | |
let path = '' | |
if (service === 'webclient') { | |
return path | |
} | |
if (environment === 'local' && !isCore) { | |
return `/${service}/${version}` | |
} | |
if (isServerSide) { | |
return path | |
} | |
if (!isCore) { | |
path += `/${service}` | |
} | |
if (version) { | |
path += `/${version}` | |
} | |
return path | |
} | |
const getPort = (service, environment, isClientSide) => { | |
if (service !== 'webclient' && environment === 'local' && !isClientSide) { | |
return ':80' | |
} | |
return '' | |
} | |
const getSubdomain = (service, isServerSide, environment) => { | |
if (service === 'webclient') { | |
if (!isServerSide && environment === 'production') { | |
return 'www' | |
} | |
return `${environment}-${service}` | |
} | |
if (environment === 'local') { | |
return 'api' | |
} | |
if (isServerSide) { | |
return `${environment}-${service}` | |
} | |
return `${environment}-api` | |
} | |
function endpoint(service, version = '') { | |
const protocol = getProtocol(service, __SERVER__, __ENV__) | |
const subdomain = getSubdomain(service, __SERVER__, __ENV__) | |
const path = getPath(service, __SERVER__, __ENV__, version) | |
const port = getPort(service, __ENV__, __CLIENT__) | |
return `${protocol}://${subdomain}.${__DOMAIN__}${port}${path}` | |
} | |
export default endpoint |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment