Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): revert #459 & #464 #494

Merged
merged 4 commits into from
Mar 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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