question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Type-safe request extensions

See original GitHub issue

Is 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:open
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
JozefFlakuscommented, Feb 20, 2021

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:

const setUserProperties$ = r.pipe(
    r.matchPath('/:id'),
    r.matchType('PUT'),
    r.useEffect(req$ => req$.pipe(
      use(authenticate$),  // 👈
    )),
);
0reactions
sjanotacommented, Feb 19, 2021

I pressed “Comment” one test too early 😄.

I noticed that both use and useEffect return IxBuilder parametrized with HttpRequest 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:

interface RouteEffect<I extends HttpRequest = HttpRequest, O extends HttpRequest = HttpRequest> {
  path: string;
  method: HttpMethod;
  effect: HttpEffect<O, HttpEffectResponse>;
  middleware?: HttpMiddlewareEffect<I, O>;
  meta?: RouteMeta;
}

And RouteEffectSpec should expose those types too so use and useEffect 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, as RouteCombinerConfig accepts an array of middlewares.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found