Skip to content

Commit a31cc03

Browse files
Merge pull request #231 from Azure/merge-main-to-preview
Merge main to preview
2 parents 7ead0bc + 2903f7e commit a31cc03

File tree

1 file changed

+83
-64
lines changed

1 file changed

+83
-64
lines changed

src/appConfigurationImpl.ts

Lines changed: 83 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
isFeatureFlag,
1313
isSecretReference,
1414
GetSnapshotOptions,
15+
ListConfigurationSettingsForSnapshotOptions,
1516
GetSnapshotResponse,
1617
KnownSnapshotComposition
1718
} from "@azure/app-configuration";
@@ -490,71 +491,50 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
490491
*/
491492
async #loadConfigurationSettings(loadFeatureFlag: boolean = false): Promise<ConfigurationSetting[]> {
492493
const selectors = loadFeatureFlag ? this.#ffSelectors : this.#kvSelectors;
493-
const funcToExecute = async (client) => {
494-
// Use a Map to deduplicate configuration settings by key. When multiple selectors return settings with the same key,
495-
// the configuration setting loaded by the later selector in the iteration order will override the one from the earlier selector.
496-
const loadedSettings: Map<string, ConfigurationSetting> = new Map<string, ConfigurationSetting>();
497-
// deep copy selectors to avoid modification if current client fails
498-
const selectorsToUpdate: PagedSettingsWatcher[] = JSON.parse(
499-
JSON.stringify(selectors)
500-
);
501494

502-
for (const selector of selectorsToUpdate) {
503-
if (selector.snapshotName === undefined) {
504-
const listOptions: ListConfigurationSettingsOptions = {
505-
keyFilter: selector.keyFilter,
506-
labelFilter: selector.labelFilter,
507-
tagsFilter: selector.tagFilters
508-
};
509-
const pageWatchers: SettingWatcher[] = [];
510-
const pageIterator = listConfigurationSettingsWithTrace(
511-
this.#requestTraceOptions,
512-
client,
513-
listOptions
514-
).byPage();
515-
516-
for await (const page of pageIterator) {
517-
pageWatchers.push({ etag: page.etag });
518-
for (const setting of page.items) {
519-
if (loadFeatureFlag === isFeatureFlag(setting)) {
520-
loadedSettings.set(setting.key, setting);
521-
}
522-
}
523-
}
524-
selector.pageWatchers = pageWatchers;
525-
} else { // snapshot selector
526-
const snapshot = await this.#getSnapshot(selector.snapshotName);
527-
if (snapshot === undefined) {
528-
throw new InvalidOperationError(`Could not find snapshot with name ${selector.snapshotName}.`);
529-
}
530-
if (snapshot.compositionType != KnownSnapshotComposition.Key) {
531-
throw new InvalidOperationError(`Composition type for the selected snapshot with name ${selector.snapshotName} must be 'key'.`);
532-
}
533-
const pageIterator = listConfigurationSettingsForSnapshotWithTrace(
534-
this.#requestTraceOptions,
535-
client,
536-
selector.snapshotName
537-
).byPage();
538-
539-
for await (const page of pageIterator) {
540-
for (const setting of page.items) {
541-
if (loadFeatureFlag === isFeatureFlag(setting)) {
542-
loadedSettings.set(setting.key, setting);
543-
}
544-
}
545-
}
495+
// Use a Map to deduplicate configuration settings by key. When multiple selectors return settings with the same key,
496+
// the configuration setting loaded by the later selector in the iteration order will override the one from the earlier selector.
497+
const loadedSettings: Map<string, ConfigurationSetting> = new Map<string, ConfigurationSetting>();
498+
// deep copy selectors to avoid modification if current client fails
499+
const selectorsToUpdate: PagedSettingsWatcher[] = JSON.parse(
500+
JSON.stringify(selectors)
501+
);
502+
503+
for (const selector of selectorsToUpdate) {
504+
let settings: ConfigurationSetting[] = [];
505+
if (selector.snapshotName === undefined) {
506+
const listOptions: ListConfigurationSettingsOptions = {
507+
keyFilter: selector.keyFilter,
508+
labelFilter: selector.labelFilter,
509+
tagsFilter: selector.tagFilters
510+
};
511+
const { items, pageWatchers } = await this.#listConfigurationSettings(listOptions);
512+
selector.pageWatchers = pageWatchers;
513+
settings = items;
514+
} else { // snapshot selector
515+
const snapshot = await this.#getSnapshot(selector.snapshotName);
516+
if (snapshot === undefined) {
517+
throw new InvalidOperationError(`Could not find snapshot with name ${selector.snapshotName}.`);
518+
}
519+
if (snapshot.compositionType != KnownSnapshotComposition.Key) {
520+
throw new InvalidOperationError(`Composition type for the selected snapshot with name ${selector.snapshotName} must be 'key'.`);
546521
}
522+
settings = await this.#listConfigurationSettingsForSnapshot(selector.snapshotName);
547523
}
548524

549-
if (loadFeatureFlag) {
550-
this.#ffSelectors = selectorsToUpdate;
551-
} else {
552-
this.#kvSelectors = selectorsToUpdate;
525+
for (const setting of settings) {
526+
if (loadFeatureFlag === isFeatureFlag(setting)) {
527+
loadedSettings.set(setting.key, setting);
528+
}
553529
}
554-
return Array.from(loadedSettings.values());
555-
};
530+
}
556531

557-
return await this.#executeWithFailoverPolicy(funcToExecute) as ConfigurationSetting[];
532+
if (loadFeatureFlag) {
533+
this.#ffSelectors = selectorsToUpdate;
534+
} else {
535+
this.#kvSelectors = selectorsToUpdate;
536+
}
537+
return Array.from(loadedSettings.values());
558538
}
559539

560540
/**
@@ -762,13 +742,13 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
762742
/**
763743
* Gets a configuration setting by key and label.If the setting is not found, return undefine instead of throwing an error.
764744
*/
765-
async #getConfigurationSetting(configurationSettingId: ConfigurationSettingId, customOptions?: GetConfigurationSettingOptions): Promise<GetConfigurationSettingResponse | undefined> {
745+
async #getConfigurationSetting(configurationSettingId: ConfigurationSettingId, getOptions?: GetConfigurationSettingOptions): Promise<GetConfigurationSettingResponse | undefined> {
766746
const funcToExecute = async (client) => {
767747
return getConfigurationSettingWithTrace(
768748
this.#requestTraceOptions,
769749
client,
770750
configurationSettingId,
771-
customOptions
751+
getOptions
772752
);
773753
};
774754

@@ -785,13 +765,33 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
785765
return response;
786766
}
787767

788-
async #getSnapshot(snapshotName: string, customOptions?: GetSnapshotOptions): Promise<GetSnapshotResponse | undefined> {
768+
async #listConfigurationSettings(listOptions: ListConfigurationSettingsOptions): Promise<{ items: ConfigurationSetting[]; pageWatchers: SettingWatcher[] }> {
769+
const funcToExecute = async (client) => {
770+
const pageWatchers: SettingWatcher[] = [];
771+
const pageIterator = listConfigurationSettingsWithTrace(
772+
this.#requestTraceOptions,
773+
client,
774+
listOptions
775+
).byPage();
776+
777+
const items: ConfigurationSetting[] = [];
778+
for await (const page of pageIterator) {
779+
pageWatchers.push({ etag: page.etag });
780+
items.push(...page.items);
781+
}
782+
return { items, pageWatchers };
783+
};
784+
785+
return await this.#executeWithFailoverPolicy(funcToExecute);
786+
}
787+
788+
async #getSnapshot(snapshotName: string, getOptions?: GetSnapshotOptions): Promise<GetSnapshotResponse | undefined> {
789789
const funcToExecute = async (client) => {
790790
return getSnapshotWithTrace(
791791
this.#requestTraceOptions,
792792
client,
793793
snapshotName,
794-
customOptions
794+
getOptions
795795
);
796796
};
797797

@@ -808,6 +808,25 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
808808
return response;
809809
}
810810

811+
async #listConfigurationSettingsForSnapshot(snapshotName: string, listOptions?: ListConfigurationSettingsForSnapshotOptions): Promise<ConfigurationSetting[]> {
812+
const funcToExecute = async (client) => {
813+
const pageIterator = listConfigurationSettingsForSnapshotWithTrace(
814+
this.#requestTraceOptions,
815+
client,
816+
snapshotName,
817+
listOptions
818+
).byPage();
819+
820+
const items: ConfigurationSetting[] = [];
821+
for await (const page of pageIterator) {
822+
items.push(...page.items);
823+
}
824+
return items;
825+
};
826+
827+
return await this.#executeWithFailoverPolicy(funcToExecute);
828+
}
829+
811830
// Only operations related to Azure App Configuration should be executed with failover policy.
812831
async #executeWithFailoverPolicy(funcToExecute: (client: AppConfigurationClient) => Promise<any>): Promise<any> {
813832
let clientWrappers = await this.#clientManager.getClients();
@@ -883,7 +902,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
883902
#setAIConfigurationTracing(setting: ConfigurationSetting<string>): void {
884903
if (this.#requestTracingEnabled && this.#aiConfigurationTracing !== undefined) {
885904
const contentType = parseContentType(setting.contentType);
886-
// content type: "application/json; profile=\"https://azconfig.io/mime-profiles/ai\"""
905+
// content type: "application/json; profile=\"https://azconfig.io/mime-profiles/ai\""
887906
if (isJsonContentType(contentType) &&
888907
!isFeatureFlagContentType(contentType) &&
889908
!isSecretReferenceContentType(contentType)) {

0 commit comments

Comments
 (0)