20
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
// SOFTWARE.
22
22
23
- import { CloudFunction , CloudEvent } from 'firebase-functions/v2' ;
23
+ import { CloudFunction , CloudEvent } from 'firebase-functions/v2' ;
24
24
import {
25
- HttpsFunction as HttpsV2Function ,
26
- Request ,
25
+ CallableFunction ,
26
+ CallableRequest
27
27
} from 'firebase-functions/v2/https' ;
28
28
29
- import { generateCombinedCloudEvent } from './cloudevent/generate' ;
30
- import { DeepPartial } from './cloudevent/types' ;
29
+ import { generateCombinedCloudEvent } from './cloudevent/generate' ;
30
+ import { DeepPartial } from './cloudevent/types' ;
31
31
import * as express from 'express' ;
32
32
33
33
/** A function that can be called with test data and optional override values for {@link CloudEvent}
@@ -37,19 +37,18 @@ export type WrappedV2Function<T extends CloudEvent<unknown>> = (
37
37
cloudEventPartial ?: DeepPartial < T | object >
38
38
) => any | Promise < any > ;
39
39
40
- export type WrappedV2HttpsFunction = (
41
- req : express . Request ,
42
- res : express . Response
43
- ) => any | Promise < any > ;
40
+ export type WrappedV2CallableFunction < T > = (
41
+ data : CallableRequest ,
42
+ ) => T | Promise < T > ;
44
43
45
- function isHttpsV2Function < T extends CloudEvent < unknown > > (
46
- cf : CloudFunction < T > | HttpsV2Function
47
- ) : cf is HttpsV2Function {
44
+ function isCallableV2Function < T extends CloudEvent < unknown > > (
45
+ cf : CloudFunction < T > | CallableFunction < any , any >
46
+ ) : cf is CallableFunction < any , any > {
48
47
return ! ! cf ?. __endpoint ?. callableTrigger ;
49
48
}
50
49
51
50
function assertIsCloudFunction < T extends CloudEvent < unknown > > (
52
- cf : CloudFunction < T > | HttpsV2Function
51
+ cf : CloudFunction < T > | CallableFunction < any , any >
53
52
) : asserts cf is CloudFunction < T > {
54
53
if ( ! ( 'run' in cf ) || ! cf . run ) {
55
54
throw new Error (
@@ -63,11 +62,23 @@ function assertIsCloudFunction<T extends CloudEvent<unknown>>(
63
62
* which can be called in test code.
64
63
*/
65
64
export function wrapV2 < T extends CloudEvent < unknown > > (
66
- cloudFunction : CloudFunction < T > | HttpsV2Function
67
- ) : WrappedV2Function < T > | WrappedV2HttpsFunction {
68
- if ( isHttpsV2Function ( cloudFunction ) ) {
69
- return ( req : Request , res : express . Response ) => {
70
- return cloudFunction ( req , res ) ;
65
+ cloudFunction : CloudFunction < T >
66
+ ) : WrappedV2Function < T > ;
67
+
68
+ /**
69
+ * Takes a v2 HTTP function to be tested, and returns a {@link WrappedV2HttpsFunction}
70
+ * which can be called in test code.
71
+ */
72
+ export function wrapV2 (
73
+ cloudFunction : CallableFunction < any , any >
74
+ ) : WrappedV2CallableFunction < any > ;
75
+
76
+ export function wrapV2 < T extends CloudEvent < unknown > > (
77
+ cloudFunction : CloudFunction < T > | CallableFunction < any , any >
78
+ ) : WrappedV2Function < T > | WrappedV2CallableFunction < any > {
79
+ if ( isCallableV2Function ( cloudFunction ) ) {
80
+ return ( req : CallableRequest ) => {
81
+ return cloudFunction . run ( req ) ;
71
82
} ;
72
83
}
73
84
0 commit comments