-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Support for query params #40
Comments
What would be really neat is to support integration with ember inspector so you can turn features on and off right from the chrome inspector. |
I think this would be appropriate as an addon to this addon. Getting the flags into the Ember Inspector is something I'm interested in doing. Originally, when the flags were just an Ember object you could change them in there, but this undocumented functionality was lost via PR. |
A simple instance initializer that enables this feature can look like:
where |
We just implemented an augmentation/extension for the This way teams can have a dedicated way to initialize and persist FFs with QPs, dynamic HTML Head, server request, etc. This is our extended import Features from 'ember-feature-flags/services/features';
import config from '../config/environment';
import { inject as service } from '@ember/service';
const FEATURE_QP_REGEX = /^feature\[(.*)\]/;
export default class FeaturesService extends Features {
config = config;
@service('router') router;
init() {
super.init(...arguments);
if (config.featureFlags) {
let featureFlags = {
...config.featureFlags,
...this.sessionFeatureFlags,
...this.initQueryFeatureFlags(),
};
this.setup(featureFlags);
}
}
get sessionFeatureFlags() {
return JSON.parse(sessionStorage.featureFlags ?? '{}');
}
set sessionFeatureFlags(value = {}) {
sessionStorage.featureFlags = JSON.stringify(value);
}
initQueryFeatureFlags() {
let featureFlags = {};
const params = new URLSearchParams(window.location.search);
// Search query params for feature flags
params.forEach((value, key) => {
const matches = key.match(FEATURE_QP_REGEX);
if (matches) {
let [, flag] = matches;
featureFlags[flag] = value !== 'false' ? true : false;
}
});
return featureFlags;
}
persistFeatureFlag(flag, value) {
let sessionFeatureFlags = this.sessionFeatureFlags;
if (config.featureFlags[flag] == value) {
delete sessionFeatureFlags[flag];
} else {
sessionFeatureFlags[flag] = value;
}
this.sessionFeatureFlags = sessionFeatureFlags;
}
enable(flag) {
super.enable(flag);
this.persistFeatureFlag(flag, true);
}
disable(flag) {
super.disable(flag);
this.persistFeatureFlag(flag, false);
}
} But, I think that there could be an API using initializers to have an app initializer like and the above implementation would become: import { featureFlagInitializer } from 'ember-feature-flags';
import config from '../../config/environment';
const getSessionFeatureFlags = () => JSON.parse(sessionStorage.featureFlags ?? '{}');
const getQueryFeatureFlags = (featureFlags) => {
let featureFlags = {};
const params = new URLSearchParams(window.location.search);
// Search query params for feature flags
params.forEach((value, key) => {
const matches = key.match(FEATURE_QP_REGEX);
if (matches) {
let [, flag] = matches;
featureFlags[flag] = value !== 'false' ? true : false;
}
});
return featureFlags;
}
export const initialize = featureFlagInitializer((featuresService, appInstanace) => {
// New function that is called and passed to `setup`
featuresService.addFeatureFlagInitializer((featureFlags) => {
return {
...featureFlags,
...getSessionFeatureFlags(),
...getQueryFeatureFlags(featureFlags),
}
});
featuresService.on('toggleFeature', (flag, value) => {
let sessionFeatureFlags = getSessionFeatureFlags();
if (config.featureFlags[flag] == value) {
delete sessionFeatureFlags[flag];
} else {
sessionFeatureFlags[flag] = value;
}
sessionStorage.featureFlags = JSON.stringify(sessionFeatureFlags);
});
});
export default {
name: 'feature-flags',
initialize
}; The change would be adding some initialization hook and then using |
Thanks for sharing your solution. I wouldn't recommend using |
It would be useful in my app to have feature flags which are switched off but can be switched on via a query param. This would allow specific users to test a feature before it is enabled for all users.
For example
www.app.com?enabledFeatures=new-billing,new-settings
would turn on thenew-billing
andnew-settings
flags.More details:
enabledFeatures=
but can be configured. (I don't think this could be calledfeatures=
because this would have to be a property on the controller and it might conflict with the service).In the future we could also consider:
Please let me know if you think this is a good idea. I'd be happy to submit a PR.
The text was updated successfully, but these errors were encountered: