|
1 | 1 | // Copyright (c) Microsoft Corporation. |
2 | 2 | // Licensed under the MIT license. |
3 | 3 |
|
| 4 | +import { OperationOptions } from "@azure/core-client"; |
4 | 5 | import { |
5 | 6 | AppConfigurationClient, |
6 | 7 | ConfigurationSetting, |
@@ -516,19 +517,17 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration { |
516 | 517 | ); |
517 | 518 | for (const selector of selectorsToUpdate) { |
518 | 519 | if (selector.snapshotName === undefined) { |
519 | | - let listOptions: ListConfigurationSettingsOptions = { |
| 520 | + const listOptions: ListConfigurationSettingsOptions = { |
520 | 521 | keyFilter: selector.keyFilter, |
521 | 522 | labelFilter: selector.labelFilter, |
522 | 523 | tagsFilter: selector.tagFilters |
523 | 524 | }; |
524 | 525 |
|
525 | 526 | // If CDN is used, add etag to request header so that the pipeline policy can retrieve and append it to the request URL |
526 | 527 | if (this.#isCdnUsed && selectorCollection.cdnToken) { |
527 | | - listOptions = { |
528 | | - ...listOptions, |
529 | | - requestOptions: { customHeaders: { [CDN_TOKEN_LOOKUP_HEADER]: selectorCollection.cdnToken }} |
530 | | - }; |
| 528 | + this.#addCdnTokenLookupHeader(listOptions, selectorCollection.cdnToken); |
531 | 529 | } |
| 530 | + |
532 | 531 | const pageEtags: string[] = []; |
533 | 532 | const pageIterator = listConfigurationSettingsWithTrace( |
534 | 533 | this.#requestTraceOptions, |
@@ -631,10 +630,9 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration { |
631 | 630 | sentinel.etag = loaded.etag; |
632 | 631 | } else { |
633 | 632 | // Send a request to retrieve watched key-value since it may be either not loaded or loaded with a different selector |
634 | | - // If CDN is used, add etag to request header so that the pipeline policy can retrieve and append it to the request URL |
635 | | - let getOptions: GetConfigurationSettingOptions = {}; |
| 633 | + const getOptions: GetConfigurationSettingOptions = {}; |
636 | 634 | if (this.#isCdnUsed && this.#kvSelectorCollection.cdnToken) { |
637 | | - getOptions = { requestOptions: { customHeaders: { [CDN_TOKEN_LOOKUP_HEADER]: this.#kvSelectorCollection.cdnToken } } }; |
| 635 | + this.#addCdnTokenLookupHeader(getOptions, this.#kvSelectorCollection.cdnToken); |
638 | 636 | } |
639 | 637 | const response = await this.#getConfigurationSetting(sentinel, getOptions); |
640 | 638 | sentinel.etag = response?.etag; |
@@ -691,16 +689,14 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration { |
691 | 689 | } |
692 | 690 | // if watchAll is true, there should be no sentinels |
693 | 691 | for (const sentinel of this.#sentinels.values()) { |
694 | | - // if CDN is used, add etag to request header so that the pipeline policy can retrieve and append it to the request URL |
695 | | - let getOptions: GetConfigurationSettingOptions = {}; |
| 692 | + const getOptions: GetConfigurationSettingOptions = { |
| 693 | + // send conditional request only when CDN is not used |
| 694 | + onlyIfChanged: !this.#isCdnUsed |
| 695 | + }; |
696 | 696 | if (this.#isCdnUsed && this.#kvSelectorCollection.cdnToken) { |
697 | | - // if CDN is used, add etag to request header so that the pipeline policy can retrieve and append it to the request URL |
698 | | - getOptions = { |
699 | | - requestOptions: { customHeaders: { [CDN_TOKEN_LOOKUP_HEADER]: this.#kvSelectorCollection.cdnToken ?? "" } }, |
700 | | - }; |
| 697 | + this.#addCdnTokenLookupHeader(getOptions, this.#kvSelectorCollection.cdnToken); |
701 | 698 | } |
702 | | - // send conditional request only when CDN is not used |
703 | | - const response = await this.#getConfigurationSetting(sentinel, { ...getOptions, onlyIfChanged: !this.#isCdnUsed }); |
| 699 | + const response = await this.#getConfigurationSetting(sentinel, getOptions); |
704 | 700 |
|
705 | 701 | if ((response?.statusCode === 200 && sentinel.etag !== response?.etag) || |
706 | 702 | (response === undefined && sentinel.etag !== undefined) // deleted |
@@ -777,24 +773,17 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration { |
777 | 773 | if (selector.snapshotName) { // skip snapshot selector |
778 | 774 | continue; |
779 | 775 | } |
780 | | - let listOptions: ListConfigurationSettingsOptions = { |
| 776 | + const listOptions: ListConfigurationSettingsOptions = { |
781 | 777 | keyFilter: selector.keyFilter, |
782 | 778 | labelFilter: selector.labelFilter, |
783 | 779 | tagsFilter: selector.tagFilters |
784 | 780 | }; |
785 | 781 |
|
786 | 782 | if (!this.#isCdnUsed) { |
787 | 783 | // if CDN is not used, add page etags to the listOptions to send conditional request |
788 | | - listOptions = { |
789 | | - ...listOptions, |
790 | | - pageEtags: selector.pageEtags |
791 | | - }; |
| 784 | + listOptions.pageEtags = selector.pageEtags; |
792 | 785 | } else if (selectorCollection.cdnToken) { |
793 | | - // If CDN is used, add etag to request header so that the pipeline policy can retrieve and append it to the request URL |
794 | | - listOptions = { |
795 | | - ...listOptions, |
796 | | - requestOptions: { customHeaders: { [CDN_TOKEN_LOOKUP_HEADER]: selectorCollection.cdnToken } } |
797 | | - }; |
| 786 | + this.#addCdnTokenLookupHeader(listOptions, selectorCollection.cdnToken); |
798 | 787 | } |
799 | 788 |
|
800 | 789 | const pageIterator = listConfigurationSettingsWithTrace( |
@@ -1132,6 +1121,16 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration { |
1132 | 1121 | return first15Bytes.toString("base64url"); |
1133 | 1122 | } |
1134 | 1123 | } |
| 1124 | + |
| 1125 | + #addCdnTokenLookupHeader(operationOptions: OperationOptions, cdnToken: string): void { |
| 1126 | + if (!operationOptions.requestOptions) { |
| 1127 | + operationOptions.requestOptions = {}; |
| 1128 | + } |
| 1129 | + if (!operationOptions.requestOptions.customHeaders) { |
| 1130 | + operationOptions.requestOptions.customHeaders = {}; |
| 1131 | + } |
| 1132 | + operationOptions.requestOptions.customHeaders[CDN_TOKEN_LOOKUP_HEADER] = cdnToken; |
| 1133 | + } |
1135 | 1134 | } |
1136 | 1135 |
|
1137 | 1136 | function getValidSettingSelectors(selectors: SettingSelector[]): SettingSelector[] { |
|
0 commit comments