Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make array query params php friendly #486

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading