Skip to content

Commit 1ec5e29

Browse files
committed
send URLSearchParams in server functions as urlencoded
1 parent aafed33 commit 1ec5e29

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

.changeset/cyan-pumpkins-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/start": patch
3+
---
4+
5+
send URLSearchParams in server functions as urlencoded

packages/start/src/runtime/server-runtime.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,15 @@ async function fetchServerFunction(
151151
? createRequest(base, id, instance, options)
152152
: args.length === 1 && args[0] instanceof FormData
153153
? createRequest(base, id, instance, { ...options, body: args[0] })
154+
: args.length === 1 && args[0] instanceof URLSearchParams
155+
? createRequest(base, id, instance, {
156+
...options,
157+
body: args[0],
158+
headers: { ...options.headers, "Content-Type": "application/x-www-form-urlencoded" }
159+
})
154160
: createRequest(base, id, instance, {
155161
...options,
156-
body: JSON.stringify(
157-
await Promise.resolve(
158-
toJSONAsync(args, { plugins })
159-
)
160-
),
162+
body: JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins }))),
161163
headers: { ...options.headers, "Content-Type": "application/json" }
162164
}));
163165

@@ -201,14 +203,20 @@ export function createServerReference(fn: Function, id: string, name: string) {
201203
return receiver.withOptions({ method: "GET" });
202204
}
203205
if (prop === "withOptions") {
204-
const url = `${baseURL}/_server/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`
206+
const url = `${baseURL}/_server/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(
207+
name
208+
)}`;
205209
return (options: RequestInit) => {
206210
const fn = async (...args: any[]) => {
207211
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
208212
return fetchServerFunction(
209213
encodeArgs
210-
? url + (args.length ? `&args=${encodeURIComponent(JSON.stringify(await Promise.resolve(
211-
toJSONAsync(args, { plugins }))))}` : "")
214+
? url +
215+
(args.length
216+
? `&args=${encodeURIComponent(
217+
JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins })))
218+
)}`
219+
: "")
212220
: `${baseURL}/_server`,
213221
`${id}#${name}`,
214222
options,

0 commit comments

Comments
 (0)