Skip to content

Commit

Permalink
send URLSearchParams in server functions as urlencoded
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Jul 2, 2024
1 parent aafed33 commit 1ec5e29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-pumpkins-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/start": patch
---

send URLSearchParams in server functions as urlencoded
24 changes: 16 additions & 8 deletions packages/start/src/runtime/server-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,15 @@ async function fetchServerFunction(
? createRequest(base, id, instance, options)
: args.length === 1 && args[0] instanceof FormData
? createRequest(base, id, instance, { ...options, body: args[0] })
: args.length === 1 && args[0] instanceof URLSearchParams
? createRequest(base, id, instance, {
...options,
body: args[0],
headers: { ...options.headers, "Content-Type": "application/x-www-form-urlencoded" }
})
: createRequest(base, id, instance, {
...options,
body: JSON.stringify(
await Promise.resolve(
toJSONAsync(args, { plugins })
)
),
body: JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins }))),
headers: { ...options.headers, "Content-Type": "application/json" }
}));

Expand Down Expand Up @@ -201,14 +203,20 @@ export function createServerReference(fn: Function, id: string, name: string) {
return receiver.withOptions({ method: "GET" });
}
if (prop === "withOptions") {
const url = `${baseURL}/_server/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(name)}`
const url = `${baseURL}/_server/?id=${encodeURIComponent(id)}&name=${encodeURIComponent(
name
)}`;
return (options: RequestInit) => {
const fn = async (...args: any[]) => {
const encodeArgs = options.method && options.method.toUpperCase() === "GET";
return fetchServerFunction(
encodeArgs
? url + (args.length ? `&args=${encodeURIComponent(JSON.stringify(await Promise.resolve(
toJSONAsync(args, { plugins }))))}` : "")
? url +
(args.length
? `&args=${encodeURIComponent(
JSON.stringify(await Promise.resolve(toJSONAsync(args, { plugins })))
)}`
: "")
: `${baseURL}/_server`,
`${id}#${name}`,
options,
Expand Down

0 comments on commit 1ec5e29

Please sign in to comment.