-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.ts
85 lines (79 loc) · 2.69 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { createLogger } from "./createLogger.js";
import {
createEventHandler,
type EventHandler,
} from "./event-handler/index.js";
import { sign, verify } from "@octokit/webhooks-methods";
import { verifyAndReceive } from "./verify-and-receive.js";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
HandlerFunction,
RemoveHandlerFunction,
Options,
State,
WebhookError,
WebhookEventHandlerError,
EmitterWebhookEventWithStringPayloadAndSignature,
} from "./types.js";
export { createNodeMiddleware } from "./middleware/node/index.js";
export { createWebMiddleware } from "./middleware/web/index.js";
export { emitterEventNames } from "./generated/webhook-names.js";
// U holds the return value of `transform` function in Options
class Webhooks<TTransformed = unknown> {
public sign: (payload: string) => Promise<string>;
public verify: (eventPayload: string, signature: string) => Promise<boolean>;
public on: <E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>,
) => void;
public onAny: (
callback: (
event: TTransformed extends unknown
? EmitterWebhookEvent
: EmitterWebhookEvent & TTransformed,
) => any,
) => void;
public onError: (
callback: (event: WebhookEventHandlerError<TTransformed>) => any,
) => void;
public removeListener: <E extends EmitterWebhookEventName | "*">(
event: E | E[],
callback: RemoveHandlerFunction<E, TTransformed>,
) => void;
public receive: (event: EmitterWebhookEvent) => Promise<void>;
public verifyAndReceive: (
options: EmitterWebhookEventWithStringPayloadAndSignature,
) => Promise<void>;
constructor(options: Options<TTransformed> & { secret: string }) {
if (!options || !options.secret) {
throw new Error("[@octokit/webhooks] options.secret required");
}
const state: State & {
secret: string;
additionalSecrets?: string[] | undefined;
eventHandler: EventHandler<TTransformed>;
} = {
eventHandler: createEventHandler(options),
secret: options.secret,
additionalSecrets: options.additionalSecrets,
hooks: {},
log: createLogger(options.log),
};
this.sign = sign.bind(null, options.secret);
this.verify = verify.bind(null, options.secret);
this.on = state.eventHandler.on;
this.onAny = state.eventHandler.onAny;
this.onError = state.eventHandler.onError;
this.removeListener = state.eventHandler.removeListener;
this.receive = state.eventHandler.receive;
this.verifyAndReceive = verifyAndReceive.bind(null, state);
}
}
export {
createEventHandler,
Webhooks,
type EmitterWebhookEvent,
type EmitterWebhookEventName,
type WebhookError,
};