@@ -73,14 +73,40 @@ 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
+ const defaultContext = _makeDefaultContext ( cloudFunction , options ) ;
102
+ const context = merge ( { } , defaultContext , options ) ;
103
+
104
+ // @ts -ignore
105
+ return cloudFunction . run ( context ) ;
106
+ } ;
107
+ return scheduledWrapped ;
108
+ }
109
+
84
110
if (
85
111
has ( cloudFunction , '__trigger.httpsTrigger' ) &&
86
112
get ( cloudFunction , '__trigger.labels.deployment-callable' ) !== 'true'
@@ -115,20 +141,7 @@ export function wrap<T>(cloudFunction: CloudFunction<T>): WrappedFunction {
115
141
[ 'eventId' , 'timestamp' , 'params' , 'auth' , 'authType' ] ,
116
142
options
117
143
) ;
118
- let eventContextOptions = options as EventContextOptions ;
119
- const defaultContext : EventContext = {
120
- eventId : _makeEventId ( ) ,
121
- resource : cloudFunction . __trigger . eventTrigger && {
122
- service : cloudFunction . __trigger . eventTrigger . service ,
123
- name : _makeResourceName (
124
- cloudFunction . __trigger . eventTrigger . resource ,
125
- has ( eventContextOptions , 'params' ) && eventContextOptions . params
126
- ) ,
127
- } ,
128
- eventType : get ( cloudFunction , '__trigger.eventTrigger.eventType' ) ,
129
- timestamp : new Date ( ) . toISOString ( ) ,
130
- params : { } ,
131
- } ;
144
+ const defaultContext = _makeDefaultContext ( cloudFunction , options ) ;
132
145
133
146
if (
134
147
has ( defaultContext , 'eventType' ) &&
@@ -138,7 +151,7 @@ export function wrap<T>(cloudFunction: CloudFunction<T>): WrappedFunction {
138
151
defaultContext . authType = 'UNAUTHENTICATED' ;
139
152
defaultContext . auth = null ;
140
153
}
141
- context = merge ( { } , defaultContext , eventContextOptions ) ;
154
+ context = merge ( { } , defaultContext , options ) ;
142
155
}
143
156
144
157
return cloudFunction . run ( data , context ) ;
@@ -185,6 +198,27 @@ function _checkOptionValidity(
185
198
} ) ;
186
199
}
187
200
201
+ function _makeDefaultContext < T > (
202
+ cloudFunction : CloudFunction < T > ,
203
+ options : ContextOptions
204
+ ) : EventContext {
205
+ let eventContextOptions = options as EventContextOptions ;
206
+ const defaultContext : EventContext = {
207
+ eventId : _makeEventId ( ) ,
208
+ resource : cloudFunction . __trigger . eventTrigger && {
209
+ service : cloudFunction . __trigger . eventTrigger . service ,
210
+ name : _makeResourceName (
211
+ cloudFunction . __trigger . eventTrigger . resource ,
212
+ has ( eventContextOptions , 'params' ) && eventContextOptions . params
213
+ ) ,
214
+ } ,
215
+ eventType : get ( cloudFunction , '__trigger.eventTrigger.eventType' ) ,
216
+ timestamp : new Date ( ) . toISOString ( ) ,
217
+ params : { } ,
218
+ } ;
219
+ return defaultContext ;
220
+ }
221
+
188
222
/** Make a Change object to be used as test data for Firestore and real time database onWrite and onUpdate functions. */
189
223
export function makeChange < T > ( before : T , after : T ) : Change < T > {
190
224
return Change . fromObjects ( before , after ) ;
0 commit comments