-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathindex.ts
47 lines (43 loc) · 1.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
HandlerFunction,
Options,
State,
WebhookEventHandlerError,
} from "../types";
import {
receiverOn as on,
receiverOnAny as onAny,
receiverOnError as onError,
} from "./on";
import { receiverHandle as receive } from "./receive";
import { removeListener } from "./remove-listener";
interface EventHandler<TTransformed = unknown> {
on<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
): void;
onAny(handler: (event: EmitterWebhookEvent) => any): void;
onError(handler: (event: WebhookEventHandlerError) => any): void;
removeListener<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
): void;
receive(event: EmitterWebhookEvent): Promise<void>;
}
export function createEventHandler(options: Options<any>): EventHandler {
const state: State = {
hooks: {},
};
if (options && options.transform) {
state.transform = options.transform;
}
return {
on: on.bind(null, state),
onAny: onAny.bind(null, state),
onError: onError.bind(null, state),
removeListener: removeListener.bind(null, state),
receive: receive.bind(null, state),
};
}