Add route handler protocol body helper#2706
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (13)
📝 WalkthroughWalkthroughThis PR introduces a typed first-party route handler protocol that standardizes request bodies for ChangesRoute Handler Protocol
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
drew-harris
left a comment
There was a problem hiding this comment.
Thanks for taking initiative to submit this PR. I think currently this diff does a bit too much and instead we would prefer to export the bare minimum typescript type if we can get away with that.
export type InstantRouteHandlerPayloadByType = {
'sync-user': {
appId: string;
user: User | null;
};
};
/** A valid request body for Instant's first-party route handler protocol. */
export type InstantRouteHandlerBody<
Type extends InstantRouteHandlerType = InstantRouteHandlerType,
> = {
[KnownType in Type]: {
type: KnownType;
} & InstantRouteHandlerPayloadByType[KnownType];
}[Type];Would exporting these 2 types from all packages be enough for your specific usecase / reasoning for opening the PR?
We don't feel the need for any tests on this code and we prefer to use the satisfies keyword over a constructor method. For the raw body type, we think end users creating a zod / validator schema for a body is a better alternative.
Reference Discord Thread
This PR introduces a unified protocol for utils working with
firstPartyPath.Benefits
Avoids client/server drift — adds a shared
routeHandlerProtocol.tscontract used by bothReactorwhen posting auth sync updates andcreateInstantRouteHandlerwhen receiving them.Keeps future payload types easy to add — models request bodies through a payload-by-type map, so new route-handler request types can extend the map and automatically flow into the derived body union.
Provides a consistent body factory — adds
createInstantRouteHandlerBody('sync-user', { appId, user })so protocol bodies are constructed from one helper instead of hand-written object literals.Improves custom handler DX — exports
InstantRouteHandlerBody,InstantRouteHandlerRawBody,InstantRouteHandlerType, andInstantRouteHandlerPayloadByTypeso custom route handlers can type the payload they receive without duplicating the SDK's internal shape.Documents intended behavior through tests — adds runtime and type-level coverage for the helper, the existing route handler behavior, unknown request types, and the exported type contract.
Validation