Skip to content

Commit

Permalink
fix: pass through TTransformed to onAny and onError (#924)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 authored Nov 18, 2023
1 parent 4875a74 commit 6ead0b7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ interface EventHandler<TTransformed> {
event: E | E[],
callback: HandlerFunction<E, TTransformed>,
): void;
onAny(handler: (event: EmitterWebhookEvent) => any): void;
onError(handler: (event: WebhookEventHandlerError) => any): void;
onAny(handler: (event: EmitterWebhookEvent & TTransformed) => any): void;
onError(
handler: (event: WebhookEventHandlerError<TTransformed>) => any,
): void;
removeListener<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>,
Expand All @@ -43,8 +45,8 @@ export function createEventHandler<TTransformed>(

return {
on: on.bind(null, state),
onAny: onAny.bind(null, state),
onError: onError.bind(null, state),
onAny: (onAny<TTransformed>).bind(null, state),
onError: (onError<TTransformed>).bind(null, state),
removeListener: removeListener.bind(null, state),
receive: receive.bind(null, state),
};
Expand Down
8 changes: 4 additions & 4 deletions src/event-handler/on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ export function receiverOn(
handleEventHandlers(state, webhookNameOrNames, handler);
}

export function receiverOnAny(
export function receiverOnAny<TTransformed>(
state: State,
handler: (event: EmitterWebhookEvent) => any,
handler: (event: EmitterWebhookEvent & TTransformed) => any,
) {
handleEventHandlers(state, "*", handler);
}

export function receiverOnError(
export function receiverOnError<TTransformed>(
state: State,
handler: (event: WebhookEventHandlerError) => any,
handler: (event: WebhookEventHandlerError<TTransformed>) => any,
) {
handleEventHandlers(state, "error", handler);
}
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ class Webhooks<TTransformed = unknown> {
event: E | E[],
callback: HandlerFunction<E, TTransformed>,
) => void;
public onAny: (callback: (event: EmitterWebhookEvent) => any) => void;
public onError: (callback: (event: WebhookEventHandlerError) => any) => void;
public onAny: (
callback: (event: EmitterWebhookEvent & TTransformed) => any,
) => void;
public onError: (
callback: (event: WebhookEventHandlerError<TTransformed>) => any,
) => void;
public removeListener: <E extends EmitterWebhookEventName | "*">(
event: E | E[],
callback: RemoveHandlerFunction<E, TTransformed>,
Expand Down
5 changes: 3 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export interface State extends Options<any> {
export type WebhookError = Error & Partial<RequestError>;

// todo: rename to "EmitterErrorEvent"
export interface WebhookEventHandlerError extends AggregateError<WebhookError> {
event: EmitterWebhookEvent;
export interface WebhookEventHandlerError<TTransformed = unknown>
extends AggregateError<WebhookError> {
event: EmitterWebhookEvent & TTransformed;
}

/**
Expand Down

0 comments on commit 6ead0b7

Please sign in to comment.