forked from cloudevents/sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemitter_structured.js
42 lines (36 loc) · 1.26 KB
/
emitter_structured.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const { default: Axios } = require("axios");
/** @typedef {import("../../cloudevent")} CloudEvent */
const {
DATA_ATTRIBUTE,
DEFAULT_CE_CONTENT_TYPE,
HEADERS,
HEADER_CONTENT_TYPE
} = require("./constants");
const defaults = {
[HEADERS]: {
[HEADER_CONTENT_TYPE]: DEFAULT_CE_CONTENT_TYPE
},
method: "POST"
};
/**
* A class for sending {CloudEvent} instances over HTTP.
*/
class StructuredHTTPEmitter {
// TODO: Do we really need a class here? There is no state maintenance
/**
* Sends the event over HTTP
* @param {Object} options The configuration options for this event. Options
* provided will be passed along to Node.js `http.request()`.
* https://nodejs.org/api/http.html#http_http_request_options_callback
* @param {URL} options.url The HTTP/S url that should receive this event
* @param {CloudEvent} cloudevent The CloudEvent to be sent
* @returns {Promise} Promise with an eventual response from the receiver
*/
async emit(options, cloudevent) {
const config = { ...defaults, ...options };
config[DATA_ATTRIBUTE] = cloudevent.format();
// @ts-ignore Types of property 'url' are incompatible. Type 'URL' is not assignable to type 'string'.
return Axios.request(config);
}
}
module.exports = StructuredHTTPEmitter;