Skip to content

Commit 93cb511

Browse files
[App Configuration] - Update query param policy (#36344)
### Packages impacted by this PR @azure/app-configuration ### Issues associated with this PR See [comment](#35962 (comment)) ### Describe the problem that is addressed by this PR ### What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen? ### Are there test cases added in this PR? _(If not, why?)_ ### Provide a list of related PRs _(if any)_ ### Command used to generate this PR:**_(Applicable only to SDK release request PRs)_ ### Checklists - [ ] Added impacted package name to the issue description - [ ] Does this PR needs any fixes in the SDK Generator?** _(If so, create an Issue in the [Autorest/typescript](https://github.com/Azure/autorest.typescript) repository and link it here)_ - [ ] Added a changelog (if necessary) --------- Co-authored-by: Minh-Anh Phan <[email protected]>
1 parent 6b3f5e6 commit 93cb511

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

sdk/appconfiguration/app-configuration/src/internal/queryParamPolicy.ts

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
PipelineResponse,
88
SendRequest,
99
} from "@azure/core-rest-pipeline";
10+
import { logger } from "../logger.js";
1011

1112
/**
1213
* Creates a PipelinePolicy that normalizes query parameters:
@@ -18,47 +19,51 @@ export function queryParamPolicy(): PipelinePolicy {
1819
return {
1920
name: "queryParamPolicy",
2021
async sendRequest(request: PipelineRequest, next: SendRequest): Promise<PipelineResponse> {
22+
const originalUrl: string = request.url;
23+
let url: URL;
2124
try {
22-
const originalUrl: string = request.url;
23-
const url = new URL(originalUrl);
24-
25-
if (url.search === "") {
25+
url = new URL(originalUrl);
26+
} catch (error) {
27+
if (error instanceof TypeError) {
28+
logger.warning(`"[queryParamPolicy] Could not parse URL: ${request.url}"`);
2629
return next(request);
2730
}
31+
throw error;
32+
}
33+
34+
if (url.search === "") {
35+
return next(request);
36+
}
2837

29-
const params: ParamEntry[] = [];
30-
for (const entry of url.search.substring(1).split("&")) {
31-
if (entry === "") {
32-
continue;
33-
}
34-
const equalIndex = entry.indexOf("=");
35-
const name = equalIndex === -1 ? entry : entry.substring(0, equalIndex);
36-
const value = equalIndex === -1 ? "" : entry.substring(equalIndex + 1);
37-
params.push({ lowercaseName: name.toLowerCase(), value });
38+
const params: ParamEntry[] = [];
39+
for (const entry of url.search.substring(1).split("&")) {
40+
if (entry === "") {
41+
continue;
3842
}
43+
const equalIndex = entry.indexOf("=");
44+
const name = equalIndex === -1 ? entry : entry.substring(0, equalIndex);
45+
const value = equalIndex === -1 ? "" : entry.substring(equalIndex + 1);
46+
params.push({ lowercaseName: name.toLowerCase(), value });
47+
}
3948

40-
// Modern JavaScript Array.prototype.sort is stable
41-
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#sort_stability
42-
params.sort((a, b) => {
43-
if (a.lowercaseName < b.lowercaseName) {
44-
return -1;
45-
} else if (a.lowercaseName > b.lowercaseName) {
46-
return 1;
47-
}
48-
return 0;
49-
});
49+
// Modern JavaScript Array.prototype.sort is stable
50+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#sort_stability
51+
params.sort((a, b) => {
52+
if (a.lowercaseName < b.lowercaseName) {
53+
return -1;
54+
} else if (a.lowercaseName > b.lowercaseName) {
55+
return 1;
56+
}
57+
return 0;
58+
});
5059

51-
const newSearchParams: string = params
52-
.map(({ lowercaseName, value }) => `${lowercaseName}=${value}`)
53-
.join("&");
60+
const newSearchParams: string = params
61+
.map(({ lowercaseName, value }) => `${lowercaseName}=${value}`)
62+
.join("&");
5463

55-
const newUrl = url.origin + url.pathname + "?" + newSearchParams + url.hash;
56-
if (newUrl !== originalUrl) {
57-
request.url = newUrl;
58-
}
59-
} catch {
60-
// If anything goes wrong, fall back to sending the original request.
61-
console.log("Failed to normalize query parameters.");
64+
const newUrl = url.origin + url.pathname + "?" + newSearchParams + url.hash;
65+
if (newUrl !== originalUrl) {
66+
request.url = newUrl;
6267
}
6368

6469
return next(request);

sdk/appconfiguration/app-configuration/src/internal/syncTokenPolicy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function syncTokenPolicy(syncTokens: SyncTokens): PipelinePolicy {
2929

3030
if (syncTokenHeaderValue) {
3131
logger.info(
32-
"[syncTokenPolicy] Setting headers with ${SyncTokenHeaderName} and ${syncTokenHeaderValue}",
32+
`[syncTokenPolicy] Setting headers with ${SyncTokenHeaderName} and ${syncTokenHeaderValue}`,
3333
);
3434
request.headers.set(SyncTokenHeaderName, syncTokenHeaderValue);
3535
}

0 commit comments

Comments
 (0)