Skip to content

Commit 7e7c18b

Browse files
authored
provide separate built-in providers for object and map
1 parent 98006f2 commit 7e7c18b

File tree

3 files changed

+29
-18
lines changed

3 files changed

+29
-18
lines changed

src/featureProvider.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,33 @@ export interface IFeatureProvider {
55
getFeatureFlags(): Promise<FeatureDefinition[]>;
66
}
77

8-
export class ConfigurationFeatureProvider implements IFeatureProvider {
9-
#configuration: IGettable | Record<string, unknown>;
8+
/**
9+
* TBD
10+
*/
11+
export class ConfigurationMapFeatureProvider implements IFeatureProvider {
12+
#configuration: IGettable;
1013

11-
constructor(configuration: Record<string, unknown> | IGettable) {
12-
if (typeof configuration !== "object") {
13-
throw new Error("Configuration must be an object.");
14-
}
14+
constructor(configuration: IGettable) {
1515
this.#configuration = configuration;
1616
}
1717

1818
async getFeatureFlags(): Promise<FeatureDefinition[]> {
19-
if (isGettable(this.#configuration)) {
20-
const featureConfig = this.#configuration.get<FeatureConfiguration>(FEATURE_MANAGEMENT_KEY);
21-
return featureConfig?.[FEATURE_FLAGS_KEY] ?? [];
22-
} else {
23-
return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? [];
24-
}
19+
const featureConfig = this.#configuration.get<FeatureConfiguration>(FEATURE_MANAGEMENT_KEY);
20+
return featureConfig?.[FEATURE_FLAGS_KEY] ?? [];
2521
}
2622
}
23+
24+
/**
25+
* TBD
26+
*/
27+
export class ConfigurationObjectFeatureProvider implements IFeatureProvider {
28+
#configuration: Record<string, unknown>;
29+
30+
constructor(configuration: Record<string, unknown> ) {
31+
this.#configuration = configuration;
32+
}
33+
34+
async getFeatureFlags(): Promise<FeatureDefinition[]> {
35+
return this.#configuration[FEATURE_MANAGEMENT_KEY]?.[FEATURE_FLAGS_KEY] ?? [];
36+
}
37+
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
// Licensed under the MIT license.
33

44
export { FeatureManager } from "./featureManager";
5-
export { ConfigurationFeatureProvider, IFeatureProvider } from "./featureProvider";
5+
export { ConfigurationMapFeatureProvider, ConfigurationObjectFeatureProvider, IFeatureProvider } from "./featureProvider";

test/featureManager.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as chaiAsPromised from "chai-as-promised";
66
chai.use(chaiAsPromised);
77
const expect = chai.expect;
88

9-
import { FeatureManager, ConfigurationFeatureProvider } from "./exportedApi";
9+
import { FeatureManager, ConfigurationMapFeatureProvider, ConfigurationObjectFeatureProvider } from "./exportedApi";
1010

1111
describe("feature manager", () => {
1212
it("should load from json string", () => {
@@ -18,7 +18,7 @@ describe("feature manager", () => {
1818
}
1919
};
2020

21-
const provider = new ConfigurationFeatureProvider(jsonObject);
21+
const provider = new ConfigurationObjectFeatureProvider(jsonObject);
2222
const featureManager = new FeatureManager(provider);
2323
return expect(featureManager.isEnabled("Alpha")).eventually.eq(true);
2424
});
@@ -31,7 +31,7 @@ describe("feature manager", () => {
3131
],
3232
});
3333

34-
const provider = new ConfigurationFeatureProvider(dataSource);
34+
const provider = new ConfigurationMapFeatureProvider(dataSource);
3535
const featureManager = new FeatureManager(provider);
3636
return expect(featureManager.isEnabled("Alpha")).eventually.eq(true);
3737
});
@@ -44,7 +44,7 @@ describe("feature manager", () => {
4444
],
4545
});
4646

47-
const provider = new ConfigurationFeatureProvider(dataSource);
47+
const provider = new ConfigurationMapFeatureProvider(dataSource);
4848
const featureManager = new FeatureManager(provider);
4949
dataSource.set("FeatureManagement", {
5050
FeatureFlags: [
@@ -64,7 +64,7 @@ describe("feature manager", () => {
6464
],
6565
});
6666

67-
const provider = new ConfigurationFeatureProvider(dataSource);
67+
const provider = new ConfigurationMapFeatureProvider(dataSource);
6868
const featureManager = new FeatureManager(provider);
6969
return Promise.all([
7070
expect(featureManager.isEnabled("Alpha")).eventually.eq(true),

0 commit comments

Comments
 (0)