From 11d6e36c7f2f33f26840da98baac68862ac71260 Mon Sep 17 00:00:00 2001 From: Oscar Dominguez Date: Sun, 14 Mar 2021 21:20:02 +0100 Subject: [PATCH] fix(typescript): revert #459 & #464 (#494) * reverts: "fix(typescript): HandlerFunction argument (#464)" This reverts commit aad9b74ba90f8ca633c811231f1f566f59e241bd. * reverts: "fix(typescript): infer `TTransformed` from `createEventHandler()` options (#459)" This reverts commit d2a0b73be8b0fd0a4e52e90f5bd2b645df968b0d. * fix(types): adopt options type from first revert * test(typescript-validate): added explanatory comment for ts validation test --- src/event-handler/index.ts | 12 ++++++------ src/index.ts | 8 ++++---- src/types.ts | 8 ++------ test/integration/event-handler-test.ts | 4 ++-- test/typescript-validate.ts | 16 +++++----------- 5 files changed, 19 insertions(+), 29 deletions(-) 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