-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: improve request body type definition and normalize wallet address #31
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
WalkthroughUpdates the register route to normalize wallet addresses, centralize request body typing, and handle unique-constraint race conditions by retrying and returning the existing user on P2002 errors. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
actor C as Client
participant R as Register Route
participant DB as Database
C->>R: POST /register { walletAddress, ... }
R->>R: Normalize walletAddress (trim + lowercase)
R->>DB: findUnique(normalizedWalletAddress)
alt User exists
DB-->>R: user
R-->>C: 200 OK (existing user)
else Create path
R->>DB: create(user with normalizedWalletAddress)
DB-->>R: created user
R-->>C: 201 Created (new user)
end
opt Unique constraint race
note over DB,R: create throws P2002 (unique violation)
R->>DB: findUnique(normalizedWalletAddress)
alt Found after conflict
DB-->>R: user
R-->>C: 200 OK (existing user)
else Unexpected error
DB-->>R: error
R-->>C: 500/handled error
end
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests
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 (1)
src/routes/register.ts (1)
15-20
: Derive RegisterRequestBody directly from the schemaNice to see the request body constrained, but the manual
RegisterRequestBody
definition can drift from theType.Object
validator (e.g., if we tweak field names, optionality, or formats). You can keep runtime and compile-time definitions in lockstep by lifting the schema into a constant and deriving the type viaStatic<typeof schema>
, then reuse that schema in the route config.-import { Type } from "@sinclair/typebox"; +import { Static, Type } from "@sinclair/typebox"; … -// Define the expected request body type for clarity and reusability -type RegisterRequestBody = { - walletAddress: string; - message: string; - signature: string; -}; +const registerRequestBodySchema = Type.Object({ + walletAddress: Type.RegExp(/^0x[a-fA-F0-9]{40}$/), + message: Type.String({ minLength: 1, maxLength: 1000 }), + signature: Type.RegExp(/^0x[a-fA-F0-9]{130}$/), +}); +type RegisterRequestBody = Static<typeof registerRequestBodySchema>;Then wire it up with
body: registerRequestBodySchema
. This keeps types and validation aligned automatically.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/routes/register.ts
(4 hunks)
🔇 Additional comments (1)
src/routes/register.ts (1)
54-104
: Address normalization + race-condition handling looks solidTrimming/lowercasing before both the lookup and the create, plus the fallback fetch on
P2002
, closes the race nicely while keeping the response consistent.
Three improvements have been implemented in
register.ts
:RegisterRequestBody
type for the request body for better type safety and clarity.Summary by CodeRabbit