Skip to content

Commit 923c124

Browse files
committed
throw error if selectors not provided
1 parent 3cd5f59 commit 923c124

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/AzureAppConfigurationImpl.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
159159
const loadedSettings: ConfigurationSetting[] = [];
160160

161161
// validate selectors
162-
const selectors = getValidSelectors(this.#options?.selectors);
162+
const selectors = getValidKeyValueSelectors(this.#options?.selectors);
163163

164164
for (const selector of selectors) {
165165
const listOptions: ListConfigurationSettingsOptions = {
@@ -240,7 +240,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
240240

241241
async #loadFeatureFlags() {
242242
const featureFlags: FeatureFlagValue[] = [];
243-
const featureFlagSelectors = getValidSelectors(this.#options?.featureFlagOptions?.selectors);
243+
const featureFlagSelectors = getValidFeatureFlagSelectors(this.#options?.featureFlagOptions?.selectors);
244244
for (const selector of featureFlagSelectors) {
245245
const listOptions: ListConfigurationSettingsOptions = {
246246
keyFilter: `${featureFlagPrefix}${selector.keyFilter}`,
@@ -465,12 +465,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
465465
}
466466
}
467467

468-
function getValidSelectors(selectors?: SettingSelector[]) {
469-
if (!selectors || selectors.length === 0) {
470-
// Default selector: key: *, label: \0
471-
return [{ keyFilter: KeyFilter.Any, labelFilter: LabelFilter.Null }];
472-
}
473-
468+
function validateSelectors(selectors: SettingSelector[]) {
474469
// below code deduplicates selectors by keyFilter and labelFilter, the latter selector wins
475470
const uniqueSelectors: SettingSelector[] = [];
476471
for (const selector of selectors) {
@@ -495,3 +490,20 @@ function getValidSelectors(selectors?: SettingSelector[]) {
495490
return selector;
496491
});
497492
}
493+
494+
function getValidKeyValueSelectors(selectors?: SettingSelector[]) {
495+
if (!selectors || selectors.length === 0) {
496+
// Default selector: key: *, label: \0
497+
return [{ keyFilter: KeyFilter.Any, labelFilter: LabelFilter.Null }];
498+
}
499+
return validateSelectors(selectors);
500+
}
501+
502+
function getValidFeatureFlagSelectors(selectors?: SettingSelector[]) {
503+
if (!selectors || selectors.length === 0) {
504+
// selectors must be explicitly provided.
505+
throw new Error("Feature flag selectors must be provided.");
506+
} else {
507+
return validateSelectors(selectors);
508+
}
509+
}

test/featureFlag.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ describe("feature flags", function () {
3131
const connectionString = createMockedConnectionString();
3232
const settings = await load(connectionString, {
3333
featureFlagOptions: {
34-
enabled: true
34+
enabled: true,
35+
selectors: [{
36+
keyFilter: "*"
37+
}]
3538
}
3639
});
3740
expect(settings).not.undefined;
@@ -50,13 +53,22 @@ describe("feature flags", function () {
5053
expect(settings.get("feature_management")).undefined;
5154
});
5255

53-
it("should not load feature flags if not specified", async () => {
56+
it("should not load feature flags if featureFlagOptions not specified", async () => {
5457
const connectionString = createMockedConnectionString();
5558
const settings = await load(connectionString);
5659
expect(settings).not.undefined;
5760
expect(settings.get("feature_management")).undefined;
5861
});
5962

63+
it("should throw error if selectors not specified", async () => {
64+
const connectionString = createMockedConnectionString();
65+
return expect(load(connectionString, {
66+
featureFlagOptions: {
67+
enabled: true
68+
}
69+
})).eventually.rejectedWith("Feature flag selectors must be provided.");
70+
});
71+
6072
it("should load feature flags with custom selector", async () => {
6173
const connectionString = createMockedConnectionString();
6274
const settings = await load(connectionString, {

0 commit comments

Comments
 (0)