diff --git a/src/function_registry.ts b/src/function_registry.ts index fe0d99b3..fc5ddea9 100644 --- a/src/function_registry.ts +++ b/src/function_registry.ts @@ -18,6 +18,7 @@ import { HandlerFunction, TypedFunction, JsonInvocationFormat, + CloudEventFunctionWithCallback, } from './functions'; import {SignatureType} from './types'; @@ -96,7 +97,7 @@ export const http = (functionName: string, handler: HttpFunction): void => { */ export const cloudEvent = ( functionName: string, - handler: CloudEventFunction + handler: CloudEventFunction | CloudEventFunctionWithCallback ): void => { register(functionName, 'cloudevent', handler); }; diff --git a/test/integration/cloud_event.ts b/test/integration/cloud_event.ts index ac239f14..9bc9fc9f 100644 --- a/test/integration/cloud_event.ts +++ b/test/integration/cloud_event.ts @@ -41,9 +41,12 @@ describe('CloudEvent Function', () => { let receivedCloudEvent: functions.CloudEvent | null; before(() => { - functions.cloudEvent('testCloudEventFunction', ce => { - receivedCloudEvent = ce; - }); + functions.cloudEvent( + 'testCloudEventFunction', + (ce: functions.CloudEvent) => { + receivedCloudEvent = ce; + } + ); }); beforeEach(() => { @@ -288,11 +291,39 @@ describe('CloudEvent Function', () => { const testPayload = 'a test string'; // register a strongly typed CloudEvent function - functions.cloudEvent('testTypedCloudEvent', ce => { - assert.deepStrictEqual(ce.data, testPayload); - // use a property that proves this is actually typed as a string - assert.deepStrictEqual(ce.data.length, testPayload.length); - }); + functions.cloudEvent( + 'testTypedCloudEvent', + (ce: functions.CloudEvent) => { + assert.deepStrictEqual(ce.data, testPayload); + // use a property that proves this is actually typed as a string + assert.deepStrictEqual(ce.data.length, testPayload.length); + } + ); + + // invoke the function with a CloudEvent with a string payload + const server = getTestServer('testTypedCloudEvent'); + await supertest(server) + .post('/') + .send({ + ...TEST_CLOUD_EVENT, + data: testPayload, + }) + .expect(204); + }); + + it('allows customers to use a handler with callbacks for failure', async () => { + const testPayload = 'a test string'; + + // register a strongly typed CloudEvent function + functions.cloudEvent( + 'testTypedCloudEvent', + (ce: functions.CloudEvent, callback) => { + assert.deepStrictEqual(ce.data, testPayload); + // use a property that proves this is actually typed as a string + assert.deepStrictEqual(ce.data.length, testPayload.length); + callback(); + } + ); // invoke the function with a CloudEvent with a string payload const server = getTestServer('testTypedCloudEvent'); @@ -305,6 +336,7 @@ describe('CloudEvent Function', () => { .expect(204); }); + it('returns a 500 if the function throws an exception', async () => { functions.cloudEvent('testTypedCloudEvent', () => { throw 'I crashed';