@@ -36,6 +36,7 @@ import {
3636} from "../../common/providers/firestore" ;
3737import { wrapTraceContext } from "../trace" ;
3838import { withInit } from "../../common/onInit" ;
39+ import { Expression } from "../../params" ;
3940
4041export { Change } ;
4142
@@ -106,11 +107,11 @@ export interface FirestoreEvent<T, Params = Record<string, string>> extends Clou
106107/** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace. */
107108export interface DocumentOptions < Document extends string = string > extends EventHandlerOptions {
108109 /** The document path */
109- document : Document ;
110+ document : Document | Expression < string > ;
110111 /** The Firestore database */
111- database ?: string ;
112+ database ?: string | Expression < string > ;
112113 /** The Firestore namespace */
113- namespace ?: string ;
114+ namespace ?: string | Expression < string > ;
114115}
115116
116117/**
@@ -278,17 +279,20 @@ export function onDocumentDeleted<Document extends string>(
278279
279280/** @internal */
280281export function getOpts ( documentOrOpts : string | DocumentOptions ) {
281- let document : string ;
282- let database : string ;
283- let namespace : string ;
282+ let document : string | Expression < string > ;
283+ let database : string | Expression < string > ;
284+ let namespace : string | Expression < string > ;
284285 let opts : EventHandlerOptions ;
285286 if ( typeof documentOrOpts === "string" ) {
286287 document = normalizePath ( documentOrOpts ) ;
287288 database = "(default)" ;
288289 namespace = "(default)" ;
289290 opts = { } ;
290291 } else {
291- document = normalizePath ( documentOrOpts . document ) ;
292+ document =
293+ typeof documentOrOpts . document === "string"
294+ ? normalizePath ( documentOrOpts . document )
295+ : documentOrOpts . document ;
292296 database = documentOrOpts . database || "(default)" ;
293297 namespace = documentOrOpts . namespace || "(default)" ;
294298 opts = { ...documentOrOpts } ;
@@ -398,21 +402,25 @@ export function makeChangedFirestoreEvent<Params>(
398402export function makeEndpoint (
399403 eventType : string ,
400404 opts : EventHandlerOptions ,
401- document : PathPattern ,
402- database : string ,
403- namespace : string
405+ document : string | Expression < string > ,
406+ database : string | Expression < string > ,
407+ namespace : string | Expression < string >
404408) : ManifestEndpoint {
405409 const baseOpts = optionsToEndpoint ( getGlobalOptions ( ) ) ;
406410 const specificOpts = optionsToEndpoint ( opts ) ;
407411
408- const eventFilters : Record < string , string > = {
412+ const eventFilters : Record < string , string | Expression < string > > = {
409413 database,
410414 namespace,
411415 } ;
412- const eventFilterPathPatterns : Record < string , string > = { } ;
413- document . hasWildcards ( )
414- ? ( eventFilterPathPatterns . document = document . getValue ( ) )
415- : ( eventFilters . document = document . getValue ( ) ) ;
416+ const eventFilterPathPatterns : Record < string , string | Expression < string > > = { } ;
417+ const maybePattern =
418+ typeof document === "string" ? new PathPattern ( document ) . hasWildcards ( ) : true ;
419+ if ( maybePattern ) {
420+ eventFilterPathPatterns . document = document ;
421+ } else {
422+ eventFilters . document = document ;
423+ }
416424
417425 return {
418426 ...initV2Endpoint ( getGlobalOptions ( ) , opts ) ,
@@ -440,19 +448,20 @@ export function onOperation<Document extends string>(
440448) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot , ParamsOf < Document > > > {
441449 const { document, database, namespace, opts } = getOpts ( documentOrOpts ) ;
442450
443- const documentPattern = new PathPattern ( document ) ;
444-
445451 // wrap the handler
446452 const func = ( raw : CloudEvent < unknown > ) => {
447453 const event = raw as RawFirestoreEvent ;
454+ const documentPattern = new PathPattern (
455+ typeof document === "string" ? document : document . value ( )
456+ ) ;
448457 const params = makeParams ( event . document , documentPattern ) as unknown as ParamsOf < Document > ;
449458 const firestoreEvent = makeFirestoreEvent ( eventType , event , params ) ;
450459 return wrapTraceContext ( withInit ( handler ) ) ( firestoreEvent ) ;
451460 } ;
452461
453462 func . run = handler ;
454463
455- func . __endpoint = makeEndpoint ( eventType , opts , documentPattern , database , namespace ) ;
464+ func . __endpoint = makeEndpoint ( eventType , opts , document , database , namespace ) ;
456465
457466 return func ;
458467}
@@ -467,19 +476,20 @@ export function onChangedOperation<Document extends string>(
467476) : CloudFunction < FirestoreEvent < Change < QueryDocumentSnapshot > , ParamsOf < Document > > > {
468477 const { document, database, namespace, opts } = getOpts ( documentOrOpts ) ;
469478
470- const documentPattern = new PathPattern ( document ) ;
471-
472479 // wrap the handler
473480 const func = ( raw : CloudEvent < unknown > ) => {
474481 const event = raw as RawFirestoreEvent ;
482+ const documentPattern = new PathPattern (
483+ typeof document === "string" ? document : document . value ( )
484+ ) ;
475485 const params = makeParams ( event . document , documentPattern ) as unknown as ParamsOf < Document > ;
476486 const firestoreEvent = makeChangedFirestoreEvent ( event , params ) ;
477487 return wrapTraceContext ( withInit ( handler ) ) ( firestoreEvent ) ;
478488 } ;
479489
480490 func . run = handler ;
481491
482- func . __endpoint = makeEndpoint ( eventType , opts , documentPattern , database , namespace ) ;
492+ func . __endpoint = makeEndpoint ( eventType , opts , document , database , namespace ) ;
483493
484494 return func ;
485495}
0 commit comments