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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/command-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { CommandHandlerNotFoundException } from './exceptions/command-not-found.exception';
import { DefaultCommandPubSub } from './helpers/default-command-pubsub';
import { InvalidCommandHandlerException } from './index';
import { Command } from './interfaces/commands/command';
import { CommandMetadata } from './interfaces/commands/command-metadata.interface';
import {
ICommand,
Expand Down Expand Up @@ -54,6 +55,8 @@ export class CommandBus<CommandBase extends ICommand = ICommand>
* @param command The command to execute.
* @returns A promise that, when resolved, will contain the result returned by the command's handler.
*/
execute<R = void>(query: Command<R>): Promise<R>;
execute<T extends CommandBase, R = any>(command: T): Promise<R>;
execute<T extends CommandBase, R = any>(command: T): Promise<R> {
const commandId = this.getCommandId(command);
const handler = this.handlers.get(commandId);
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/commands/command-bus.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Command } from './command';
import { ICommand } from './command.interface';

/**
Expand All @@ -8,5 +9,6 @@ export interface ICommandBus<CommandBase extends ICommand = ICommand> {
* Executes a command.
* @param command The command to execute.
*/
execute<R = void>(query: Command<R>): Promise<R>;
execute<T extends CommandBase, R = any>(command: T): Promise<R>;
}
17 changes: 13 additions & 4 deletions src/interfaces/commands/command-handler.interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { Command } from './command';
import { ICommand } from './command.interface';

/**
* Represents a command handler.
* Command handlers are used to execute commands.
*/
export interface ICommandHandler<
TCommand extends ICommand = any,
TResult = any,
> {
export type ICommandHandler<
CommandType extends ICommand = any,
TRes = any,
> = CommandType extends Command<infer ResultType>
? BaseICommandHandler<CommandType, ResultType>
: BaseICommandHandler<CommandType, TRes>;

/**
* Basic interface for CommandHandlers
* Can be used for both: inferred and declared return types
*/
interface BaseICommandHandler<TCommand extends ICommand = any, TResult = any> {
/**
* Executes a command.
* @param command The command to execute.
Expand Down
4 changes: 4 additions & 0 deletions src/interfaces/commands/command-result.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Command } from './command';

export type CommandResult<CommandT extends Command<unknown>> =
CommandT extends Command<infer ResultT> ? ResultT : never;
5 changes: 5 additions & 0 deletions src/interfaces/commands/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ICommand } from './command.interface';

export class Command<T> implements ICommand {
resultType$e1ca39fa!: T;
Sikora00 marked this conversation as resolved.
Show resolved Hide resolved
}
4 changes: 4 additions & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export * from './commands/command';
export * from './commands/command-bus.interface';
export * from './commands/command-handler.interface';
export * from './commands/command-publisher.interface';
export * from './commands/command-result.type';
export * from './commands/command.interface';
export * from './events/event-bus.interface';
export * from './events/event-handler.interface';
Expand All @@ -9,9 +11,11 @@ export * from './events/event.interface';
export * from './events/message-source.interface';
export * from './exceptions/unhandled-exception-info.interface';
export * from './exceptions/unhandled-exception-publisher.interface';
export * from './queries/query';
export * from './queries/query-bus.interface';
export * from './queries/query-handler.interface';
export * from './queries/query-publisher.interface';
export * from './queries/query-result.interface';
export * from './queries/query-result.type';
export * from './queries/query.interface';
export * from './saga.type';
14 changes: 13 additions & 1 deletion src/interfaces/queries/query-handler.interface.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { Query } from './query';
import { IQuery } from './query.interface';

/**
* Represents a query handler.
*/
export interface IQueryHandler<T extends IQuery = any, TRes = any> {
export type IQueryHandler<
QueryType extends IQuery = any,
TRes = any,
> = QueryType extends Query<infer ResultType>
? BaseIQueryHandler<QueryType, ResultType>
: BaseIQueryHandler<QueryType, TRes>;

/**
* Basic interface for QueryHandlers
* Can be used for both: inferred and declared return types
*/
interface BaseIQueryHandler<T extends IQuery = any, TRes = any> {
/**
* Executes a query.
* @param query The query to execute.
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/queries/query-result.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Query } from './query';

export type QueryResult<QueryT extends Query<unknown>> = QueryT extends Query<
infer ResultT
>
? ResultT
: never;
5 changes: 5 additions & 0 deletions src/interfaces/queries/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
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 😉👍

}
3 changes: 3 additions & 0 deletions src/query-bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IQueryPublisher,
IQueryResult,
} from './interfaces';
import { Query } from './interfaces/queries/query';
import { QueryMetadata } from './interfaces/queries/query-metadata.interface';
import { ObservableBus } from './utils/observable-bus';

Expand Down Expand Up @@ -53,6 +54,8 @@ export class QueryBus<QueryBase extends IQuery = IQuery>
* Executes a query.
* @param query The query to execute.
*/
execute<TResult>(query: Query<TResult>): Promise<TResult>;
execute<T extends QueryBase, TResult = any>(query: T): Promise<TResult>;
async execute<T extends QueryBase, TResult = any>(
query: T,
): Promise<TResult> {
Expand Down