import { WebhookEvent } from "@octokit/webhooks-types" ; import AggregateError from "aggregate-error" ; type IncomingMessage = any ; export function getPayload ( request ) { if ( request . body ) return Promise . resolve ( request . body as WebhookEvent ) ; return new Promise ( ( resolve , reject ) => { let data = "" ; request . setEncoding ( "utf8" ) ; request . on ( "error" , ( error ) => reject ( new AggregateError ( [ error ] ) ) ) ; request . on ( "data" , ( chunk ) => ( data += chunk ) ) ; request . on ( "end" , ( ) => { try { resolve ( JSON . parse ( data ) ) ; } catch ( error ) { error . message = "Invalid JSON" ; error . status = 400 ; reject ( new AggregateError ( [ error ] ) ) ; } } ) ; } ) ; }