Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
martinboulais committed Dec 20, 2024
1 parent 0de6e91 commit 09aac6e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/public/utilities/fetch/buildUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ export const buildUrl = (baseURL, parameters) => {

if (Array.isArray(value)) {
for (const subValue of value) {
formatAndPushQueryParameter(`${sanitize(key)}[]`, subValue);
formatAndPushQueryParameter(`${key}[]`, subValue);
}
return;
}

if (typeof value === 'object' && value !== null) {
for (const [subKey, subValue] of Object.entries(value)) {
formatAndPushQueryParameter(`${sanitize(key)}[${sanitize(subKey)}]`, subValue);
formatAndPushQueryParameter(`${key}[${sanitize(subKey)}]`, subValue);

Check warning on line 122 in lib/public/utilities/fetch/buildUrl.js

View check run for this annotation

Codecov / codecov/patch

lib/public/utilities/fetch/buildUrl.js#L122

Added line #L122 was not covered by tests
}
return;
}

serializedQueryParameters.push(`${sanitize(key)}=${sanitize(value)}`);
serializedQueryParameters.push(`${key}=${sanitize(value)}`);

Check warning on line 127 in lib/public/utilities/fetch/buildUrl.js

View check run for this annotation

Codecov / codecov/patch

lib/public/utilities/fetch/buildUrl.js#L127

Added line #L127 was not covered by tests
};

for (const [key, parameter] of Object.entries(parameters)) {
formatAndPushQueryParameter(key, parameter);
formatAndPushQueryParameter(sanitize(key), parameter);

Check warning on line 131 in lib/public/utilities/fetch/buildUrl.js

View check run for this annotation

Codecov / codecov/patch

lib/public/utilities/fetch/buildUrl.js#L131

Added line #L131 was not covered by tests
}

return `${url}?${serializedQueryParameters.join('&')}`;
Expand Down
8 changes: 4 additions & 4 deletions lib/utilities/buildUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ exports.buildUrl = (baseURL, parameters) => {

if (Array.isArray(value)) {
for (const subValue of value) {
formatAndPushQueryParameter(`${sanitize(key)}[]`, subValue);
formatAndPushQueryParameter(`${key}[]`, subValue);
}
return;
}

if (typeof value === 'object' && value !== null) {
for (const [subKey, subValue] of Object.entries(value)) {
formatAndPushQueryParameter(`${sanitize(key)}[${sanitize(subKey)}]`, subValue);
formatAndPushQueryParameter(`${key}[${sanitize(subKey)}]`, subValue);
}
return;
}

serializedQueryParameters.push(`${sanitize(key)}=${sanitize(value)}`);
serializedQueryParameters.push(`${key}=${sanitize(value)}`);
};

for (const [key, parameter] of Object.entries(parameters)) {
formatAndPushQueryParameter(key, parameter);
formatAndPushQueryParameter(sanitize(key), parameter);
}

return `${url}?${serializedQueryParameters.join('&')}`;
Expand Down
2 changes: 2 additions & 0 deletions test/lib/utilities/buildUrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ module.exports = () => {
simple: 'hello',
key: {
nested: [1, 2],
'=': 12,
},
}).split('?');

const parametersExpressions = fullParametersExpressions.split('&');
expect(parametersExpressions).to.includes('simple=hello');
expect(parametersExpressions).to.includes('key[nested][]=1');
expect(parametersExpressions).to.includes('key[nested][]=2');
expect(parametersExpressions).to.includes('key[%3D]=12');
});
it('should successfully build URL by combining existing parameters', () => {
const [, fullParametersExpressions] = buildUrl('https://example.com?simple=hello&key1[key2][]=13&key1[key2][]=35', {
Expand Down

0 comments on commit 09aac6e

Please sign in to comment.