Skip to content

Commit

Permalink
fix(types): use CloudEvent<unknown> for typed events (#137)
Browse files Browse the repository at this point in the history
* fix(types): use parameterized types for CloudEvent functions

Fixes type incompatibility with typed CloudEvent functions.

Also, sometimes `instanceof` is not enough. Also check the constructor name to ensure we're getting a CloudEvent back. (This appears to happen primarily when a function returns a `Promise<CloudEvent>` instead of a plain `CloudEvent`.

/kind fix

Signed-off-by: Lance Ball <lball@redhat.com>
  • Loading branch information
lance committed Nov 9, 2022
1 parent 54ee908 commit 03b1ffe
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/invoker.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ module.exports = function invoker(func) {

// If the response is a CloudEvent, we need to convert it
// to a Message first and respond with the headers/body
if (fnReturn instanceof CloudEvent) {
if (fnReturn instanceof CloudEvent || fnReturn.constructor?.name === 'CloudEvent') {
try {
const message = HTTP.binary(fnReturn);
payload.headers = {...payload.headers, ...message.headers};
Expand Down
8 changes: 4 additions & 4 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { CloudEvent } from 'cloudevents';
/**
* CloudEventFunction describes the function signature for a function that accepts CloudEvents.
*/
export interface CloudEventFunction {
(context: Context, event: CloudEvent): CloudEventFunctionReturn;
export interface CloudEventFunction<I=never, R=unknown> {
(context: Context, event?: CloudEvent<I>): CloudEventFunctionReturn<R>;
}

// CloudEventFunctionReturn is the return type for a CloudEventFunction.
export type CloudEventFunctionReturn = Promise<CloudEvent> | CloudEvent | HTTPFunctionReturn;
export type CloudEventFunctionReturn<T=unknown> = Promise<CloudEvent<T>> | CloudEvent<T> | HTTPFunctionReturn;

/**
* HTTPFunction describes the function signature for a function that handles
Expand Down Expand Up @@ -50,7 +50,7 @@ export interface Context {
httpVersion: string;
httpVersionMajor: number;
httpVersionMinor: number;
cloudevent: CloudEvent;
cloudevent: CloudEvent<unknown>;
cloudEventResponse(data: string|object): CloudEventResponse;
}

Expand Down
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/types/context.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ expectType<string>(context.method);
expectType<string>(context.httpVersion);
expectType<number>(context.httpVersionMajor);
expectType<number>(context.httpVersionMinor);
expectType<CloudEvent>(context.cloudevent);
expectType<CloudEvent<unknown>>(context.cloudevent);
expectAssignable<Record<string, any>|string|undefined>(context.body);

// CloudEventResponse
Expand Down
4 changes: 2 additions & 2 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { CloudEvent } from 'cloudevents';

const fn: Invokable = function(
context: Context,
cloudevent?: CloudEvent
cloudevent?: CloudEvent<unknown>
) {
expectType<Context>(context);
expectType<CloudEvent|undefined>(cloudevent);
expectType<CloudEvent<unknown>|undefined>(cloudevent);
return undefined;
};

Expand Down

0 comments on commit 03b1ffe

Please sign in to comment.