File tree Expand file tree Collapse file tree 5 files changed +26
-12
lines changed
Expand file tree Collapse file tree 5 files changed +26
-12
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import type {
55 HandlerFunction ,
66 Options ,
77 State ,
8+ WebhookError ,
89 WebhookEventHandlerError ,
910} from "../types.js" ;
1011import {
@@ -15,7 +16,7 @@ import {
1516import { receiverHandle as receive } from "./receive.js" ;
1617import { removeListener } from "./remove-listener.js" ;
1718
18- interface EventHandler < TTransformed > {
19+ export interface EventHandler < TTransformed > {
1920 on < E extends EmitterWebhookEventName > (
2021 event : E | E [ ] ,
2122 callback : HandlerFunction < E , TTransformed > ,
@@ -34,7 +35,7 @@ interface EventHandler<TTransformed> {
3435 event : E | E [ ] ,
3536 callback : HandlerFunction < E , TTransformed > ,
3637 ) : void ;
37- receive ( event : EmitterWebhookEvent ) : Promise < void > ;
38+ receive ( event : EmitterWebhookEvent | WebhookError ) : Promise < void > ;
3839}
3940
4041export function createEventHandler < TTransformed > (
Original file line number Diff line number Diff line change 22import AggregateError from "aggregate-error" ;
33import type {
44 EmitterWebhookEvent ,
5- EmitterWebhookEventName ,
65 State ,
76 WebhookError ,
7+ WebhookEventName ,
88 WebhookEventHandlerError ,
99} from "../types.js" ;
1010import { wrapErrorHandler } from "./wrap-error-handler.js" ;
@@ -17,7 +17,7 @@ type EventAction = Extract<
1717function getHooks (
1818 state : State ,
1919 eventPayloadAction : EventAction | null ,
20- eventName : EmitterWebhookEventName ,
20+ eventName : WebhookEventName ,
2121) : Function [ ] {
2222 const hooks = [ state . hooks [ eventName ] , state . hooks [ "*" ] ] ;
2323
@@ -29,7 +29,10 @@ function getHooks(
2929}
3030
3131// main handler function
32- export function receiverHandle ( state : State , event : EmitterWebhookEvent ) {
32+ export function receiverHandle (
33+ state : State ,
34+ event : EmitterWebhookEvent | WebhookError ,
35+ ) {
3336 const errorHandlers = state . hooks . error || [ ] ;
3437
3538 if ( event instanceof Error ) {
Original file line number Diff line number Diff line change 11import { createLogger } from "./createLogger.js" ;
2- import { createEventHandler } from "./event-handler/index.js" ;
2+ import {
3+ createEventHandler ,
4+ type EventHandler ,
5+ } from "./event-handler/index.js" ;
36import { sign , verify } from "@octokit/webhooks-methods" ;
47import { verifyAndReceive } from "./verify-and-receive.js" ;
58import type {
@@ -49,7 +52,10 @@ class Webhooks<TTransformed = unknown> {
4952 throw new Error ( "[@octokit/webhooks] options.secret required" ) ;
5053 }
5154
52- const state : State & { secret : string } = {
55+ const state : State & {
56+ secret : string ;
57+ eventHandler : EventHandler < TTransformed > ;
58+ } = {
5359 eventHandler : createEventHandler ( options ) ,
5460 secret : options . secret ,
5561 hooks : { } ,
Original file line number Diff line number Diff line change @@ -3,7 +3,9 @@ import type {
33 WebhookEventMap ,
44 WebhookEventName ,
55} from "@octokit/webhooks-types" ;
6+ export type { WebhookEventName } from "@octokit/webhooks-types" ;
67import type { Logger } from "./createLogger.js" ;
8+ import type { EventHandler } from "./event-handler/index.js" ;
79import type { emitterEventNames } from "./generated/webhook-names.js" ;
810
911export type EmitterWebhookEventName = ( typeof emitterEventNames ) [ number ] ;
@@ -17,7 +19,7 @@ export type EmitterWebhookEvent<
1719
1820export type EmitterWebhookEventWithStringPayloadAndSignature = {
1921 id : string ;
20- name : EmitterWebhookEventName ;
22+ name : WebhookEventName ;
2123 payload : string ;
2224 signature : string ;
2325} ;
@@ -51,7 +53,7 @@ type Hooks = {
5153} ;
5254
5355export interface State extends Options < any > {
54- eventHandler ?: any ;
56+ eventHandler ?: EventHandler < unknown > ;
5557 hooks : Hooks ;
5658 log : Logger ;
5759}
Original file line number Diff line number Diff line change @@ -6,10 +6,12 @@ import type {
66 EmitterWebhookEvent ,
77 EmitterWebhookEventWithStringPayloadAndSignature ,
88 State ,
9+ WebhookError ,
910} from "./types.js" ;
11+ import type { EventHandler } from "./event-handler/index.js" ;
1012
1113export async function verifyAndReceive (
12- state : State & { secret : string } ,
14+ state : State & { secret : string ; eventHandler : EventHandler < unknown > } ,
1315 event : EmitterWebhookEventWithStringPayloadAndSignature ,
1416) : Promise < void > {
1517 // verify will validate that the secret is not undefined
@@ -25,7 +27,7 @@ export async function verifyAndReceive(
2527 ) ;
2628
2729 return state . eventHandler . receive (
28- Object . assign ( error , { event, status : 400 } ) ,
30+ Object . assign ( error , { event, status : 400 } ) as WebhookError ,
2931 ) ;
3032 }
3133
@@ -42,5 +44,5 @@ export async function verifyAndReceive(
4244 id : event . id ,
4345 name : event . name ,
4446 payload,
45- } ) ;
47+ } as EmitterWebhookEvent ) ;
4648}
You can’t perform that action at this time.
0 commit comments