Skip to content

Commit

Permalink
feat: remove support for * event
Browse files Browse the repository at this point in the history
BREAKING CHANGE: passing `*` as an event name is no longer supported
  • Loading branch information
G-Rath committed Feb 5, 2021
1 parent b2d5c70 commit 4b20ca7
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 57 deletions.
10 changes: 5 additions & 5 deletions scripts/generate-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,8 @@ const asLink = (event: string): string => {
const updateReadme = (properties: string[]) => {
const headers = "| Event | Actions |";

const events = properties
.filter((property) => property !== "*")
.reduce<Record<string, string[]>>((events, property) => {
const events = properties.reduce<Record<string, string[]>>(
(events, property) => {
console.log(property);
const [event, action] = property.split(".");

Expand All @@ -139,7 +138,9 @@ const updateReadme = (properties: string[]) => {
}

return events;
}, {});
},
{}
);

const rows = Object.entries(events).map(
([event, actions]) =>
Expand Down Expand Up @@ -193,7 +194,6 @@ const run = () => {
"// make edits in scripts/generate-types.ts",
"",
"export const emitterEventNames = [",
'"*",',
...properties.map(([key]) => `"${key}",`),
"];",
]);
Expand Down
3 changes: 1 addition & 2 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type {
EmitterAnyEvent,
EmitterEventName,
EmitterWebhookEvent,
HandlerFunction,
Expand All @@ -20,7 +19,7 @@ interface EventHandler<TTransformed = unknown> {
event: E | E[],
callback: HandlerFunction<E, TTransformed>
): void;
onAny(handler: (event: EmitterAnyEvent) => any): void;
onAny(handler: (event: EmitterWebhookEvent) => any): void;
onError(handler: (event: WebhookEventHandlerError) => any): void;
removeListener<E extends EmitterEventName>(
event: E | E[],
Expand Down
12 changes: 3 additions & 9 deletions src/event-handler/on.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { emitterEventNames } from "../generated/webhook-names";
import {
EmitterAnyEvent,
EmitterEventName,
EmitterWebhookEvent,
State,
WebhookEventHandlerError,
} from "../types";

function handleEventHandlers(
state: State,
webhookName: EmitterEventName | "error",
webhookName: EmitterEventName | "error" | "*",
handler: Function
) {
if (!state.hooks[webhookName]) {
Expand All @@ -35,18 +35,12 @@ export function receiverOn(
);
}

if (webhookNameOrNames === "*") {
console.warn(
`Using the "${webhookNameOrNames}" event with the regular Webhooks.on() function is deprecated. Please use the Webhooks.onAny() method instead`
);
}

handleEventHandlers(state, webhookNameOrNames, handler);
}

export function receiverOnAny(
state: State,
handler: (event: EmitterAnyEvent) => any
handler: (event: EmitterWebhookEvent) => any
) {
handleEventHandlers(state, "*", handler);
}
Expand Down
1 change: 0 additions & 1 deletion src/generated/webhook-names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// make edits in scripts/generate-types.ts

export const emitterEventNames = [
"*",
"check_run",
"check_run.completed",
"check_run.created",
Expand Down
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from "./middleware/verify-and-receive";
import { sign } from "./sign/index";
import {
EmitterAnyEvent,
EmitterEventName,
EmitterWebhookEvent,
EmitterWebhookEventMap,
Expand All @@ -31,7 +30,7 @@ class Webhooks<
event: E | E[],
callback: HandlerFunction<E, TTransformed>
) => void;
public onAny: (callback: (event: EmitterAnyEvent) => any) => void;
public onAny: (callback: (event: EmitterWebhookEvent) => any) => void;
public onError: (callback: (event: WebhookEventHandlerError) => any) => void;
public removeListener: <E extends EmitterEventName>(
event: E | E[],
Expand Down
5 changes: 1 addition & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { RequestError } from "@octokit/request-error";
import type { Schema } from "@octokit/webhooks-definitions/schema";
import type { EmitterEventWebhookPayloadMap } from "./generated/get-webhook-payload-type-from-event";

type EmitterEventPayloadMap = { "*": Schema } & EmitterEventWebhookPayloadMap;
type EmitterEventPayloadMap = EmitterEventWebhookPayloadMap;

export type EmitterWebhookEventMap = {
[K in keyof EmitterEventPayloadMap]: BaseWebhookEvent<K>;
Expand All @@ -19,8 +18,6 @@ export type EmitterEventMap = EmitterWebhookEventMap;
export type EmitterEventName = keyof EmitterEventMap;
export type EmitterEvent = EmitterEventMap[EmitterEventName];

export type EmitterAnyEvent = EmitterWebhookEventMap["*"];

export type ToWebhookEvent<
TEmitterEvent extends string
> = TEmitterEvent extends `${infer TWebhookEvent}.${string}`
Expand Down
24 changes: 1 addition & 23 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,11 @@ import { HandlerFunction, EmitterWebhookEventName } from "../src/types";
// ************************************************************

const fn = (webhookEvent: EmitterWebhookEvent) => {
if (webhookEvent.name === "*") {
if (
"action" in webhookEvent.payload &&
webhookEvent.payload.action === "completed"
) {
console.log(webhookEvent.payload.sender);
}
}
// @ts-expect-error TS2367:
// This condition will always return 'false' since the types '"*" | "check_run" | ... many more ... | "workflow_run"' and '"check_run.completed"' have no overlap.
// This condition will always return 'false' since the types '"check_run" | ... many more ... | "workflow_run"' and '"check_run.completed"' have no overlap.
if (webhookEvent.name === "check_run.completed") {
//
}

if (webhookEvent.name === "*") {
// @ts-expect-error TS2339:
// Property 'action' does not exist on type 'Schema'.
// Property 'action' does not exist on type 'CreateEvent'.
console.log(webhookEvent.payload.action);
}
};

declare const on: <E extends EmitterWebhookEventName>(
Expand Down Expand Up @@ -119,13 +104,6 @@ export default async function () {

verify("randomSecret", {}, "randomSignature");

// This is deprecated usage
webhooks.on("*", ({ id, name, payload }) => {
console.log(name, "event received", id);
const sig = webhooks.sign(payload);
webhooks.verify(payload, sig);
});

webhooks.onAny(({ id, name, payload }) => {
console.log(name, "event received", id);
const sig = webhooks.sign(payload);
Expand Down
11 changes: 0 additions & 11 deletions test/unit/event-handler-on-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,3 @@ test("receiver.on with invalid event name", () => {
'"foo" is not a known webhook name (https://developer.github.com/v3/activity/events/types/)'
);
});

test("receiver.on with event name of '*' logs deprecation notice", () => {
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(noop);

receiverOn(state, "*", noop);

expect(consoleWarnSpy).toHaveBeenCalledTimes(1);
expect(consoleWarnSpy).toHaveBeenLastCalledWith(
'Using the "*" event with the regular Webhooks.on() function is deprecated. Please use the Webhooks.onAny() method instead'
);
});

0 comments on commit 4b20ca7

Please sign in to comment.