Skip to content

Commit 0f11b02

Browse files
authored
fix: defaults properly handled for emitterFor() (#399)
* fix: defaults properly handled for emitterFor() Fixes: #391
1 parent 447252e commit 0f11b02

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/transport/emitter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface TransportFunction {
2828
(message: Message, options?: Options): Promise<unknown>;
2929
}
3030

31+
const emitterDefaults = { binding: HTTP, mode: Mode.BINARY };
3132
/**
3233
* emitterFactory creates and returns an EmitterFunction using the supplied
3334
* TransportFunction. The returned EmitterFunction will invoke the Binding's
@@ -41,11 +42,11 @@ export interface TransportFunction {
4142
* @param {Mode} options.mode the encoding mode (Mode.BINARY or Mode.STRUCTURED)
4243
* @returns {EmitterFunction} an EmitterFunction to send events with
4344
*/
44-
export function emitterFor(fn: TransportFunction, options = { binding: HTTP, mode: Mode.BINARY }): EmitterFunction {
45+
export function emitterFor(fn: TransportFunction, options = emitterDefaults): EmitterFunction {
4546
if (!fn) {
4647
throw new TypeError("A TransportFunction is required");
4748
}
48-
const { binding, mode } = options;
49+
const { binding, mode } = { ...emitterDefaults, ...options };
4950
return function emit(event: CloudEvent, opts?: Options): Promise<unknown> {
5051
opts = opts || {};
5152

test/integration/emitter_factory_test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,46 @@ function gotEmitter(message: Message, options?: Options): Promise<unknown> {
5959
);
6060
}
6161

62+
describe("emitterFor() defaults", () => {
63+
it("Defaults to HTTP binding, binary mode", () => {
64+
function transport(message: Message): Promise<unknown> {
65+
// A binary message will have the source attribute as a header
66+
expect(message.headers[CONSTANTS.CE_HEADERS.TYPE]).to.equal("emitter.test");
67+
return Promise.resolve();
68+
}
69+
const emitter = emitterFor(transport);
70+
emitter(
71+
new CloudEvent({
72+
id: "1234",
73+
source: "/emitter/test",
74+
type: "emitter.test",
75+
}),
76+
);
77+
});
78+
79+
it("Supports HTTP binding, structured mode", () => {
80+
function transport(message: Message): Promise<unknown> {
81+
console.error(message);
82+
// A structured message will have the application/cloudevents+json header
83+
expect(message.headers["content-type"]).to.equal(CONSTANTS.DEFAULT_CE_CONTENT_TYPE);
84+
const body = JSON.parse(message.body as string);
85+
expect(body.id).to.equal("1234");
86+
return Promise.resolve();
87+
}
88+
// Ignore the next line to ensure that HTTP transport is still the default.
89+
// Otherwise, tslint would complain that the param did not have `binding: <val>`
90+
/* @ts-ignore */
91+
const emitter = emitterFor(transport, { mode: Mode.STRUCTURED });
92+
emitter(
93+
new CloudEvent({
94+
id: "1234",
95+
source: "/emitter/test",
96+
type: "emitter.test",
97+
}),
98+
);
99+
});
100+
});
101+
62102
describe("HTTP Transport Binding for emitterFactory", () => {
63103
beforeEach(() => {
64104
nock(sink)

0 commit comments

Comments
 (0)