From f9fd391db6869ce49fd1a172339f80ab2fbdf269 Mon Sep 17 00:00:00 2001 From: wolfy1339 Date: Mon, 26 Apr 2021 12:14:21 -0400 Subject: [PATCH] fix: adapt code for `aggregate-error@v4` --- src/event-handler/receive.ts | 1 - src/middleware/node/get-payload.ts | 1 - src/middleware/node/middleware.ts | 3 ++- src/types.ts | 18 +----------------- test/typescript-validate.ts | 2 +- 5 files changed, 4 insertions(+), 21 deletions(-) diff --git a/src/event-handler/receive.ts b/src/event-handler/receive.ts index 0652f5ae..f5af8984 100644 --- a/src/event-handler/receive.ts +++ b/src/event-handler/receive.ts @@ -1,4 +1,3 @@ -// @ts-ignore to address #245 import AggregateError from "aggregate-error"; import type { EmitterWebhookEvent, diff --git a/src/middleware/node/get-payload.ts b/src/middleware/node/get-payload.ts index 29e7a45f..13f37d65 100644 --- a/src/middleware/node/get-payload.ts +++ b/src/middleware/node/get-payload.ts @@ -1,5 +1,4 @@ import { WebhookEvent } from "@octokit/webhooks-types"; -// @ts-ignore to address #245 import AggregateError from "aggregate-error"; // remove type imports from http for Deno compatibility diff --git a/src/middleware/node/middleware.ts b/src/middleware/node/middleware.ts index 319e1732..1f6a9731 100644 --- a/src/middleware/node/middleware.ts +++ b/src/middleware/node/middleware.ts @@ -80,7 +80,8 @@ export async function middleware( if (didTimeout) return; - const statusCode = Array.from(error as WebhookEventHandlerError)[0].status; + const statusCode = Array.from((error as WebhookEventHandlerError).errors)[0] + .status; response.statusCode = typeof statusCode !== "undefined" ? statusCode : 500; response.end(error.toString()); } diff --git a/src/types.ts b/src/types.ts index b9b3978a..9e0681a6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,5 @@ import { RequestError } from "@octokit/request-error"; +import AggregateError from "aggregate-error"; import type { WebhookEventMap, WebhookEventName, @@ -53,20 +54,3 @@ export type WebhookError = Error & Partial; export interface WebhookEventHandlerError extends AggregateError { event: EmitterWebhookEvent; } - -/** - * Workaround for TypeScript incompatibility with types exported by aggregate-error. - * Credit: https://git.io/JUEEr - * @copyright Sindre Sorhus - * @license MIT (https://git.io/JUEEK) - * @see https://github.com/octokit/webhooks.js/pull/270/files - */ -declare class AggregateError - extends Error - implements Iterable -{ - readonly name: "AggregateError"; - constructor(errors: ReadonlyArray); - - [Symbol.iterator](): IterableIterator; -} diff --git a/test/typescript-validate.ts b/test/typescript-validate.ts index 055a80d5..cc8762dd 100644 --- a/test/typescript-validate.ts +++ b/test/typescript-validate.ts @@ -172,7 +172,7 @@ export default async function () { webhooks.onError((error) => { console.log(error.event.name); - const [firstError] = Array.from(error); + const [firstError] = Array.from(error.errors); console.log(firstError.status); console.log(firstError.headers); console.log(firstError.request);