-
Notifications
You must be signed in to change notification settings - Fork 401
fix(tanstack-react-start): Do not consume the body of the request in middleware #7020
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(tanstack-react-start): Do not consume the body of the request in middleware #7020
Conversation
🦋 Changeset detectedLatest commit: 51741b2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
|
@p9f is attempting to deploy a commit to the Clerk Production Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughWraps incoming requests with a patched ClerkRequest in clerkMiddleware to avoid consuming the request body; updates loadOptions to accept a ClerkRequest type and reference its URL/clerkUrl for proxy and domain resolution; adds a changeset documenting the bug fix. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Middleware as clerkMiddleware
participant Patch as patchRequest
participant ClerkReq as ClerkRequest
participant LoadOpts as loadOptions
participant Handler as Route Handler
Client->>Middleware: HTTP POST (with body)
Middleware->>Patch: patchRequest(args.request)
Patch-->>ClerkReq: wrapped ClerkRequest
Middleware->>LoadOpts: loadOptions(ClerkRequest, ...)
LoadOpts->>LoadOpts: read request.url / request.clerkUrl (no body read)
Middleware->>Handler: forward original request to handler
Handler->>Handler: safely read request.body
Handler-->>Client: response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch, thanks!
…body-of-the-request-in-middleware
There was a problem hiding this 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
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
.changeset/full-horses-repair.md(1 hunks)packages/tanstack-react-start/src/server/loadOptions.ts(3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- .changeset/full-horses-repair.md
🧰 Additional context used
📓 Path-based instructions (6)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
**/*.{js,jsx,ts,tsx}: All code must pass ESLint checks with the project's configuration
Follow established naming conventions (PascalCase for components, camelCase for variables)
Maintain comprehensive JSDoc comments for public APIs
Use dynamic imports for optional features
All public APIs must be documented with JSDoc
Provide meaningful error messages to developers
Include error recovery suggestions where applicable
Log errors appropriately for debugging
Lazy load components and features when possible
Implement proper caching strategies
Use efficient data structures and algorithms
Profile and optimize critical paths
Validate all inputs and sanitize outputs
Implement proper logging with different levels
Files:
packages/tanstack-react-start/src/server/loadOptions.ts
**/*.{js,jsx,ts,tsx,json,css,scss,md,yaml,yml}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use Prettier for consistent code formatting
Files:
packages/tanstack-react-start/src/server/loadOptions.ts
packages/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/tanstack-react-start/src/server/loadOptions.ts
packages/**/*.{ts,tsx,d.ts}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Packages should export TypeScript types alongside runtime code
Files:
packages/tanstack-react-start/src/server/loadOptions.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use proper TypeScript error types
**/*.{ts,tsx}: Always define explicit return types for functions, especially public APIs
Use proper type annotations for variables and parameters where inference isn't clear
Avoidanytype - preferunknownwhen type is uncertain, then narrow with type guards
Useinterfacefor object shapes that might be extended
Usetypefor unions, primitives, and computed types
Preferreadonlyproperties for immutable data structures
Useprivatefor internal implementation details
Useprotectedfor inheritance hierarchies
Usepublicexplicitly for clarity in public APIs
Preferreadonlyfor properties that shouldn't change after construction
Prefer composition and interfaces over deep inheritance chains
Use mixins for shared behavior across unrelated classes
Implement dependency injection for loose coupling
Let TypeScript infer when types are obvious
Useconst assertionsfor literal types:as const
Usesatisfiesoperator for type checking without widening
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Use type-only imports:import type { ... } from ...
Noanytypes without justification
Proper error handling with typed errors
Consistent use ofreadonlyfor immutable data
Proper generic constraints
No unused type parameters
Proper use of utility types instead of manual type construction
Type-only imports where possible
Proper tree-shaking friendly exports
No circular dependencies
Efficient type computations (avoid deep recursion)
Files:
packages/tanstack-react-start/src/server/loadOptions.ts
**/*.{js,ts,tsx,jsx}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Support multiple Clerk environment variables (CLERK_, NEXT_PUBLIC_CLERK_, etc.) for configuration.
Files:
packages/tanstack-react-start/src/server/loadOptions.ts
🧬 Code graph analysis (1)
packages/tanstack-react-start/src/server/loadOptions.ts (3)
packages/remix/src/ssr/loadOptions.ts (1)
loadOptions(13-88)packages/tanstack-react-start/src/server/types.ts (1)
LoaderOptions(28-28)packages/tanstack-react-start/src/utils/env.ts (1)
getPublicEnvVariables(4-25)
🔇 Additional comments (3)
packages/tanstack-react-start/src/server/loadOptions.ts (3)
1-1: LGTM: Type-only import is correct.The change to
import typeis appropriate sinceClerkRequestis used only as a type annotation. This follows TypeScript best practices and enables better tree-shaking.
28-33: Consistent usage of ClerkRequest properties.The proxyUrl construction correctly uses
request.clerkUrl(line 30), consistent with the updated function signature. This change aligns with the PR objective to avoid re-creating ClerkRequest internally (which could consume the request body).
20-22: ClerkRequest has both required properties—no issues found.The verification confirms that
ClerkRequest(extendingRequest) provides theurlproperty through inheritance and explicitly definesclerkUrlas a readonly property. The code at lines 20-22 is type-safe and correct.
|
|
||
| export const loadOptions = (request: Request, overrides: LoaderOptions = {}) => { | ||
| const clerkRequest = createClerkRequest(patchRequest(request)); | ||
| export const loadOptions = (request: ClerkRequest, overrides: LoaderOptions = {}) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Add explicit return type annotation.
The function signature lacks an explicit return type. Per coding guidelines, public APIs should have explicit return types for clarity and to prevent unintended type widening.
As per coding guidelines.
Apply this diff:
-export const loadOptions = (request: ClerkRequest, overrides: LoaderOptions = {}) => {
+export const loadOptions = (request: ClerkRequest, overrides: LoaderOptions = {}): ReturnType<typeof loadOptions> => {Alternatively, define an explicit interface for the return type:
interface LoadOptionsResult {
secretKey: string;
machineSecretKey?: string;
publishableKey: string;
jwtKey?: string;
apiUrl: string;
domain: string;
isSatellite: boolean;
proxyUrl?: string;
signInUrl: string;
signUpUrl: string;
afterSignInUrl: string;
afterSignUpUrl: string;
// ... other properties from overrides
}
export const loadOptions = (request: ClerkRequest, overrides: LoaderOptions = {}): LoadOptionsResult => {Note: This change from Request to ClerkRequest is a breaking API change. Ensure this is documented in the changeset.
…middleware (#7020) Co-authored-by: Robert Soriano <sorianorobertc@gmail.com>
Description
Should fix #7019.
I am not familiar with the codebase so please feel free to close if not appropriate.
But i took inspiration fromt a similar implementation in the @clerk/react-router (here), and this seems to fix the linked issue.
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit
Bug Fixes
Documentation