Skip to content
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

feat: dependent features #520

Merged
merged 11 commits into from
Sep 21, 2023
30 changes: 27 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,32 @@ export default class UnleashClient extends EventEmitter {
}
}

isParentDependencySatisfied(feature: FeatureInterface | undefined, context: Context) {
sjaanus marked this conversation as resolved.
Show resolved Hide resolved
if (!feature?.name || !feature.dependencies?.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's with the name check here? Name should always be a non empty string, do I have the dumb or is the same as

if (!feature?.dependencies?.length) {
return true;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still passes the spec. not sure why I did the name check but it's not in the type anymore. I'm hallucinating :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

return true;
}

return feature.dependencies.every(parent => {
if (this.repository.getToggle(parent.feature)?.dependencies?.length) {
return false;
}

if (parent.enabled !== false) {
if (parent.variants?.length) {
return parent.variants.includes(this.getVariant(parent.feature, context).name);
}
return this.isEnabled(parent.feature, context, () => false);
}

return !this.isEnabled(parent.feature, context, () => false);
});
}

isEnabled(name: string, context: Context, fallback: Function): boolean {
const feature = this.repository.getToggle(name);
if(!this.isParentDependencySatisfied(feature, context)) {
sighphyre marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
const { enabled } = this.isFeatureEnabled(feature, context, fallback);
if (feature?.impressionData) {
this.emit(
Expand Down Expand Up @@ -92,10 +116,10 @@ export default class UnleashClient extends EventEmitter {
}

if (feature.strategies.length === 0) {
return { enabled: feature.enabled };
return { enabled: feature.enabled } as StrategyResult;
}

let strategyResult = { enabled: false };
let strategyResult: StrategyResult = { enabled: false };

feature.strategies?.some((strategySelector): boolean => {
const strategy = this.getStrategy(strategySelector.name);
Expand Down Expand Up @@ -185,7 +209,7 @@ export default class UnleashClient extends EventEmitter {
): VariantWithFeatureStatus {
const fallback = fallbackVariant || getDefaultVariant();

if (typeof feature === 'undefined') {
if (typeof feature === 'undefined' || !this.isParentDependencySatisfied(feature, context)) {
return { ...fallback, featureEnabled: false };
}

Expand Down
7 changes: 7 additions & 0 deletions src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { Segment } from './strategy/strategy';
// eslint-disable-next-line import/no-cycle
import { VariantDefinition } from './variant';

export interface Dependency {
feature: string;
variants?: string[];
enabled?: boolean;
}

export interface FeatureInterface {
name: string;
type: string;
Expand All @@ -13,6 +19,7 @@ export interface FeatureInterface {
impressionData: boolean;
strategies: StrategyTransportInterface[];
variants: VariantDefinition[];
dependencies?: Dependency[];
}

export interface ClientFeaturesResponse {
Expand Down