@@ -30,6 +30,8 @@ import {
30
30
config ,
31
31
database ,
32
32
firestore ,
33
+ HttpsFunction ,
34
+ Runnable ,
33
35
} from 'firebase-functions' ;
34
36
35
37
/** Fields of the event context that can be overridden/customized. */
@@ -88,14 +90,16 @@ export type CallableContextOptions = {
88
90
} ;
89
91
90
92
/* Fields for both Event and Callable contexts, checked at runtime */
91
- export type ContextOptions = EventContextOptions | CallableContextOptions ;
93
+ export type ContextOptions < T = void > = T extends HttpsFunction & Runnable < T >
94
+ ? CallableContextOptions
95
+ : EventContextOptions ;
92
96
93
97
/** A function that can be called with test data and optional override values for the event context.
94
98
* It will subsequently invoke the cloud function it wraps with the provided test data and a generated event context.
95
99
*/
96
- export type WrappedFunction < T > = (
100
+ export type WrappedFunction < T , U = void > = (
97
101
data : T ,
98
- options ?: ContextOptions
102
+ options ?: ContextOptions < U >
99
103
) => any | Promise < any > ;
100
104
101
105
/** A scheduled function that can be called with optional override values for the event context.
@@ -106,9 +110,12 @@ export type WrappedScheduledFunction = (
106
110
) => any | Promise < any > ;
107
111
108
112
/** Takes a cloud function to be tested, and returns a WrappedFunction which can be called in test code. */
113
+ export function wrapV1 < T > (
114
+ cloudFunction : HttpsFunction & Runnable < T >
115
+ ) : WrappedFunction < T , HttpsFunction & Runnable < T > > ;
109
116
export function wrapV1 < T > (
110
117
cloudFunction : CloudFunction < T >
111
- ) : WrappedScheduledFunction | WrappedFunction < T > {
118
+ ) : WrappedScheduledFunction | WrappedFunction < T , CloudFunction < T > > {
112
119
if ( ! has ( cloudFunction , '__trigger' ) ) {
113
120
throw new Error (
114
121
'Wrap can only be called on functions written with the firebase-functions SDK.'
@@ -150,26 +157,26 @@ export function wrapV1<T>(
150
157
const isCallableFunction =
151
158
get ( cloudFunction , '__trigger.labels.deployment-callable' ) === 'true' ;
152
159
153
- let wrapped : WrappedFunction < T > = ( data : T , options : ContextOptions ) => {
160
+ let wrapped : WrappedFunction < T , typeof cloudFunction > = ( data , options ) => {
154
161
// Although in Typescript we require `options` some of our JS samples do not pass it.
155
- options = options || { } ;
162
+ const _options = { ... options } ;
156
163
let context ;
157
164
158
165
if ( isCallableFunction ) {
159
166
_checkOptionValidity (
160
167
[ 'app' , 'auth' , 'instanceIdToken' , 'rawRequest' ] ,
161
- options
168
+ _options
162
169
) ;
163
- let callableContextOptions = options as CallableContextOptions ;
170
+ let callableContextOptions = _options as CallableContextOptions ;
164
171
context = {
165
172
...callableContextOptions ,
166
173
} ;
167
174
} else {
168
175
_checkOptionValidity (
169
176
[ 'eventId' , 'timestamp' , 'params' , 'auth' , 'authType' , 'resource' ] ,
170
- options
177
+ _options
171
178
) ;
172
- const defaultContext = _makeDefaultContext ( cloudFunction , options , data ) ;
179
+ const defaultContext = _makeDefaultContext ( cloudFunction , _options , data ) ;
173
180
174
181
if (
175
182
has ( defaultContext , 'eventType' ) &&
@@ -179,7 +186,7 @@ export function wrapV1<T>(
179
186
defaultContext . authType = 'UNAUTHENTICATED' ;
180
187
defaultContext . auth = null ;
181
188
}
182
- context = merge ( { } , defaultContext , options ) ;
189
+ context = merge ( { } , defaultContext , _options ) ;
183
190
}
184
191
185
192
return cloudFunction . run ( data , context ) ;
@@ -226,9 +233,14 @@ function _checkOptionValidity(
226
233
} ) ;
227
234
}
228
235
236
+ function _makeDefaultContext < T > (
237
+ cloudFunction : HttpsFunction & Runnable < T > ,
238
+ options : CallableContextOptions ,
239
+ triggerData ?: T
240
+ ) ;
229
241
function _makeDefaultContext < T > (
230
242
cloudFunction : CloudFunction < T > ,
231
- options : ContextOptions ,
243
+ options : EventContextOptions ,
232
244
triggerData ?: T
233
245
) : EventContext {
234
246
let eventContextOptions = options as EventContextOptions ;
0 commit comments