diff --git a/src/event-handler/index.ts b/src/event-handler/index.ts index 754081b3..24393e6e 100644 --- a/src/event-handler/index.ts +++ b/src/event-handler/index.ts @@ -15,23 +15,23 @@ import { import { receiverHandle as receive } from "./receive"; import { removeListener } from "./remove-listener"; -interface EventHandler { +interface EventHandler { on( event: E | E[], - callback: HandlerFunction + callback: HandlerFunction ): void; onAny(handler: (event: EmitterWebhookEvent) => any): void; onError(handler: (event: WebhookEventHandlerError) => any): void; removeListener( event: E | E[], - callback: HandlerFunction + callback: HandlerFunction ): void; receive(event: EmitterWebhookEvent): Promise; } -export function createEventHandler( - options?: T -): EventHandler { +export function createEventHandler( + options: Options +): EventHandler { const state: State = { hooks: {}, log: createLogger(options && options.log), diff --git a/src/index.ts b/src/index.ts index 08c2bf00..12ac19de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,18 +17,18 @@ import { import { verify } from "./verify/index"; // U holds the return value of `transform` function in Options -class Webhooks { +class Webhooks { public sign: (payload: string | object) => string; public verify: (eventPayload: string | object, signature: string) => boolean; public on: ( event: E | E[], - callback: HandlerFunction + callback: HandlerFunction ) => void; public onAny: (callback: (event: EmitterWebhookEvent) => any) => void; public onError: (callback: (event: WebhookEventHandlerError) => any) => void; public removeListener: ( event: E | E[], - callback: HandlerFunction + callback: HandlerFunction ) => void; public receive: (event: EmitterWebhookEvent) => Promise; public middleware: ( @@ -40,7 +40,7 @@ class Webhooks { options: EmitterWebhookEvent & { signature: string } ) => Promise; - constructor(options: T) { + constructor(options: Options) { if (!options || !options.secret) { throw new Error("[@octokit/webhooks] options.secret required"); } diff --git a/src/types.ts b/src/types.ts index b25b3ff7..7e3bd72e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -32,12 +32,8 @@ type TransformMethod = (event: EmitterWebhookEvent) => T | PromiseLike; export type HandlerFunction< TName extends EmitterWebhookEventName, - TTransform -> = ( - event: TTransform extends TransformMethod - ? T - : EmitterWebhookEvent -) => any; + TTransformed +> = (event: EmitterWebhookEvent & TTransformed) => any; type Hooks = { [key: string]: Function[]; diff --git a/test/integration/event-handler-test.ts b/test/integration/event-handler-test.ts index 278e171c..8b5e15b1 100644 --- a/test/integration/event-handler-test.ts +++ b/test/integration/event-handler-test.ts @@ -135,7 +135,7 @@ test("options.transform", (done) => { }, }); - eventHandler.on("push", (event: string) => { + eventHandler.on("push", (event: EmitterWebhookEvent) => { expect(event).toBe("funky"); done(); @@ -155,7 +155,7 @@ test("async options.transform", (done) => { }, }); - eventHandler.on("push", (event: string) => { + eventHandler.on("push", (event: EmitterWebhookEvent) => { expect(event).toBe("funky"); done(); }); diff --git a/test/typescript-validate.ts b/test/typescript-validate.ts index ba46570e..8a9e3fbc 100644 --- a/test/typescript-validate.ts +++ b/test/typescript-validate.ts @@ -72,6 +72,10 @@ export default async function () { const webhooks = new Webhooks({ secret: "blah", path: "/webhooks", + transform: (event) => { + console.log(event.payload); + return Object.assign(event, { foo: "bar" }); + }, }); // Check named exports of new API work @@ -162,19 +166,9 @@ export default async function () { }); webhooks.on("issues", (event) => { + // ⚠️ This test is for assuring 'transform' method is preserving event.payload console.log(event.payload.issue); }); -} - -{ - const webhooks = new Webhooks({ - secret: "blah", - path: "/webhooks", - transform: (event) => { - console.log(event.payload); - return Object.assign(event, { foo: "bar" }); - }, - }); webhooks.on("issues", (event) => { // foo is set by options.transform