Skip to content

Commit f6be285

Browse files
authored
fix: extend Node.js IncomingHttpHeaders in our Headers type (cloudevents#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 <lball@redhat.com>
1 parent 1446898 commit f6be285

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ const CONSTANTS = Object.freeze({
4848
DATA_SCHEMA: "dataschema",
4949
DATA_BASE64: "data_base64",
5050
},
51-
});
51+
} as const);
5252

5353
export default CONSTANTS;

src/message/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { IncomingHttpHeaders } from "http";
12
import { CloudEvent } from "..";
23
import { binary, deserialize, structured, isEvent } from "./http";
34
import { headersFor } from "./http/headers";
@@ -18,8 +19,8 @@ export interface Binding {
1819
* Headers is an interface representing transport-agnostic headers as
1920
* key/value string pairs
2021
*/
21-
export interface Headers {
22-
[key: string]: string;
22+
export interface Headers extends IncomingHttpHeaders {
23+
[key: string]: string | string[] | undefined;
2324
}
2425

2526
/**

src/parsers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import CONSTANTS from "./constants";
22
import { isString, isDefinedOrThrow, isStringOrObjectOrThrow, ValidationError } from "./event/validation";
33

44
export abstract class Parser {
5-
abstract parse(payload: Record<string, unknown> | string): unknown;
5+
abstract parse(payload: Record<string, unknown> | string | string[] | undefined): unknown;
66
}
77

88
export class JSONParser implements Parser {

test/integration/emitter_factory_test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function superagentEmitter(message: Message, options?: Options): Promise<unknown
4848
}
4949
// set headers
5050
for (const key of Object.getOwnPropertyNames(message.headers)) {
51-
post.set(key, message.headers[key]);
51+
post.set(key, message.headers[key] as string);
5252
}
5353
return post.send(message.body as string);
5454
}

test/integration/message_test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from "path";
22
import fs from "fs";
33

44
import { expect } from "chai";
5+
import { IncomingHttpHeaders } from "http";
56
import { CloudEvent, CONSTANTS, Version } from "../../src";
67
import { asBase64 } from "../../src/event/validation";
78
import { Message, HTTP } from "../../src/message";
@@ -93,6 +94,25 @@ describe("HTTP transport", () => {
9394
}).to.throw;
9495
});
9596

97+
it("Can be created with Node's IncomingHttpHeaders", () => {
98+
const headers: IncomingHttpHeaders = {
99+
"content-type": CONSTANTS.DEFAULT_CE_CONTENT_TYPE,
100+
};
101+
const body = JSON.stringify({
102+
id,
103+
type,
104+
source,
105+
specversion: Version.V1,
106+
data: { lunch: "tacos" },
107+
});
108+
const message: Message = {
109+
headers,
110+
body,
111+
};
112+
const event = HTTP.toEvent(message);
113+
expect(event.data).to.deep.equal({ lunch: "tacos" });
114+
});
115+
96116
describe("Specification version V1", () => {
97117
const fixture: CloudEvent = new CloudEvent({
98118
specversion: Version.V1,

0 commit comments

Comments
 (0)