Skip to content
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

Return non-null writable for defined defaultValue #87

Merged
merged 4 commits into from
Jul 28, 2024

Conversation

gwax
Copy link
Contributor

@gwax gwax commented Jul 27, 2024

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

  • New Features

    • Introduced dedicated functions for encoding and decoding various data types, enhancing modularity and flexibility.
    • Improved queryParam function with new overloads for more flexible usage.
  • Improvements

    • Enhanced type safety by centralizing encoding and decoding logic within new functions.
    • Updated export structure for clearer delineation of encoding and decoding strategies, improving readability.
    • Ensured non-null writable values are returned when a defaultValue is provided, enhancing module reliability.
    • Modernized workflow configurations to utilize the latest versions of tools, enhancing performance and reliability.

Copy link

changeset-bot bot commented Jul 27, 2024

🦋 Changeset detected

Latest commit: 90fef99

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

Copy link
Contributor

coderabbitai bot commented Jul 27, 2024

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
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • 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 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

netlify bot commented Jul 27, 2024

Deploy Preview for sveltekit-search-params ready!

Name Link
🔨 Latest commit 90fef99
🔍 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8b85bc1 and 78fd687.

Files selected for processing (1)
  • src/lib/sveltekit-search-params.ts (4 hunks)
Additional comments not posted (7)
src/lib/sveltekit-search-params.ts (7)

136-143: LGTM! The function is well-structured.

The primitiveEncodeAndDecodeOptions function correctly handles encoding and decoding for primitive types and provides overloads for handling default values.


Line range hint 145-160:
LGTM! The function is well-structured.

The objectEncodeAndDecodeOptions function correctly handles encoding and decoding for objects and provides overloads for handling default values.


Line range hint 162-177:
LGTM! The function is well-structured.

The arrayEncodeAndDecodeOptions function correctly handles encoding and decoding for arrays and provides overloads for handling default values.


179-194: LGTM! The function is well-structured.

The lzEncodeAndDecodeOptions function correctly handles encoding and decoding using LZ compression and provides overloads for handling default values.


196-212: LGTM! The ssp object is well-defined.

The ssp object correctly utilizes the newly defined encoding and decoding functions, enhancing modularity and type safety.


Line range hint 224-332:
LGTM! The function maintains its functionality.

The queryParameters function correctly handles query parameter management and maintains type safety.


333-336: LGTM! The function overloads are well-defined.

The queryParam function correctly handles the new overloads to ensure non-nullable types when a default value is provided.

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

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

Fixes paoloricciuti#75
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 78fd687 and 3591113.

Files selected for processing (2)
  • .changeset/poor-birds-dance.md (1 hunks)
  • src/lib/sveltekit-search-params.ts (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/lib/sveltekit-search-params.ts
Additional comments not posted (1)
.changeset/poor-birds-dance.md (1)

1-5: Changeset file looks good.

The metadata and description accurately reflect the changes made in the sveltekit-search-params library.

@paoloricciuti
Copy link
Owner

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
Copy link
Contributor Author

gwax commented Jul 27, 2024

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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3591113 and 45f8679.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
Files selected for processing (3)
  • .github/workflows/release.yml (1 hunks)
  • .github/workflows/run-tests.yml (1 hunks)
  • src/lib/sveltekit-search-params.ts (5 hunks)
Additional comments not posted (10)
.github/workflows/release.yml (2)

16-16: Verify compatibility of pnpm version 9.

The version has been updated from 8 to 9. Ensure that the new version is compatible with the workflow and doesn't introduce breaking changes.

Verification successful

Verify compatibility of pnpm version 9.

The version has been updated from 8 to 9. Ensure that the new version is compatible with the workflow and doesn't introduce breaking changes.


Verify compatibility of pnpm version 9.

The version has been updated from 8 to 9. Ensure that the new version is compatible with the workflow and doesn't introduce breaking changes.


Verified compatibility of pnpm version 9.

The update from version 8 to 9 does not introduce any breaking changes that would affect the workflow. The changes are mostly enhancements and bug fixes.

  • Minor Changes: Support for specifying node version for running lifecycle scripts, and support for the catalogs: protocol.
  • Patch Changes: Enhancements to commands like pnpm deploy and pnpm outdated, and bug fixes.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the compatibility of `pnpm` version 9.

# Test: Check the release notes and documentation for breaking changes.
gh release view 9 --repo pnpm/pnpm --json body | jq -r .body

Length of output: 80


Script:

#!/bin/bash
# Description: Verify the compatibility of `pnpm` version 9.

# Step 1: List all releases to find the correct tag for version 9.
gh release list --repo pnpm/pnpm

# Step 2: Fetch the release notes for the correct tag.
gh release view <correct-tag> --repo pnpm/pnpm --json body | jq -r .body

Length of output: 1486


Script:

#!/bin/bash
# Description: Verify the compatibility of `pnpm` version 9.

# Fetch the release notes for the correct tag (v9.6.0).
gh release view v9.6.0 --repo pnpm/pnpm --json body | jq -r .body

Length of output: 8511


14-14: Verify compatibility of pnpm/action-setup@v4.

The version has been updated from v2 to v4. Ensure that the new version is compatible with the workflow and doesn't introduce breaking changes.

Verification successful

Verify the existence of pnpm/action-setup@v4.

The script did not find the release for v4. We need to verify if v4 exists and check for any breaking changes.


Verify compatibility of pnpm/action-setup@v4.0.0.

The version v4.0.0 exists. We need to check the release notes for any breaking changes or important updates.


Verify compatibility of pnpm/action-setup@v4.0.0.

The release notes indicate a breaking change: an error is thrown if one version of pnpm is specified in the packageManager field of package.json and a different version is specified in the action's settings. Verify the pnpm version specified in package.json to ensure compatibility.


No issues found with pnpm/action-setup@v4.0.0.

The packageManager field is not set in package.json, so the breaking change mentioned in the release notes should not impact the workflow.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the compatibility of `pnpm/action-setup@v4`.

# Test: Check the release notes and documentation for breaking changes.
gh release view v4 --repo pnpm/action-setup --json body | jq -r .body

Length of output: 89


Script:

#!/bin/bash
# Description: List all available releases for `pnpm/action-setup`.

# Test: Check the available releases.
gh release list --repo pnpm/action-setup

Length of output: 781


Script:

#!/bin/bash
# Description: Verify the compatibility of `pnpm/action-setup@v4.0.0`.

# Test: Check the release notes and documentation for breaking changes.
gh release view v4.0.0 --repo pnpm/action-setup --json body | jq -r .body

Length of output: 294


Script:

#!/bin/bash
# Description: Verify the pnpm version specified in `package.json`.

# Test: Check the `packageManager` field in `package.json` for the pnpm version.
jq -r '.packageManager' package.json

Length of output: 41

.github/workflows/run-tests.yml (3)

13-13: Verify compatibility of pnpm/action-setup@v4.

The version has been updated from v2 to v4. Ensure that the new version is compatible with the workflow and doesn't introduce breaking changes.


15-15: Verify compatibility of pnpm version 9.

The version has been updated from 8 to 9. Ensure that the new version is compatible with the workflow and doesn't introduce breaking changes.


18-18: Verify compatibility of Node.js version 20.

The version has been updated from 18 to 20. Ensure that the new version is compatible with the workflow and doesn't introduce breaking changes.

src/lib/sveltekit-search-params.ts (5)

136-151: LGTM!

The primitiveEncodeAndDecodeOptions function correctly handles encoding and decoding for primitive types. The overloads are properly defined.


Line range hint 153-174:
LGTM!

The objectEncodeAndDecodeOptions function correctly handles encoding and decoding for objects. The overloads are properly defined.


Line range hint 176-197:
LGTM!

The arrayEncodeAndDecodeOptions function correctly handles encoding and decoding for arrays. The overloads are properly defined.


Line range hint 199-223:
LGTM!

The lzEncodeAndDecodeOptions function correctly handles encoding and decoding using LZ compression. The overloads are properly defined.


364-373: LGTM!

The queryParam function has been updated with new overloads to improve type safety and flexibility. The changes are properly defined and enhance the functionality.

@paoloricciuti
Copy link
Owner

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
Copy link
Owner

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
Copy link
Owner

@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!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 45f8679 and f2ba5e1.

Files selected for processing (1)
  • src/lib/sveltekit-search-params.ts (10 hunks)
Additional comments not posted (7)
src/lib/sveltekit-search-params.ts (7)

146-161: LGTM!

The primitiveEncodeAndDecodeOptions function is well-implemented, providing clear and type-safe encoding and decoding options for primitive types.


Line range hint 163-184:
LGTM!

The objectEncodeAndDecodeOptions function is well-implemented, providing clear and type-safe encoding and decoding options for object types.


Line range hint 186-207:
LGTM!

The arrayEncodeAndDecodeOptions function is well-implemented, providing clear and type-safe encoding and decoding options for array types.


Line range hint 209-233:
LGTM!

The lzEncodeAndDecodeOptions function is well-implemented, providing clear and type-safe encoding and decoding options using LZ compression.


235-254: LGTM!

The updates to the ssp object improve readability and maintainability by centralizing the encoding and decoding logic.


Line range hint 264-363:
LGTM!

The updates to the queryParameters function align with the new encoding and decoding options and improve type safety.


376-385: LGTM! But verify the function usage in the codebase.

The updates to the queryParam function align with the objective of ensuring non-nullable return types when a default value is provided.

However, ensure that all function calls to queryParam match the new signature.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f2ba5e1 and 90fef99.

Files selected for processing (1)
  • .changeset/poor-birds-dance.md (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • .changeset/poor-birds-dance.md

@paoloricciuti paoloricciuti merged commit 49ab6cc into paoloricciuti:master Jul 28, 2024
5 checks passed
@github-actions github-actions bot mentioned this pull request Jul 28, 2024
@gwax gwax deleted the gwax/non-null-writables branch July 29, 2024 16:24
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.

Value is nullable if default value is provided
2 participants