Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export { WrappedV2Function } from './v2';
export function wrap<T>(
cloudFunction: HttpsFunction & Runnable<T>
): WrappedFunction<T, HttpsFunction & Runnable<T>>;
export function wrap<T>(
cloudFunction: CallableFunction<any, T>
): WrappedV2CallableFunction<T>;
export function wrap<T, Return>(
cloudFunction: CallableFunction<T, Return>
): WrappedV2CallableFunction<T, Return>;
export function wrap<T>(
cloudFunction: CloudFunctionV1<T>
): WrappedScheduledFunction | WrappedFunction<T>;
Expand All @@ -79,7 +79,7 @@ export function wrap<T, V extends CloudEvent<unknown>>(
| WrappedScheduledFunction
| WrappedFunction<T>
| WrappedV2Function<V>
| WrappedV2CallableFunction<T> {
| WrappedV2CallableFunction<any, any> {
if (isV2CloudFunction<V>(cloudFunction)) {
return wrapV2<V>(cloudFunction as CloudFunctionV2<V>);
}
Expand Down
30 changes: 19 additions & 11 deletions src/v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
// SOFTWARE.

import { CloudFunction, CloudEvent } from 'firebase-functions/v2';
import { CallableFunction, CallableRequest } from 'firebase-functions/v2/https';
import {
CallableFunction,
CallableRequest,
Request,
} from 'firebase-functions/v2/https';

import { generateCombinedCloudEvent } from './cloudevent/generate';
import { DeepPartial } from './cloudevent/types';
import * as express from 'express';

/** A function that can be called with test data and optional override values for {@link CloudEvent}
* It will subsequently invoke the cloud function it wraps with the provided {@link CloudEvent}
Expand All @@ -34,9 +37,14 @@ export type WrappedV2Function<T extends CloudEvent<unknown>> = (
cloudEventPartial?: DeepPartial<T | object>
) => any | Promise<any>;

export type WrappedV2CallableFunction<T> = (
data: CallableRequest
) => T | Promise<T>;
type WrappedV2CallableRequest<T> = {
data: T;
rawRequest?: Partial<Request>;
} & Partial<Omit<CallableRequest<T>, 'data' | 'rawRequest'>>;
Comment thread
IzaakGough marked this conversation as resolved.
Outdated

export type WrappedV2CallableFunction<T, Return> = (
data: WrappedV2CallableRequest<T>
) => Return;
Comment thread
IzaakGough marked this conversation as resolved.

function isCallableV2Function<T extends CloudEvent<unknown>>(
cf: CloudFunction<T> | CallableFunction<any, any>
Expand Down Expand Up @@ -66,16 +74,16 @@ export function wrapV2<T extends CloudEvent<unknown>>(
* Takes a v2 HTTP function to be tested, and returns a {@link WrappedV2HttpsFunction}
* which can be called in test code.
*/
export function wrapV2(
cloudFunction: CallableFunction<any, any>
): WrappedV2CallableFunction<any>;
export function wrapV2<T, Return>(
cloudFunction: CallableFunction<T, Return>
): WrappedV2CallableFunction<T, Return>;

export function wrapV2<T extends CloudEvent<unknown>>(
cloudFunction: CloudFunction<T> | CallableFunction<any, any>
): WrappedV2Function<T> | WrappedV2CallableFunction<any> {
): WrappedV2Function<T> | WrappedV2CallableFunction<any, any> {
if (isCallableV2Function(cloudFunction)) {
return (req: CallableRequest) => {
return cloudFunction.run(req);
return (req: WrappedV2CallableRequest<any>) => {
return cloudFunction.run(req as CallableRequest<any>);
};
Comment thread
IzaakGough marked this conversation as resolved.
Outdated
}

Expand Down
Loading