Skip to content

fix: .next broken when calling multiple times#337

Merged
dinwwwh merged 1 commit intomainfrom
fix/dot-next-on-multiple-call
Apr 7, 2025
Merged

fix: .next broken when calling multiple times#337
dinwwwh merged 1 commit intomainfrom
fix/dot-next-on-multiple-call

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Apr 7, 2025

Summary by CodeRabbit

  • Refactor
    • Streamlined internal processing logic to ensure more consistent and reliable handling of sequential operations.
  • Tests
    • Expanded test coverage by adding scenarios that validate the system's behavior when executing multiple sequential steps.
    • Improved test setup for more accurate state management between test runs.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 7, 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 7, 2025 4:16am

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 7, 2025

Walkthrough

This PR updates both test and production code related to middleware and interceptor flows. In the tests, it replaces vi.clearAllMocks() with vi.resetAllMocks() and adds a new test case to verify multiple calls to the next function. In the production code, the middleware execution in the procedure client and interceptor handling have been refactored—updating the next function to take explicit parameters (index, context, input) and streamlining input/output validation. No public API declarations were modified.

Changes

File(s) Change Summary
packages/server/src/procedure-client.test.ts, packages/shared/src/interceptor.test.ts Updated test setups by replacing vi.clearAllMocks() with vi.resetAllMocks() and added a "can multiple .next calls" test case to verify sequential middleware/interceptor invocations and proper context updates.
packages/server/src/procedure-client.ts, packages/shared/src/interceptor.ts Refactored middleware and interceptor logic: the next function now accepts explicit parameters (index, context, input), streamlining input validation, output processing, and interceptor index handling without relying on mutable state.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Procedure Test
    participant PC as Procedure Client
    participant MW as Middleware (multiple)
    participant Handler as Final Handler

    Test->>PC: Invoke procedure client
    PC->>MW: Call next(index, context, input)
    MW-->>PC: Return output and update context
    PC->>MW: Call next(index+1, mergedContext, newInput)
    MW-->>PC: Return output
    PC->>Handler: Execute final handler with context
    Handler-->>PC: Return final result
    PC-->>Test: Output aggregated responses
Loading
sequenceDiagram
    participant Test as Interceptor Test
    participant Int as intercept Function
    participant I1 as Interceptor1
    participant I2 as Interceptor2
    participant Main as Main Function

    Test->>Int: Call intercept with options and main
    Int->>I1: Invoke interceptor at index 0
    I1->>Int: Call next(index+1) with updated options
    Int->>I2: Invoke interceptor at index 1
    I2->>Int: Call next(index+1) with updated options
    Int->>Main: Invoke main function
    Main-->>Int: Return result
    I2-->>Int: Return result after processing
    I1-->>Int: Return result after processing
    Int-->>Test: Return aggregated results
Loading

Possibly related PRs

  • unnoq/orpc#245: This PR also focuses on modifications to middleware execution logic and context handling in the createProcedureClient function, establishing a strong connection with the changes in this PR.

Poem

In a code garden I happily hop,
Changing mocks and middleware non-stop,
With explicit indexes I guide the flow,
Through tests and interceptors, watch them go!
A rabbit’s cheer in every line—
Coding magic, so spry and fine! 🐇

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

 WARN  GET http://10.0.0.28:4873/@antfu/eslint-config/-/eslint-config-3.16.0.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/typescript/-/typescript-5.7.3.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/vitest/-/vitest-3.0.5.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@eslint-react/eslint-plugin/-/eslint-plugin-1.26.2.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-5.1.0.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-react-refresh/-/eslint-plugin-react-refresh-0.4.19.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-ban/-/eslint-plugin-ban-2.0.0.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint/-/eslint-9.20.1.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-format/-/eslint-plugin-format-0.1.3.tgz error (ERR_PNPM_FETCH_503). Will retry in 10 seconds. 2 retries left.
 WARN  GET http://10.0.0.28:4873/@antfu/eslint-config/-/eslint-config-3.16.0.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/typescript/-/typescript-5.7.3.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/vitest/-/vitest-3.0.5.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/@eslint-react/eslint-plugin/-/eslint-plugin-1.26.2.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-5.1.0.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-react-refresh/-/eslint-plugin-react-refresh-0.4.19.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-ban/-/eslint-plugin-ban-2.0.0.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint/-/eslint-9.20.1.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 WARN  GET http://10.0.0.28:4873/eslint-plugin-format/-/eslint-plugin-format-0.1.3.tgz error (ERR_PNPM_FETCH_503). Will retry in 1 minute. 1 retries left.
 ERR_PNPM_FETCH_503  GET http://10.0.0.28:4873/typescript/-/typescript-5.7.3.tgz: Service Unavailable - 503

This error happened while installing a direct dependency of /tmp/eslint

✨ 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 7, 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 7, 2025

More templates

@orpc/arktype

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

@orpc/client

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

@orpc/contract

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

@orpc/openapi

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

@orpc/openapi-client

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

@orpc/react-query

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

@orpc/react

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

@orpc/server

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

@orpc/shared

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

@orpc/solid-query

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

@orpc/standard-server

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

@orpc/standard-server-fetch

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

@orpc/standard-server-node

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

@orpc/svelte-query

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

@orpc/valibot

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

@orpc/vue-colada

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

@orpc/vue-query

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

@orpc/zod

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

commit: 665dd5a

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/server/src/procedure-client.test.ts (1)

483-512: Great test coverage for multiple .next calls

This test case effectively verifies that multiple calls to next() within middleware work correctly, checking both the output array and the proper context propagation between calls. This directly addresses the issue mentioned in the PR title.

However, consider using a type-safe approach instead of (handler as any).mock.calls:

-  expect((handler as any).mock.calls[0][0].context.preMid2).toBe(0)
+  expect(handler.mock.calls[0][0].context.preMid2).toBe(0)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6b06c96 and 665dd5a.

📒 Files selected for processing (4)
  • packages/server/src/procedure-client.test.ts (2 hunks)
  • packages/server/src/procedure-client.ts (1 hunks)
  • packages/shared/src/interceptor.test.ts (1 hunks)
  • packages/shared/src/interceptor.ts (1 hunks)
🧰 Additional context used
🧬 Code Definitions (2)
packages/server/src/procedure-client.ts (2)
packages/server/src/context.ts (2)
  • Context (1-1)
  • mergeCurrentContext (11-16)
packages/server/src/middleware.ts (1)
  • middlewareOutputFn (64-66)
packages/shared/src/interceptor.test.ts (1)
packages/shared/src/interceptor.ts (1)
  • intercept (101-120)
🔇 Additional comments (4)
packages/server/src/procedure-client.test.ts (1)

48-48: Improved test isolation by using resetAllMocks

Replacing clearAllMocks() with resetAllMocks() is a good change. While clearAllMocks() only clears the record of calls, resetAllMocks() additionally resets all mock implementations to their initial state, providing better isolation between tests.

packages/shared/src/interceptor.test.ts (1)

119-152: Good test case for multiple interceptor .next calls

This test case thoroughly verifies that the intercept function properly handles multiple calls to next(). It creates an interceptor that calls next() three times and verifies:

  1. The result is an array containing the expected values
  2. Each interceptor is called the expected number of times
  3. The arguments passed to each function are correct

This test works well with the refactored implementation in interceptor.ts.

packages/server/src/procedure-client.ts (1)

165-196: Improved middleware execution with explicit parameters

The refactored next function with explicit parameters (index, context, input) is a significant improvement over using closures to manage state. This change:

  1. Makes the flow of data through middleware more predictable
  2. Explicitly tracks the execution context through each middleware call
  3. Fixes the bug with multiple .next calls by ensuring proper context and index management

The use of mergeCurrentContext ensures that context is properly accumulated during multiple calls.

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

106-120: Enhanced interceptor execution with explicit index tracking

This change improves the intercept function by making the next function take an explicit index parameter instead of relying on mutable state. This:

  1. Makes the interceptor chain more predictable
  2. Allows for multiple .next calls without index tracking issues
  3. Aligns with the pattern used in the procedure client

The explicit passing of index + 1 to the next call ensures proper progression through the interceptor chain.

@dinwwwh dinwwwh merged commit 25532a8 into main Apr 7, 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