Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

azaxarov
Copy link

@azaxarov azaxarov commented Apr 8, 2025

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.

Copy link

vercel bot commented Apr 8, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
tsdocs-client ⬜️ Ignored (Inspect) Apr 9, 2025 10:21am

@@ -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))
Copy link
Contributor

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.

Copy link
Member

@stipsan stipsan left a 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 🙌

Copy link
Contributor

github-actions bot commented Apr 9, 2025

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 87.42% 2607 / 2982
🔵 Statements 87.42% 2607 / 2982
🔵 Functions 86.11% 217 / 252
🔵 Branches 88.41% 832 / 941
File Coverage
File Stmts Branches Functions Lines Uncovered Lines
Changed Files
src/data/encodeQueryString.ts 100% 100% 100% 100%
src/util/isDefined.ts 100% 100% 100% 100%
Generated in workflow #3117 for commit e4b7fcb by the Vitest Coverage Report Action

@rexxars rexxars removed their request for review May 6, 2025 18:32
@bjoerge
Copy link
Member

bjoerge commented May 7, 2025

@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 :)

@bjoerge bjoerge removed their request for review May 7, 2025 08:24
@mak-koda
Copy link

my team is waiting for this, what's the status?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants