Skip to content

Commit

Permalink
fix: adapt code for aggregate-error@v4
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy1339 committed May 28, 2021
1 parent f24686c commit f9fd391
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/event-handler/receive.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-ignore to address #245
import AggregateError from "aggregate-error";
import type {
EmitterWebhookEvent,
Expand Down
1 change: 0 additions & 1 deletion src/middleware/node/get-payload.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/node/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
18 changes: 1 addition & 17 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestError } from "@octokit/request-error";
import AggregateError from "aggregate-error";
import type {
WebhookEventMap,
WebhookEventName,
Expand Down Expand Up @@ -53,20 +54,3 @@ export type WebhookError = Error & Partial<RequestError>;
export interface WebhookEventHandlerError extends AggregateError<WebhookError> {
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<T extends Error = Error>
extends Error
implements Iterable<T>
{
readonly name: "AggregateError";
constructor(errors: ReadonlyArray<T | { [key: string]: any } | string>);

[Symbol.iterator](): IterableIterator<T>;
}
2 changes: 1 addition & 1 deletion test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit f9fd391

Please sign in to comment.