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(microservices): add event typings to the @EventPattern decorator #14798

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/microservices/decorators/event-pattern.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,39 @@ import {
import { Transport } from '../enums';
import { PatternHandler } from '../enums/pattern-handler.enum';

export type EventDataMethodDecorator<
TEventTypes extends Record<string, any>,
TKey extends keyof TEventTypes,
> = (
target: object,
propertyKey: string | symbol,
descriptor: TypedPropertyDescriptor<
(data: TEventTypes[TKey], ...args: unknown[]) => any
>,
) => void;

/**
* Subscribes to incoming events which fulfils chosen pattern.
*
* @publicApi
*/
export const EventPattern: {
<TEventTypes extends Record<string, any>>(
topic: keyof TEventTypes,
): EventDataMethodDecorator<TEventTypes, typeof topic>;
<TEventTypes extends Record<string, any>>(
topic: keyof TEventTypes,
transport: Transport | symbol,
): EventDataMethodDecorator<TEventTypes, typeof topic>;
<TEventTypes extends Record<string, any>>(
topic: keyof TEventTypes,
extras: Record<string, any>,
): EventDataMethodDecorator<TEventTypes, typeof topic>;
<TEventTypes extends Record<string, any>>(
topic: keyof TEventTypes,
transport: Transport | symbol,
extras: Record<string, any>,
): EventDataMethodDecorator<TEventTypes, typeof topic>;
<T = string>(metadata?: T): MethodDecorator;
<T = string>(metadata?: T, transport?: Transport | symbol): MethodDecorator;
<T = string>(metadata?: T, extras?: Record<string, any>): MethodDecorator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ describe('@EventPattern', () => {
public static test() {}
}

type TestComponent6EventTypes = {
'event-pattern-foo': { foo: string };
'event-pattern-bar': { bar: number };
};

// Static tests exclusively for type safety:
class TypeTestComponent {
@EventPattern<TestComponent6EventTypes>('event-pattern-foo')
testFoo(_event: { foo: string }, _additionalArg: string) {}

@EventPattern<TestComponent6EventTypes>('event-pattern-bar')
testBar(_event: { bar: number }) {}

// @ts-expect-error -- `foo` should be a string, not a number
@EventPattern<TestComponent6EventTypes>('event-pattern-foo')
testFooMismatch(_event: { foo: number }) {}

// @ts-expect-error -- `_event` should be `{ foo: string; }`, not a string
@EventPattern<TestComponent6EventTypes>('event-pattern-foo')
testFooMismatch2(_event: string) {}
}

it(`should enhance method with ${PATTERN_METADATA} metadata`, () => {
const [metadataArg] = Reflect.getMetadata(
PATTERN_METADATA,
Expand Down