Skip to content
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

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

Merged
merged 2 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/message/http/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function deserialize(message: Message): CloudEvent {
* @returns {Mode} the transport mode
*/
function getMode(headers: Headers): Mode {
const contentType = headers[CONSTANTS.HEADER_CONTENT_TYPE];
const contentType = headers[CONSTANTS.HEADER_CONTENT_TYPE] as string;
Copy link

@aldirrix aldirrix Oct 2, 2020

Choose a reason for hiding this comment

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

i think you can add as const to the actual constants file instead of in every usage of this.
Ref: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions

in sdk-javascript/src/constants.ts

const CONSTANTS = Object.freeze({
  CHARSET_DEFAULT: "utf-8",
  ... 
} as const);

export default CONSTANTS;

if (contentType && contentType.startsWith(CONSTANTS.MIME_CE)) {
return Mode.STRUCTURED;
}
Expand Down Expand Up @@ -198,7 +198,7 @@ function parseStructured(message: Message, version: Version): CloudEvent {
// Clone and low case all headers names
const sanitizedHeaders = sanitize(headers);

const contentType = sanitizedHeaders[CONSTANTS.HEADER_CONTENT_TYPE];
const contentType = sanitizedHeaders[CONSTANTS.HEADER_CONTENT_TYPE] as string;
const parser: Parser = contentType ? parserByContentType[contentType] : new JSONParser();
if (!parser) throw new ValidationError(`invalid content type ${sanitizedHeaders[CONSTANTS.HEADER_CONTENT_TYPE]}`);
const incoming = { ...(parser.parse(payload) as Record<string, unknown>) };
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