1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/util/flag-resolver.ts
Nuno Góis 7ce5b3de64
refactor: flag resolver should use stricter types (#2571)
Adding stricter types to `FlagResolver` can possibly help improve our DX
- Help us prevent errors like typos, guide us to correctly add a flag
when needed, and warn us of stray checks whenever we do a clean up at a
later stage.
2022-12-20 15:10:06 +00:00

40 lines
1.0 KiB
TypeScript

import {
IExperimentalOptions,
IExternalFlagResolver,
IFlagContext,
IFlags,
IFlagResolver,
IFlagKey,
} from '../types/experimental';
export default class FlagResolver implements IFlagResolver {
private experiments: IFlags;
private externalResolver: IExternalFlagResolver;
constructor(expOpt: IExperimentalOptions) {
this.experiments = expOpt.flags;
this.externalResolver = expOpt.externalResolver;
}
getAll(context?: IFlagContext): IFlags {
const flags: IFlags = { ...this.experiments };
Object.keys(flags).forEach((flagName: IFlagKey) => {
if (!this.experiments[flagName])
flags[flagName] = this.externalResolver.isEnabled(
flagName,
context,
);
});
return flags;
}
isEnabled(expName: IFlagKey, context?: IFlagContext): boolean {
if (this.experiments[expName]) {
return true;
}
return this.externalResolver.isEnabled(expName, context);
}
}