8000 chore: add a transition guide. fixes #360 · cloudevents/sdk-javascript@2e7db49 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e7db49

Browse files
committed
chore: add a transition guide. fixes #360
Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
1 parent 9f86cfd commit 2e7db49

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

API_TRANSITION_GUIDE.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
## Deprecated API Transition Guide
2+
3+
When APIs are deprecated, the following guide will show how to transition from removed APIs to the new ones
4+
5+
6+
### Upgrading From 3.x to 4.0
7+
8+
In the 3.2.0 release, a few APIs were set to be deprecated in the 4.0 release. With the release of 4.0.0, those APIs have been removed.
9+
10+
#### Receiever
11+
12+
The `Receiver` class has been removed.
13+
14+
`Receiver.accept` should be transitioned to `HTTP.toEvent`
15+
16+
Here is an example of what a `HTTP.toEvent` might look like using Express.js
17+
18+
```js
19+
const app = require("express")();
20+
const { HTTP } = require("cloudevents");
21+
22+
app.post("/", (req, res) => {
23+
// body and headers come from an incoming HTTP request, e.g. express.js
24+
const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
25+
console.log(receivedEvent);
26+
});
27+
```
28+
29+
#### Emitter
30+
31+
`Emit.send` should be transitioned to `HTTP.binary` for binary events and `HTTP.structured` for structured events
32+
33+
`Emit.send` would use axios to emit the events. Since this now longer available, you are free to choose your own transport protocol.
34+
35+
So for axios, it might look something like this:
36+
37+
```js
38+
const axios = require('axios').default;
39+
const { HTTP } = require("cloudevents");
40+
41+
42+
const ce = new CloudEvent({ type, source, data })
43+
const message = HTTP.binary(ce); // Or HTTP.structured(ce)
44+
45+
axios({
46+
method: 'post',
47+
url: '...',
48+
data: message.body,
49+
headers: message.headers,
50+
});
51+
```
52+
53+
You may also use the `emitterFor()` function as a convenience.
54+
55+
```js
56+
const axios = require('axios').default;
57+
const { emitterFor, Mode } = require("cloudevents");
58+
59+
function sendWithAxios(message) {
60+
// Do what you need with the message headers
61+
// and body in this function, then send the
62+
// event
63+
axios({
64+
method: 'post',
65+
url: '...',
66+
data: message.body,
67+
headers: message.headers,
68+
});
69+
}
70+
71+
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
72+
emit(new CloudEvent({ type, source, data }));
73+
```
74+
75+
You may also use the `Emitter` singleton
76+
77+
```js
78+
const axios = require("axios").default;
79+
const { emitterFor, Mode, CloudEvent, Emitter } = require("cloudevents");
80+
81+
function sendWithAxios(message) {
82+
// Do what you need with the message headers
83+
// and body in this function, then send the
84+
// event
85+
axios({
86+
method: "post",
87+
url: "...",
88+
data: message.body,
89+
headers: message.headers,
90+
});
91+
}
92+
93+
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
94+
// Set the emit
95+
Emitter.on("cloudevent", emit);
96+
97+
...
98+
// In any part of the code will send the event
99+
new CloudEvent({ type, source, data }).emit();
100+
101+
// You can also have several listener to send the event to several endpoint
102+
```

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ There are a few trivial example applications in
138138
[the examples folder](https://github.com/cloudevents/sdk-javascript/tree/main/examples).
139139
There you will find Express.js, TypeScript and Websocket examples.
140140

141+
142+
### API Transition Guide
143+
144+
[Guide Link](./API_TRANSITION_GUIDE.md)
145+
141146
## Supported specification features
142147

143148
| Core Specification | [v0.3](https://github.com/cloudevents/spec/blob/v0.3/spec.md) | [v1.0](https://github.com/cloudevents/spec/blob/v1.0/spec.md) |

0 commit comments

Comments
 (0)
0