@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
69 lines • 2.3 kB
JavaScript
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
export { ApiVersion, Channel, ReservedDittoProtocolDittoHeaders, DefaultDittoProtocolEnvelope, DefaultDittoProtocolResponse };
var ApiVersion;
(function (ApiVersion) {
ApiVersion[ApiVersion["V2"] = 2] = "V2";
})(ApiVersion || (ApiVersion = {}));
var Channel;
(function (Channel) {
Channel["twin"] = "twin";
Channel["live"] = "live";
})(Channel || (Channel = {}));
/**
* Known and reserved headers in Ditto.
*/
var ReservedDittoProtocolDittoHeaders;
(function (ReservedDittoProtocolDittoHeaders) {
ReservedDittoProtocolDittoHeaders["CORRELATION_ID"] = "correlation-id";
})(ReservedDittoProtocolDittoHeaders || (ReservedDittoProtocolDittoHeaders = {}));
class DefaultDittoProtocolEnvelope {
constructor(topic, headers, path, value, fields) {
this.topic = topic;
this.headers = headers;
this.path = path;
this.value = value;
this.fields = fields;
}
toJson() {
return JSON.stringify(this);
}
}
class DefaultDittoProtocolResponse {
constructor(topic, headers, path, value, status) {
this.topic = topic;
this.headers = headers;
this.path = path;
this.value = value;
this.status = status;
}
static tryParseJson(json) {
try {
return JSON.parse(json);
}
catch (e) {
console.error('Unable to parse json: ', json);
return {};
}
}
static fromJson(json) {
const parsed = this.tryParseJson(json);
return new DefaultDittoProtocolResponse(parsed['topic'], parsed['headers'] !== undefined ? parsed['headers'] : {}, parsed['path'], parsed['value'], parsed['status']);
}
correlationId() {
if (this.headers !== undefined) {
return this.headers[ReservedDittoProtocolDittoHeaders.CORRELATION_ID];
}
}
}
//# sourceMappingURL=ditto-protocol.js.map