Skip to content

Commit 108e93b

Browse files
authored
Merge pull request #486 from taslangraham/fix-usefetch-array-query-params
2 parents fe00d94 + 2f2f137 commit 108e93b

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

src/composables/useFetch.js

+27-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,32 @@ export function getCSRFToken() {
1818
return FALLBACK_TOKEN;
1919
}
2020

21+
/**
22+
* This workaround addresses the limitation where ofetch does not handle array query params in PHP friendly way.
23+
* Once this issue is resolved in ofetch, this function can be removed.
24+
* Fo reference see:
25+
* - https://github.com/unjs/ufo/issues/185
26+
* - https://github.com/unjs/ufo/issues/208
27+
* - https://github.com/unjs/ofetch/pull/440
28+
*
29+
*/
30+
function _formatQueryParams(params) {
31+
if (!params) {
32+
return {}
33+
}
34+
35+
const formatedParams = {};
36+
Object.entries(params).forEach(([key, value]) => {
37+
if (Array.isArray(value)) {
38+
formatedParams[`${key}[]`] = value;
39+
} else {
40+
formatedParams[key] = value;
41+
}
42+
});
43+
44+
return formatedParams;
45+
}
46+
2147
/**
2248
*
2349
* Composable for handling API requests
@@ -87,7 +113,7 @@ export function useFetch(url, options = {}) {
87113

88114
const opts = {
89115
...ofetchOptions,
90-
query: query.value,
116+
query: _formatQueryParams(query.value),
91117
body: body.value,
92118
signal,
93119
};

0 commit comments

Comments
 (0)