@@ -82,6 +82,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
8282 #featureFlagTracing: FeatureFlagTracingOptions | undefined ;
8383 #fmVersion: string | undefined ;
8484 #aiConfigurationTracing: AIConfigurationTracingOptions | undefined ;
85+ #useSnapshotReference: boolean = false ;
8586
8687 // Refresh
8788 #refreshInProgress: boolean = false ;
@@ -213,7 +214,8 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
213214 isFailoverRequest : this . #isFailoverRequest,
214215 featureFlagTracing : this . #featureFlagTracing,
215216 fmVersion : this . #fmVersion,
216- aiConfigurationTracing : this . #aiConfigurationTracing
217+ aiConfigurationTracing : this . #aiConfigurationTracing,
218+ useSnapshotReference : this . #useSnapshotReference
217219 } ;
218220 }
219221
@@ -504,17 +506,26 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
504506 selector . pageWatchers = pageWatchers ;
505507 settings = items ;
506508 } else { // snapshot selector
507- const snapshot = await this . #getSnapshot( selector . snapshotName ) ;
508- if ( snapshot === undefined ) {
509- throw new InvalidOperationError ( `Could not find snapshot with name ${ selector . snapshotName } .` ) ;
510- }
511- if ( snapshot . compositionType != KnownSnapshotComposition . Key ) {
512- throw new InvalidOperationError ( `Composition type for the selected snapshot with name ${ selector . snapshotName } must be 'key'.` ) ;
513- }
514- settings = await this . #listConfigurationSettingsForSnapshot( selector . snapshotName ) ;
509+ settings = await this . #loadConfigurationSettingsFromSnapshot( selector . snapshotName ) ;
515510 }
516511
517512 for ( const setting of settings ) {
513+ if ( isSnapshotReference ( setting ) && ! loadFeatureFlag ) {
514+ this . #useSnapshotReference = true ;
515+
516+ // TODO: When SDK supports snapshot reference, use the helper method from SDK.
517+ const snapshotName = parseSnapshotReference ( setting ) . value . snapshotName ;
518+ const settingsFromSnapshot = await this . #loadConfigurationSettingsFromSnapshot( snapshotName ) ;
519+
520+ for ( const snapshotSetting of settingsFromSnapshot ) {
521+ if ( ! isFeatureFlag ( snapshotSetting ) ) {
522+ // Feature flags inside snapshot are ignored. This is consistent the behavior that key value selectors ignore feature flags.
523+ loadedSettings . set ( snapshotSetting . key , snapshotSetting ) ;
524+ }
525+ }
526+ continue ;
527+ }
528+
518529 if ( loadFeatureFlag === isFeatureFlag ( setting ) ) {
519530 loadedSettings . set ( setting . key , setting ) ;
520531 }
@@ -575,6 +586,18 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
575586 }
576587 }
577588
589+ async #loadConfigurationSettingsFromSnapshot( snapshotName : string ) : Promise < ConfigurationSetting [ ] > {
590+ const snapshot = await this . #getSnapshot( snapshotName ) ;
591+ if ( snapshot === undefined ) {
592+ throw new InvalidOperationError ( `Could not find snapshot with name ${ snapshotName } .` ) ;
593+ }
594+ if ( snapshot . compositionType != KnownSnapshotComposition . Key ) {
595+ throw new InvalidOperationError ( `Composition type for the selected snapshot with name ${ snapshotName } must be 'key'.` ) ;
596+ }
597+ const settings : ConfigurationSetting [ ] = await this . #listConfigurationSettingsForSnapshot( snapshotName ) ;
598+ return settings ;
599+ }
600+
578601 /**
579602 * Clears all existing key-values in the local configuration except feature flags.
580603 */
@@ -1068,3 +1091,28 @@ function validateTagFilters(tagFilters: string[]): void {
10681091 }
10691092 }
10701093}
1094+
1095+ // TODO: Temporary workaround until SDK supports snapshot reference
1096+ const snapshotReferenceContentType = "application/json; profile=\"https://azconfig.io/mime-profiles/snapshot-ref\"; charset=utf-8" ;
1097+
1098+ interface JsonSnapshotReferenceValue {
1099+ snapshot_name : string ;
1100+ }
1101+
1102+ function isSnapshotReference ( setting : ConfigurationSetting ) :
1103+ setting is ConfigurationSetting & Required < Pick < ConfigurationSetting , "value" > > {
1104+ return ( setting && setting . contentType === snapshotReferenceContentType && typeof setting . value === "string" ) ;
1105+ }
1106+
1107+ function parseSnapshotReference ( setting : ConfigurationSetting ) {
1108+ if ( ! isSnapshotReference ( setting ) ) {
1109+ throw new Error ( `Invalid snapshot reference: ${ setting } ` ) ;
1110+ }
1111+ const jsonSnapshotReferenceValue = JSON . parse ( setting . value ) as JsonSnapshotReferenceValue ;
1112+
1113+ const snapshotReference = {
1114+ ...setting ,
1115+ value : { snapshotName : jsonSnapshotReferenceValue . snapshot_name } ,
1116+ } ;
1117+ return snapshotReference ;
1118+ }
0 commit comments