sdk-javascript/docs/cloudevent.js.html

349 lines
11 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
cloudevent.js - Documentation
</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
<link type="text/css" rel="stylesheet" href="styles/collapse.css">
</head>
<body>
<svg style="display: none;">
<defs>
<symbol id="linkIcon" fill="#706d77" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M0 0h24v24H0z" fill="none"/>
<path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/>
</symbol>
</defs>
</svg>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<div class="top-nav-wrapper">
<ul>
<li >
<a href="index.html">
<svg fill="#6D6D6D" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/>
<path d="M0 0h24v24H0z" fill="none"/>
</svg>
</a>
</li>
</ul>
</div>
<nav>
<h3 class="reference-title">
cloudevents-sdk
</h3>
<h3>
Resources
</h3>
<a href="concepts.html">Concepts</a>
<h3>Classes</h3><ul><li id="CloudEvent-nav"><a href="CloudEvent.html">CloudEvent</a><ul class='methods'><li data-type="method" id="CloudEvent-addExtension-nav"><a href="CloudEvent.html#addExtension">addExtension</a></li><li data-type="method" id="CloudEvent-data-nav"><a href="CloudEvent.html#data">data</a></li><li data-type="method" id="CloudEvent-dataContenttype-nav"><a href="CloudEvent.html#dataContenttype">dataContenttype</a></li><li data-type="method" id="CloudEvent-format-nav"><a href="CloudEvent.html#format">format</a></li><li data-type="method" id="CloudEvent-getData-nav"><a href="CloudEvent.html#getData">getData</a></li><li data-type="method" id="CloudEvent-getDataContenttype-nav"><a href="CloudEvent.html#getDataContenttype">getDataContenttype</a></li><li data-type="method" id="CloudEvent-getExtensions-nav"><a href="CloudEvent.html#getExtensions">getExtensions</a></li><li data-type="method" id="CloudEvent-getFormats-nav"><a href="CloudEvent.html#getFormats">getFormats</a></li><li data-type="method" id="CloudEvent-getId-nav"><a href="CloudEvent.html#getId">getId</a></li><li data-type="method" id="CloudEvent-getSource-nav"><a href="CloudEvent.html#getSource">getSource</a></li><li data-type="method" id="CloudEvent-getSpecversion-nav"><a href="CloudEvent.html#getSpecversion">getSpecversion</a></li><li data-type="method" id="CloudEvent-getTime-nav"><a href="CloudEvent.html#getTime">getTime</a></li><li data-type="method" id="CloudEvent-getType-nav"><a href="CloudEvent.html#getType">getType</a></li><li data-type="method" id="CloudEvent-id-nav"><a href="CloudEvent.html#id">id</a></li><li data-type="method" id="CloudEvent-source-nav"><a href="CloudEvent.html#source">source</a></li><li data-type="method" id="CloudEvent-time-nav"><a href="CloudEvent.html#time">time</a></li><li data-type="method" id="CloudEvent-toString-nav"><a href="CloudEvent.html#toString">toString</a></li><li data-type="method" id="CloudEvent-type-nav"><a href="CloudEvent.html#type">type</a></li></ul></li><li id="HTTPReceiver-nav"><a href="HTTPReceiver.html">HTTPReceiver</a><ul class='methods'><li data-type="method" id="HTTPReceiver-accept-nav"><a href="HTTPReceiver.html#accept">accept</a></li></ul></li></ul>
</nav>
<div id="main">
<h1 class="page-title">
cloudevent.js
</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const Spec = require("./specs/spec_1.js");
const Formatter = require("./formats/json/formatter.js");
/**
* An instance of a CloudEvent.
*/
class CloudEvent {
/**
* Creates a new CloudEvent instance
* @param {Spec} [UserSpec] A CloudEvent version specification
* @param {Formatter} [UserFormatter] Converts the event into a readable string
*/
constructor(UserSpec, UserFormatter) {
this.spec = (UserSpec) ? new UserSpec(CloudEvent) : new Spec(CloudEvent);
this.formatter = (UserFormatter) ? new UserFormatter() : new Formatter();
// The map of extensions
this.extensions = {};
}
/**
* Get the formatters available to this CloudEvent
* @returns {Object} a JSON formatter
*/
getFormats() {
return { json: Formatter };
}
/**
* Format the CloudEvent as JSON. Validates the event according
* to the CloudEvent specification and throws an exception if
* it's invalid.
* @returns {JSON} the CloudEvent in JSON form
*/
format() {
// Check the constraints
this.spec.check();
// To run asData()
this.getData();
// Then, format
return this.formatter.format(this.spec.payload);
}
/**
* Formats the CLoudEvent as JSON. No specification validation
* is performed.
* @returns {JSON} the CloudEvent in JSON form
*/
toString() {
return this.formatter.toString(this.spec.payload);
}
/**
* Sets the event type
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
* @param {string} type the type of event related to the originating source
* @returns {CloudEvent} this CloudEvent
*/
type(type) {
this.spec.type(type);
return this;
}
/**
* Gets the event type
* @see https://github.com/cloudevents/spec/blob/master/spec.md#type
* @returns {String} the type of event related to the originating source
*/
getType() {
return this.spec.getType();
}
// TODO: The fact that this is exposed is problematic, given that it's
// immutable and this method will have no effect. The specification
// version is determined via the constructor - specifically the use
// of cloud event creator functions in /v03 and /v1. By default this
// object is created as a version 1.0 CloudEvent. Not documenting.
specversion(version) {
return this.spec.specversion(version);
}
/**
* Gets the CloudEvent specification version
* @see https://github.com/cloudevents/spec/blob/master/spec.md#specversion
* @returns {string} The CloudEvent version that this event adheres to
*/
getSpecversion() {
return this.spec.getSpecversion();
}
/**
* Sets the origination source of this event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
* @param {URI} source the context in which the event happened
* @returns {CloudEvent} this CloudEvent instance
*/
source(source) {
this.spec.source(source);
return this;
}
/**
* Gets the origination source of this event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#source-1
* @returns {string} the event source
*/
getSource() {
return this.spec.getSource();
}
/**
* Sets the event id. Source + id must be unique for each distinct event.
* @see https://github.com/cloudevents/spec/blob/master/spec.md#id
* @param {string} id source+id must be unique for each distinct event
* @returns {CloudEvent} this CloudEvent instance
*/
id(id) {
this.spec.id(id);
return this;
}
/**
* Gets the event id.
* @returns {string} the event id
*/
getId() {
return this.spec.getId();
}
/**
* Sets the timestamp for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
* @param {Date} time timestamp when the event occurred
* @returns {CloudEvent} this CloudEvent instance
*/
time(time) {
// TODO: Ensure that this is represented as a Date internally,
// or update the JSDoc
this.spec.time(time);
return this;
}
/**
* Gets the timestamp for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#time
* @returns {Date} the timestamp for this event
*/
getTime() {
// TODO: Ensure that this is represented as a Date internally,
// or update the JSDoc
return this.spec.getTime();
}
// TODO: Deprecated in 1.0
schemaurl(schemaurl) {
this.spec.schemaurl(schemaurl);
return this;
}
// TODO: Deprecated in 1.0
getSchemaurl() {
return this.spec.getSchemaurl();
}
/**
* Sets the content type of the data value for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
* @param {string} contenttype per https://tools.ietf.org/html/rfc2046
* @returns {CloudEvent} this CloudEvent instance
*/
dataContenttype(contenttype) {
this.spec.dataContenttype(contenttype);
return this;
}
/**
* Gets the content type of the data value for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#datacontenttype
* @returns {string} the content type for the data in this event
*/
getDataContenttype() {
return this.spec.getDataContenttype();
}
/**
* Sets the data for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
* @param {*} data any data associated with this event
* @returns {CloudEvent} this CloudEvent instance
*/
data(data) {
this.spec.data(data);
return this;
}
/**
* Gets any data that has been set for this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#event-data
* @returns {*} any data set for this event
*/
getData() {
return this.spec.getData();
}
/**
* Adds an extension attribute to this CloudEvent
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
* @param {*} key the name of the exteneion attribute
* @param {*} value the value of the extension attribute
* @returns {CloudEvent} this CloudEvent instance
*/
addExtension(key, value) {
this.spec.addExtension(key, value);
// Stores locally
this.extensions[key] = value;
return this;
}
/**
* Gets the extension attributes, if any, associated with this event
* @see https://github.com/cloudevents/spec/blob/master/spec.md#extension-context-attributes
* @returns {Object} the extensions attributes - if none exist will will be {}
* // TODO - this should return null or undefined if no extensions
*/
getExtensions() {
return this.extensions;
}
}
module.exports = CloudEvent;
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.4</a>
</footer>
<script src="scripts/linenumber.js"></script>
<script src="scripts/pagelocation.js"></script>
</body>
</html>