angular-environment-config 
Hostname based environment configuration module for AngularJS
Contents
Get it
- Download the source (minified);
- Install using npm: run
$ npm i angular-environment-config
from your console; or
- Install using bower: run
$ bower install angular-environment-config
from your console
Use it
-
Add 'luminous.environment'
to your main module's list of dependencies
angular.module('myWebApp', ['luminous.environment']);
-
Configure your environments during config phase using the $appEnvironmentProvider
$appEnvironmentProvider
.addEnvironment('local', ['127.0.0.1', 'localhost', /\.local$/i], {});
-
Set default properties for all environments using $appEnvironmentProvider.setDefaults()
$appEnvironmentProvider
.setDefaults({ titlePrefix: '', apiUrl: '/api/' });
-
Access the readonly config variables for the current environment using the $appEnvironment
service via the config
property.
$document[0].title = $appEnvironment.config.titlePrefix + 'My App';
Stare intently at this example of it
angular.module('myWebApp', [
'luminous.environment',
]);
angular.module('myWebApp')
.config(myWebAppConfig)
.controller('MainViewController', MainViewController);
myWebAppConfig.$inject = ['$appEnvironmentProvider'];
function myWebAppConfig ( $appEnvironmentProvider){
$appEnvironmentProvider
.setDefaults({
titlePrefix: '',
apiUrl: '/api/',
})
.addEnvironment('local', ['127.0.0.1', 'localhost', /\.local$/i], {
titlePrefix: 'LOCAL :: ',
apiUrl: 'http://localhost:7331/',
})
.addEnvironment('production', /^(|www\.)my-app.com$/, {
apiUrl: 'https://api.my-app.com/',
})
.defaultEnvironmentName('local');
}
MainViewController.$inject = ['$appEnvironment', '$document'];
function MainViewController ( $appEnvironment, $document) {
$document[0].title = $appEnvironment.config.titlePrefix
+ 'My App - Powered by things, that do things!';
}
API
Provider
$appEnvironmentProvider
addEnvironment(environmentName, hostnames, config)
Adds a new environment config
-- identified by environmentName
-- that will be used when the Angular app's $location.host()
matches one of the String
s or RegExp
s provided in hostnames
.
$appEnvironmentProvider
.addEnvironment('local', ['127.0.0.1', 'localhost', /\.local$/i], {
titlePrefix: 'LOCAL :: ',
apiUrl: 'http://localhost:7331/',
})
.addEnvironment('testing', 'test.my-app.com', {
titlePrefix: 'TESTING :: ',
apiUrl: 'https://test-api.my-app.com/',
})
.addEnvironment('production', /^(www\.|)my-app.com$/i, {
apiUrl: 'https://api.my-app.com/',
});
Arguments
environmentName | String | The internal name for this environment (e.g. local) |
hostnames | String RegExp Array | Hostname String or RegExp or an Array of such, used to match against $location.host() . |
config | Object | The environment configuration, to be applied to a new AppEnvironmentConfig on top of any defaults specified via $appEnvironmentProvider.setDefault() . |
Returns
$appEnvironmentProvider
useConfigFor(environmentName).whenHostnameMatches(hostname)
Specify the environmentName
to use when the hostname matches hostname
.
$appEnvironmentProvider
.useConfigFor('testing')
.whenHostnameMatches(/^preview\.[a-z0-9-]{3,}\.my-app.com/i);
Arguments
environmentName | String | The internal name of the environment (e.g. local) |
hostname | String RegExp | Hostname String or RegExp for matching against $location.host() |
Returns
$appEnvironmentProvider
defaultEnvironmentName(environmentName)
Specify the environmentName
to fall back on when none of the specified environment hostnames match the current hostname.
$appEnvironmentProvider
.defaultEnvironmentName('local');
Important note: If a default environment name is not specified, and the current hostname does not match a configured host name, the $appEnvironment service will throw an EnvLookupError
during init.
Arguments
environmentName | String | The internal name of the environment to use when all else fails (e.g. local) |
Returns
$appEnvironmentProvider
setDefaults(properties)
Set multiple default properties
for $appEnvironment.config
at once.
$appEnvironmentProvider
.setDefaults({
titlePrefix: '',
apiUrl: '/api/',
});
Arguments
properties | Object | Default properties to add to $appEnvironment.config for all environments. |
Returns
$appEnvironmentProvider
setDefault(key, value)
Specify a default value
for key
in $appEnvironment.config
$appEnvironmentProvider
.setDefault('apiUrl', '/api/');
Arguments
key | String | The property name |
value | * | The default value |
Returns
$appEnvironmentProvider
Service
$appEnvironment
is(environmentName)
Determine if environmentName
is the name of the current environment.
if (!$appEnvironment.is('production')) {
}
Arguments
environmentName | String | The name of the environment |
Returns
Boolean
--- true
if the current environment name is environmentName
, otherwise false
.
environmentName
String
--- The name of the current environment
if ('testing' === $appEnvironment.environmentName) {
} else {
}
config
AppEnvironmentConfig
--- Configuration object for the current environment, values are read-only.
$document[0].title = $appEnvironment.config.titlePrefix +
'MyApp - Created by things, for things';
hostname
String
-- The hostname of the current environment.
if ('localhost' === $appEnvironment.hostname) {
}
isDefault
Boolean
--- true
if default environment configuration is being used, otherwise false
.
if (true === $appEnvironment.isDefault) {
}