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