-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathindex.ts
93 lines (86 loc) · 2.77 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
86
87
88
89
90
91
92
93
import { IncomingMessage, ServerResponse } from "http";
import { createEventHandler } from "./event-handler/index";
import { createMiddleware } from "./middleware/index";
import { middleware } from "./middleware/middleware";
import {
verifyAndReceive,
WebhookEventName,
} from "./middleware/verify-and-receive";
import { sign } from "./sign/index";
import {
EmitterAnyEvent,
EmitterEventName,
EmitterWebhookEvent,
EmitterWebhookEventMap,
HandlerFunction,
Options,
State,
WebhookError,
WebhookEventHandlerError,
} from "./types";
import { verify } from "./verify/index";
// U holds the return value of `transform` function in Options
class Webhooks<
E extends EmitterWebhookEvent = EmitterWebhookEvent,
TTransformed = unknown
> {
public sign: (payload: string | object) => string;
public verify: (eventPayload: string | object, signature: string) => boolean;
public on: <E extends EmitterEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
) => void;
public onAny: (callback: (event: EmitterAnyEvent) => any) => void;
public onError: (callback: (event: WebhookEventHandlerError) => any) => void;
public removeListener: <E extends EmitterEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
) => void;
public receive: (event: EmitterWebhookEvent) => Promise<void>;
public middleware: (
request: IncomingMessage,
response: ServerResponse,
next?: (err?: any) => void
) => void | Promise<void>;
public verifyAndReceive: (
options: EmitterWebhookEventMap[WebhookEventName] & { signature: string }
) => Promise<void>;
constructor(options?: Options<E, TTransformed>) {
if (!options || !options.secret) {
throw new Error("[@octokit/webhooks] options.secret required");
}
const state: State = {
eventHandler: createEventHandler(options),
path: options.path || "/",
secret: options.secret,
hooks: {},
};
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.middleware = middleware.bind(null, state);
this.verifyAndReceive = verifyAndReceive.bind(null, state);
}
}
const createWebhooksApi = Webhooks.prototype.constructor;
export { EventPayloads } from "./generated/event-payloads";
export {
EmitterEventMap,
EmitterEventName,
EmitterEventMap as EventTypesPayload,
EmitterEventName as WebhookEvents,
} from "./types";
export {
createEventHandler,
createMiddleware,
createWebhooksApi,
Webhooks,
EmitterWebhookEvent,
WebhookError,
sign,
verify,
};