Skip to content

Commit

Permalink
fix: correct some type discrepencies (#970)
Browse files Browse the repository at this point in the history
Fixes #969
  • Loading branch information
wolfy1339 authored Feb 15, 2024
1 parent 917e0cf commit fb2db0b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
HandlerFunction,
Options,
State,
WebhookError,
WebhookEventHandlerError,
} from "../types.js";
import {
Expand All @@ -15,7 +16,7 @@ import {
import { receiverHandle as receive } from "./receive.js";
import { removeListener } from "./remove-listener.js";

interface EventHandler<TTransformed> {
export interface EventHandler<TTransformed> {
on<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>,
Expand All @@ -34,7 +35,7 @@ interface EventHandler<TTransformed> {
event: E | E[],
callback: HandlerFunction<E, TTransformed>,
): void;
receive(event: EmitterWebhookEvent): Promise<void>;
receive(event: EmitterWebhookEvent | WebhookError): Promise<void>;
}

export function createEventHandler<TTransformed>(
Expand Down
9 changes: 6 additions & 3 deletions src/event-handler/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import AggregateError from "aggregate-error";
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
State,
WebhookError,
WebhookEventName,
WebhookEventHandlerError,
} from "../types.js";
import { wrapErrorHandler } from "./wrap-error-handler.js";
Expand All @@ -17,7 +17,7 @@ type EventAction = Extract<
function getHooks(
state: State,
eventPayloadAction: EventAction | null,
eventName: EmitterWebhookEventName,
eventName: WebhookEventName,
): Function[] {
const hooks = [state.hooks[eventName], state.hooks["*"]];

Expand All @@ -29,7 +29,10 @@ function getHooks(
}

// main handler function
export function receiverHandle(state: State, event: EmitterWebhookEvent) {
export function receiverHandle(
state: State,
event: EmitterWebhookEvent | WebhookError,
) {
const errorHandlers = state.hooks.error || [];

if (event instanceof Error) {
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createLogger } from "./createLogger.js";
import { createEventHandler } from "./event-handler/index.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 {
Expand Down Expand Up @@ -49,7 +52,10 @@ class Webhooks<TTransformed = unknown> {
throw new Error("[@octokit/webhooks] options.secret required");
}

const state: State & { secret: string } = {
const state: State & {
secret: string;
eventHandler: EventHandler<TTransformed>;
} = {
eventHandler: createEventHandler(options),
secret: options.secret,
hooks: {},
Expand Down
6 changes: 4 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import type {
WebhookEventMap,
WebhookEventName,
} from "@octokit/webhooks-types";
export type { WebhookEventName } from "@octokit/webhooks-types";
import type { Logger } from "./createLogger.js";
import type { EventHandler } from "./event-handler/index.js";
import type { emitterEventNames } from "./generated/webhook-names.js";

export type EmitterWebhookEventName = (typeof emitterEventNames)[number];
Expand All @@ -17,7 +19,7 @@ export type EmitterWebhookEvent<

export type EmitterWebhookEventWithStringPayloadAndSignature = {
id: string;
name: EmitterWebhookEventName;
name: WebhookEventName;
payload: string;
signature: string;
};
Expand Down Expand Up @@ -51,7 +53,7 @@ type Hooks = {
};

export interface State extends Options<any> {
eventHandler?: any;
eventHandler?: EventHandler<unknown>;
hooks: Hooks;
log: Logger;
}
Expand Down
8 changes: 5 additions & 3 deletions src/verify-and-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import type {
EmitterWebhookEvent,
EmitterWebhookEventWithStringPayloadAndSignature,
State,
WebhookError,
} from "./types.js";
import type { EventHandler } from "./event-handler/index.js";

export async function verifyAndReceive(
state: State & { secret: string },
state: State & { secret: string; eventHandler: EventHandler<unknown> },
event: EmitterWebhookEventWithStringPayloadAndSignature,
): Promise<void> {
// verify will validate that the secret is not undefined
Expand All @@ -25,7 +27,7 @@ export async function verifyAndReceive(
);

return state.eventHandler.receive(
Object.assign(error, { event, status: 400 }),
Object.assign(error, { event, status: 400 }) as WebhookError,
);
}

Expand All @@ -42,5 +44,5 @@ export async function verifyAndReceive(
id: event.id,
name: event.name,
payload,
});
} as EmitterWebhookEvent);
}

0 comments on commit fb2db0b

Please sign in to comment.