Skip to content

fix(server): onSuccess, onError, ... cannot use as a middleware#331

Merged
dinwwwh merged 1 commit intomainfrom
fix/server/onX-middleware-type-error
Apr 5, 2025
Merged

fix(server): onSuccess, onError, ... cannot use as a middleware#331
dinwwwh merged 1 commit intomainfrom
fix/server/onX-middleware-type-error

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Apr 5, 2025

Closes: https://discord.com/channels/1308966753044398161/1357918036568313906

Summary by CodeRabbit

  • Refactor
    • Improved internal processing to support more flexible outcomes, enhancing overall system stability.
  • Tests
    • Expanded test coverage for key processing stages to ensure consistent, reliable behavior.

@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 5, 2025

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

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 5, 2025 8:57am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 5, 2025

Walkthrough

This pull request introduces new tests for interceptor functions by adding assertions for type safety on the onStart, onSuccess, onError, and onFinish interceptors using a defined context structure. Additionally, the interceptor implementations have been revised to add a new generic parameter and update their return types—from a specialized promise type to a more flexible one (Promisable<TResult>). No changes were made to any exported or public entities.

Changes

File(s) Change Summary
packages/shared/src/.../interceptor.test-d.ts Added tests for onStart, onSuccess, onError, and onFinish interceptors with assertions for specific context types, the next function, and correct return value types.
packages/shared/src/.../interceptor.ts Updated type signatures for the interceptor functions: introduced a new generic type parameter and changed return types from PromiseWithError to Promisable<TResult>, allowing functions to return either a value or a promise.

Sequence Diagram(s)

sequenceDiagram
    participant C as Client
    participant I as Interceptor
    participant N as Next Middleware
    C->>I: onStart(context)
    I->>N: next()
    alt If Success
        N-->>I: data
        I->>C: onSuccess(data)
    else If Error
        N-->>I: error
        I->>C: onError(Error)
    end
    I->>C: onFinish(final state)
Loading

Possibly related PRs

  • unnoq/orpc#310: Adjusted return type handling in interceptor functions, aligning closely with the type modifications and flexibility introduced in this pull request.

Poem

I'm a rabbit on a coding spree,
Hopping through tests with type safety as key.
Interceptors march with flexible returns,
Each check and assertion a lesson learned.
My code garden blooms where no error can flee!
🐰💻😄

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 5, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 5, 2025

More templates

@orpc/arktype

npm i https://pkg.pr.new/@orpc/arktype@331

@orpc/client

npm i https://pkg.pr.new/@orpc/client@331

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@331

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@331

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@331

@orpc/react

npm i https://pkg.pr.new/@orpc/react@331

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@331

@orpc/server

npm i https://pkg.pr.new/@orpc/server@331

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@331

@orpc/solid-query

npm i https://pkg.pr.new/@orpc/solid-query@331

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@331

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@331

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@331

@orpc/svelte-query

npm i https://pkg.pr.new/@orpc/svelte-query@331

@orpc/valibot

npm i https://pkg.pr.new/@orpc/valibot@331

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@331

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@331

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@331

commit: b0de0cb

Copy link
Copy Markdown

@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

🧹 Nitpick comments (1)
packages/shared/src/interceptor.ts (1)

20-20: Consider updating comments for clarity.

The comments "Can used for interceptors or middlewares" have a minor grammar issue. Consider updating to "Can be used for interceptors or middlewares" or "Usable for both interceptors and middlewares".

-/**
- * Can used for interceptors or middlewares
- */
+/**
+ * Can be used for both interceptors and middlewares
+ */

Also applies to: 32-32, 45-45, 68-70

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ce2f84d and b0de0cb.

📒 Files selected for processing (2)
  • packages/shared/src/interceptor.test-d.ts (5 hunks)
  • packages/shared/src/interceptor.ts (5 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
packages/shared/src/interceptor.test-d.ts (1)
packages/shared/src/interceptor.ts (4)
  • onStart (23-30)
  • onSuccess (35-43)
  • onError (48-64)
  • onFinish (71-97)
packages/shared/src/interceptor.ts (5)
packages/shared/src/index.ts (1)
  • Promisable (13-13)
packages/server/src/index.ts (5)
  • onStart (42-42)
  • onSuccess (42-42)
  • onError (42-42)
  • ThrowableError (43-43)
  • onFinish (42-42)
packages/client/src/index.ts (5)
  • onStart (8-8)
  • onSuccess (8-8)
  • onError (8-8)
  • ThrowableError (9-9)
  • onFinish (8-8)
packages/shared/src/types.ts (2)
  • PromiseWithError (5-5)
  • ThrowableError (16-16)
packages/contract/src/index.ts (1)
  • ThrowableError (19-19)
🔇 Additional comments (12)
packages/shared/src/interceptor.test-d.ts (7)

1-2: Import organization looks good.

The imports for Context and middleware-related types are necessary for the new test cases.


5-5: Import from server package is appropriate.

The os import is needed for testing interceptors as middleware in a builder context.


15-20: Good test coverage for onStart middleware usage.

This test ensures type safety when using onStart as middleware with a typed context. The test verifies that both context and next function maintain their proper types throughout the middleware chain.


32-38: Good test coverage for onSuccess middleware usage.

This test properly verifies that the data parameter receives the correct type from the middleware result, and that context and next function maintain their proper types. This ensures type safety when using onSuccess as middleware.


50-56: Good test coverage for onError middleware usage.

This test validates type safety for the error handler, ensuring that the error parameter is of type Error and that context and next maintain their proper types when using onError as middleware.


68-69: Simple usage test for onFinish.

Good to include a minimal test case alongside the more complex one.


70-76: Good test coverage for onFinish middleware usage.

This test thoroughly verifies the state type with correct union typing for success and error cases, along with context and next type validation. This ensures type safety when using onFinish as middleware.

packages/shared/src/interceptor.ts (5)

18-18: Good change from PromiseWithError to Promisable.

Changing the Interceptor return type to Promisable<TResult> instead of the more specific PromiseWithError<TResult, TError> allows for more flexibility, enabling interceptors to work better as middleware. This aligns with the PR objective to fix interceptors for middleware usage.


23-25: Good flexibility improvement for onStart.

Adding the generic type parameter T and changing the return type to T | Promise<Awaited<ReturnType<TOptions['next']>>> makes the onStart interceptor more flexible. This change allows it to work both in traditional interceptor chains and as middleware.


35-37: Good flexibility improvement for onSuccess.

Adding the generic type parameter T and changing the return type to T | Promise<Awaited<ReturnType<TOptions['next']>>> makes the onSuccess interceptor more flexible. This change allows it to work both in traditional interceptor chains and as middleware.


48-54: Good flexibility improvement for onError.

Adding the generic type parameter T and changing the return type to T | Promise<Awaited<ReturnType<TOptions['next']>>> makes the onError interceptor more flexible. This change allows it to work both in traditional interceptor chains and as middleware.


71-80: Good flexibility improvement for onFinish.

Adding the generic type parameter T and changing the return type to T | Promise<Awaited<ReturnType<TOptions['next']>>> makes the onFinish interceptor more flexible. This change allows it to work both in traditional interceptor chains and as middleware.

@dinwwwh dinwwwh merged commit 3191601 into main Apr 5, 2025
8 checks passed
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