API based, read-only feature flags for Ember. To install:
ember install ember-api-feature-flags
ember-api-feature-flags
installs a service into your app. This service will let you fetch your feature flag data from a specified URL.
For example, call fetchFeatures
in your application route:
// application/route.js
import Ember from 'ember';
const { inject: { Service }, Route } = Ember;
export default Route.extend({
featureFlags: service(),
beforeModel() {
this.get('featureFlags')
.fetchFeatures()
.then((data) => featureFlags.receiveData(data))
.catch((reason) => featureFlags.receiveError(reason));
}
});
Once fetched, you can then easily check if a given feature is enabled/disabled in both Handlebars:
...and JavaScript:
import Ember from 'ember';
const {
Component,
inject: { service },
get
} = Ember;
export default Component.extend({
featureFlags: service(),
actions: {
save() {
let isDisabled = get(this, 'featureFlags.myFeature.isDisabled');
if (isDisabled) {
return;
}
// stuff
}
}
});
When the fetch fails, the service enters "error" mode. In this mode, feature flag lookups via HBS or JS will still function as normal. However, they will always return the default value set in the config (which defaults to false
). You can set this to true
if you want all features to be enabled in event of fetch failure.
To configure, add to your config/environment.js
:
/* eslint-env node */
module.exports = function(environment) {
var ENV = {
'ember-api-feature-flags': {
featureUrl: 'https://www.example.com/api/v1/features',
featureKey: 'feature_key',
enabledKey: 'value',
shouldMemoize: true,
defaultValue: false
}
}
return ENV;
featureUrl
must be defined, or ember-api-feature-flags
will not be able to fetch feature flag data from your API.
For example, call fetchFeatures
in your application route:
// application/route.js
import Ember from 'ember';
const { inject: { Service }, Route } = Ember;
export default Route.extend({
featureFlags: service(),
beforeModel() {
this.get('featureFlags')
.fetchFeatures()
.then((data) => featureFlags.receiveData(data))
.catch((reason) => featureFlags.receiveError(reason));
}
});
In the following example, the application uses ember-simple-auth
, and the authenticated
data includes the user's email
and token
:
import Ember from 'ember';
import Session from 'ember-simple-auth/services/session';
const {
inject: { service },
get
} = Ember;
export default Session.extend({
featureFlags: service(),
// call this function after the session is authenticated
fetchFeatureFlags() {
let featureFlags = get(this, 'featureFlags');
let { authenticated: { email, token } } = get(this, 'data');
let headers = { Authorization: `Token token=${token}, email=${email}`};
featureFlags
.fetchFeatures({ headers })
.then((data) => featureFlags.receiveData(data))
.catch((reason) => featureFlags.receiveError(reason));
}
Required. The URL where your API returns feature flag data. You can change this per environment in config/environment.js
:
if (environment === 'canary') {
ENV['ember-api-feature-flags'].featureUrl = 'https://www.example.com/api/v1/features';
}
This key is the key on your feature flag data object yielding the feature's name. In other words, this key's value determines what you will use to access your feature flag (e.g. this.get('featureFlags.newProfilePage.isEnabled')
):
// example feature flag data object
{
"id": 26,
"feature_key": "new_profile_page", // <-
"key": "boolean",
"value": "true",
"created_at": "2017-03-22T03:30:10.270Z",
"updated_at": "2017-03-22T03:30:10.270Z"
}
The value on this key will be normalized by the normalizeKey
method.
This determines which key to pick off of the feature flag data object. This value is then used by the FeatureFlag
object (a wrapper around the single feature flag) when determining if a feature flag is enabled.
// example feature flag data object
{
"id": 26,
"feature_key": "new_profile_page",
"key": "boolean",
"value": "true", // <-
"created_at": "2017-03-22T03:30:10.270Z",
"updated_at": "2017-03-22T03:30:10.270Z"
}
By default, the service will instantiate and cache FeatureFlag
objects. Set this to false
to disable.
If the service is in error mode, all feature flag lookups will return this value as their isEnabled
value.
- Properties
- Methods
Returns a boolean value that represents the success state of fetching data from your API. If the GET request fails, this will be false
and the service will be set to "error" mode. In error mode, all feature flags will return the default value as the value for isEnabled
.
A computed property that represents the normalized feature flag data.
let data = service.get('data');
/**
{
"newProfilePage": { value: "true" },
"newFriendList": { value: "true" }
}
**/
Configure the service. You can use this method to change service options at runtime. Acceptable options are the same as in the configuration section.
service.configure({
featureUrl: 'http://www.example.com/features',
featureKey: 'feature_key',
enabledKey: 'value',
shouldMemoize: true,
defaultValue: false
});
Performs the GET request to the specified URL, with optional headers to be passed to ember-ajax
. Returns a Promise.
service.fetchFeatures().then((data) => doStuff(data));
service.fetchFeatures({ headers: /* ... */}).then((data) => doStuff(data));
Receive data from API and set internal properties. If data is blank, we set the service in error mode.
service.receiveData([
{
"id": 26,
"feature_key": "new_profile_page",
"key": "boolean",
"value": "true",
"created_at": "2017-03-22T03:30:10.270Z",
"updated_at": "2017-03-22T03:30:10.270Z"
},
{
"id": 27,
"feature_key": "new_friend_list",
"key": "boolean",
"value": "true",
"created_at": "2017-03-22T03:30:10.287Z",
"updated_at": "2017-03-22T03:30:10.287Z"
}
]);
service.get('data') // normalized data
Set service in errored state. Records failure reason as a side effect.
service.receiveError('Something went wrong');
service.get('didFetchData', false);
service.get('error', 'Something went wrong');
Normalizes keys. Defaults to camelCase.
service.normalizeKey('new_profile_page'); // "newProfilePage"
Fetches the feature flag. Use in conjunction with isEnabled
or isDisabled
on the feature flag.
service.get('newProfilePage.isEnabled'); // true
service.get('newFriendList.isEnabled'); // true
service.get('oldProfilePage.isDisabled'); // true
Sets the service in testing mode. This is useful when writing acceptance/integration tests in your application as you don't need to intercept the request to your API. When the service is in testing mode, all features are enabled.
service.setupForTesting();
service.get('newFriendList.isEnabled'); // true
git clone <repository-url>
this repositorycd ember-api-feature-flags
npm install
bower install
ember serve
- Visit your app at http://localhost:4200.
npm test
(Runsember try:each
to test your addon against multiple Ember versions)ember test
ember test --server
ember build
For more information on using ember-cli, visit https://ember-cli.com/.