From f6be285b8319919029d8c11f7bb49cac4bcc5c14 Mon Sep 17 00:00:00 2001 From: Lance Ball Date: Tue, 6 Oct 2020 15:50:33 -0400 Subject: [PATCH] fix: extend Node.js IncomingHttpHeaders in our Headers type (#346) This commit extends Node.js IncomingHttpHeaders in our Headers type. Changes the Headers type to make it more compatible with Node.js TypeScript projects. Signed-off-by: Lance Ball --- src/constants.ts | 2 +- src/message/index.ts | 5 +++-- src/parsers.ts | 2 +- test/integration/emitter_factory_test.ts | 2 +- test/integration/message_test.ts | 20 ++++++++++++++++++++ 5 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/constants.ts b/src/constants.ts index 449fe06..30d1dbe 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/message/index.ts b/src/message/index.ts index 6a1343c..d04486d 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 bbfe128..37890e0 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 5e2a103..7d9baa1 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,