Skip to content

Instantly share code, notes, and snippets.

@D7Torres
Last active April 30, 2020 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save D7Torres/852be6c411dd5f0138f250cfd2fbbcab to your computer and use it in GitHub Desktop.
Save D7Torres/852be6c411dd5f0138f250cfd2fbbcab to your computer and use it in GitHub Desktop.
Endpoint Refactor - complete endpoint file
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