Skip to content

Commit 4bd13ed

Browse files
fix(buildParams) array values without [] suffixed keys are not transformed to LHS format (#39)
* fix missing slash in delete function * refactoring searchParams builder * fix: temp key * add test coverage for object array parameter * simplify `buildSearchParams` --------- Co-authored-by: EdsolXY <[email protected]> Co-authored-by: daedalus <[email protected]>
1 parent f4d12e4 commit 4bd13ed

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/utils/buildSearchParams.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@
77
const buildSearchParams = (query) => {
88
const params = new URLSearchParams();
99

10-
for (const key in query) {
11-
if (key.endsWith('[]')) {
12-
query[key].forEach((entry) => {
13-
params.append(key, entry);
14-
});
15-
} else {
16-
params.set(key, query[key]);
10+
for (let key in query) {
11+
if (Object.hasOwnProperty.call(query, key)) {
12+
const value = query[key];
13+
14+
// LHS requires array variables to be added ones per item in array
15+
if (Array.isArray(value)) {
16+
// LHS bracket notiation requires array keys to end with []
17+
if (!key.endsWith('[]')) {
18+
key += '[]';
19+
}
20+
21+
for (const entry of value) {
22+
params.append(key, entry);
23+
}
24+
} else {
25+
params.set(key, value);
26+
}
1727
}
1828
}
19-
2029
return params;
2130
};
2231

test/utils.spec.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ describe('Testing buildSearchParams util', () => {
1313
archive: false,
1414
order_by: 'due_date',
1515
'statuses[]': ['in progress', 'completed'],
16+
assignees: [1, 2],
1617
};
1718

1819
const expectedOutput = new URLSearchParams([
1920
['archived', 'false'],
2021
['order_by', 'due_date'],
2122
['statuses[]', 'in progress'],
2223
['statuses[]', 'completed'],
24+
['assignees[]', 1],
25+
['assignees[]', 2],
2326
]);
2427

2528
assert.deepEqual(buildSearchParams(params), expectedOutput);

0 commit comments

Comments
 (0)