Last active
October 1, 2017 18:04
-
-
Save jmosul/41e8f3025cfb38139840b2c2ad0f6bc7 to your computer and use it in GitHub Desktop.
ES6 class for supporting Urls in my applications that talk to my api
This file contains hidden or 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 JadeConfig = { | |
environment: 'prod', | |
domain: 'jamesmosullivan', | |
topLevelDomain: 'uk' | |
}; |
This file contains hidden or 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
class JadeService { | |
constructor( $window, JadeConfig = {} ){ | |
this.JadeConfig = JadeConfig; | |
this._env = this.JadeConfig.environment || this._parseHref( $window.location.href ); | |
} | |
get environment() { | |
return this._env; | |
} | |
get domain(){ | |
return this.JadeConfig.domain || 'jamesmosullivan'; | |
} | |
get topLevelDomain(){ | |
return this.isDev() ? 'dev' : this.JadeConfig.topLevelDomain || 'uk'; | |
} | |
get uri() { | |
return `${this.domain}.${this.topLevelDomain}`; | |
} | |
get apiBase() { | |
return `api.${this.uri}`; | |
} | |
isDev(){ | |
return this.environment === 'dev'; | |
} | |
/** | |
* Determines environment from given href | |
* | |
* @param href | |
* @returns {string} | |
* @private | |
*/ | |
_parseHref( href ){ | |
return href.indexOf( `${this.domain}.dev`) > 0 ? 'dev' : 'prod'; | |
} | |
} |
This file contains hidden or 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
{ | |
"name": "jade-service", | |
"version": "1.0.0", | |
"description": "Core ES6 class contacting services within Jade (Jmosul Application Development Enviroment", | |
"author": "James O'Sullivan" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment