Skip to content

fix: extend Node.js IncomingHttpHeaders in our Headers type #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ const CONSTANTS = Object.freeze({
DATA_SCHEMA: "dataschema",
DATA_BASE64: "data_base64",
},
});
} as const);

export default CONSTANTS;
1 change: 0 additions & 1 deletion src/event/cloudevent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions src/message/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IncomingHttpHeaders } from "http";
import { CloudEvent } from "..";
import { binary, deserialize, structured, isEvent } from "./http";
import { headersFor } from "./http/headers";
Expand All @@ -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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> | string): unknown;
abstract parse(payload: Record<string, unknown> | string | string[] | undefined): unknown;
}

export class JSONParser implements Parser {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/emitter_factory_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function superagentEmitter(message: Message, options?: Options): Promise<unknown
}
// set headers
for (const key of Object.getOwnPropertyNames(message.headers)) {
post.set(key, message.headers[key]);
post.set(key, message.headers[key] as string);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess we also have to submit an issue to superagent 😂

}
return post.send(message.body);
}
Expand Down
20 changes: 20 additions & 0 deletions test/integration/message_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from "chai";
import { IncomingHttpHeaders } from "http";
import { CloudEvent, CONSTANTS, Version } from "../../src";
import { asBase64 } from "../../src/event/validation";
import { Message, HTTP } from "../../src/message";
Expand Down Expand Up @@ -85,6 +86,25 @@ describe("HTTP transport", () => {
}).to.throw;
});

it("Can be created with Node's IncomingHttpHeaders", () => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

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,
Expand Down