UNPKG

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