-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathon.ts
68 lines (59 loc) · 1.83 KB
/
on.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { emitterEventNames } from "../generated/webhook-names.js";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
State,
WebhookEventHandlerError,
} from "../types.js";
function handleEventHandlers(
state: State,
webhookName: EmitterWebhookEventName | "error" | "*",
handler: Function,
) {
if (!state.hooks[webhookName]) {
state.hooks[webhookName] = [];
}
state.hooks[webhookName].push(handler);
}
export function receiverOn(
state: State,
webhookNameOrNames: EmitterWebhookEventName | EmitterWebhookEventName[],
handler: Function,
) {
if (Array.isArray(webhookNameOrNames)) {
webhookNameOrNames.forEach((webhookName) =>
receiverOn(state, webhookName, handler),
);
return;
}
if (["*", "error"].includes(webhookNameOrNames)) {
const webhookName =
(webhookNameOrNames as string) === "*" ? "any" : webhookNameOrNames;
const message = `Using the "${webhookNameOrNames}" event with the regular Webhooks.on() function is not supported. Please use the Webhooks.on${
webhookName.charAt(0).toUpperCase() + webhookName.slice(1)
}() method instead`;
throw new Error(message);
}
if (!emitterEventNames.includes(webhookNameOrNames)) {
state.log.warn(
`"${webhookNameOrNames}" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)`,
);
}
handleEventHandlers(state, webhookNameOrNames, handler);
}
export function receiverOnAny<TTransformed>(
state: State,
handler: (
event: TTransformed extends unknown
? EmitterWebhookEvent
: EmitterWebhookEvent & TTransformed,
) => any,
) {
handleEventHandlers(state, "*", handler);
}
export function receiverOnError<TTransformed>(
state: State,
handler: (event: WebhookEventHandlerError<TTransformed>) => any,
) {
handleEventHandlers(state, "error", handler);
}