@@ -73,14 +73,53 @@ export type WrappedFunction = (
73
73
options ?: ContextOptions
74
74
) => any | Promise < any > ;
75
75
76
+ /** A scheduled function that can be called with optional override values for the event context.
77
+ * It will subsequently invoke the cloud function it wraps with a generated event context.
78
+ */
79
+ export type WrappedScheduledFunction = (
80
+ options ?: ContextOptions
81
+ ) => any | Promise < any > ;
82
+
76
83
/** Takes a cloud function to be tested, and returns a WrappedFunction which can be called in test code. */
77
- export function wrap < T > ( cloudFunction : CloudFunction < T > ) : WrappedFunction {
84
+ export function wrap < T > (
85
+ cloudFunction : CloudFunction < T >
86
+ ) : WrappedScheduledFunction | WrappedFunction {
78
87
if ( ! has ( cloudFunction , '__trigger' ) ) {
79
88
throw new Error (
80
89
'Wrap can only be called on functions written with the firebase-functions SDK.'
81
90
) ;
82
91
}
83
92
93
+ if ( get ( cloudFunction , '__trigger.labels.deployment-scheduled' ) === 'true' ) {
94
+ const scheduledWrapped : WrappedScheduledFunction = (
95
+ options : ContextOptions
96
+ ) => {
97
+ // Although in Typescript we require `options` some of our JS samples do not pass it.
98
+ options = options || { } ;
99
+
100
+ _checkOptionValidity ( [ 'eventId' , 'timestamp' ] , options ) ;
101
+ let eventContextOptions = options as EventContextOptions ;
102
+ const defaultContext : EventContext = {
103
+ eventId : _makeEventId ( ) ,
104
+ resource : cloudFunction . __trigger . eventTrigger && {
105
+ service : cloudFunction . __trigger . eventTrigger . service ,
106
+ name : _makeResourceName (
107
+ cloudFunction . __trigger . eventTrigger . resource ,
108
+ has ( eventContextOptions , 'params' ) && eventContextOptions . params
109
+ ) ,
110
+ } ,
111
+ eventType : get ( cloudFunction , '__trigger.eventTrigger.eventType' ) ,
112
+ timestamp : new Date ( ) . toISOString ( ) ,
113
+ params : { } ,
114
+ } ;
115
+ const context = merge ( { } , defaultContext , eventContextOptions ) ;
116
+
117
+ // @ts -ignore
118
+ return cloudFunction . run ( context ) ;
119
+ } ;
120
+ return scheduledWrapped ;
121
+ }
122
+
84
123
if (
85
124
has ( cloudFunction , '__trigger.httpsTrigger' ) &&
86
125
get ( cloudFunction , '__trigger.labels.deployment-callable' ) !== 'true'
0 commit comments