Skip to content

Commit 31c38db

Browse files
committed
refactor
1 parent ee23ddb commit 31c38db

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/appConfigurationImpl.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import { AIConfigurationTracingOptions } from "./requestTracing/aiConfigurationT
6060
import { KeyFilter, LabelFilter, SettingWatcher, SettingSelector, PagedSettingsWatcher, WatchedSetting } from "./types.js";
6161
import { ConfigurationClientManager } from "./configurationClientManager.js";
6262
import { getFixedBackoffDuration, getExponentialBackoffDuration } from "./common/backoffUtils.js";
63+
import { getStatusCode } from "./common/utils.js";
6364
import { InvalidOperationError, ArgumentError, isFailoverableError, isInputError, SnapshotReferenceError } from "./common/errors.js";
6465
import { ErrorMessages } from "./common/errorMessages.js";
6566

@@ -659,7 +660,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
659660

660661
const watcher: SettingWatcher = this.#sentinels.get(watchedSetting)!; // watcher should always exist for sentinels
661662
const isDeleted = response === undefined && watcher.etag !== undefined; // previously existed, now deleted
662-
const isChanged = response && Number(response.statusCode) === 200 && watcher.etag !== response.etag; // etag changed
663+
const isChanged = response && getStatusCode(response.statusCode) === 200 && watcher.etag !== response.etag; // etag changed
663664
if (isDeleted || isChanged) {
664665
changedSentinel = watchedSetting;
665666
changedSentinelWatcher = { etag: isChanged ? response.etag : undefined };
@@ -750,7 +751,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
750751

751752
for await (const page of pageIterator) {
752753
// when conditional request is sent, the response will be 304 if not changed
753-
if (Number(page._response.status) === 200) { // created or changed
754+
if (getStatusCode(page._response.status) === 200) { // created or changed
754755
return true;
755756
}
756757
}
@@ -779,7 +780,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
779780
try {
780781
response = await this.#executeWithFailoverPolicy(funcToExecute);
781782
} catch (error) {
782-
if (isRestError(error) && Number(error.statusCode) === 404) {
783+
if (isRestError(error) && getStatusCode(error.statusCode) === 404) {
783784
response = undefined;
784785
} else {
785786
throw error;
@@ -822,7 +823,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
822823
try {
823824
response = await this.#executeWithFailoverPolicy(funcToExecute);
824825
} catch (error) {
825-
if (isRestError(error) && Number(error.statusCode) === 404) {
826+
if (isRestError(error) && getStatusCode(error.statusCode) === 404) {
826827
response = undefined;
827828
} else {
828829
throw error;

src/common/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ export function shuffleList<T>(array: T[]): T[] {
1212
export function instanceOfTokenCredential(obj: unknown) {
1313
return obj && typeof obj === "object" && "getToken" in obj && typeof obj.getToken === "function";
1414
}
15+
16+
/**
17+
* Normalizes an HTTP status code to a number.
18+
*
19+
* The underlying App Configuration client may surface the status code either as a number
20+
* or as a string (e.g. "200", "304", "404") depending on the runtime/transport. This helper
21+
* coerces the value to a number so that status code comparisons behave consistently and
22+
* refresh logic is not broken when the status is a string.
23+
*/
24+
export function getStatusCode(statusCode: number | string | undefined): number {
25+
return Number(statusCode);
26+
}

0 commit comments

Comments
 (0)