-
Notifications
You must be signed in to change notification settings - Fork 419
Refactor createClerkRequest
#7421
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
Conversation
…es instead of prototype
🦋 Changeset detectedLatest commit: 4bb04af The changes in this PR will be included in the next version bump. This PR includes changesets to release 10 packages
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 |
|
@chris-kreidl is attempting to deploy a commit to the Clerk Production Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughFixes a bug in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5–10 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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
packages/backend/src/tokens/clerkRequest.ts (1)
84-90: Bugfix logic looks correct; consider extracting a proper type guard forClerkRequest.The new shape-based check on
clerkUrlandcookiesis a good way to avoid the unreliableinstanceofbehavior while still reusing an existingClerkRequestwhen present. This should prevent rawRequestinstances from slipping through and aligns with the described TanStack Start failure path.One refinement: instead of an inline
isClerkRequestboolean plusas ClerkRequestcast, you can codify this as a dedicated type guard. That both documents the contract and lets TypeScript narrow correctly without a cast:+const isClerkRequest = (value: unknown): value is ClerkRequest => { + return ( + !!value && + typeof value === 'object' && + 'clerkUrl' in value && + 'cookies' in value + ); +}; + export const createClerkRequest = (...args: ConstructorParameters<typeof ClerkRequest>): ClerkRequest => { - const isClerkRequest = args[0] && - typeof args[0] === 'object' && - 'clerkUrl' in args[0] && - 'cookies' in args[0]; - - return isClerkRequest ? args[0] as ClerkRequest : new ClerkRequest(...args); + if (isClerkRequest(args[0])) { + return args[0]; + } + + return new ClerkRequest(...args); };This keeps the runtime behavior the same, removes the cast, and follows the guideline of using explicit
isType(value: unknown): value is Typeguards..changeset/full-frogs-play.md (1)
1-5: Minor wording/grammar polish for the changeset entry (optional).The description is clear and accurate. You could tighten the last sentence a bit and add a period, which also addresses the “whether or not” style hint:
-Instead of checking `instanceof`, we check for the presence of the `cookies` and `clerkUrl` keys to determine whether or not we're dealing with a `Request` or `ClerkRequest` +Instead of checking `instanceof`, we now check for the presence of the `cookies` and `clerkUrl` keys to determine whether we're dealing with a `Request` or `ClerkRequest`.Purely stylistic; no need to block on this.
📜 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-frogs-play.md(1 hunks)packages/backend/src/tokens/clerkRequest.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (9)
**/*.{js,jsx,ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
All code must pass ESLint checks with the project's configuration
Files:
packages/backend/src/tokens/clerkRequest.ts
**/*.{js,jsx,ts,tsx,json,md,yml,yaml}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use Prettier for consistent code formatting
Files:
packages/backend/src/tokens/clerkRequest.ts
packages/**/src/**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
TypeScript is required for all packages
Files:
packages/backend/src/tokens/clerkRequest.ts
**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Follow established naming conventions (PascalCase for components, camelCase for variables)
Files:
packages/backend/src/tokens/clerkRequest.ts
packages/**/src/**/*.{ts,tsx,js,jsx}
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
packages/**/src/**/*.{ts,tsx,js,jsx}: Maintain comprehensive JSDoc comments for public APIs
Use tree-shaking friendly exports
Validate all inputs and sanitize outputs
All public APIs must be documented with JSDoc
Use dynamic imports for optional features
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
Implement proper logging with different levels
Files:
packages/backend/src/tokens/clerkRequest.ts
**/*.ts?(x)
📄 CodeRabbit inference engine (.cursor/rules/development.mdc)
Use proper TypeScript error types
Files:
packages/backend/src/tokens/clerkRequest.ts
**/*.{ts,tsx}
📄 CodeRabbit inference engine (.cursor/rules/typescript.mdc)
**/*.{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
Implement type guards forunknowntypes using the patternfunction isType(value: unknown): value is Type
Useinterfacefor object shapes that might be extended
Usetypefor unions, primitives, and computed types
Preferreadonlyproperties for immutable data structures
Useprivatefor internal implementation details in classes
Useprotectedfor inheritance hierarchies
Usepublicexplicitly for clarity in public APIs
Use mixins for shared behavior across unrelated classes in TypeScript
Use generic constraints with bounded type parameters like<T extends { id: string }>
Use utility types likeOmit,Partial, andPickfor data transformation instead of manual type construction
Use discriminated unions instead of boolean flags for state management and API responses
Use mapped types for transforming object types
Use conditional types for type-level logic
Leverage template literal types for string manipulation at the type level
Use ES6 imports/exports consistently
Use default exports sparingly, prefer named exports
Document functions with JSDoc comments including @param, @returns, @throws, and @example tags
Create custom error classes that extend Error for specific error types
Use the Result pattern for error handling instead of throwing exceptions
Use optional chaining (?.) and nullish coalescing (??) operators for safe property access
Let TypeScript infer obvious types to reduce verbosity
Useconst assertionswithas constfor literal types
Usesatisfiesoperator for type checking without widening types
Declare readonly arrays and objects for immutable data structures
Use spread operator and array spread for immutable updates instead of mutations
Use lazy loading for large types...
Files:
packages/backend/src/tokens/clerkRequest.ts
**/*.{js,ts,jsx,tsx}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Use ESLint with custom configurations tailored for different package types
Files:
packages/backend/src/tokens/clerkRequest.ts
**/*.{js,ts,jsx,tsx,json,md,yml,yaml}
📄 CodeRabbit inference engine (.cursor/rules/monorepo.mdc)
Use Prettier for code formatting across all packages
Files:
packages/backend/src/tokens/clerkRequest.ts
🪛 LanguageTool
.changeset/full-frogs-play.md
[style] ~5-~5: Consider shortening this phrase to just ‘whether’, unless you mean ‘regardless of whether’.
Context: ...okiesandclerkUrlkeys to determine whether or not we're dealing with aRequestorCler...
(WHETHER)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: semgrep-cloud-platform/scan
|
Hello, thanks for this and the effort to find the issue! I will open another PR to run the integration tests properly and add you as co-author and reference this PR. |
Description
createClerkRequestin clerk/backend checks to see ifargs[0]is aninstanceof ClerkRequest. If it is, it returnsargs[0]untouched. If not, it instantiates a new instance.The TanStack Start middleware calls
createClerkRequestusing the return value of[patchRequest](https://github.com/clerk/javascript/blob/7b7c90f7a0c2258be62a8e851f9449772f7ee3cc/packages/react-router/src/server/utils.ts#L125-L140)as its argument.patchRequestreturns an instance ofRequestFor some reason, one I haven't figured out yet, the check in
createClerkRequestis accepting aRequestinstance and returning theRequestuntouched.If I patch
createClerkRequestto read as follows:I am presented with the following:
This PR refactors
createClerkRequestto check for the keys that are unique toClerkRequestas compared toRequest, bypassing this apparently faultyinstanceofcheck.Fixes #6996
Checklist
pnpm testruns as expected.pnpm buildruns as expected.Type of change
Summary by CodeRabbit
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.