Skip to content

Commit

Permalink
Merge pull request pkp#486 from taslangraham/fix-usefetch-array-query…
Browse files Browse the repository at this point in the history
…-params
  • Loading branch information
taslangraham authored Jan 13, 2025
2 parents fe00d94 + 2f2f137 commit 108e93b
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/composables/useFetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,32 @@ export function getCSRFToken() {
return FALLBACK_TOKEN;
}

/**
* This workaround addresses the limitation where ofetch does not handle array query params in PHP friendly way.
* Once this issue is resolved in ofetch, this function can be removed.
* Fo reference see:
* - https://github.com/unjs/ufo/issues/185
* - https://github.com/unjs/ufo/issues/208
* - https://github.com/unjs/ofetch/pull/440
*
*/
function _formatQueryParams(params) {
if (!params) {
return {}
}

const formatedParams = {};
Object.entries(params).forEach(([key, value]) => {
if (Array.isArray(value)) {
formatedParams[`${key}[]`] = value;
} else {
formatedParams[key] = value;
}
});

return formatedParams;
}

/**
*
* Composable for handling API requests
Expand Down Expand Up @@ -87,7 +113,7 @@ export function useFetch(url, options = {}) {

const opts = {
...ofetchOptions,
query: query.value,
query: _formatQueryParams(query.value),
body: body.value,
signal,
};
Expand Down

0 comments on commit 108e93b

Please sign in to comment.