-
Notifications
You must be signed in to change notification settings - Fork 31
fix: only allow non-nullable values in search params #1043
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
base: main
Are you sure you want to change the base?
fix: only allow non-nullable values in search params #1043
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Skipped Deployment
|
@@ -18,7 +19,7 @@ export const encodeQueryString = ({ | |||
|
|||
// Iterate params, the keys are prefixed with `$` and their values JSON stringified | |||
for (const [key, value] of Object.entries(params)) { | |||
searchParams.append(`$${key}`, JSON.stringify(value)) | |||
if (isDefined(value)) searchParams.append(`$${key}`, JSON.stringify(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a lot of context on the client so I will leave it to other folks to weight in, however, this might be considered a breaking change for other devs that might have been using the ability to send in nullable values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR 👋
Please add tests that demonstrate the fix. A great test for this is one that fails if we run it on main
, while it passes on your PR ❤
I ran some tests and it seems like the case you're wanting to fix is:
await client.fetch(`*[_type == "post" && slug.current == $slug]`, {slug: undefined})
Is that correct? I was surprised to see the client throwing an error here.
At the same time, this is valid and need to continue to pass (is this what you allude to @RitaDias?):
await client.fetch(`*[_type == "post" && slug.current == $slug]`, {slug: null})
As a common pattern is to read params from an URLSearchParams
instance:
const url = new URL(location.href)
const slug = url.searchParams.get('slug')
const post = await client.fetch(
`*[_type == "post" && defined(slug.current) && slug.current == $slug]`,
{slug} // string, or `null` if the search param doesn't exist
)
Thus we have to continue forwarding null
🙌
…-in-search-params
Coverage Report
File Coverage
|
Signed-off-by: Cody Olsen <[email protected]>
@azaxarov whats the status on this one? Unassigning myself as reviewer, but feel free to add me back once it's ready for review again :) |
my team is waiting for this, what's the status? |
Sanity Client accepts both nullable and non-nullable search parameters. Due to the fact that our API rejects values that are
undefined
, it is best that we filter them out and thus only allow nun-nullable values.