Skip to content

Commit f86388a

Browse files
committed
static send
1 parent 8ac3eb0 commit f86388a

File tree

4 files changed

+23
-44
lines changed

4 files changed

+23
-44
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/transport/emitter.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,6 @@ interface EmitterFunction {
3838
* @see https://github.com/cloudevents/spec/blob/v1.0/http-protocol-binding.md#13-content-modes
3939
*/
4040
export class Emitter {
41-
url?: string;
42-
protocol: Protocol;
43-
emitter: EmitterFunction;
44-
45-
constructor(options: TransportOptions = { protocol: Protocol.HTTPBinary }) {
46-
this.protocol = options.protocol as Protocol;
47-
this.url = options.url;
48-
this.emitter = emitBinary;
49-
if (this.protocol === Protocol.HTTPStructured) {
50-
this.emitter = emitStructured;
51-
}
52-
}
53-
5441
/**
5542
* Sends the {CloudEvent} to an event receiver over HTTP POST
5643
*
@@ -64,13 +51,12 @@ export class Emitter {
6451
* be overridden by providing a URL here.
6552
* @returns {Promise} Promise with an eventual response from the receiver
6653
*/
67-
send(event: CloudEvent, options?: TransportOptions): Promise<AxiosResponse> {
68-
options = options || {};
69-
options.url = options.url || this.url;
70-
if (options.protocol != this.protocol) {
71-
if (this.protocol === Protocol.HTTPBinary) return emitBinary(event, options);
72-
return emitStructured(event, options);
54+
static send(event: CloudEvent, options: TransportOptions): Promise<AxiosResponse> {
55+
const { protocol } = options;
56+
let emitter = emitBinary;
57+
if (protocol === Protocol.HTTPStructured) {
58+
emitter = emitStructured;
7359
}
74-
return this.emitter(event, options);
60+
return emitter(event, options);
7561
}
7662
}

test/integration/http_emitter_test.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import CONSTANTS from "../../src/constants";
66
const DEFAULT_CE_CONTENT_TYPE = CONSTANTS.DEFAULT_CE_CONTENT_TYPE;
77
const DEFAULT_CONTENT_TYPE = CONSTANTS.DEFAULT_CONTENT_TYPE;
88

9-
import { CloudEvent, Version, Emitter, Protocol, headersFor } from "../../src";
9+
import { CloudEvent, Version, Emitter, Protocol, headersFor, TransportOptions } from "../../src";
1010
import { AxiosResponse } from "axios";
1111

1212
const receiver = "https://cloudevents.io/";
@@ -40,7 +40,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
4040
});
4141

4242
describe("V1", () => {
43-
const emitter = new Emitter({ url: receiver });
43+
const send = (e: CloudEvent, opts: TransportOptions = {}) => Emitter.send(e, { url: receiver, ...opts });
4444
const event = new CloudEvent({
4545
type,
4646
source,
@@ -52,8 +52,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
5252
});
5353

5454
it("Sends a binary 1.0 CloudEvent by default", () =>
55-
emitter
56-
.send(event)
55+
send(event)
5756
.then((response: AxiosResponse) => {
5857
// A binary message will have a ce-id header
5958
expect(response.data["content-type"]).to.equal(DEFAULT_CONTENT_TYPE);
@@ -78,8 +77,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
7877
});
7978

8079
it("Sends a binary CloudEvent with Custom Headers", () =>
81-
emitter
82-
.send(event, { headers: { customheader: "value" } })
80+
send(event, { headers: { customheader: "value" } })
8381
.then((response: { data: { [k: string]: string } }) => {
8482
// A binary message will have a ce-id header
8583
expect(response.data.customheader).to.equal("value");
@@ -92,8 +90,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
9290
.catch(expect.fail));
9391

9492
it("Sends a structured 1.0 CloudEvent if specified", () =>
95-
emitter
96-
.send(event, { protocol: Protocol.HTTPStructured })
93+
send(event, { protocol: Protocol.HTTPStructured })
9794
.then((response: { data: { [k: string]: string | Record<string, string>; data: { lunchBreak: string } } }) => {
9895
// A structured message will have a cloud event content type
9996
expect(response.data["content-type"]).to.equal(DEFAULT_CE_CONTENT_TYPE);
@@ -121,8 +118,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
121118
return [201, returnBody];
122119
});
123120

124-
return emitter
125-
.send(event, { protocol: Protocol.HTTPStructured, url: `${receiver}alternate` })
121+
return send(event, { protocol: Protocol.HTTPStructured, url: `${receiver}alternate` })
126122
.then((response: AxiosResponse) => {
127123
// A structured message will have a cloud event content type
128124
expect(response.data["content-type"]).to.equal(DEFAULT_CE_CONTENT_TYPE);
@@ -137,7 +133,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
137133
});
138134

139135
describe("V03", () => {
140-
const emitter = new Emitter({ url: receiver });
136+
const send = (e: CloudEvent, opts: TransportOptions = {}) => Emitter.send(e, { url: receiver, ...opts });
141137
const event = new CloudEvent({
142138
specversion: Version.V03,
143139
type,
@@ -150,8 +146,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
150146
});
151147

152148
it("Sends a binary 0.3 CloudEvent", () =>
153-
emitter
154-
.send(event)
149+
send(event)
155150
.then((response: AxiosResponse) => {
156151
// A binary message will have a ce-id header
157152
expect(response.data[CONSTANTS.CE_HEADERS.ID]).to.equal(event.id);
@@ -175,8 +170,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
175170
});
176171

177172
it("Sends a structured 0.3 CloudEvent if specified", () =>
178-
emitter
179-
.send(event, { protocol: Protocol.HTTPStructured })
173+
send(event, { protocol: Protocol.HTTPStructured })
180174
.then(
181175
(response: {
182176
data: { [k: string]: string | Record<string, string>; specversion: string; data: { lunchBreak: string } };
@@ -208,8 +202,7 @@ describe("HTTP Transport Binding Emitter for CloudEvents", () => {
208202
return [201, returnBody];
209203
});
210204

211-
return emitter
212-
.send(event, { protocol: Protocol.HTTPStructured, url: `${receiver}alternate` })
205+
return send(event, { protocol: Protocol.HTTPStructured, url: `${receiver}alternate` })
213206
.then(
214207
(response: {
215208
data: { specversion: string; data: { lunchBreak: string }; [k: string]: string | Record<string, string> };

test/integration/sdk_test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ describe("The SDK Requirements", () => {
1818
expect(receiver instanceof Receiver).to.equal(true);
1919
});
2020

21-
it("should expose an Emitter type", () => {
22-
const emitter = new Emitter({
23-
url: "http://example.com",
24-
});
25-
expect(emitter instanceof Emitter).to.equal(true);
26-
});
21+
// it("should expose an Emitter type", () => {
22+
// const emitter = new Emitter({
23+
// url: "http://example.com",
24+
// });
25+
// expect(emitter instanceof Emitter).to.equal(true);
26+
// });
2727

2828
describe("v0.3", () => {
2929
it("should create an event using the right spec version", () => {

0 commit comments

Comments
 (0)