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
38 changes: 34 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,38 @@ export default class UnleashClient extends EventEmitter {
}
}

isParentDependencySatisfied(feature: FeatureInterface | undefined, context: Context) {
sjaanus marked this conversation as resolved.
Show resolved Hide resolved
if (!feature?.dependencies?.length) {
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);
const { enabled } = this.isFeatureEnabled(feature, context, fallback);
let enabled: boolean;

if (!this.isParentDependencySatisfied(feature, context)) {
enabled = false;
} else {
enabled = this.isFeatureEnabled(feature, context, fallback).enabled;
}

// Emit the impression event if needed
if (feature?.impressionData) {
this.emit(
UnleashEvents.Impression,
Expand All @@ -69,6 +98,7 @@ export default class UnleashClient extends EventEmitter {
}),
);
}

return enabled;
}

Expand All @@ -92,10 +122,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 +215,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
22 changes: 22 additions & 0 deletions src/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,28 @@ test('should trigger events on isEnabled if impressionData is true', (t) => {

});

test('should trigger events on unsatisfied dependency if impressionData is true', (t) => {
let called = false;
const repo = {
getToggle() {
return {
name: 'feature-x',
dependencies: [{feature: 'irrelevant'}],
strategies: [{ name: 'default' }],
variants: [],
impressionData: true,
}
},
};
const client = new Client(repo, []);
client.on(UnleashEvents.Impression, () => {
called = true;
});
client.isEnabled('feature-x', {}, () => false);
t.true(called);

});

test('should trigger events on getVariant if impressionData is true', (t) => {
let called = false;
const repo = {
Expand Down