Skip to content

Feature request: infer ParsedResult types from inputs of parse function #3096

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

Closed
dreamorosi opened this issue Sep 20, 2024 · 5 comments · Fixed by #3568
Closed

Feature request: infer ParsedResult types from inputs of parse function #3096

dreamorosi opened this issue Sep 20, 2024 · 5 comments · Fixed by #3568
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility

Comments

@dreamorosi
Copy link
Contributor

Use case

When using the Parser with the safeParse option enabled, the result is a ParsedResult type that can contain either the parsed payload or an error depending on the result of the parsing.

Today, customers need to specify two types: one for the input type, usually the one of the envelope, and an output type, either the schema or the transformed type.

This is ok when using envelopes, but when parsing a schema directly the two types can be redundant and/or confusing.

I don't know if it's possible, but we could explore the possibility of making the second type optional when typing the ParsedResult or even making it optional and infer the types from the decorator usage.

Solution/User Experience

Before

import { parser } from '@aws-lambda-powertools/parser';
import { APIGatewayProxyEventV2Schema } from '@aws-lambda-powertools/parser/schemas/api-gatewayv2';
import type {
  ParsedResult,
  APIGatewayProxyEventV2,
} from '@aws-lambda-powertools/parser/types';
import type { Context } from 'aws-lambda';

class Lambda {
  @parser({ schema: APIGatewayProxyEventV2Schema, safeParse: true })
  public async handler(
    event: ParsedResult<APIGatewayProxyEventV2, APIGatewayProxyEventV2>,
    _context: Context
  ) {
    if (event.success) {
      console.log(event.data); // this is APIGatewayProxyEventV2
    } else {
      // event.error is now strongly typed
      console.error(event.error);
      // event.originalEvent is now strongly typed
      console.error(event.originalEvent);
    }
  }
}

export const handler = new Lambda().handler;

After

class Lambda {
  @parser({ schema: APIGatewayProxyEventV2Schema, safeParse: true })
  public async handler(
-    event: ParsedResult<unknown, APIGatewayProxyEventV2>,
+    event: ParsedResult<APIGatewayProxyEventV2>,
    _context: Context
  ) {
    if (event.success) {
      console.log(event.data); // this is APIGatewayProxyEventV2
    } else {
      // event.error is now strongly typed
      console.error(event.error);
      // event.originalEvent is now strongly typed
      console.error(event.originalEvent);
    }
  }
}

Especially in cases where we don't use an envelope, the Output type is always the z.infer<typeof XXXSchema>.

Alternative solutions

If this is not feasible, I would suggest refactoring the `ParsedResult` type which today looks like this:


type ParsedResult<Input = unknown, Output = unknown> =
  | ParsedResultSuccess<Output>
  | ParsedResultError<Input>;


To switch the order and make it more obvious what is what, i.e.

```ts
type ParsedResult<PayloadSchema = unknown, EnvelopeSchema = unknown> =
  | ParsedResultSuccess<PayloadSchema>
  | ParsedResultError< EnvelopeSchema>;

So that one can just specify the first one when not using envelopes.

This solution however might be counterintuitive because we are now reverting the order of the parameters. The good thing about the current solution is that logically input comes before the output.



### Acknowledgment

- [X] This feature request meets [Powertools for AWS Lambda (TypeScript) Tenets](https://docs.powertools.aws.dev/lambda/typescript/latest/#tenets)
- [ ] Should this be considered in other Powertools for AWS Lambda languages? i.e. [Python](https://github.com/aws-powertools/powertools-lambda-python/), [Java](https://github.com/aws-powertools/powertools-lambda-java/), and [.NET](https://github.com/aws-powertools/powertools-lambda-dotnet/)

### Future readers

Please react with 👍 and your use case to help us understand customer demand.
@dreamorosi dreamorosi added feature-request This item refers to a feature request for an existing or new utility confirmed The scope is clear, ready for implementation parser This item relates to the Parser Utility labels Sep 20, 2024
@dreamorosi
Copy link
Contributor Author

I also noticed that the API docs of the feature are incorrect and don't mention the fact that you need two types.

I have started a branch to fix this, if we decide to fix this we can continue working on it.

@am29d am29d self-assigned this Jan 27, 2025
@am29d am29d moved this from Ideas to Backlog in Powertools for AWS Lambda (TypeScript) Jan 27, 2025
@dreamorosi
Copy link
Contributor Author

@am29d - note that I might have been far off with this suggestion and this is simply how Zod types work.

Before moving onto an implementation I'd like us to discuss options if that's ok with you - I'd want to avoid us touching dozen files just to later throw it away.

Also, nobody seem to have engaged with this issue - so perhaps we could also just close it and revisit this if there's ever demand.

@dreamorosi dreamorosi moved this from Backlog to Working on it in Powertools for AWS Lambda (TypeScript) Feb 3, 2025
@dreamorosi
Copy link
Contributor Author

As discussed during today's meeting @am29d has been looking into this and the change will require updating the type of the LambdaInterface in commons.

He's going to do a small PoC to see if it's worth changing, but if too big we'll just drop the request and close the issue as not planned by the end of the iteration.

Copy link
Contributor

github-actions bot commented Feb 7, 2025

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed confirmed The scope is clear, ready for implementation labels Feb 7, 2025
Copy link
Contributor

This is now released under v2.14.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Feb 11, 2025
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility parser This item relates to the Parser Utility
Projects
Development

Successfully merging a pull request may close this issue.

2 participants