1
1
# JavaScript SDK for CloudEvents
2
2
3
- [ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Grade/bd66e7c52002481993cd6d610534b0f7 )] ( https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com& ; utm_medium=referral& ; utm_content=cloudevents/sdk-javascript& ; utm_campaign=Badge_Grade )
4
- [ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Coverage/bd66e7c52002481993cd6d610534b0f7 )] ( https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com& ; utm_medium=referral& ; utm_content=cloudevents/sdk-javascript& ; utm_campaign=Badge_Coverage )
3
+ [ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Grade/bd66e7c52002481993cd6d610534b0f7 )] ( https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Grade )
4
+ [ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Coverage/bd66e7c52002481993cd6d610534b0f7 )] ( https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com&utm_medium=referral&utm_content=cloudevents/sdk-javascript&utm_campaign=Badge_Coverage )
5
5
![ Node.js CI] ( https://github.com/cloudevents/sdk-javascript/workflows/Node.js%20CI/badge.svg )
6
6
[ ![ npm version] ( https://img.shields.io/npm/v/cloudevents.svg )] ( https://www.npmjs.com/package/cloudevents )
7
7
[ ![ vulnerabilities] ( https://snyk.io/test/github/cloudevents/sdk-javascript/badge.svg )] ( https://snyk.io/test/github/cloudevents/sdk-javascript )
@@ -10,9 +10,9 @@ The CloudEvents SDK for JavaScript.
10
10
11
11
## Features
12
12
13
- * Represent CloudEvents in memory
14
- * Serialize and deserialize CloudEvents in different [ event formats] ( https://github.com/cloudevents/spec/blob/v1.0/spec.md#event-format ) .
15
- * Send and recieve CloudEvents with via different [ protocol bindings] ( https://github.com/cloudevents/spec/blob/v1.0/spec.md#protocol-binding ) .
13
+ - Represent CloudEvents in memory
14
+ - Serialize and deserialize CloudEvents in different [ event formats] ( https://github.com/cloudevents/spec/blob/v1.0/spec.md#event-format ) .
15
+ - Send and recieve CloudEvents with via different [ protocol bindings] ( https://github.com/cloudevents/spec/blob/v1.0/spec.md#protocol-binding ) .
16
16
17
17
_ Note:_ Supports CloudEvent versions 0.3, 1.0
18
18
@@ -51,16 +51,15 @@ using the `HTTP` binding to create a `Message` which has properties
51
51
for ` headers ` and ` body ` .
52
52
53
53
``` js
54
- const axios = require (' axios' ).default ;
54
+ const axios = require (" axios" ).default ;
55
55
const { HTTP } = require (" cloudevents" );
56
56
57
-
58
- const ce = new CloudEvent ({ type, source, data })
57
+ const ce = new CloudEvent ({ type, source, data });
59
58
const message = HTTP .binary (ce); // Or HTTP.structured(ce)
60
59
61
60
axios ({
62
- method: ' post' ,
63
- url: ' ...' ,
61
+ method: " post" ,
62
+ url: " ..." ,
64
63
data: message .body ,
65
64
headers: message .headers ,
66
65
});
@@ -69,16 +68,16 @@ axios({
69
68
You may also use the ` emitterFor() ` function as a convenience.
70
69
71
70
``` js
72
- const axios = require (' axios' ).default ;
71
+ const axios = require (" axios" ).default ;
73
72
const { emitterFor , Mode } = require (" cloudevents" );
74
73
75
74
function sendWithAxios (message ) {
76
75
// Do what you need with the message headers
77
76
// and body in this function, then send the
78
77
// event
79
78
axios ({
80
- method: ' post' ,
81
- url: ' ...' ,
79
+ method: " post" ,
80
+ url: " ..." ,
82
81
data: message .body ,
83
82
headers: message .headers ,
84
83
});
@@ -88,9 +87,38 @@ const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
88
87
emit (new CloudEvent ({ type, source, data }));
89
88
```
90
89
90
+ You may also use the ` Emitter ` singleton
91
+
92
+ ``` js
93
+ const axios = require (" axios" ).default ;
94
+ const { emitterFor , Mode , CloudEvent , Emitter } = require (" cloudevents" );
95
+
96
+ function sendWithAxios (message ) {
97
+ // Do what you need with the message headers
98
+ // and body in this function, then send the
99
+ // event
100
+ axios ({
101
+ method: " post" ,
102
+ url: " ..." ,
103
+ data: message .body ,
104
+ headers: message .headers ,
105
+ });
106
+ }
107
+
108
+ const emit = emitterFor (sendWithAxios, { mode: Mode .BINARY });
109
+ // Set the emit
110
+ Emitter .getSingleton ().on (" event" , emit);
111
+
112
+ ...
113
+ // In any part of the code will send the event
114
+ new CloudEvent ({ type, source, data }).emit ();
115
+
116
+ // You can also have several listener to send the event to several endpoint
117
+ ```
118
+
91
119
## CloudEvent Objects
92
120
93
- All created ` CloudEvent ` objects are read-only. If you need to update a property or add a new extension to an existing cloud event object, you can use the ` cloneWith ` method. This will return a new ` CloudEvent ` with any update or new properties. For example:
121
+ All created ` CloudEvent ` objects are read-only. If you need to update a property or add a new extension to an existing cloud event object, you can use the ` cloneWith ` method. This will return a new ` CloudEvent ` with any update or new properties. For example:
94
122
95
123
``` js
96
124
const {
@@ -112,24 +140,26 @@ There you will find Express.js, TypeScript and Websocket examples.
112
140
113
141
## Supported specification features
114
142
115
- | 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 ) |
116
- | ----------------------------- | --- | --- |
117
- | CloudEvents Core | :heavy_check_mark : | :heavy_check_mark : |
143
+ | 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 ) |
144
+ | ------------------ | ------------------------------------------------------------- | ------------------------------------------------------------- |
145
+ | CloudEvents Core | :heavy_check_mark : | :heavy_check_mark : |
146
+
118
147
---
119
148
120
- | Event Formats | [ v0.3] ( https://github.com/cloudevents/spec/tree/v0.3 ) | [ v1.0] ( https://github.com/cloudevents/spec/tree/v1.0 ) |
121
- | ----------------------------- | --- | --- |
122
- | AVRO Event Format | :x : | :x : |
123
- | JSON Event Format | :heavy_check_mark : | :heavy_check_mark : |
149
+ | Event Formats | [ v0.3] ( https://github.com/cloudevents/spec/tree/v0.3 ) | [ v1.0] ( https://github.com/cloudevents/spec/tree/v1.0 ) |
150
+ | ----------------- | ----------------------------------------------------- | ----------------------------------------------------- |
151
+ | AVRO Event Format | :x : | :x : |
152
+ | JSON Event Format | :heavy_check_mark : | :heavy_check_mark : |
153
+
124
154
---
125
155
126
- | Transport Protocols | [ v0.3] ( https://github.com/cloudevents/spec/tree/v0.3 ) | [ v1.0] ( https://github.com/cloudevents/spec/tree/v1.0 ) |
127
- | ----------------------------- | --- | --- |
128
- | AMQP Protocol Binding | : x : | :x : |
129
- | HTTP Protocol Binding | : heavy_check_mark : | :heavy_check_mark : |
130
- | Kafka Protocol Binding | : x : | :x : |
131
- | MQTT Protocol Binding | : x : | :x : |
132
- | NATS Protocol Binding | : x : | :x : |
156
+ | Transport Protocols | [ v0.3] ( https://github.com/cloudevents/spec/tree/v0.3 ) | [ v1.0] ( https://github.com/cloudevents/spec/tree/v1.0 ) |
157
+ | ---------------------- | ----------------------------------------------------- | -------------------------------------------------- --- |
158
+ | AMQP Protocol Binding | : x : | :x : |
159
+ | HTTP Protocol Binding | : heavy_check_mark : | :heavy_check_mark : |
160
+ | Kafka Protocol Binding | : x : | :x : |
161
+ | MQTT Protocol Binding | : x : | :x : |
162
+ | NATS Protocol Binding | : x : | :x : |
133
163
134
164
## Community
135
165
0 commit comments