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

v8.0.0 #444

Merged
merged 14 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/04_feature_request.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: "🧚‍♂️ Feature Request"
about: "Wouldnt it be nice if 💭"
about: "Wouldn't it be nice if 💭"
labels: feature
---

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ Asynchronous `error` event handler are not blocking the `.receive()` method from
<strong>Required.</strong>
Method to be run each time a webhook event handler throws an error or returns a promise that rejects.
The <code>handler</code> function can be an async function,
return a Promise. The handler is called with an error object that has a .event property which hass all the information on the event: <code>{id, name, payload}</code>.
return a Promise. The handler is called with an error object that has a .event property which has all the information on the event: <code>{id, name, payload}</code>.
</td>
</tr>
</table>
Expand Down
3 changes: 1 addition & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
"prettier": {},
"dependencies": {
"@octokit/request-error": "^2.0.2",
"@octokit/webhooks-definitions": "^3.57.2",
"aggregate-error": "^3.1.0",
"debug": "^4.0.0"
},
"devDependencies": {
"@jest/types": "^26.6.2",
"@octokit/tsconfig": "^1.0.1",
"@octokit/webhooks-definitions": "3.57.2",
"@pika/pack": "^0.5.0",
"@pika/plugin-build-node": "^0.9.2",
"@pika/plugin-ts-standard-pkg": "^0.9.2",
Expand Down
11 changes: 5 additions & 6 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 !== "*" && property !== "error")
.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,8 +194,6 @@ const run = () => {
"// make edits in scripts/generate-types.ts",
"",
"export const emitterEventNames = [",
'"*",',
'"error",',
...properties.map(([key]) => `"${key}",`),
"];",
]);
Expand Down
9 changes: 4 additions & 5 deletions src/event-handler/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
EmitterAnyEvent,
EmitterEventName,
EmitterWebhookEvent,
EmitterWebhookEventName,
HandlerFunction,
Options,
State,
Expand All @@ -16,13 +15,13 @@ import { receiverHandle as receive } from "./receive";
import { removeListener } from "./remove-listener";

interface EventHandler<TTransformed = unknown> {
on<E extends EmitterEventName>(
on<E extends EmitterWebhookEventName>(
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>(
removeListener<E extends EmitterWebhookEventName>(
event: E | E[],
callback: HandlerFunction<E, TTransformed>
): void;
Expand Down
19 changes: 5 additions & 14 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,
EmitterWebhookEventName,
State,
WebhookEventHandlerError,
} from "../types";

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

if (webhookNameOrNames === "*" || webhookNameOrNames === "error") {
const webhookName = webhookNameOrNames === "*" ? "any" : webhookNameOrNames;
console.warn(
`Using the "${webhookNameOrNames}" event with the regular Webhooks.on() function is deprecated. Please use the Webhooks.on${
webhookName.charAt(0).toUpperCase() + webhookName.slice(1)
}() method instead`
);
}

handleEventHandlers(state, webhookNameOrNames, handler);
}

export function receiverOnAny(
state: State,
handler: (event: EmitterAnyEvent) => any
handler: (event: EmitterWebhookEvent) => any
) {
handleEventHandlers(state, "*", handler);
}
Expand Down
7 changes: 4 additions & 3 deletions src/event-handler/receive.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @ts-ignore to address #245
import AggregateError from "aggregate-error";
import { EmitterEventWebhookPayloadMap } from "../generated/get-webhook-payload-type-from-event";
import {
EmitterEventName,
import type {
EmitterWebhookEvent,
EmitterWebhookEventName,
State,
WebhookError,
WebhookEventHandlerError,
Expand All @@ -18,7 +18,7 @@ type EventAction = Extract<
function getHooks(
state: State,
eventPayloadAction: EventAction | null,
eventName: EmitterEventName
eventName: EmitterWebhookEventName
): Function[] {
const hooks = [state.hooks[eventName], state.hooks["*"]];

Expand Down Expand Up @@ -67,6 +67,7 @@ export function receiverHandle(state: State, event: EmitterWebhookEvent) {
let promise = Promise.resolve(event);

if (state.transform) {
// @ts-expect-error
promise = promise.then(state.transform);
}

Expand Down
4 changes: 2 additions & 2 deletions src/event-handler/remove-listener.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { EmitterEventName, State } from "../types";
import { EmitterWebhookEventName, State } from "../types";

export function removeListener(
state: State,
webhookNameOrNames: EmitterEventName | EmitterEventName[],
webhookNameOrNames: EmitterWebhookEventName | EmitterWebhookEventName[],
handler: Function
) {
if (Array.isArray(webhookNameOrNames)) {
Expand Down
6 changes: 3 additions & 3 deletions src/event-handler/wrap-error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Errors thrown or rejected Promises in "error" event handlers are not handled
// as they are in the webhook event handlers. If errors occur, we log a
// "Fatal: Error occured" message to stdout
// "Fatal: Error occurred" message to stdout
export function wrapErrorHandler(handler: Function, error: Error) {
let returnValue;

try {
returnValue = handler(error);
} catch (error) {
console.log('FATAL: Error occured in "error" event handler');
console.log('FATAL: Error occurred in "error" event handler');
console.log(error);
}

if (returnValue && returnValue.catch) {
returnValue.catch((error: Error) => {
console.log('FATAL: Error occured in "error" event handler');
console.log('FATAL: Error occurred in "error" event handler');
console.log(error);
});
}
Expand Down
Loading