Skip to content

[Feature request] Typescript - Support generic request / response types #244

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

Open
jrbarnard opened this issue Jul 17, 2023 · 6 comments · May be fixed by #284
Open

[Feature request] Typescript - Support generic request / response types #244

jrbarnard opened this issue Jul 17, 2023 · 6 comments · May be fixed by #284
Milestone

Comments

@jrbarnard
Copy link

Currently the Request and Response types are defined without any easy way to override them.
I would like to be able to either supply generic response types or extended request types for a given route which allows me to have clearly defined types for the input/output of that route.

E.g if I have a route that should return a given shape of JSON, currently the .json method on response expects any, however if I could define the route with a Response type of a given shape, typescript would fail to compile if I had implemented the transformation in my route wrong.

And for request extension, one use case is you have middleware that does your validation, so by the time you get to your route, you know the shape of your request. E.g which path params are present, or query string params are required and so on. It would be nice to be able to define a request extension for that route to avoid unnecessary checks or casting or non null assertions.

I'm happy to work on this and submit a couple of PRs, but would be keen to get some feedback first of all on how we'd want this to work / any considerations that need to be taken into account before hand.

A rough code example:

import { Request, Response } from 'lambda-api';

interface MyDefinedRequest extends Request {
  params: {
    thingId: string;
    anotherThingId: string;
  };
  context: Context & { metrics: MetricsLogger };
}

interface MyDefinedResponseBody {
  hello: string;
  foo: string;
}

export default (request: MyDefinedRequest, response: Response<MyDefinedResponseBody>): void => {
  const { metrics } = request.context;

  metrics.setProperty('test', true);

  const { thingId, anotherThingId } = request.params;

  response.json({
    hello: thingId,
    foo: anotherThingId,
  });
};

@naorpeled
Copy link
Collaborator

@jrbarnard great suggestion!

Will give this some thought and keep you updated with my thoughts :)

@phillip-le
Copy link

@naorpeled I would definitely love to see the support for generics. Have you had a chance to think about this?

@naorpeled
Copy link
Collaborator

@naorpeled I would definitely love to see the support for generics. Have you had a chance to think about this?
Hey, yes 🙏
What @jrbarnard suggested sounds great.

I'm planning on working on it, if anyone else wants to let me know 🙏
Sorry it's taking a long time

@antpingelli
Copy link

@naorpeled I have been using res.locals to hold authentication information passed to handlers from a middleware as is the pattern in Express. I was looking into transitioning to TS but I see that there is no way currently to support this as I would need to extend the Response object. Would the change you are planning to make support this functionality?

Just started learning TS so I may have missed something, apologies!

@naorpeled naorpeled added this to the v1.2.0 milestone Dec 31, 2024
@naorpeled naorpeled linked a pull request Feb 6, 2025 that will close this issue
@naorpeled
Copy link
Collaborator

Hey everyone,
the type defs rework PR is now ready for review:

#284

if anyone has time to take a look please let me know what you think as your guys' feedback is super important to me 🙏

@naorpeled
Copy link
Collaborator

@naorpeled I have been using res.locals to hold authentication information passed to handlers from a middleware as is the pattern in Express. I was looking into transitioning to TS but I see that there is no way currently to support this as I would need to extend the Response object. Would the change you are planning to make support this functionality?

Just started learning TS so I may have missed something, apologies!

Hey @antpingelli,
sorry for the huge delay, missed your comment,
at the moment we don't have a locals property but you can create a new property on the req/res and update the typescript types using this method, like in Express.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants