@@ -5,6 +5,7 @@ import type {
55 OnPublishHandlerAggregateFn ,
66 OnPublishHandlerFn ,
77 OnSubscribeHandler ,
8+ ResolveOptions ,
89} from '../types/appsync-events.js' ;
910import { Router } from './Router.js' ;
1011import { UnauthorizedException } from './errors.js' ;
@@ -67,7 +68,9 @@ class AppSyncEventsResolver extends Router {
6768 * }
6869 *
6970 * async handler(event, context) {
70- * return app.resolve(event, context);
71+ * return app.resolve(event, context, {
72+ * scope: this, // bind decorated methods to the class instance
73+ * });
7174 * }
7275 * }
7376 *
@@ -78,7 +81,11 @@ class AppSyncEventsResolver extends Router {
7881 * @param event - The incoming event from AppSync Events
7982 * @param context - The context object provided by AWS Lambda
8083 */
81- public async resolve ( event : unknown , context : Context ) {
84+ public async resolve (
85+ event : unknown ,
86+ context : Context ,
87+ options ?: ResolveOptions
88+ ) {
8289 if ( ! isAppSyncEventsEvent ( event ) ) {
8390 this . logger . warn (
8491 'Received an event that is not compatible with this resolver'
@@ -87,11 +94,12 @@ class AppSyncEventsResolver extends Router {
8794 }
8895
8996 if ( isAppSyncEventsPublishEvent ( event ) ) {
90- return await this . handleOnPublish ( event , context ) ;
97+ return await this . handleOnPublish ( event , context , options ) ;
9198 }
9299 return await this . handleOnSubscribe (
93100 event as AppSyncEventsSubscribeEvent ,
94- context
101+ context ,
102+ options
95103 ) ;
96104 }
97105
@@ -100,10 +108,12 @@ class AppSyncEventsResolver extends Router {
100108 *
101109 * @param event - The incoming event from AppSync Events
102110 * @param context - The context object provided by AWS Lambda
111+ * @param options - Optional resolve options
103112 */
104113 protected async handleOnPublish (
105114 event : AppSyncEventsPublishEvent ,
106- context : Context
115+ context : Context ,
116+ options ?: ResolveOptions
107117 ) {
108118 const { path } = event . info . channel ;
109119 const routeHandlerOptions = this . onPublishRegistry . resolve ( path ) ;
@@ -114,11 +124,10 @@ class AppSyncEventsResolver extends Router {
114124 if ( aggregate ) {
115125 try {
116126 return {
117- events : await ( handler as OnPublishHandlerAggregateFn ) . apply ( this , [
118- event . events ,
119- event ,
120- context ,
121- ] ) ,
127+ events : await ( handler as OnPublishHandlerAggregateFn ) . apply (
128+ options ?. scope ?? this ,
129+ [ event . events , event , context ]
130+ ) ,
122131 } ;
123132 } catch ( error ) {
124133 this . logger . error ( `An error occurred in handler ${ path } ` , error ) ;
@@ -131,11 +140,10 @@ class AppSyncEventsResolver extends Router {
131140 event . events . map ( async ( message ) => {
132141 const { id, payload } = message ;
133142 try {
134- const result = await ( handler as OnPublishHandlerFn ) . apply ( this , [
135- payload ,
136- event ,
137- context ,
138- ] ) ;
143+ const result = await ( handler as OnPublishHandlerFn ) . apply (
144+ options ?. scope ?? this ,
145+ [ payload , event , context ]
146+ ) ;
139147 return {
140148 id,
141149 payload : result ,
@@ -161,10 +169,12 @@ class AppSyncEventsResolver extends Router {
161169 *
162170 * @param event - The incoming event from AppSync Events
163171 * @param context - The context object provided by AWS Lambda
172+ * @param options - Optional resolve options
164173 */
165174 protected async handleOnSubscribe (
166175 event : AppSyncEventsSubscribeEvent ,
167- context : Context
176+ context : Context ,
177+ options ?: ResolveOptions
168178 ) {
169179 const { path } = event . info . channel ;
170180 const routeHandlerOptions = this . onSubscribeRegistry . resolve ( path ) ;
@@ -173,7 +183,10 @@ class AppSyncEventsResolver extends Router {
173183 }
174184 const { handler } = routeHandlerOptions ;
175185 try {
176- await ( handler as OnSubscribeHandler ) . apply ( this , [ event , context ] ) ;
186+ await ( handler as OnSubscribeHandler ) . apply ( options ?. scope ?? this , [
187+ event ,
188+ context ,
189+ ] ) ;
177190 } catch ( error ) {
178191 this . logger . error ( `An error occurred in handler ${ path } ` , error ) ;
179192 if ( error instanceof UnauthorizedException ) throw error ;
0 commit comments