core-api: remove FeatureFlagsRegistry class
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
|
||||
import { ApiRef, createApiRef } from '../system';
|
||||
import { UserFlags, FeatureFlagsRegistry } from '../../app/FeatureFlags';
|
||||
import { UserFlags } from '../../app/FeatureFlags';
|
||||
|
||||
/**
|
||||
* The feature flags API is used to toggle functionality to users across plugins and Backstage.
|
||||
@@ -46,15 +46,15 @@ export interface FeatureFlagsApi {
|
||||
*/
|
||||
registerFlag(flag: FeatureFlag): void;
|
||||
|
||||
/**
|
||||
* Get a list of all registered flags.
|
||||
*/
|
||||
getRegisteredFlags(): FeatureFlag[];
|
||||
|
||||
/**
|
||||
* Get a list of all feature flags from the current user.
|
||||
*/
|
||||
getFlags(): UserFlags;
|
||||
|
||||
/**
|
||||
* Get a list of all registered flags.
|
||||
*/
|
||||
getRegisteredFlags(): FeatureFlagsRegistry;
|
||||
}
|
||||
|
||||
export const featureFlagsApiRef: ApiRef<FeatureFlagsApi> = createApiRef({
|
||||
|
||||
@@ -141,11 +141,11 @@ describe('FeatureFlags', () => {
|
||||
|
||||
it('should return an empty list', () => {
|
||||
featureFlags = new FeatureFlagsImpl();
|
||||
expect(featureFlags.getRegisteredFlags().toObject()).toEqual([]);
|
||||
expect(featureFlags.getRegisteredFlags()).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return an valid list', () => {
|
||||
expect(featureFlags.getRegisteredFlags().toObject()).toMatchObject([
|
||||
expect(featureFlags.getRegisteredFlags()).toEqual([
|
||||
{ name: 'registered-flag-1', pluginId: 'plugin-one' },
|
||||
{ name: 'registered-flag-2', pluginId: 'plugin-one' },
|
||||
{ name: 'registered-flag-3', pluginId: 'plugin-two' },
|
||||
@@ -179,7 +179,7 @@ describe('FeatureFlags', () => {
|
||||
pluginId: 'plugin-three',
|
||||
});
|
||||
|
||||
expect(flags.toObject()).toMatchObject([
|
||||
expect(flags).toEqual([
|
||||
{ name: 'registered-flag-1', pluginId: 'plugin-one' },
|
||||
{ name: 'registered-flag-2', pluginId: 'plugin-one' },
|
||||
{ name: 'registered-flag-3', pluginId: 'plugin-two' },
|
||||
|
||||
@@ -126,45 +126,6 @@ export class UserFlags extends Map<string, FeatureFlagState> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The FeatureFlagsRegistry class.
|
||||
*
|
||||
* This acts as a holding data structure for feature flags
|
||||
* that plugins wish to register for use in Backstage.
|
||||
*/
|
||||
|
||||
export class FeatureFlagsRegistry extends Array<FeatureFlag> {
|
||||
static from(entries: FeatureFlag[]) {
|
||||
Array.from(entries).forEach(entry => validateFlagName(entry.name));
|
||||
return new FeatureFlagsRegistry(...entries);
|
||||
}
|
||||
|
||||
push(...entries: FeatureFlag[]): number {
|
||||
Array.from(entries).forEach(entry => validateFlagName(entry.name));
|
||||
return super.push(...entries);
|
||||
}
|
||||
|
||||
concat(
|
||||
...entries: (FeatureFlag | ConcatArray<FeatureFlag>)[]
|
||||
): FeatureFlag[] {
|
||||
const _concat = super.concat(...entries);
|
||||
Array.from(_concat).forEach(entry => validateFlagName(entry.name));
|
||||
return _concat;
|
||||
}
|
||||
|
||||
toObject() {
|
||||
return [...this.values()];
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
return JSON.stringify(this.toObject());
|
||||
}
|
||||
|
||||
toString() {
|
||||
return this.toJSON();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the FeatureFlags implementation based on the API.
|
||||
*/
|
||||
@@ -177,12 +138,12 @@ export class FeatureFlags implements FeatureFlagsApi {
|
||||
this.registeredFeatureFlags.push(flag);
|
||||
}
|
||||
|
||||
getRegisteredFlags(): FeatureFlag[] {
|
||||
return this.registeredFeatureFlags.slice();
|
||||
}
|
||||
|
||||
getFlags(): UserFlags {
|
||||
if (!this.userFlags) this.userFlags = UserFlags.load();
|
||||
return this.userFlags;
|
||||
}
|
||||
|
||||
getRegisteredFlags(): FeatureFlagsRegistry {
|
||||
return FeatureFlagsRegistry.from(this.registeredFeatureFlags);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user