1 | import type { RequestError } from "@octokit/request-error";
|
2 | import type { webhooks as OpenAPIWebhooks } from "@octokit/openapi-webhooks-types";
|
3 | import type { EventPayloadMap } from "./generated/webhook-identifiers.js";
|
4 | import type { Logger } from "./createLogger.js";
|
5 | import type { EventHandler } from "./event-handler/index.js";
|
6 | import type { emitterEventNames } from "./generated/webhook-names.js";
|
7 | export type WebhookEventName = keyof EventPayloadMap;
|
8 | export type ExtractEvents<TEventName> = TEventName extends `${infer _TWebhookEvent}.${infer _TAction}` ? never : TEventName;
|
9 | export type WebhookEvents = ExtractEvents<EmitterWebhookEventName>;
|
10 | export type WebhookEventDefinition<TEventName extends keyof OpenAPIWebhooks> = OpenAPIWebhooks[TEventName]["post"]["requestBody"]["content"]["application/json"];
|
11 | export type EmitterWebhookEventName = (typeof emitterEventNames)[number];
|
12 | export type EmitterWebhookEvent<TEmitterEvent extends EmitterWebhookEventName = EmitterWebhookEventName> = TEmitterEvent extends `${infer TWebhookEvent}.${infer TAction}` ? BaseWebhookEvent<Extract<TWebhookEvent, WebhookEventName>> & {
|
13 | payload: {
|
14 | action: TAction;
|
15 | };
|
16 | } : BaseWebhookEvent<Extract<TEmitterEvent, WebhookEventName>>;
|
17 | export type EmitterWebhookEventWithStringPayloadAndSignature = {
|
18 | id: string;
|
19 | name: WebhookEventName;
|
20 | payload: string;
|
21 | signature: string;
|
22 | };
|
23 | interface BaseWebhookEvent<TName extends WebhookEventName> {
|
24 | id: string;
|
25 | name: TName;
|
26 | payload: EventPayloadMap[TName];
|
27 | }
|
28 | export interface Options<TTransformed = unknown> {
|
29 | secret?: string;
|
30 | transform?: TransformMethod<TTransformed>;
|
31 | log?: Partial<Logger>;
|
32 | }
|
33 | type TransformMethod<T> = (event: EmitterWebhookEvent) => T | PromiseLike<T>;
|
34 | export type HandlerFunction<TName extends EmitterWebhookEventName, TTransformed> = (event: EmitterWebhookEvent<TName> & TTransformed) => any;
|
35 | export type RemoveHandlerFunction<TName extends EmitterWebhookEventName | "*", TTransformed> = (event: EmitterWebhookEvent<Exclude<TName, "*">> & TTransformed) => any;
|
36 | type Hooks = {
|
37 | [key: string]: Function[];
|
38 | };
|
39 | export interface State extends Options<any> {
|
40 | eventHandler?: EventHandler<unknown>;
|
41 | hooks: Hooks;
|
42 | log: Logger;
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 | export type WebhookError = Error & Partial<RequestError>;
|
48 | export interface AggregateWebhookError extends AggregateError {
|
49 | errors: WebhookError[];
|
50 | }
|
51 | export interface WebhookEventHandlerError<TTransformed = unknown> extends AggregateWebhookError {
|
52 | event: TTransformed extends unknown ? EmitterWebhookEvent : EmitterWebhookEvent & TTransformed;
|
53 | }
|
54 | export {};
|