@@ -35,6 +35,12 @@ import {
35
35
// @ts -ignore
36
36
resetCache ,
37
37
} from 'firebase-functions' ;
38
+ import { Expression } from 'firebase-functions/params' ;
39
+ import {
40
+ getEventFilters ,
41
+ getEventType ,
42
+ resolveStringExpression ,
43
+ } from './cloudevent/mocks/helpers' ;
38
44
39
45
/** Fields of the event context that can be overridden/customized. */
40
46
export type EventContextOptions = {
@@ -118,13 +124,13 @@ export function wrapV1<T>(
118
124
export function wrapV1 < T > (
119
125
cloudFunction : CloudFunction < T >
120
126
) : WrappedScheduledFunction | WrappedFunction < T , CloudFunction < T > > {
121
- if ( ! has ( cloudFunction , '__trigger ' ) ) {
127
+ if ( ! has ( cloudFunction , '__endpoint ' ) ) {
122
128
throw new Error (
123
129
'Wrap can only be called on functions written with the firebase-functions SDK.'
124
130
) ;
125
131
}
126
132
127
- if ( get ( cloudFunction , '__trigger.labels.deployment-scheduled' ) === 'true' ) {
133
+ if ( has ( cloudFunction , '__endpoint.scheduleTrigger' ) ) {
128
134
const scheduledWrapped : WrappedScheduledFunction = (
129
135
options : ContextOptions
130
136
) => {
@@ -141,10 +147,7 @@ export function wrapV1<T>(
141
147
return scheduledWrapped ;
142
148
}
143
149
144
- if (
145
- has ( cloudFunction , '__trigger.httpsTrigger' ) &&
146
- get ( cloudFunction , '__trigger.labels.deployment-callable' ) !== 'true'
147
- ) {
150
+ if ( has ( cloudFunction , '__endpoint.httpsTrigger' ) ) {
148
151
throw new Error (
149
152
'Wrap function is only available for `onCall` HTTP functions, not `onRequest`.'
150
153
) ;
@@ -156,8 +159,7 @@ export function wrapV1<T>(
156
159
) ;
157
160
}
158
161
159
- const isCallableFunction =
160
- get ( cloudFunction , '__trigger.labels.deployment-callable' ) === 'true' ;
162
+ const isCallableFunction = has ( cloudFunction , '__endpoint.callableTrigger' ) ;
161
163
162
164
let wrapped : WrappedFunction < T , typeof cloudFunction > = ( data , options ) => {
163
165
// Although in Typescript we require `options` some of our JS samples do not pass it.
@@ -199,11 +201,12 @@ export function wrapV1<T>(
199
201
200
202
/** @internal */
201
203
export function _makeResourceName (
202
- triggerResource : string ,
204
+ triggerResource : string | Expression < string > ,
203
205
params = { }
204
206
) : string {
207
+ const resource = resolveStringExpression ( triggerResource ) ;
205
208
const wildcardRegex = new RegExp ( '{[^/{}]*}' , 'g' ) ;
206
- let resourceName = triggerResource . replace ( wildcardRegex , ( wildcard ) => {
209
+ let resourceName = resource . replace ( wildcardRegex , ( wildcard ) => {
207
210
let wildcardNoBraces = wildcard . slice ( 1 , - 1 ) ; // .slice removes '{' and '}' from wildcard
208
211
let sub = get ( params , wildcardNoBraces ) ;
209
212
return sub || wildcardNoBraces + random ( 1 , 9 ) ;
@@ -246,8 +249,8 @@ function _makeDefaultContext<T>(
246
249
triggerData ?: T
247
250
) : EventContext {
248
251
let eventContextOptions = options as EventContextOptions ;
249
- const eventResource = cloudFunction . __trigger . eventTrigger ?. resource ;
250
- const eventType = cloudFunction . __trigger . eventTrigger ?. eventType ;
252
+ const eventType = getEventType ( cloudFunction ) ;
253
+ const eventResource = getEventFilters ( cloudFunction ) . resource ;
251
254
252
255
const optionsParams = eventContextOptions . params ?? { } ;
253
256
let triggerParams = { } ;
@@ -283,7 +286,7 @@ function _makeDefaultContext<T>(
283
286
const defaultContext : EventContext = {
284
287
eventId : _makeEventId ( ) ,
285
288
resource : eventResource && {
286
- service : cloudFunction . __trigger . eventTrigger ?. service ,
289
+ service : serviceFromEventType ( eventType ) ,
287
290
name : _makeResourceName ( eventResource , params ) ,
288
291
} ,
289
292
eventType,
@@ -294,20 +297,22 @@ function _makeDefaultContext<T>(
294
297
}
295
298
296
299
function _extractDatabaseParams (
297
- triggerResource : string ,
300
+ triggerResource : string | Expression < string > ,
298
301
data : database . DataSnapshot
299
302
) : EventContext [ 'params' ] {
303
+ const resource = resolveStringExpression ( triggerResource ) ;
300
304
const path = data . ref . toString ( ) . replace ( data . ref . root . toString ( ) , '' ) ;
301
- return _extractParams ( triggerResource , path ) ;
305
+ return _extractParams ( resource , path ) ;
302
306
}
303
307
304
308
function _extractFirestoreDocumentParams (
305
- triggerResource : string ,
309
+ triggerResource : string | Expression < string > ,
306
310
data : firestore . DocumentSnapshot
307
311
) : EventContext [ 'params' ] {
312
+ const resource = resolveStringExpression ( triggerResource ) ;
308
313
// Resource format: databases/(default)/documents/<path>
309
314
return _extractParams (
310
- triggerResource . replace ( / ^ d a t a b a s e s \/ [ ^ \/ ] + \/ d o c u m e n t s \/ / , '' ) ,
315
+ resource . replace ( / ^ d a t a b a s e s \/ [ ^ \/ ] + \/ d o c u m e n t s \/ / , '' ) ,
311
316
data . ref . path
312
317
) ;
313
318
}
@@ -340,6 +345,29 @@ export function _extractParams(
340
345
return params ;
341
346
}
342
347
348
+ function serviceFromEventType ( eventType ?: string ) : string {
349
+ if ( eventType ) {
350
+ const providerToService : Array < [ string , string ] > = [
351
+ [ 'google.analytics' , 'app-measurement.com' ] ,
352
+ [ 'google.firebase.auth' , 'firebaseauth.googleapis.com' ] ,
353
+ [ 'google.firebase.database' , 'firebaseio.com' ] ,
354
+ [ 'google.firestore' , 'firestore.googleapis.com' ] ,
355
+ [ 'google.pubsub' , 'pubsub.googleapis.com' ] ,
356
+ [ 'google.firebase.remoteconfig' , 'firebaseremoteconfig.googleapis.com' ] ,
357
+ [ 'google.storage' , 'storage.googleapis.com' ] ,
358
+ [ 'google.testing' , 'testing.googleapis.com' ] ,
359
+ ] ;
360
+
361
+ const match = providerToService . find ( ( [ provider ] ) => {
362
+ eventType . includes ( provider ) ;
363
+ } ) ;
364
+ if ( match ) {
365
+ return match [ 1 ] ;
366
+ }
367
+ }
368
+ return 'unknown-service.googleapis.com' ;
369
+ }
370
+
343
371
/** Make a Change object to be used as test data for Firestore and real time database onWrite and onUpdate functions. */
344
372
export function makeChange < T > ( before : T , after : T ) : Change < T > {
345
373
return Change . fromObjects ( before , after ) ;
0 commit comments