diff --git a/src/constants.ts b/src/constants.ts index 449fe06a..30d1dbe5 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -48,6 +48,6 @@ const CONSTANTS = Object.freeze({ DATA_SCHEMA: "dataschema", DATA_BASE64: "data_base64", }, -}); +} as const); export default CONSTANTS; diff --git a/src/event/cloudevent.ts b/src/event/cloudevent.ts index c9ade6f1..a8231cc1 100644 --- a/src/event/cloudevent.ts +++ b/src/event/cloudevent.ts @@ -175,7 +175,6 @@ export class CloudEvent implements CloudEventV1, CloudEventV03 { try { return validateCloudEvent(this); } catch (e) { - console.error(e.errors); if (e instanceof ValidationError) { throw e; } else { diff --git a/src/message/index.ts b/src/message/index.ts index 14ed270c..5b4ac9a6 100644 --- a/src/message/index.ts +++ b/src/message/index.ts @@ -1,3 +1,4 @@ +import { IncomingHttpHeaders } from "http"; import { CloudEvent } from ".."; import { binary, deserialize, structured, isEvent } from "./http"; import { headersFor } from "./http/headers"; @@ -18,8 +19,8 @@ export interface Binding { * Headers is an interface representing transport-agnostic headers as * key/value string pairs */ -export interface Headers { - [key: string]: string; +export interface Headers extends IncomingHttpHeaders { + [key: string]: string | string[] | undefined; } /** diff --git a/src/parsers.ts b/src/parsers.ts index bbfe128c..37890e00 100644 --- a/src/parsers.ts +++ b/src/parsers.ts @@ -2,7 +2,7 @@ import CONSTANTS from "./constants"; import { isString, isDefinedOrThrow, isStringOrObjectOrThrow, ValidationError } from "./event/validation"; export abstract class Parser { - abstract parse(payload: Record | string): unknown; + abstract parse(payload: Record | string | string[] | undefined): unknown; } export class JSONParser implements Parser { diff --git a/test/integration/emitter_factory_test.ts b/test/integration/emitter_factory_test.ts index 4f6cba35..43bb9cfe 100644 --- a/test/integration/emitter_factory_test.ts +++ b/test/integration/emitter_factory_test.ts @@ -48,7 +48,7 @@ function superagentEmitter(message: Message, options?: Options): Promise { }).to.throw; }); + it("Can be created with Node's IncomingHttpHeaders", () => { + const headers: IncomingHttpHeaders = { + "content-type": CONSTANTS.DEFAULT_CE_CONTENT_TYPE, + }; + const body = JSON.stringify({ + id, + type, + source, + specversion: Version.V1, + data: { lunch: "tacos" }, + }); + const message: Message = { + headers, + body, + }; + const event = HTTP.toEvent(message); + expect(event.data).to.deep.equal({ lunch: "tacos" }); + }); + describe("Specification version V1", () => { const fixture: CloudEvent = new CloudEvent({ specversion: Version.V1,