N
Nuxt2y ago
BT1010

useFetch & URLSearchParams nuxt3 global middlewere

Guys i'm trying out useFetch inside my global middlewere (ts file) I basically want to sent http request from the server side and use URLSearchParams as my API requires it
useFetch(`https://oauth.my-domain/oauth2/token`, {
method: "POST",
params: new URLSearchParams({
scope: "openid",
redirect_uri: "http://localhost:3000/",
grant_type: "authorization_code",
code: `${route.query.code}`
}),
body: {},
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
useFetch(`https://oauth.my-domain/oauth2/token`, {
method: "POST",
params: new URLSearchParams({
scope: "openid",
redirect_uri: "http://localhost:3000/",
grant_type: "authorization_code",
code: `${route.query.code}`
}),
body: {},
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
});
with this code I'm getting following error:
Error: Unknown object type "urlsearchparams" at Object._object (index.mjs?v=b79dfc53:57:17) at Object.dispatch (index.mjs?v=b79dfc53:34:30) at Object._array (index.mjs?v=b79dfc53:88:16) at Object._object (index.mjs?v=b79dfc53:53:30) at Object.dispatch (index.mjs?v=b79dfc53:34:30) at objectHash (index.mjs?v=b79dfc53:13:10) at hash (index.mjs?v=b79dfc53:481:56) at useFetch (fetch.mjs?v=b79dfc53:6:28) at default (auth.server.global.ts:23:7) at fn (nuxt.mjs?v=b79dfc53:141:27)
URLSearchParams should be available in Node so what's going on?
2 Replies
manniL
manniL2y ago
You don't need to wrap your params in URLSearchParams. using a JS object is fine
manniL
manniL2y ago
GitHub
GitHub - unjs/ofetch: 😱 A better fetch API. Works on node, browser ...
😱 A better fetch API. Works on node, browser and workers. - GitHub - unjs/ofetch: 😱 A better fetch API. Works on node, browser and workers.