Skip to content

Commit

Permalink
fix(typescript): HandlerFunction argument
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Feb 13, 2021
1 parent 6106784 commit 2b56097
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 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<TTransformed> {
interface EventHandler<T extends Options> {
on<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
callback: HandlerFunction<E, T["transform"]>
): void;
onAny(handler: (event: EmitterWebhookEvent) => any): void;
onError(handler: (event: WebhookEventHandlerError) => any): void;
removeListener<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
callback: HandlerFunction<E, T["transform"]>
): void;
receive(event: EmitterWebhookEvent): Promise<void>;
}

export function createEventHandler<TTransformed>(
options?: Options<TTransformed>
): EventHandler<TTransformed> {
export function createEventHandler<T extends Options>(
options?: T
): EventHandler<T> {
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<TTransformed> {
class Webhooks<T extends Options> {
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, TTransformed>
callback: HandlerFunction<E, T["transform"]>
) => 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, TTransformed>
callback: HandlerFunction<E, T["transform"]>
) => void;
public receive: (event: EmitterWebhookEvent) => Promise<void>;
public middleware: (
Expand All @@ -40,7 +40,7 @@ class Webhooks<TTransformed> {
options: EmitterWebhookEvent & { signature: string }
) => Promise<void>;

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

export type HandlerFunction<
TName extends EmitterWebhookEventName,
TTransformed
> = (event: EmitterWebhookEvent<TName> & TTransformed) => any;
TTransform
> = (
event: TTransform extends TransformMethod<infer T>
? T
: EmitterWebhookEvent<TName>
) => 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: EmitterWebhookEvent) => {
eventHandler.on("push", (event: string) => {
expect(event).toBe("funky");

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

eventHandler.on("push", (event: EmitterWebhookEvent) => {
eventHandler.on("push", (event: string) => {
expect(event).toBe("funky");
done();
});
Expand Down
17 changes: 12 additions & 5 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ 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 @@ -168,6 +164,17 @@ export default async function () {
webhooks.on("issues", (event) => {
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 All @@ -190,7 +197,7 @@ export default async function () {
});

createServer(webhooks.middleware).listen(3000);
}
};

export function webhookErrorTest(error: WebhookError) {
const { request } = error;
Expand Down

0 comments on commit 2b56097

Please sign in to comment.