Skip to content

Commit

Permalink
fix(typescript): revert #459 & #464 (#494)
Browse files Browse the repository at this point in the history
* reverts: "fix(typescript): HandlerFunction argument (#464)"

This reverts commit aad9b74.

* reverts: "fix(typescript): infer `TTransformed` from `createEventHandler()` options (#459)"

This reverts commit d2a0b73.

* fix(types): adopt options type from first revert

* test(typescript-validate): added explanatory comment for ts validation test
  • Loading branch information
oscard0m authored Mar 14, 2021
1 parent 01c7655 commit 11d6e36
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 29 deletions.
12 changes: 6 additions & 6 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ import {
import { receiverHandle as receive } from "./receive";
import { removeListener } from "./remove-listener";

interface EventHandler<T extends Options> {
interface EventHandler<TTransformed> {
on<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E, TTransformed>
): void;
onAny(handler: (event: EmitterWebhookEvent) => any): void;
onError(handler: (event: WebhookEventHandlerError) => any): void;
removeListener<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E, TTransformed>
): void;
receive(event: EmitterWebhookEvent): Promise<void>;
}

export function createEventHandler<T extends Options>(
options?: T
): EventHandler<T> {
export function createEventHandler<TTransformed>(
options: Options<TTransformed>
): EventHandler<TTransformed> {
const state: State = {
hooks: {},
log: createLogger(options && options.log),
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ import {
import { verify } from "./verify/index";

// U holds the return value of `transform` function in Options
class Webhooks<T extends Options> {
class Webhooks<TTransformed> {
public sign: (payload: string | object) => string;
public verify: (eventPayload: string | object, signature: string) => boolean;
public on: <E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E, TTransformed>
) => void;
public onAny: (callback: (event: EmitterWebhookEvent) => any) => void;
public onError: (callback: (event: WebhookEventHandlerError) => any) => void;
public removeListener: <E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, T["transform"]>
callback: HandlerFunction<E, TTransformed>
) => void;
public receive: (event: EmitterWebhookEvent) => Promise<void>;
public middleware: (
Expand All @@ -40,7 +40,7 @@ class Webhooks<T extends Options> {
options: EmitterWebhookEvent & { signature: string }
) => Promise<void>;

constructor(options: T) {
constructor(options: Options<TTransformed>) {
if (!options || !options.secret) {
throw new Error("[@octokit/webhooks] options.secret required");
}
Expand Down
8 changes: 2 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ type TransformMethod<T> = (event: EmitterWebhookEvent) => T | PromiseLike<T>;

export type HandlerFunction<
TName extends EmitterWebhookEventName,
TTransform
> = (
event: TTransform extends TransformMethod<infer T>
? T
: EmitterWebhookEvent<TName>
) => any;
TTransformed
> = (event: EmitterWebhookEvent<TName> & TTransformed) => any;

type Hooks = {
[key: string]: Function[];
Expand Down
4 changes: 2 additions & 2 deletions test/integration/event-handler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ test("options.transform", (done) => {
},
});

eventHandler.on("push", (event: string) => {
eventHandler.on("push", (event: EmitterWebhookEvent) => {
expect(event).toBe("funky");

done();
Expand All @@ -155,7 +155,7 @@ test("async options.transform", (done) => {
},
});

eventHandler.on("push", (event: string) => {
eventHandler.on("push", (event: EmitterWebhookEvent) => {
expect(event).toBe("funky");
done();
});
Expand Down
16 changes: 5 additions & 11 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 11d6e36

Please sign in to comment.