Skip to content

Commit 47cd4c6

Browse files
authored
fix: Fix typings for functions.cloudEvent to include callback. (#631)
1 parent 16709c6 commit 47cd4c6

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/function_registry.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
HandlerFunction,
1919
TypedFunction,
2020
JsonInvocationFormat,
21+
CloudEventFunctionWithCallback,
2122
} from './functions';
2223
import {SignatureType} from './types';
2324

@@ -96,7 +97,7 @@ export const http = (functionName: string, handler: HttpFunction): void => {
9697
*/
9798
export const cloudEvent = <T = unknown>(
9899
functionName: string,
99-
handler: CloudEventFunction<T>
100+
handler: CloudEventFunction<T> | CloudEventFunctionWithCallback<T>
100101
): void => {
101102
register(functionName, 'cloudevent', handler);
102103
};

test/integration/cloud_event.ts

+40-8
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ describe('CloudEvent Function', () => {
4141

4242
let receivedCloudEvent: functions.CloudEvent<unknown> | null;
4343
before(() => {
44-
functions.cloudEvent('testCloudEventFunction', ce => {
45-
receivedCloudEvent = ce;
46-
});
44+
functions.cloudEvent(
45+
'testCloudEventFunction',
46+
(ce: functions.CloudEvent<unknown>) => {
47+
receivedCloudEvent = ce;
48+
}
49+
);
4750
});
4851

4952
beforeEach(() => {
@@ -288,11 +291,39 @@ describe('CloudEvent Function', () => {
288291
const testPayload = 'a test string';
289292

290293
// register a strongly typed CloudEvent function
291-
functions.cloudEvent<string>('testTypedCloudEvent', ce => {
292-
assert.deepStrictEqual(ce.data, testPayload);
293-
// use a property that proves this is actually typed as a string
294-
assert.deepStrictEqual(ce.data.length, testPayload.length);
295-
});
294+
functions.cloudEvent<string>(
295+
'testTypedCloudEvent',
296+
(ce: functions.CloudEvent<string>) => {
297+
assert.deepStrictEqual(ce.data, testPayload);
298+
// use a property that proves this is actually typed as a string
299+
assert.deepStrictEqual(ce.data.length, testPayload.length);
300+
}
301+
);
302+
303+
// invoke the function with a CloudEvent with a string payload
304+
const server = getTestServer('testTypedCloudEvent');
305+
await supertest(server)
306+
.post('/')
307+
.send({
308+
...TEST_CLOUD_EVENT,
309+
data: testPayload,
310+
})
311+
.expect(204);
312+
});
313+
314+
it('allows customers to use a handler with callbacks for failure', async () => {
315+
const testPayload = 'a test string';
316+
317+
// register a strongly typed CloudEvent function
318+
functions.cloudEvent<string>(
319+
'testTypedCloudEvent',
320+
(ce: functions.CloudEvent<string>, callback) => {
321+
assert.deepStrictEqual(ce.data, testPayload);
322+
// use a property that proves this is actually typed as a string
323+
assert.deepStrictEqual(ce.data.length, testPayload.length);
324+
callback();
325+
}
326+
);
296327

297328
// invoke the function with a CloudEvent with a string payload
298329
const server = getTestServer('testTypedCloudEvent');
@@ -305,6 +336,7 @@ describe('CloudEvent Function', () => {
305336
.expect(204);
306337
});
307338

339+
308340
it('returns a 500 if the function throws an exception', async () => {
309341
functions.cloudEvent('testTypedCloudEvent', () => {
310342
throw 'I crashed';

0 commit comments

Comments
 (0)