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

fix: withUnkey should not fail build (@unkey/nextjs) #2651

Merged
merged 5 commits into from
Nov 13, 2024

Conversation

harshsbhat
Copy link
Contributor

@harshsbhat harshsbhat commented Nov 7, 2024

What does this PR do?

Fixes #2554

After Nextjs 15 the @unkey/nextjs needs to have a promise for the params

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • Chore (refactoring code, technical debt, workflow improvements)
  • Enhancement (small improvements)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How should this be tested?

  • Use withUnkey in nextjs 15 application
  • Run npm run build or build command with your preferred package manager
  • There won't be the build errors any more

Checklist

Required

  • Filled out the "How to test" section in this PR
  • Read Contributing Guide
  • Self-reviewed my own code
  • Commented on my code in hard-to-understand areas
  • Ran pnpm build
  • Ran pnpm fmt
  • Checked for warnings, there are none
  • Removed all console.logs
  • Merged the latest changes from main onto my branch with git pull origin main
  • My changes don't cause any responsiveness issues

Appreciated

  • If a UI change was made: Added a screen recording or screenshots to this PR
  • Updated the Unkey Docs if changes were necessary

Summary by CodeRabbit

  • New Features

    • Enhanced NextContext to support asynchronous parameter resolution.
    • Updated the @unkey/nextjs package to align with Next.js version 15 for improved type compatibility.
  • Bug Fixes

    • Improved error handling in the withUnkey function for better response management.

Copy link

changeset-bot bot commented Nov 7, 2024

🦋 Changeset detected

Latest commit: 5b3f80a

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

This PR includes changesets to release 1 package
Name Type
@unkey/nextjs Minor

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 Nov 7, 2024

Caution

Review failed

The pull request is closed.

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request involve modifications to the NextContext type and the withUnkey function within the packages/nextjs/src/index.ts file. The params property of NextContext has been updated to return a promise, indicating that the parameters will now be resolved asynchronously. Additionally, the withUnkey function's signature has been adjusted to accommodate this new asynchronous context while retaining its original error handling logic.

Changes

File Path Change Summary
packages/nextjs/src/index.ts Updated NextContext type to use a Promise for params. Modified withUnkey function to accept the updated context type.
.changeset/little-melons-collect.md Minor version update to @unkey/nextjs package for compatibility with Next.js 15 type definitions.

Assessment against linked issues

Objective Addressed Explanation
Update withUnkey to expect a promise for params (#[2554])

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>, please review it.
    • 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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 using 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.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

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

vercel bot commented Nov 7, 2024

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

Name Status Preview Comments Updated (UTC)
engineering ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 13, 2024 10:52am

Copy link

vercel bot commented Nov 7, 2024

@harshsbhat is attempting to deploy a commit to the Unkey Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

github-actions bot commented Nov 7, 2024

Thank you for following the naming conventions for pull request titles! 🙏

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

🧹 Outside diff range and nitpick comments (2)
packages/nextjs/src/index.ts (2)

Line range hint 58-62: Add documentation for async params handling.

While the implementation correctly handles the async context, it would be helpful to add JSDoc comments explaining that params are now asynchronous in Next.js 15.

Add documentation like this:

 export function withUnkey<TContext extends NextContext = NextContext>(
+  /**
+   * @param handler - Request handler function
+   * @param config - Configuration options
+   * @remarks Since Next.js 15, context.params is asynchronous and must be awaited
+   * @example
+   * ```ts
+   * export default withUnkey(async (req, context) => {
+   *   const params = await context.params;
+   *   // ... rest of the handler
+   * });
+   * ```
+   */
   handler: (
     req: NextRequestWithUnkeyContext,
     context: TContext,
   ) => Response | NextResponse | Promise<Response | NextResponse>,
   config?: WithUnkeyConfig,

Line range hint 63-106: LGTM: Implementation handles async context correctly.

The function implementation:

  • Properly maintains type safety with the generic constraint
  • Preserves existing error handling
  • Correctly handles the async nature of the context

Consider adding a middleware or utility function to handle common params extraction patterns, making it easier for users to work with the async params. This could be a future enhancement.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1aa643b and 038c347.

📒 Files selected for processing (1)
  • packages/nextjs/src/index.ts (1 hunks)
🔇 Additional comments (1)
packages/nextjs/src/index.ts (1)

55-55: LGTM: Type modification aligns with Next.js 15 requirements.

The change correctly updates NextContext to handle params as a Promise, addressing the core issue with Next.js 15 compatibility.

Let's verify there are no other instances of synchronous params usage:

✅ Verification successful

Let me try a more focused search to find actual JavaScript/TypeScript code that uses context.params:


Let me try one more search using ast-grep to find any usage of the NextContext type or params access patterns:


Let me correct the ast-grep command with proper language specification:


Let me try one final search focusing on the specific file and its imports:


Type modification for NextContext is correctly implemented and isolated

The change to make params a Promise in the NextContext type is correctly implemented and doesn't impact other parts of the codebase because:

  1. The type is only used as a generic constraint in the withUnkey function
  2. The params field is never directly accessed in the implementation
  3. The type is properly exported for consumers to use with Next.js 15
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for any direct params access that might need Promise handling
rg -l "context.params" | xargs ast-grep --pattern "context.params" -A 3

Length of output: 20177


Script:

#!/bin/bash
# Search for TypeScript/JavaScript files containing context.params
rg -t ts -t js "context.params" -A 3

Length of output: 36


Script:

#!/bin/bash
# Search for NextContext type usage and any params access patterns
ast-grep --pattern 'NextContext' -l
ast-grep --pattern 'const { params } = context' -l
ast-grep --pattern 'await context.params' -l

Length of output: 453


Script:

#!/bin/bash
# Search for NextContext type usage and any params access patterns in TypeScript files
ast-grep --lang ts --pattern 'NextContext' -l
ast-grep --lang ts --pattern 'const { params } = context' -l
ast-grep --lang ts --pattern 'await context.params' -l

Length of output: 483


Script:

#!/bin/bash
# Check the content of the file and any related imports
cat packages/nextjs/src/index.ts

Length of output: 3345

@harshsbhat harshsbhat changed the title fix:withUnkey should not fail build (@unkey/nextjs) fix: withUnkey should not fail build (@unkey/nextjs) Nov 7, 2024
@harshsbhat
Copy link
Contributor Author

I tried this by locally setting up the @unkey/nextjs and then using the local version instead of the SDK in a NextJS 15 project.

@chronark chronark merged commit 77ca701 into unkeyed:main Nov 13, 2024
3 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update withUnkey for Next.js 15
2 participants