paoloricciuti / sveltekit-search-params

The easiest way to read and WRITE from query parameters in sveltekit.
https://sveltekit-search-params.netlify.app
MIT License
547 stars 19 forks source link

Return non-null writable for defined defaultValue #87

Closed gwax closed 4 months ago

gwax commented 4 months ago

This change modifies the return type of queryParam to be Writable instead of Writable<T | null> if options is included and specifies a defaultValue.

We additionally modify the various ssp helper functions to return Writable instead of Writable<T | null> when a default value is provided.

Fixes #75

Summary by CodeRabbit

changeset-bot[bot] commented 4 months ago

πŸ¦‹ Changeset detected

Latest commit: 90fef99c8342e117f337371a076343a5ea0871b2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ----------------------- | ---- | | sveltekit-search-params | |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

coderabbitai[bot] commented 4 months ago

Walkthrough

The changes to src/lib/sveltekit-search-params.ts significantly enhance the encoding and decoding capabilities of the ssp object by introducing dedicated functions for different data types. This restructuring improves code clarity, modularity, and type safety, while also ensuring that users receive non-null values when default parameters are provided, addressing previous issues with nullable values.

Changes

File Change Summary
src/lib/sveltekit-search-params.ts Restructured ssp object to use dedicated encoding/decoding functions, enhancing modularity and type safety. Updated queryParam function with new overloads for greater flexibility.
.github/workflows/release.yml Upgraded pnpm/action-setup from v2 to v4 and pnpm version from 8 to 9 for improved workflow reliability.
.github/workflows/run-tests.yml Updated indentation style and upgraded Node.js version from 18 to 20, along with pnpm/action-setup from v2 to v4.

Assessment against linked issues

Objective Addressed Explanation
Value is nullable if default value is provided (#75) βœ…

Poem

In fields so bright, we hop and play,
With new code changes here today!
Encoding’s clear, no more a mess,
Our queries now, they truly bless!
So let’s rejoice, with joy we bound,
For better code, we’ve surely found! πŸ‡βœ¨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit .` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai generate interesting stats about this repository and render them as a table.` - `@coderabbitai show all the console.log statements in this repository.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (invoked as PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. Additionally, you can add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. ### CodeRabbit Configuration File (`.coderabbit.yaml`) - You can programmatically configure CodeRabbit by adding a `.coderabbit.yaml` file to the root of your repository. - Please see the [configuration documentation](https://docs.coderabbit.ai/guides/configure-coderabbit) for more information. - If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: `# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json` ### Documentation and Community - Visit our [Documentation](https://coderabbit.ai/docs) for detailed information on how to use CodeRabbit. - Join our [Discord Community](https://discord.com/invite/GsXnASn26c) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
netlify[bot] commented 4 months ago

Deploy Preview for sveltekit-search-params ready!

Name Link
Latest commit 90fef99c8342e117f337371a076343a5ea0871b2
Latest deploy log https://app.netlify.com/sites/sveltekit-search-params/deploys/66a64c663ed7c300081e0dde
Deploy Preview https://deploy-preview-87--sveltekit-search-params.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

paoloricciuti commented 4 months ago

Uh this is nice. I tried to do something like this for a long time and failed to do so. I will try to take a look today or tomorrow or else I will get to it on Monday, thanks for contributing

gwax commented 4 months ago

Yeah, getting types right is a challenge. I wasn't feeling up to figuring out how to make queryParameters work right. I think this implementation is pretty straightforward, though it's pretty verbose to get ssp to work right 🀷 .

It's a great library and it'll be nice to avoid casting my Writable<string | null> to Writable<string>.

Please let me know if there's anything that I can do or clarify to help with your review process.

paoloricciuti commented 4 months ago

Hey @gwax great work...i pushed a small fix for queryParam that was defaulting to unknown when no encode and decode options was passed.

There's a typing problem with queryParameters tho that i've yet to figure out how to solve so i'm pushing the fix to queryParam in the meantime. If you have any idea how to fix this please go ahead.

const store: Writable<LooseAutocomplete<{
    str: unknown;
    num: number;
    bools: boolean;
    obj: {
        str: string;
    };
    arr: number[];
    lz: string;
}>>

this is the type that i'm getting back in ./playground/src/routes/queryparameters/+page.svelte where the store is defined like so:

const store = queryParameters({
    str: true,
    num: ssp.number(),
    bools: ssp.boolean(),
    obj: ssp.object<{ str: string }>(),
    arr: ssp.array<number>(),
    lz: ssp.lz<string>(),
});

in theory the expected type for this should be

const store: Writable<LooseAutocomplete<{
    str: string | null;
    num: number | null;
    bools: boolean | null;
    obj: {
        str: string;
    } | null;
    arr: number[] | null;
    lz: string | null;
}>>

and obviously passing an option with a default should also work there. I'll try to see if i can figure ths out

paoloricciuti commented 4 months ago

Oh i'm actually an idiot: this was already like this in older versions...so no fault from you here sorry.

Let me see if i can make something work.

paoloricciuti commented 4 months ago

@gwax i actually did it (the types for queryParameters were much simpler than what you did and it seems to work fine. Just need to check if this change in types is not a breaking change and then i'll merge and publish a new release. Thanks a lot!