4343_event_type_updated_with_auth_context = "google.cloud.firestore.document.v1.updated.withAuthContext"
4444_event_type_deleted_with_auth_context = "google.cloud.firestore.document.v1.deleted.withAuthContext"
4545
46- AuthType = _typing .Literal ["service_account" , "api_key" , "system" ,
47- "unauthenticated" , "unknown" ]
48-
4946
5047@_dataclass .dataclass (frozen = True )
5148class Event (_core .CloudEvent [_core .T ]):
@@ -78,12 +75,6 @@ class Event(_core.CloudEvent[_core.T]):
7875 The document path.
7976 """
8077
81- auth_type : _typing .Optional [AuthType ]
82- """The type of principal that triggered the event"""
83-
84- auth_id : _typing .Optional [str ]
85- """The unique identifier for the principal"""
86-
8778 params : dict [str , str ]
8879 """
8980 An dict containing the values of the path patterns.
@@ -96,9 +87,26 @@ class Event(_core.CloudEvent[_core.T]):
9687_C1 = _typing .Callable [[_E1 ], None ]
9788_C2 = _typing .Callable [[_E2 ], None ]
9889
90+ AuthType = _typing .Literal ["service_account" , "api_key" , "system" ,
91+ "unauthenticated" , "unknown" ]
92+
93+
94+ @_dataclass .dataclass (frozen = True )
95+ class EventWithAuthContext (Event [_core .T ]):
96+ auth_type : AuthType
97+ """The type of principal that triggered the event"""
98+ auth_id : str
99+ """The unique identifier for the principal"""
100+
101+
102+ _E3 = EventWithAuthContext [Change [DocumentSnapshot | None ]]
103+ _E4 = EventWithAuthContext [DocumentSnapshot | None ]
104+ _C3 = _typing .Callable [[_E3 ], None ]
105+ _C4 = _typing .Callable [[_E4 ], None ]
106+
99107
100108def _firestore_endpoint_handler (
101- func : _C1 | _C2 ,
109+ func : _C1 | _C2 | _C3 | _C4 ,
102110 event_type : str ,
103111 document_pattern : _path_pattern .PathPattern ,
104112 raw : _ce .CloudEvent ,
@@ -195,19 +203,17 @@ def _firestore_endpoint_handler(
195203 data = firestore_event_data ,
196204 subject = event_attributes ["subject" ],
197205 params = params ,
198- auth_type = None ,
199- auth_id = None ,
200206 )
201207
202208 if event_type .endswith (".withAuthContext" ):
203- event_attrs = vars ( database_event ). copy ()
204- event_attrs . update ({
205- " auth_type" : event_auth_type ,
206- " auth_id" : event_auth_id ,
207- } )
208- database_event = Event ( ** event_attrs )
209-
210- func (database_event )
209+ database_event_with_auth_context = EventWithAuthContext (
210+ ** vars ( database_event ),
211+ auth_type = event_auth_type ,
212+ auth_id = event_auth_id )
213+ func ( database_event_with_auth_context )
214+ else :
215+ # mypy cannot infer that the event type is correct, hence the cast
216+ _typing . cast ( _C1 | _C2 , func ) (database_event )
211217
212218
213219@_util .copy_func_kwargs (FirestoreOptions )
@@ -271,13 +277,13 @@ def on_document_written_with_auth_context(**kwargs
271277 .. code-block:: python
272278
273279 @on_document_written_with_auth_context(document="*")
274- def example(event: Event [Change[DocumentSnapshot]]) -> None:
280+ def example(event: EventWithAuthContext [Change[DocumentSnapshot]]) -> None:
275281 pass
276282
277283 :param \\ *\\ *kwargs: Firestore options.
278284 :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
279285 :rtype: :exc:`typing.Callable`
280- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
286+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
281287 :exc:`firebase_functions.db.Change` \\ ] \\ ], `None` \\ ]
282288 A function that takes a Firestore event and returns ``None``.
283289 """
@@ -370,13 +376,13 @@ def on_document_updated_with_auth_context(**kwargs
370376 .. code-block:: python
371377
372378 @on_document_updated_with_auth_context(document="*")
373- def example(event: Event [Change[DocumentSnapshot]]) -> None:
379+ def example(event: EventWithAuthContext [Change[DocumentSnapshot]]) -> None:
374380 pass
375381
376382 :param \\ *\\ *kwargs: Firestore options.
377383 :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
378384 :rtype: :exc:`typing.Callable`
379- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
385+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
380386 :exc:`firebase_functions.db.Change` \\ ] \\ ], `None` \\ ]
381387 A function that takes a Firestore event and returns ``None``.
382388 """
@@ -469,13 +475,13 @@ def on_document_created_with_auth_context(**kwargs
469475 .. code-block:: python
470476
471477 @on_document_created_with_auth_context(document="*")
472- def example(event: Event [DocumentSnapshot]):
478+ def example(event: EventWithAuthContext [DocumentSnapshot]):
473479 pass
474480
475481 :param \\ *\\ *kwargs: Firestore options.
476482 :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
477483 :rtype: :exc:`typing.Callable`
478- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
484+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
479485 :exc:`object` \\ ] \\ ], `None` \\ ]
480486 A function that takes a Firestore event and returns ``None``.
481487 """
@@ -568,13 +574,13 @@ def on_document_deleted_with_auth_context(**kwargs
568574 .. code-block:: python
569575
570576 @on_document_deleted_with_auth_context(document="*")
571- def example(event: Event [DocumentSnapshot]) -> None:
577+ def example(event: EventWithAuthContext [DocumentSnapshot]) -> None:
572578 pass
573579
574580 :param \\ *\\ *kwargs: Firestore options.
575581 :type \\ *\\ *kwargs: as :exc:`firebase_functions.options.FirestoreOptions`
576582 :rtype: :exc:`typing.Callable`
577- \\ [ \\ [ :exc:`firebase_functions.firestore_fn.Event ` \\ [
583+ \\ [ \\ [ :exc:`firebase_functions.firestore_fn.EventWithAuthContext ` \\ [
578584 :exc:`object` \\ ] \\ ], `None` \\ ]
579585 A function that takes a Firestore event and returns ``None``.
580586 """
0 commit comments