feat: Strongly-typed route path parameters using template literals - #19
Merged
Conversation
…and generic handler interfaces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Description: Strongly-Typed Path Parameters via TS Template Literal Type Inference
This PR introduces compile-time path parameter type extraction to Volten's routing methods. Developers will now enjoy auto-completion and type safety when accessing
ctx.paramsvariables directly based on the route string pattern.High-Level Overview
Previously,
ctx.paramswas typed asRecord<string, string>, meaning accessing a route parameter did not guarantee the parameter existed on the path, requiring unsafe casts or unchecked property lookups.This PR adds advanced type level helpers to Volten. When defining a route, such as
app.get("/users/:userId/posts/:postId", (ctx) => { ... }), the keys"userId"and"postId"are parsed directly from the path string literal type.ctx.paramsis then dynamically typed as{ userId: string; postId: string }.Key Technical Changes
1. Compile-Time Path Parsing Type Helpers (
src/core/types.ts)ExtractParamKeys<T>type helper mapping path segments recursively::id->"id").*->"*").ExtractParams<T>type helper converting path literals to key-value objects.VoltenHandler,PreflightHandler, andErrorHandlersignatures to propagate the generic path string literal parameterP.2. Router API Generic Integrations (
src/core/router.ts)get,post,patch,put,delete) to define<P extends string>generics.VoltenHandler<P>[].3. Context Params Mapping (
src/utils/requestCtx.ts)RequestContextto accept generic parameter<P extends string = string>.ctx.paramsproperty typing to matchExtractParams<P>instead of generalParams.4. Tests Added (
tests/unit/core/types.test.ts[NEW])Breaking Changes
VoltenHandlermight require updating signatures to accept or default a type parameterP extends string.ctx.paramsto a generic dictionary might require casting or widening if the route path is statically typed.