Skip to content

Commit

Permalink
fix(types): transform method only accepts current event type
Browse files Browse the repository at this point in the history
  • Loading branch information
oscard0m committed Mar 12, 2021
1 parent b5fdf2a commit 12afb27
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import {
import { receiverHandle as receive } from "./receive";
import { removeListener } from "./remove-listener";

interface EventHandler<T extends Options> {
interface EventHandler {
on<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E>
): void;
onAny(handler: (event: EmitterWebhookEvent) => any): void;
onError(handler: (event: WebhookEventHandlerError) => any): void;
removeListener<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E>
): void;
receive(event: EmitterWebhookEvent): Promise<void>;
}

export function createEventHandler<T extends Options>(
options?: T
): EventHandler<T> {
): EventHandler {
const state: State = {
hooks: {},
log: createLogger(options && options.log),
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ class Webhooks<T extends Options> {
public verify: (eventPayload: string | object, signature: string) => boolean;
public on: <E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E>
) => void;
public onAny: (callback: (event: EmitterWebhookEvent) => any) => void;
public onError: (callback: (event: WebhookEventHandlerError) => any) => void;
public removeListener: <E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E>
) => void;
public receive: (event: EmitterWebhookEvent) => Promise<void>;
public middleware: (
Expand Down
9 changes: 2 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,8 @@ export interface Options<TTransformed = unknown> {

type TransformMethod<T> = (event: EmitterWebhookEvent) => T | PromiseLike<T>;

export type HandlerFunction<
TName extends EmitterWebhookEventName,
TTransform
> = (
event: TTransform extends TransformMethod<infer T>
? T
: EmitterWebhookEvent<TName>
export type HandlerFunction<TName extends EmitterWebhookEventName> = (
event: EmitterWebhookEvent<TName>
) => any;

type Hooks = {
Expand Down

0 comments on commit 12afb27

Please sign in to comment.