Skip to content

Commit

Permalink
feat: updates event and cloudevent interfaces (#276)
Browse files Browse the repository at this point in the history
- Updates the function interfaces for the Node FF
  - Adds a `LegacyEvent` interface
  - Updates `CloudEvent` interface to CE 1.0 (must have been <1.0 with `schemaurl`)
  • Loading branch information
grant committed Apr 21, 2021
1 parent 6b8e664 commit f67d11d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/cloudevents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export function getBinaryCloudEventContext(
const context: CloudEventsContext = {};
for (const name in req.headers) {
if (name.startsWith('ce-')) {
const attributeName = name.substr('ce-'.length);
const attributeName = name.substr(
'ce-'.length
) as keyof CloudEventsContext;
context[attributeName] = req.header(name);
}
}
Expand Down
36 changes: 30 additions & 6 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ export type HandlerFunction =
| CloudEventFunction
| CloudEventFunctionWithCallback;

/**
* A legacy event.
*/
export interface LegacyEvent {
data: object;
context: CloudFunctionsContext;
}

interface Data {
data: object;
}
export type LegacyCloudFunctionsContext = CloudFunctionsContext | Data;

/**
* The Cloud Functions context object for the event.
*
Expand All @@ -62,7 +75,7 @@ export interface CloudFunctionsContext {
/**
* The resource that emitted the event.
*/
resource?: string;
resource?: string | object;
}

/**
Expand Down Expand Up @@ -91,17 +104,28 @@ export interface CloudEventsContext {
* Timestamp of when the event happened.
*/
time?: string;
/**
* Describes the subject of the event in the context of the event producer.
*/
subject?: string;
/**
* A link to the schema that the event data adheres to.
*/
schemaurl?: string;
dataschema?: string;
/**
* Content type of the event data.
*/
contenttype?: string;

// CloudEvents extension attributes.
[key: string]: any;
datacontenttype?: string;
/**
* The event data.
*/
data?:
| Record<string, unknown | string | number | boolean>
| string
| number
| boolean
| null
| unknown;
}

export type Context = CloudFunctionsContext | CloudEventsContext;

0 comments on commit f67d11d

Please sign in to comment.