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 {
5
5
HandlerFunction ,
6
6
Options ,
7
7
State ,
8
+ WebhookError ,
8
9
WebhookEventHandlerError ,
9
10
} from "../types.js" ;
10
11
import {
@@ -15,7 +16,7 @@ import {
15
16
import { receiverHandle as receive } from "./receive.js" ;
16
17
import { removeListener } from "./remove-listener.js" ;
17
18
18
- interface EventHandler < TTransformed > {
19
+ export interface EventHandler < TTransformed > {
19
20
on < E extends EmitterWebhookEventName > (
20
21
event : E | E [ ] ,
21
22
callback : HandlerFunction < E , TTransformed > ,
@@ -34,7 +35,7 @@ interface EventHandler<TTransformed> {
34
35
event : E | E [ ] ,
35
36
callback : HandlerFunction < E , TTransformed > ,
36
37
) : void ;
37
- receive ( event : EmitterWebhookEvent ) : Promise < void > ;
38
+ receive ( event : EmitterWebhookEvent | WebhookError ) : Promise < void > ;
38
39
}
39
40
40
41
export function createEventHandler < TTransformed > (
Original file line number Diff line number Diff line change 2
2
import AggregateError from "aggregate-error" ;
3
3
import type {
4
4
EmitterWebhookEvent ,
5
- EmitterWebhookEventName ,
6
5
State ,
7
6
WebhookError ,
7
+ WebhookEventName ,
8
8
WebhookEventHandlerError ,
9
9
} from "../types.js" ;
10
10
import { wrapErrorHandler } from "./wrap-error-handler.js" ;
@@ -17,7 +17,7 @@ type EventAction = Extract<
17
17
function getHooks (
18
18
state : State ,
19
19
eventPayloadAction : EventAction | null ,
20
- eventName : EmitterWebhookEventName ,
20
+ eventName : WebhookEventName ,
21
21
) : Function [ ] {
22
22
const hooks = [ state . hooks [ eventName ] , state . hooks [ "*" ] ] ;
23
23
@@ -29,7 +29,10 @@ function getHooks(
29
29
}
30
30
31
31
// main handler function
32
- export function receiverHandle ( state : State , event : EmitterWebhookEvent ) {
32
+ export function receiverHandle (
33
+ state : State ,
34
+ event : EmitterWebhookEvent | WebhookError ,
35
+ ) {
33
36
const errorHandlers = state . hooks . error || [ ] ;
34
37
35
38
if ( event instanceof Error ) {
Original file line number Diff line number Diff line change 1
1
import { 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" ;
3
6
import { sign , verify } from "@octokit/webhooks-methods" ;
4
7
import { verifyAndReceive } from "./verify-and-receive.js" ;
5
8
import type {
@@ -49,7 +52,10 @@ class Webhooks<TTransformed = unknown> {
49
52
throw new Error ( "[@octokit/webhooks] options.secret required" ) ;
50
53
}
51
54
52
- const state : State & { secret : string } = {
55
+ const state : State & {
56
+ secret : string ;
57
+ eventHandler : EventHandler < TTransformed > ;
58
+ } = {
53
59
eventHandler : createEventHandler ( options ) ,
54
60
secret : options . secret ,
55
61
hooks : { } ,
Original file line number Diff line number Diff line change @@ -3,7 +3,9 @@ import type {
3
3
WebhookEventMap ,
4
4
WebhookEventName ,
5
5
} from "@octokit/webhooks-types" ;
6
+ export type { WebhookEventName } from "@octokit/webhooks-types" ;
6
7
import type { Logger } from "./createLogger.js" ;
8
+ import type { EventHandler } from "./event-handler/index.js" ;
7
9
import type { emitterEventNames } from "./generated/webhook-names.js" ;
8
10
9
11
export type EmitterWebhookEventName = ( typeof emitterEventNames ) [ number ] ;
@@ -17,7 +19,7 @@ export type EmitterWebhookEvent<
17
19
18
20
export type EmitterWebhookEventWithStringPayloadAndSignature = {
19
21
id : string ;
20
- name : EmitterWebhookEventName ;
22
+ name : WebhookEventName ;
21
23
payload : string ;
22
24
signature : string ;
23
25
} ;
@@ -51,7 +53,7 @@ type Hooks = {
51
53
} ;
52
54
53
55
export interface State extends Options < any > {
54
- eventHandler ?: any ;
56
+ eventHandler ?: EventHandler < unknown > ;
55
57
hooks : Hooks ;
56
58
log : Logger ;
57
59
}
Original file line number Diff line number Diff line change @@ -6,10 +6,12 @@ import type {
6
6
EmitterWebhookEvent ,
7
7
EmitterWebhookEventWithStringPayloadAndSignature ,
8
8
State ,
9
+ WebhookError ,
9
10
} from "./types.js" ;
11
+ import type { EventHandler } from "./event-handler/index.js" ;
10
12
11
13
export async function verifyAndReceive (
12
- state : State & { secret : string } ,
14
+ state : State & { secret : string ; eventHandler : EventHandler < unknown > } ,
13
15
event : EmitterWebhookEventWithStringPayloadAndSignature ,
14
16
) : Promise < void > {
15
17
// verify will validate that the secret is not undefined
@@ -25,7 +27,7 @@ export async function verifyAndReceive(
25
27
) ;
26
28
27
29
return state . eventHandler . receive (
28
- Object . assign ( error , { event, status : 400 } ) ,
30
+ Object . assign ( error , { event, status : 400 } ) as WebhookError ,
29
31
) ;
30
32
}
31
33
@@ -42,5 +44,5 @@ export async function verifyAndReceive(
42
44
id : event . id ,
43
45
name : event . name ,
44
46
payload,
45
- } ) ;
47
+ } as EmitterWebhookEvent ) ;
46
48
}
You can’t perform that action at this time.
0 commit comments