Skip to content

Commit

Permalink
fix: Fix typings for functions.cloudEvent to include callback. (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
thekumar authored Dec 12, 2024
1 parent 16709c6 commit 47cd4c6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/function_registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
HandlerFunction,
TypedFunction,
JsonInvocationFormat,
CloudEventFunctionWithCallback,
} from './functions';
import {SignatureType} from './types';

Expand Down Expand Up @@ -96,7 +97,7 @@ export const http = (functionName: string, handler: HttpFunction): void => {
*/
export const cloudEvent = <T = unknown>(
functionName: string,
handler: CloudEventFunction<T>
handler: CloudEventFunction<T> | CloudEventFunctionWithCallback<T>
): void => {
register(functionName, 'cloudevent', handler);
};
Expand Down
48 changes: 40 additions & 8 deletions test/integration/cloud_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ describe('CloudEvent Function', () => {

let receivedCloudEvent: functions.CloudEvent<unknown> | null;
before(() => {
functions.cloudEvent('testCloudEventFunction', ce => {
receivedCloudEvent = ce;
});
functions.cloudEvent(
'testCloudEventFunction',
(ce: functions.CloudEvent<unknown>) => {
receivedCloudEvent = ce;
}
);
});

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

// register a strongly typed CloudEvent function
functions.cloudEvent<string>('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<string>(
'testTypedCloudEvent',
(ce: functions.CloudEvent<string>) => {
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<string>(
'testTypedCloudEvent',
(ce: functions.CloudEvent<string>, 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');
Expand All @@ -305,6 +336,7 @@ describe('CloudEvent Function', () => {
.expect(204);
});


Check failure on line 339 in test/integration/cloud_event.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
it('returns a 500 if the function throws an exception', async () => {
functions.cloudEvent('testTypedCloudEvent', () => {
throw 'I crashed';
Expand Down

0 comments on commit 47cd4c6

Please sign in to comment.