@@ -36,6 +36,7 @@ import {
36
36
} from "../../common/providers/firestore" ;
37
37
import { wrapTraceContext } from "../trace" ;
38
38
import { withInit } from "../../common/onInit" ;
39
+ import { Expression } from "../../params" ;
39
40
40
41
export { Change } ;
41
42
@@ -106,11 +107,11 @@ export interface FirestoreEvent<T, Params = Record<string, string>> extends Clou
106
107
/** DocumentOptions extend EventHandlerOptions with provided document and optional database and namespace. */
107
108
export interface DocumentOptions < Document extends string = string > extends EventHandlerOptions {
108
109
/** The document path */
109
- document : Document ;
110
+ document : Document | Expression < string > ;
110
111
/** The Firestore database */
111
- database ?: string ;
112
+ database ?: string | Expression < string > ;
112
113
/** The Firestore namespace */
113
- namespace ?: string ;
114
+ namespace ?: string | Expression < string > ;
114
115
}
115
116
116
117
/**
@@ -278,17 +279,20 @@ export function onDocumentDeleted<Document extends string>(
278
279
279
280
/** @internal */
280
281
export 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 > ;
284
285
let opts : EventHandlerOptions ;
285
286
if ( typeof documentOrOpts === "string" ) {
286
287
document = normalizePath ( documentOrOpts ) ;
287
288
database = "(default)" ;
288
289
namespace = "(default)" ;
289
290
opts = { } ;
290
291
} else {
291
- document = normalizePath ( documentOrOpts . document ) ;
292
+ document =
293
+ typeof documentOrOpts . document === "string"
294
+ ? normalizePath ( documentOrOpts . document )
295
+ : documentOrOpts . document ;
292
296
database = documentOrOpts . database || "(default)" ;
293
297
namespace = documentOrOpts . namespace || "(default)" ;
294
298
opts = { ...documentOrOpts } ;
@@ -398,21 +402,25 @@ export function makeChangedFirestoreEvent<Params>(
398
402
export function makeEndpoint (
399
403
eventType : string ,
400
404
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 >
404
408
) : ManifestEndpoint {
405
409
const baseOpts = optionsToEndpoint ( getGlobalOptions ( ) ) ;
406
410
const specificOpts = optionsToEndpoint ( opts ) ;
407
411
408
- const eventFilters : Record < string , string > = {
412
+ const eventFilters : Record < string , string | Expression < string > > = {
409
413
database,
410
414
namespace,
411
415
} ;
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
+ }
416
424
417
425
return {
418
426
...initV2Endpoint ( getGlobalOptions ( ) , opts ) ,
@@ -440,19 +448,20 @@ export function onOperation<Document extends string>(
440
448
) : CloudFunction < FirestoreEvent < QueryDocumentSnapshot , ParamsOf < Document > > > {
441
449
const { document, database, namespace, opts } = getOpts ( documentOrOpts ) ;
442
450
443
- const documentPattern = new PathPattern ( document ) ;
444
-
445
451
// wrap the handler
446
452
const func = ( raw : CloudEvent < unknown > ) => {
447
453
const event = raw as RawFirestoreEvent ;
454
+ const documentPattern = new PathPattern (
455
+ typeof document === "string" ? document : document . value ( )
456
+ ) ;
448
457
const params = makeParams ( event . document , documentPattern ) as unknown as ParamsOf < Document > ;
449
458
const firestoreEvent = makeFirestoreEvent ( eventType , event , params ) ;
450
459
return wrapTraceContext ( withInit ( handler ) ) ( firestoreEvent ) ;
451
460
} ;
452
461
453
462
func . run = handler ;
454
463
455
- func . __endpoint = makeEndpoint ( eventType , opts , documentPattern , database , namespace ) ;
464
+ func . __endpoint = makeEndpoint ( eventType , opts , document , database , namespace ) ;
456
465
457
466
return func ;
458
467
}
@@ -467,19 +476,20 @@ export function onChangedOperation<Document extends string>(
467
476
) : CloudFunction < FirestoreEvent < Change < QueryDocumentSnapshot > , ParamsOf < Document > > > {
468
477
const { document, database, namespace, opts } = getOpts ( documentOrOpts ) ;
469
478
470
- const documentPattern = new PathPattern ( document ) ;
471
-
472
479
// wrap the handler
473
480
const func = ( raw : CloudEvent < unknown > ) => {
474
481
const event = raw as RawFirestoreEvent ;
482
+ const documentPattern = new PathPattern (
483
+ typeof document === "string" ? document : document . value ( )
484
+ ) ;
475
485
const params = makeParams ( event . document , documentPattern ) as unknown as ParamsOf < Document > ;
476
486
const firestoreEvent = makeChangedFirestoreEvent ( event , params ) ;
477
487
return wrapTraceContext ( withInit ( handler ) ) ( firestoreEvent ) ;
478
488
} ;
479
489
480
490
func . run = handler ;
481
491
482
- func . __endpoint = makeEndpoint ( eventType , opts , documentPattern , database , namespace ) ;
492
+ func . __endpoint = makeEndpoint ( eventType , opts , document , database , namespace ) ;
483
493
484
494
return func ;
485
495
}
0 commit comments