Skip to content

Commit 138de37

Browse files
authored
docs: update README with latest API changes (#347)
Signed-off-by: Lance Ball <lball@redhat.com>
1 parent 76688c4 commit 138de37

File tree

1 file changed

+40
-43
lines changed

1 file changed

+40
-43
lines changed

Diff for: README.md

+40-43
Original file line numberDiff line numberDiff line change
@@ -29,66 +29,63 @@ npm install cloudevents
2929

3030
#### Receiving Events
3131

32-
You can choose almost any popular web framework for port binding. Use a
33-
`Receiver` to process the incoming HTTP request. The receiver accepts
34-
binary and structured events in either the 1.0 or 0.3 protocol formats.
32+
You can choose any popular web framework for port binding. A `CloudEvent`
33+
object can be created by simply providing the `HTTP` protocol binding
34+
the incoming headers and request body.
3535

3636
```js
3737
const app = require("express")();
38-
const {Receiver} = require("cloudevents");
38+
const { HTTP } = require("cloudevents");
3939

4040
app.post("/", (req, res) => {
4141
// body and headers come from an incoming HTTP request, e.g. express.js
42-
const receivedEvent = Receiver.accept(req.headers, req.body);
42+
const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
4343
console.log(receivedEvent);
4444
});
4545
```
4646

4747
#### Emitting Events
4848

49-
You can send events over HTTP in either binary or structured format.
50-
51-
By default, the `Emitter` will emit events over HTTP POST using the
52-
binary transport protocol. The `Emitter` will examine the `specversion`
53-
of the event being sent, and use the appropriate protocol version. To send
54-
structured events, add `Protocol.HTTPStructured` as a parameter to
55-
`emitter.send()`.
49+
You can send events over HTTP in either binary or structured format
50+
using the `HTTP` binding to create a `Message` which has properties
51+
for `headers` and `body`.
5652

5753
```js
58-
const { CloudEvent, Emitter, Protocol, Version } = require("cloudevents");
54+
const axios = require('axios').default;
55+
const { HTTP } = require("cloudevents");
5956

60-
// With only an endpoint URL, this creates a v1 emitter
61-
const emitter = new Emitter({
62-
url: "https://cloudevents.io/example"
63-
});
64-
const event = new CloudEvent({
65-
type, source, data
57+
58+
const ce = new CloudEvent({ type, source, data })
59+
const message = HTTP.binary(ce); // Or HTTP.structured(ce)
60+
61+
axios({
62+
method: 'post',
63+
url: '...',
64+
data: message.body,
65+
headers: message.headers,
6666
});
67+
```
68+
69+
You may also use the `emitterFor()` function as a convenience.
6770

68-
// By default, the emitter will send binary events
69-
emitter.send(event).then((response) => {
70-
// handle the response
71-
}).catch(console.error);
72-
73-
// To send a structured event, just add that as an option
74-
emitter.send(event, { protocol: Protocol.HTTPStructured })
75-
.then((response) => {
76-
// handle the response
77-
}).catch(console.error);
78-
79-
// To send an event to an alternate URL, add that as an option
80-
emitter.send(event, { url: "https://alternate.com/api" })
81-
.then((response) => {
82-
// handle the response
83-
}).catch(console.error);
84-
85-
// Sending a v0.3 event works the same, If your event has a
86-
// specversion property of Version.V03, then it will be sent
87-
// using the 0.3 transport protocol
88-
emitter.send(new CloudEvent({ specversion: Version.V03, source, type }))
89-
.then((response) => {
90-
// handle the response
91-
}).catch(console.error);
71+
```js
72+
const axios = require('axios').default;
73+
const { emitterFor, Mode } = require("cloudevents");
74+
75+
function sendWithAxios(message) {
76+
// Do what you need with the message headers
77+
// and body in this function, then send the
78+
// event
79+
axios({
80+
method: 'post',
81+
url: '...',
82+
data: message.body,
83+
headers: message.headers,
84+
});
85+
}
86+
87+
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
88+
emit(new CloudEvent({ type, source, data }));
9289
```
9390

9491
## CloudEvent Objects

0 commit comments

Comments
 (0)