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

feat(trpc): trpc v11 support #1656

Merged
merged 3 commits into from
Aug 23, 2024
Merged

feat(trpc): trpc v11 support #1656

merged 3 commits into from
Aug 23, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Aug 23, 2024

Fixes #859

Copy link
Contributor

coderabbitai bot commented Aug 23, 2024

Walkthrough

Walkthrough

The changes introduce new TypeScript functions and utility types to facilitate the integration of TRPC with Next.js and React applications. Additionally, they enhance type safety, flexibility, and version handling capabilities for the generator and helper functions. These modifications streamline customization for different TRPC library versions, ensuring robust type management and functionality across various contexts.

Changes

Files Change Summary
packages/plugins/trpc/res/client/v11/next.ts Added createTRPCNext function for integrating TRPC with Next.js, enhancing type safety with custom router types.
packages/plugins/trpc/res/client/v11/react.ts Introduced createTRPCReact function to facilitate TRPC integration with React, improving type inference through customizable options.
packages/plugins/trpc/res/client/v11/utils.ts Added utility types DeepOverride and DeepOverrideAtPath for recursive and path-specific type merging, enhancing type manipulation capabilities in TypeScript.
packages/plugins/trpc/src/generator.ts Modified generate and related functions to include a version parameter, differentiating logic between TRPC versions 'v10' and 'v11', and adjusting import handling and router creation accordingly.
packages/plugins/trpc/src/helpers.ts Enhanced generateRouterTyping and generateRouterTypingImports functions by adding a version parameter, influencing TypeScript typing generation and imports based on TRPC version.
packages/plugins/trpc/tests/t3.test.ts Added a test case for TRPC version 11, validating the setup and build process by checking dependency installation and execution of relevant commands.
packages/plugins/trpc/tests/projects/t3-trpc-v11/.eslintrc.cjs Introduced ESLint configuration for TypeScript projects, defining parsing and style rules to ensure coding standards.
packages/plugins/trpc/tests/projects/t3-trpc-v11/.gitignore Created a .gitignore file to manage exclusions of files and directories common in Next.js development.
packages/plugins/trpc/tests/projects/t3-trpc-v11/README.md Added README.md as an introductory guide for users of the T3 Stack project, outlining project purpose and resources.
packages/plugins/trpc/tests/projects/t3-trpc-v11/next.config.js Introduced next.config.js for configuring a Next.js application, including environment settings.
packages/plugins/trpc/tests/projects/t3-trpc-v11/prisma/schema.prisma Generated Prisma schema defining User and Post models for database interactions.
packages/plugins/trpc/tests/projects/t3-trpc-v11/schema.zmodel Added schema for Prisma models, including configurations for TRPC plugin and generator.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/_components/post.tsx Introduced LatestPost React component for displaying and creating posts using TRPC hooks for data management.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/api/trpc/[trpc]/route.ts Established tRPC API handler for processing requests in a Next.js application, including context creation and error handling.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/index.module.css Added comprehensive styles for a responsive web application interface, focusing on layout and typography.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/layout.tsx Created foundational layout component for a Next.js application, integrating global styles and setting up necessary providers.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/page.tsx Introduced home page component for the T3 application, including links and the LatestPost component.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/env.js Established structured environment variable schema for a Next.js application, validating and managing server/client variables.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/root.ts Introduced primary router for tRPC server, facilitating API route management and strong typing.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated-router-helper.ts Exported createTRPCRouter and publicProcedure for better organization and usability within the TRPC framework.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/trpc.ts Established foundational setup for the tRPC server, including context creation, error formatting, and middleware definition.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/db.ts Introduced database connection management using Prisma, implementing a singleton pattern for the Prisma client.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/styles/globals.css Added global styles for consistent layout and typography throughout the application.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/query-client.ts Introduced createQueryClient function for initializing a QueryClient instance with default options and hydration behaviors.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/react.tsx Provided React context for integrating tRPC with React applications, including TRPCReactProvider for managing API state and context.
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/server.ts Implemented server-side handling for tRPC calls, creating context, and managing data fetching in React Server Components.

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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
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: 1

Outside diff range, codebase verification and nitpick comments (8)
packages/plugins/trpc/tests/t3.test.ts (1)

31-42: Ensure test isolation and cleanup.

The test case for TRPC v11 is well-structured, but consider adding steps to ensure test isolation and cleanup, such as removing installed dependencies or resetting the environment after the test.

packages/plugins/trpc/src/helpers.ts (2)

Line range hint 187-243: Add comments to explain version-specific logic.

The generateRouterTyping function includes conditional logic based on the TRPC version. Adding comments to explain the differences between versions v10 and v11 would improve code readability and maintainability.


261-276: Add comments to explain version-specific imports.

The generateRouterTypingImports function uses conditional imports based on the TRPC version. Adding comments to clarify why certain imports are used for each version would enhance understanding.

packages/plugins/trpc/src/generator.ts (5)

53-69: Add comments to explain version checks and required options.

The generate function includes checks for the TRPC version and required options for v11. Adding comments to explain these checks and the rationale behind them would improve code clarity.


121-147: Add comments to explain version-specific imports.

The createAppRouter function uses conditional imports based on the TRPC version. Adding comments to clarify the differences in imports for v10 and v11 would enhance understanding.


Line range hint 157-178: Add comments to explain version-specific router creation logic.

The createAppRouter function includes different router creation logic for TRPC v10 and v11. Adding comments to explain these differences would improve code readability.


263-280: Add comments to explain version-specific client helper creation.

The createClientHelpers function loads different resources based on the TRPC version. Adding comments to explain the rationale behind these choices would enhance code clarity.


306-334: Add comments to explain version-specific imports in model router creation.

The generateModelCreateRouter function uses conditional imports based on the TRPC version. Adding comments to clarify these differences would improve understanding.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1d546d4 and dcfa0a8.

Files selected for processing (6)
  • packages/plugins/trpc/res/client/v11/next.ts (1 hunks)
  • packages/plugins/trpc/res/client/v11/react.ts (1 hunks)
  • packages/plugins/trpc/res/client/v11/utils.ts (1 hunks)
  • packages/plugins/trpc/src/generator.ts (10 hunks)
  • packages/plugins/trpc/src/helpers.ts (3 hunks)
  • packages/plugins/trpc/tests/t3.test.ts (1 hunks)
Additional context used
Biome
packages/plugins/trpc/res/client/v11/utils.ts

[error] 5-5: Don't use 'Function' as a type.

Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.

(lint/complexity/noBannedTypes)


[error] 5-5: Don't use 'Symbol' as a type.

Use lowercase primitives for consistency.
Safe fix: Use 'symbol' instead

(lint/complexity/noBannedTypes)

Additional comments not posted (2)
packages/plugins/trpc/res/client/v11/next.ts (1)

9-16: LGTM! The function is well-implemented.

The createTRPCNext function correctly integrates TRPC with Next.js, leveraging utility types for enhanced type safety and flexibility.

packages/plugins/trpc/res/client/v11/react.ts (1)

9-16: LGTM! The function is well-implemented.

The createTRPCReact function correctly integrates TRPC with React, leveraging utility types for enhanced type safety and flexibility.

packages/plugins/trpc/res/client/v11/utils.ts Show resolved Hide resolved
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: 7

Outside diff range, codebase verification and nitpick comments (2)
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/api/trpc/[trpc]/route.ts (1)

1-6: Potential issue with env import path

The imports for appRouter and createTRPCContext are correctly set up and verified. However, the env import might be incorrect or missing, as the expected env.ts file was not found in the relevant directory.

  • Ensure that the env.ts file exists in the correct path or update the import statement accordingly.
Analysis chain

Imports are relevant and necessary.

The imports are correctly set up for TRPC handler configuration. Ensure that the paths are correct and accessible.

Run the following script to verify the existence of the specified import paths:

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of the specified import paths in the route.ts file.

# Test: Check if the specified paths exist. Expect: Paths should exist.
fd 'root' --type f --glob 'packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/**'
fd 'trpc' --type f --glob 'packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/**'

Length of output: 497


Script:

#!/bin/bash
# Description: Verify the existence of the specific import files in the route.ts file.

# Search for the 'root' file that corresponds to the appRouter import
fd 'root.ts' --type f

# Search for the 'trpc' file that corresponds to the createTRPCContext import
fd 'trpc.ts' --type f

# Search for the 'env' file that corresponds to the env import
fd 'env.ts' --type f

Length of output: 440

packages/plugins/trpc/tests/projects/t3-trpc-v11/README.md (1)

23-23: Consider using a more formal term than 'awesome'.

The word "awesome" is informal. Consider using a more formal alternative like "excellent" or "comprehensive" to maintain a professional tone.

Tools
LanguageTool

[style] ~23-~23: Consider using a more formal and expressive alternative to ‘awesome’.
Context: ...-currently-available) — Check out these awesome tutorials You can check out the [creat...

(AWESOME)

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between dcfa0a8 and e33c499.

Files ignored due to path filters (10)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/package-lock.json is excluded by !**/package-lock.json, !**/*.json
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/package.json is excluded by !**/*.json
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/public/favicon.ico is excluded by !**/*.ico, !**/*.ico
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated/client/react.ts is excluded by !**/generated/**
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated/client/utils.ts is excluded by !**/generated/**
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated/helper.ts is excluded by !**/generated/**
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated/routers/Post.router.ts is excluded by !**/generated/**
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated/routers/User.router.ts is excluded by !**/generated/**
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated/routers/index.ts is excluded by !**/generated/**
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/tsconfig.json is excluded by !**/*.json
Files selected for processing (20)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/.eslintrc.cjs (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/.gitignore (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/README.md (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/next.config.js (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/prisma/schema.prisma (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/schema.zmodel (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/_components/post.tsx (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/api/trpc/[trpc]/route.ts (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/index.module.css (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/layout.tsx (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/page.tsx (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/env.js (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/root.ts (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated-router-helper.ts (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/trpc.ts (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/db.ts (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/styles/globals.css (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/query-client.ts (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/react.tsx (1 hunks)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/server.ts (1 hunks)
Files skipped from review due to trivial changes (4)
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/.gitignore
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/prisma/schema.prisma
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated-router-helper.ts
  • packages/plugins/trpc/tests/projects/t3-trpc-v11/src/styles/globals.css
Additional context used
LanguageTool
packages/plugins/trpc/tests/projects/t3-trpc-v11/README.md

[style] ~23-~23: Consider using a more formal and expressive alternative to ‘awesome’.
Context: ...-currently-available) — Check out these awesome tutorials You can check out the [creat...

(AWESOME)

Biome
packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/react.tsx

[error] 20-20: The assignment should not be in an expression.

The use of assignments in expressions is confusing.
Expressions are often considered as side-effect free.

(lint/suspicious/noAssignInExpressions)

Additional comments not posted (21)
packages/plugins/trpc/tests/projects/t3-trpc-v11/next.config.js (1)

5-5: Consider the implications of asynchronous import in configuration.

Using await import in a configuration file is unconventional and may lead to issues if the configuration is required synchronously. Ensure that this asynchronous behavior is intentional and handled appropriately.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/db.ts (1)

1-17: LGTM! Verify environment variable usage.

The setup for the Prisma client and the use of a global variable is well-implemented. Ensure that the environment variables, especially NODE_ENV, are correctly set and used across the application.

Run the following script to check the usage of env.NODE_ENV:

Verification successful

Environment Variable Usage Verified

The usage of env.NODE_ENV is consistent and correctly implemented across the application, particularly in the t3-trpc-v11 project files. The environment variable is accessed both directly and through the env object, which is a common practice for centralized management. No issues found.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of `env.NODE_ENV`.

# Test: Search for the usage of `env.NODE_ENV`. Expect: Correct usage across the application.
rg --type js --type ts 'env\.NODE_ENV'

Length of output: 1512

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/root.ts (1)

1-21: LGTM!

The setup for the primary router and the server-side caller factory is well-structured and clearly documented. The use of the factory pattern is a good practice for creating server-side callers.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/layout.tsx (1)

1-24: LGTM!

The RootLayout component is well-structured and integrates TRPC with Next.js effectively. The use of TRPCReactProvider ensures that TRPC is available throughout the application. The metadata setup is also appropriate for a Next.js app.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/query-client.ts (1)

1-25: LGTM!

The createQueryClient function is well-configured for SSR with appropriate serialization and deserialization using SuperJSON. The default staleTime is set to avoid immediate refetching, which is a good practice.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/trpc/server.ts (1)

1-30: LGTM!

The server setup for TRPC is efficient and well-structured. The use of cache for context and query client creation is optimal for performance. The integration with React Server Components is handled correctly.

packages/plugins/trpc/tests/projects/t3-trpc-v11/schema.zmodel (4)

4-11: Generator and datasource configuration looks good.

The setup for Prisma Client JS and SQLite datasource is standard for local development.


22-28: User model definition is correct.

The model is well-defined with appropriate constraints and relations.


30-41: Post model definition is correct.

The model is well-defined with appropriate constraints, relations, and indexing.


13-19: TRPC plugin configuration is well-defined.

The configuration aligns with TRPC v11 integration. Ensure that the specified paths are correct and accessible.

Run the following script to verify the existence of the specified paths:

Verification successful

To correctly verify the existence of the specified paths in the TRPC plugin configuration, let's adjust the script to search for the specific files without using unsupported glob patterns.


TRPC plugin paths are valid and accessible.

The generated-router-helper.ts file was found in the expected directory, confirming that the specified paths in the TRPC plugin configuration are correct. No further action is required.

  • Path verified: ./packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/routers/generated-router-helper.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence of the specified paths in the TRPC plugin configuration.

# Test: Check if the specified paths exist. Expect: Paths should exist.
fd 'generated-router-helper' --type f --glob 'packages/plugins/trpc/tests/projects/t3-trpc-v11/src/**'

Length of output: 245


Script:

#!/bin/bash
# Description: Verify the existence of the specified paths in the TRPC plugin configuration.

# Test: Check if the 'generated-router-helper' file exists in the expected directory.
fd 'generated-router-helper' --type f --exec echo "Found: {}"

Length of output: 169

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/api/trpc/[trpc]/route.ts (2)

12-16: Context creation function is correctly implemented.

The function provides necessary context for TRPC requests using headers from NextRequest.


18-34: Handler setup is correctly implemented.

The handler is well-configured for TRPC requests with appropriate error handling in development mode.

packages/plugins/trpc/tests/projects/t3-trpc-v11/.eslintrc.cjs (2)

1-6: Parser configuration looks good.

The parser is correctly set up for TypeScript projects with the project option enabled.


7-40: Plugins, extends, and rules configuration is comprehensive.

The configuration follows best practices for TypeScript projects with appropriate customizations.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/env.js (1)

1-42: Well-structured environment configuration.

The use of zod for environment variable validation is a good practice, and the file is well-documented.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/page.tsx (1)

1-37: Well-structured React component.

The component is well-organized, and the use of Next.js features and custom components is appropriate.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/app/index.module.css (1)

1-177: CSS styles are well-structured.

The CSS styles are consistent and well-organized.

packages/plugins/trpc/tests/projects/t3-trpc-v11/src/server/api/trpc.ts (4)

1-14: File header and imports look good.

The file header provides clear guidance on when to edit the file, and the necessary imports are included.


27-32: createTRPCContext function is well-implemented.

The function correctly sets up the context for tRPC procedures by including the database and request headers.


41-52: tRPC initialization is correctly configured.

The use of superjson as a transformer and the error formatter for ZodError ensures type-safe error handling.


59-59: createCallerFactory export is appropriate.

This export is necessary for enabling server-side calls in tRPC.

@ymc9 ymc9 merged commit 40ea9fa into dev Aug 23, 2024
12 of 13 checks passed
@ymc9 ymc9 deleted the feat/trpc-11 branch August 23, 2024 05:19
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.

1 participant