@@ -93,7 +93,11 @@ export interface RawFirestoreEvent extends CloudEvent<Uint8Array | RawFirestoreD
93
93
document : string ;
94
94
datacontenttype ?: string ;
95
95
dataschema : string ;
96
- authtype ?: string ;
96
+ }
97
+
98
+ /** @internal */
99
+ export interface RawFirestoreAuthEvent extends RawFirestoreEvent {
100
+ authtype ?: AuthType ;
97
101
authid ?: string ;
98
102
}
99
103
@@ -125,17 +129,21 @@ export interface FirestoreEvent<T, Params = Record<string, string>> extends Clou
125
129
namespace : string ;
126
130
/** The document path */
127
131
document : string ;
128
- /** The type of principal that triggered the event */
129
- authType ?: AuthType ;
130
- /** The unique identifier for the principal */
131
- authId ?: string ;
132
132
/**
133
133
* An object containing the values of the path patterns.
134
134
* Only named capture groups will be populated - {key}, {key=*}, {key=**}
135
135
*/
136
136
params : Params ;
137
137
}
138
138
139
+ export interface FirestoreAuthEvent < T , Params = Record < string , string > >
140
+ extends FirestoreEvent < T , Params > {
141
+ /** The type of principal that triggered the event */
142
+ authType : AuthType ;
143
+ /** The unique identifier for the principal */
144
+ authId ?: string ;
145
+ }
146
+
139
147
/** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace. */
140
148
export interface DocumentOptions < Document extends string = string > extends EventHandlerOptions {
141
149
/** The document path */
@@ -197,9 +205,9 @@ export function onDocumentWritten<Document extends string>(
197
205
export function onDocumentWrittenWithAuthContext < Document extends string > (
198
206
document : Document ,
199
207
handler : (
200
- event : FirestoreEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > >
208
+ event : FirestoreAuthEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > >
201
209
) => any | Promise < any >
202
- ) : CloudFunction < FirestoreEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > > > ;
210
+ ) : CloudFunction < FirestoreAuthEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > > > ;
203
211
204
212
/**
205
213
* Event handler that triggers when a document is created, updated, or deleted in Firestore.
@@ -211,9 +219,9 @@ export function onDocumentWrittenWithAuthContext<Document extends string>(
211
219
export function onDocumentWrittenWithAuthContext < Document extends string > (
212
220
opts : DocumentOptions < Document > ,
213
221
handler : (
214
- event : FirestoreEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > >
222
+ event : FirestoreAuthEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > >
215
223
) => any | Promise < any >
216
- ) : CloudFunction < FirestoreEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > > > ;
224
+ ) : CloudFunction < FirestoreAuthEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > > > ;
217
225
218
226
/**
219
227
* Event handler that triggers when a document is created, updated, or deleted in Firestore.
@@ -223,12 +231,19 @@ export function onDocumentWrittenWithAuthContext<Document extends string>(
223
231
* @param handler - Event handler which is run every time a Firestore create, update, or delete occurs.
224
232
*/
225
233
export function onDocumentWrittenWithAuthContext < Document extends string > (
226
- documentorOpts : Document | DocumentOptions < Document > ,
234
+ documentOrOpts : Document | DocumentOptions < Document > ,
227
235
handler : (
228
- event : FirestoreEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > >
236
+ event : FirestoreAuthEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > >
229
237
) => any | Promise < any >
230
- ) : CloudFunction < FirestoreEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > > > {
231
- return onChangedOperation ( writtenEventWithAuthContextType , documentorOpts , handler ) ;
238
+ ) : CloudFunction < FirestoreAuthEvent < Change < DocumentSnapshot > | undefined , ParamsOf < Document > > > {
239
+ // const fn = (
240
+ // event: FirestoreEvent<Change<QueryDocumentSnapshot> | undefined, ParamsOf<Document>> & {
241
+ // foo: string;
242
+ // }
243
+ // ): any => {
244
+ // return event;
245
+ // };
246
+ return onChangedOperation ( writtenEventWithAuthContextType , documentOrOpts , handler ) ;
232
247
}
233
248
234
249
/**
@@ -282,9 +297,9 @@ export function onDocumentCreated<Document extends string>(
282
297
export function onDocumentCreatedWithAuthContext < Document extends string > (
283
298
document : Document ,
284
299
handler : (
285
- event : FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
300
+ event : FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
286
301
) => any | Promise < any >
287
- ) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
302
+ ) : CloudFunction < FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
288
303
289
304
/**
290
305
* Event handler that triggers when a document is created in Firestore.
@@ -296,9 +311,9 @@ export function onDocumentCreatedWithAuthContext<Document extends string>(
296
311
export function onDocumentCreatedWithAuthContext < Document extends string > (
297
312
opts : DocumentOptions < Document > ,
298
313
handler : (
299
- event : FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
314
+ event : FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
300
315
) => any | Promise < any >
301
- ) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
316
+ ) : CloudFunction < FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
302
317
303
318
/**
304
319
* Event handler that triggers when a document is created in Firestore.
@@ -309,9 +324,9 @@ export function onDocumentCreatedWithAuthContext<Document extends string>(
309
324
export function onDocumentCreatedWithAuthContext < Document extends string > (
310
325
documentOrOpts : Document | DocumentOptions < Document > ,
311
326
handler : (
312
- event : FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
327
+ event : FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
313
328
) => any | Promise < any >
314
- ) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > {
329
+ ) : CloudFunction < FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > {
315
330
return onOperation ( createdEventWithAuthContextType , documentOrOpts , handler ) ;
316
331
}
317
332
@@ -365,9 +380,10 @@ export function onDocumentUpdated<Document extends string>(
365
380
export function onDocumentUpdatedWithAuthContext < Document extends string > (
366
381
document : Document ,
367
382
handler : (
368
- event : FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > >
383
+ event : FirestoreAuthEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > >
369
384
) => any | Promise < any >
370
- ) : CloudFunction < FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > > > ;
385
+ ) : CloudFunction < FirestoreAuthEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > > > ;
386
+
371
387
/**
372
388
* Event handler that triggers when a document is updated in Firestore.
373
389
* This trigger also provides the authentication context of the principal who triggered the event.
@@ -378,9 +394,9 @@ export function onDocumentUpdatedWithAuthContext<Document extends string>(
378
394
export function onDocumentUpdatedWithAuthContext < Document extends string > (
379
395
opts : DocumentOptions < Document > ,
380
396
handler : (
381
- event : FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > >
397
+ event : FirestoreAuthEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > >
382
398
) => any | Promise < any >
383
- ) : CloudFunction < FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > > > ;
399
+ ) : CloudFunction < FirestoreAuthEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > > > ;
384
400
385
401
/**
386
402
* Event handler that triggers when a document is updated in Firestore.
@@ -391,9 +407,11 @@ export function onDocumentUpdatedWithAuthContext<Document extends string>(
391
407
export function onDocumentUpdatedWithAuthContext < Document extends string > (
392
408
documentOrOpts : Document | DocumentOptions < Document > ,
393
409
handler : (
394
- event : FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > >
410
+ event : FirestoreAuthEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > >
395
411
) => any | Promise < any >
396
- ) : CloudFunction < FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > > > {
412
+ ) : CloudFunction <
413
+ FirestoreAuthEvent < Change < QueryDocumentSnapshot > | undefined , ParamsOf < Document > >
414
+ > {
397
415
return onChangedOperation ( updatedEventWithAuthContextType , documentOrOpts , handler ) ;
398
416
}
399
417
@@ -448,9 +466,9 @@ export function onDocumentDeleted<Document extends string>(
448
466
export function onDocumentDeletedWithAuthContext < Document extends string > (
449
467
document : Document ,
450
468
handler : (
451
- event : FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
469
+ event : FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
452
470
) => any | Promise < any >
453
- ) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
471
+ ) : CloudFunction < FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
454
472
455
473
/**
456
474
* Event handler that triggers when a document is deleted in Firestore.
@@ -462,9 +480,9 @@ export function onDocumentDeletedWithAuthContext<Document extends string>(
462
480
export function onDocumentDeletedWithAuthContext < Document extends string > (
463
481
opts : DocumentOptions < Document > ,
464
482
handler : (
465
- event : FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
483
+ event : FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
466
484
) => any | Promise < any >
467
- ) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
485
+ ) : CloudFunction < FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > ;
468
486
469
487
/**
470
488
* Event handler that triggers when a document is deleted in Firestore.
@@ -475,9 +493,9 @@ export function onDocumentDeletedWithAuthContext<Document extends string>(
475
493
export function onDocumentDeletedWithAuthContext < Document extends string > (
476
494
documentOrOpts : Document | DocumentOptions < Document > ,
477
495
handler : (
478
- event : FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
496
+ event : FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > >
479
497
) => any | Promise < any >
480
- ) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > {
498
+ ) : CloudFunction < FirestoreAuthEvent < QueryDocumentSnapshot | undefined , ParamsOf < Document > > > {
481
499
return onOperation ( deletedEventWithAuthContextType , documentOrOpts , handler ) ;
482
500
}
483
501
@@ -566,43 +584,68 @@ export function makeParams(document: string, documentPattern: PathPattern) {
566
584
/** @internal */
567
585
export function makeFirestoreEvent < Params > (
568
586
eventType : string ,
569
- event : RawFirestoreEvent ,
587
+ event : RawFirestoreEvent | RawFirestoreAuthEvent ,
570
588
params : Params
571
- ) : FirestoreEvent < QueryDocumentSnapshot | undefined , Params > {
589
+ ) :
590
+ | FirestoreEvent < QueryDocumentSnapshot | undefined , Params >
591
+ | FirestoreAuthEvent < QueryDocumentSnapshot | undefined , Params > {
572
592
const data = event . data
573
- ? eventType === createdEventType
593
+ ? eventType === createdEventType || eventType === createdEventWithAuthContextType
574
594
? createSnapshot ( event )
575
595
: createBeforeSnapshot ( event )
576
596
: undefined ;
577
597
const firestoreEvent : FirestoreEvent < QueryDocumentSnapshot | undefined , Params > = {
578
598
...event ,
579
599
params,
580
600
data,
581
- authType : event . authtype as AuthType ,
582
- authId : event . authid ,
583
601
} ;
602
+
584
603
delete ( firestoreEvent as any ) . datacontenttype ;
585
604
delete ( firestoreEvent as any ) . dataschema ;
605
+
606
+ if ( "authtype" in event ) {
607
+ const eventWithAuth = {
608
+ ...firestoreEvent ,
609
+ authType : event . authtype ,
610
+ authId : event . authid ,
611
+ } ;
612
+ delete ( eventWithAuth as any ) . authtype ;
613
+ delete ( eventWithAuth as any ) . authid ;
614
+ return eventWithAuth ;
615
+ }
616
+
586
617
return firestoreEvent ;
587
618
}
588
619
589
620
/** @internal */
590
621
export function makeChangedFirestoreEvent < Params > (
591
- event : RawFirestoreEvent ,
622
+ event : RawFirestoreEvent | RawFirestoreAuthEvent ,
592
623
params : Params
593
- ) : FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , Params > {
624
+ ) :
625
+ | FirestoreEvent < Change < DocumentSnapshot > | undefined , Params >
626
+ | FirestoreAuthEvent < Change < DocumentSnapshot > | undefined , Params > {
594
627
const data = event . data
595
628
? Change . fromObjects ( createBeforeSnapshot ( event ) , createSnapshot ( event ) )
596
629
: undefined ;
597
630
const firestoreEvent : FirestoreEvent < Change < QueryDocumentSnapshot > | undefined , Params > = {
598
631
...event ,
599
632
params,
600
633
data,
601
- authType : event . authtype as AuthType ,
602
- authId : event . authid ,
603
634
} ;
604
635
delete ( firestoreEvent as any ) . datacontenttype ;
605
636
delete ( firestoreEvent as any ) . dataschema ;
637
+
638
+ if ( "authtype" in event ) {
639
+ const eventWithAuth = {
640
+ ...firestoreEvent ,
641
+ authType : event . authtype ,
642
+ authId : event . authid ,
643
+ } ;
644
+ delete ( eventWithAuth as any ) . authtype ;
645
+ delete ( eventWithAuth as any ) . authid ;
646
+ return eventWithAuth ;
647
+ }
648
+
606
649
return firestoreEvent ;
607
650
}
608
651
@@ -649,16 +692,19 @@ export function makeEndpoint(
649
692
}
650
693
651
694
/** @internal */
652
- export function onOperation < Document extends string > (
695
+ export function onOperation <
696
+ Document extends string ,
697
+ Event extends FirestoreEvent < QueryDocumentSnapshot , ParamsOf < Document > >
698
+ > (
653
699
eventType : string ,
654
700
documentOrOpts : Document | DocumentOptions < Document > ,
655
- handler : ( event : FirestoreEvent < QueryDocumentSnapshot , ParamsOf < Document > > ) => any | Promise < any >
656
- ) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot , ParamsOf < Document > > > {
701
+ handler : ( event : Event ) => any | Promise < any >
702
+ ) : CloudFunction < Event > {
657
703
const { document, database, namespace, opts } = getOpts ( documentOrOpts ) ;
658
704
659
705
// wrap the handler
660
706
const func = ( raw : CloudEvent < unknown > ) => {
661
- const event = raw as RawFirestoreEvent ;
707
+ const event = raw as RawFirestoreEvent | RawFirestoreAuthEvent ;
662
708
const documentPattern = new PathPattern (
663
709
typeof document === "string" ? document : document . value ( )
664
710
) ;
@@ -675,18 +721,19 @@ export function onOperation<Document extends string>(
675
721
}
676
722
677
723
/** @internal */
678
- export function onChangedOperation < Document extends string > (
724
+ export function onChangedOperation <
725
+ Document extends string ,
726
+ Event extends FirestoreEvent < Change < DocumentSnapshot > , ParamsOf < Document > >
727
+ > (
679
728
eventType : string ,
680
729
documentOrOpts : Document | DocumentOptions < Document > ,
681
- handler : (
682
- event : FirestoreEvent < Change < QueryDocumentSnapshot > , ParamsOf < Document > >
683
- ) => any | Promise < any >
684
- ) : CloudFunction < FirestoreEvent < Change < QueryDocumentSnapshot > , ParamsOf < Document > > > {
730
+ handler : ( event : Event ) => any | Promise < any >
731
+ ) : CloudFunction < Event > {
685
732
const { document, database, namespace, opts } = getOpts ( documentOrOpts ) ;
686
733
687
734
// wrap the handler
688
735
const func = ( raw : CloudEvent < unknown > ) => {
689
- const event = raw as RawFirestoreEvent ;
736
+ const event = raw as RawFirestoreEvent | RawFirestoreAuthEvent ;
690
737
const documentPattern = new PathPattern (
691
738
typeof document === "string" ? document : document . value ( )
692
739
) ;
0 commit comments