@parity/jsonrpc
Version:
JSON and JS interface defintions for RPC
99 lines (76 loc) • 3.62 kB
JavaScript
;var _createClass = function () {function defineProperties(target, props) {for (var i = 0; i < props.length; i++) {var descriptor = props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if ("value" in descriptor) descriptor.writable = true;Object.defineProperty(target, descriptor.key, descriptor);}}return function (Constructor, protoProps, staticProps) {if (protoProps) defineProperties(Constructor.prototype, protoProps);if (staticProps) defineProperties(Constructor, staticProps);return Constructor;};}();function _classCallCheck(instance, Constructor) {if (!(instance instanceof Constructor)) {throw new TypeError("Cannot call a class as a function");}} // Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
// A dummy placeholder object that will stringify literally to anything
// in the example source.
//
// { {
// foo: new Dummy('{ ... }') -------> "foo": { ... }
// } {
//
var
Dummy = function () {
function Dummy(value) {_classCallCheck(this, Dummy);
this.value = value;
}_createClass(Dummy, [{ key: 'toString', value: function toString()
{
return this.value;
} }, { key: 'toJSON', value: function toJSON()
{
return '##' + this.value + '##';
} }]);return Dummy;}();
Dummy.fixJSON = function (json) {
return json.replace(/"##([^#]+)##"/g, '$1');
};
Dummy.isDummy = function (obj) {
return obj instanceof Dummy;
};
Dummy.stringifyJSON = function (any) {
return Dummy.fixJSON(JSON.stringify(any));
};
// Enrich the API spec by additional markdown-formatted preamble
function withPreamble(preamble, spec) {
Object.defineProperty(spec, '_preamble', {
value: preamble.trim(),
enumerable: false });
return spec;
}
// Enrich any example value with a comment to print in the docs
function withComment(example, comment) {
var constructor = example == null ? null : example.constructor;
if (constructor === Object || constructor === Array) {
Object.defineProperty(example, '_comment', {
value: comment,
enumerable: false });
return example;
}
// Convert primitives
return new PrimitiveWithComment(example, comment);
}
// Turn a decimal number into a hexadecimal string with comment to it's original value
function fromDecimal(decimal) {
return withComment('0x' + decimal.toString(16), decimal.toString());
}
// Internal helper
var PrimitiveWithComment = function () {
function PrimitiveWithComment(primitive, comment) {_classCallCheck(this, PrimitiveWithComment);
this._value = primitive;
this._comment = comment;
}_createClass(PrimitiveWithComment, [{ key: 'toJSON', value: function toJSON()
{
return this._value;
} }]);return PrimitiveWithComment;}();
module.exports = {
Dummy: Dummy,
fromDecimal: fromDecimal,
withComment: withComment,
withPreamble: withPreamble };