Skip to content

Commit afe815c

Browse files
committed
fix: types
1 parent 640d6ce commit afe815c

File tree

3 files changed

+41
-36
lines changed

3 files changed

+41
-36
lines changed

spec/v2.spec.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,8 +1423,8 @@ describe('v2', () => {
14231423
});
14241424

14251425
describe('callable functions', () => {
1426-
it('return an error because they are not supported', () => {
1427-
const cloudFunction = https.onCall((data) => data);
1426+
it('should return correct data', () => {
1427+
const cloudFunction = https.onCall((req) => 'hello');
14281428
cloudFunction.__endpoint = {
14291429
platform: 'gcfv2',
14301430
labels: {},
@@ -1434,14 +1434,9 @@ describe('v2', () => {
14341434
region: ['us-west1', 'us-central1'],
14351435
};
14361436

1437-
try {
1438-
const wrappedCF = wrapV2(cloudFunction as any);
1439-
wrappedCF();
1440-
} catch (e) {
1441-
expect(e.message).to.equal(
1442-
'Wrap function is not available for callableTriggers functions.'
1443-
);
1444-
}
1437+
const wrappedCF = wrapV2(cloudFunction);
1438+
const result = wrappedCF({} as any);
1439+
expect(result).equal('hello');
14451440
});
14461441
});
14471442

src/main.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
// SOFTWARE.
2222

2323
import {
24-
CloudFunction as CloudFunctionV1,
25-
HttpsFunction,
24+
CloudFunction as CloudFunctionV1, HttpsFunction,
2625
Runnable,
2726
} from 'firebase-functions';
2827

@@ -31,15 +30,15 @@ import {
3130
CloudEvent,
3231
} from 'firebase-functions/v2';
3332

34-
import { HttpsFunction as HttpsFunctionV2 } from 'firebase-functions/v2/https';
33+
import {CallableFunction, HttpsFunction as HttpsFunctionV2} from 'firebase-functions/v2/https';
3534

3635
import { wrapV1, WrappedFunction, WrappedScheduledFunction } from './v1';
3736

38-
import { wrapV2, WrappedV2Function, WrappedV2HttpsFunction } from './v2';
37+
import {wrapV2, WrappedV2Function, WrappedV2CallableFunction} from './v2';
3938

40-
type HttpsFunctionOrCloudFunctionV1<T, U> = U extends HttpsFunction &
39+
type HttpsFunctionOrCloudFunctionV1<T, U> = U extends CallableFunction<any, T> &
4140
Runnable<T>
42-
? HttpsFunction & Runnable<T>
41+
? CallableFunction<any, T> & Runnable<T>
4342
: CloudFunctionV1<T>;
4443

4544
// Re-exporting V1 (to reduce breakage)
@@ -59,7 +58,7 @@ export { WrappedV2Function } from './v2';
5958
export function wrap<T>(
6059
cloudFunction: HttpsFunction & Runnable<T>
6160
): WrappedFunction<T, HttpsFunction & Runnable<T>>;
62-
export function wrap(cloudFunction: HttpsFunctionV2): WrappedV2HttpsFunction;
61+
export function wrap<T>(cloudFunction: CallableFunction<any, T>): WrappedV2CallableFunction<T>;
6362
export function wrap<T>(
6463
cloudFunction: CloudFunctionV1<T>
6564
): WrappedScheduledFunction | WrappedFunction<T>;
@@ -72,7 +71,7 @@ export function wrap<T, V extends CloudEvent<unknown>>(
7271
| WrappedScheduledFunction
7372
| WrappedFunction<T>
7473
| WrappedV2Function<V>
75-
| WrappedV2HttpsFunction {
74+
| WrappedV2CallableFunction<T> {
7675
if (isV2CloudFunction<V>(cloudFunction)) {
7776
return wrapV2<V>(cloudFunction as CloudFunctionV2<V>);
7877
}

src/v2.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
import { CloudFunction, CloudEvent } from 'firebase-functions/v2';
23+
import {CloudFunction, CloudEvent} from 'firebase-functions/v2';
2424
import {
25-
HttpsFunction as HttpsV2Function,
26-
Request,
25+
CallableFunction,
26+
CallableRequest
2727
} from 'firebase-functions/v2/https';
2828

29-
import { generateCombinedCloudEvent } from './cloudevent/generate';
30-
import { DeepPartial } from './cloudevent/types';
29+
import {generateCombinedCloudEvent} from './cloudevent/generate';
30+
import {DeepPartial} from './cloudevent/types';
3131
import * as express from 'express';
3232

3333
/** 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>> = (
3737
cloudEventPartial?: DeepPartial<T | object>
3838
) => any | Promise<any>;
3939

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>;
4443

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> {
4847
return !!cf?.__endpoint?.callableTrigger;
4948
}
5049

5150
function assertIsCloudFunction<T extends CloudEvent<unknown>>(
52-
cf: CloudFunction<T> | HttpsV2Function
51+
cf: CloudFunction<T> | CallableFunction<any, any>
5352
): asserts cf is CloudFunction<T> {
5453
if (!('run' in cf) || !cf.run) {
5554
throw new Error(
@@ -63,11 +62,23 @@ function assertIsCloudFunction<T extends CloudEvent<unknown>>(
6362
* which can be called in test code.
6463
*/
6564
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);
7182
};
7283
}
7384

0 commit comments

Comments
 (0)