Skip to content
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

feat: typed results #1527

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

Sikora00
Copy link
Contributor

@Sikora00 Sikora00 commented Nov 1, 2023

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #490

What is the new behavior?

There is a new option to pass an instance of a class to the bus which is a generic type and from it later we can infer the return type of the handler.
The behavior is the same as with this package https://github.com/nestjs-architects/typed-cqrs

Does this PR introduce a breaking change?

  • Yes
  • No

It's not a breaking change to the API. It's rater an extension. However, if someone was extending or implementing the original Query/CommandBus it is a breaking change for them because they have to follow the extended interface now.

Other information

Add Query and Command classes.
Thenks to that if they have a generic type
we can assign it to one of its props
and infer the return type of the handler
using this property.
import { IQuery } from './query.interface';

export class Query<T> implements IQuery {
resultType$f9fbca36!: T;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be resultType: T; ? right ?

Suggested change
resultType$f9fbca36!: T;
resultType: T;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this field is made unique intentionally so no one should be surprised while creating a query like

class GetApiResponse extends Query<Response> {
  resultType: string;
  }
  
  const response = queryBus.exec(new GetApiResponse({resultType: 'json'}))

in such case, the type of the response variable would be string instead of Response.
That's why @Dyostiq added a unique suffix to that necessary field.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reply 😉👍

src/interfaces/commands/command.ts Show resolved Hide resolved
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 this pull request may close these issues.

None yet

2 participants