Type-safe request extensions
See original GitHub issueIs your feature request related to a problem? Please describe.
Right now HttpRequest
is can be assigned any property. Thanks to that you can pass custom properties down the stream as middleware-jwt does.
However, custom properties are of type any
. I can’t see a way to make them type-safe. Am I missing something?
Describe the solution you’d like
I’d like to pass custom properties in a type-safe manner as it is done with params
, query
, and body
.
Describe alternatives you’ve considered
I tried to extend the HttpRequest
type and make my middleware return the extended version, but with no success.
Additional context
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Typesafe Config Extensions - GitHub
Typesafe Config Extensions. Libraries supporting Typesafe Config. This project's release artifacts are available in the Maven Central Repository.
Read more >Typesafe updaters FAQ - Relay
Typesafe updaters is the name given to a project to provide a typesafe and ergonomic alternative to the existing APIs for imperatively updating...
Read more >Specialized extensions using generic type constraints
In this article, let's take a look at how that keyword can be applied to extensions, and what sort of patterns that can...
Read more >node.js - Extend Express Request object using Typescript
Inside the express directory create a file and name it index.d.ts (MUST BE EXACTLY LIKE THAT); Finally to make the extension of the...
Read more >Extension types in TypeScript - Medium
If you are a consumer of a library, a pull-request giving it ... Intersection extensions can be achieved with a generic type (let's...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have to analyze this topic deeper but the problem that you described is already known. For now
r.use
doesn’t infer types from middlewares but you can achieve the same thing using a similar approach:I pressed “Comment” one test too early 😄.
I noticed that both
use
anduseEffect
returnIxBuilder
parametrized withHttpRequest
as the last parameter. After I changed that to the types they are parametrized with it type-checks as expected. I’m talking of course about the declaration file in the npm bundle, not the source code.Looks like
RouteEffect
could be defined as follows:And
RouteEffectSpec
should expose those types too souse
anduseEffect
can use it. It’s doable, I can even prepare a PR if you find it feasible.However, it would be great if one could achieve the same thing with
combineRoutes
. I think it’s impossible without changing the API, asRouteCombinerConfig
accepts an array of middlewares.