fix(server): onSuccess, onError, ... cannot use as a middleware#331
fix(server): onSuccess, onError, ... cannot use as a middleware#331
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
WalkthroughThis pull request introduces new tests for interceptor functions by adding assertions for type safety on the Changes
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)
Possibly related PRs
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅ 📢 Thoughts on this report? Let us know! |
More templates
@orpc/arktype
@orpc/client
@orpc/openapi
@orpc/openapi-client
@orpc/contract
@orpc/react
@orpc/react-query
@orpc/server
@orpc/shared
@orpc/solid-query
@orpc/standard-server
@orpc/standard-server-fetch
@orpc/standard-server-node
@orpc/svelte-query
@orpc/valibot
@orpc/vue-colada
@orpc/vue-query
@orpc/zod
commit: |
There was a problem hiding this comment.
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
📒 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 specificPromiseWithError<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
Tand changing the return type toT | 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
Tand changing the return type toT | 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
Tand changing the return type toT | 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
Tand changing the return type toT | 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.
Closes: https://discord.com/channels/1308966753044398161/1357918036568313906
Summary by CodeRabbit