jaysonic
Version:
A feature rich JSON-RPC 1.0/2.0 compliant client and server library
179 lines (147 loc) • 4.4 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
/**
* Generates a stringified JSON-RPC request object with appended delimiter.
*
* @function formatRequest
* @memberof Utils.format
* @param {object} request
* @param {string} request.method
* @param {array|object} request.params
* @param {string|number} request.id
* @param {string|number} request.version
* @param {string} request.delimiter
*/
var formatRequest = function formatRequest(_ref) {
var method = _ref.method,
params = _ref.params,
id = _ref.id,
version = _ref.version,
delimiter = _ref.delimiter;
if (typeof method !== "string") {
throw new TypeError("".concat(method, " must be a string"));
}
var request = {
method: method
}; // assume 2.0 request unless otherwise specified
if (!version || version !== 1) {
request.jsonrpc = "2.0";
}
if (params && _typeof(params) !== "object" && !Array.isArray(params)) {
throw new TypeError("".concat(params, " must be an object or array"));
} else if (params) {
request.params = params;
} // assume notification otherwise
if (typeof id !== "undefined") {
request.id = id;
}
return JSON.stringify(request) + delimiter;
};
/**
* Generates a stringified JSON-RPC response object with appended delimiter.
*
* @function formatResponse
* @memberof Utils.format
* @param {object} response
* @param {string} response.method
* @param {string|number} response.id
* @param {string|number} response.jsonrpc
* @param {string} response.delimiter
* @param response.result
*/
var formatResponse = function formatResponse(_ref2) {
var jsonrpc = _ref2.jsonrpc,
id = _ref2.id,
method = _ref2.method,
result = _ref2.result,
params = _ref2.params,
delimiter = _ref2.delimiter;
if (params && result) {
throw new Error("Cannot send response with both params and result");
}
if (method && id) {
throw new Error("Cannot send response with both a method and non-null id");
}
if (method && typeof method !== "string") {
throw new TypeError("Method must be a string");
}
if (params && _typeof(params) !== "object" && !Array.isArray(params)) {
throw new TypeError("Params must be an object or array");
}
var response = {};
if (typeof result !== "undefined") {
response.result = result;
}
if (params) {
response.params = params;
}
if (!jsonrpc || jsonrpc === 1) {
// 1.0 response
response.error = null; // 1.0 notification
if (!id) {
response.id = null;
}
} else {
// assume 2.0 response, dont include null error and include jsonrpc version
response.jsonrpc = "2.0";
}
if (method) {
response.method = method;
}
if (id) {
response.id = id;
}
return JSON.stringify(response) + delimiter;
};
/**
* Generates a stringified JSON-RPC error object with appended delimiter.
*
* @function formatError
* @memberof Utils.format
* @param {object} error
* @param {string} error.message
* @param {array|object} error.code
* @param {string|number} error.id
* @param {string|number} error.jsonrpc
* @param {string} error.delimiter
* @param {string|object|array} error.data
*/
var formatError = function formatError(_ref3) {
var jsonrpc = _ref3.jsonrpc,
id = _ref3.id,
code = _ref3.code,
message = _ref3.message,
data = _ref3.data,
delimiter = _ref3.delimiter;
if (!message) {
throw new Error("Must include message in error response");
} // we're going to assume a 2.0 response if the version isnt explicitly 1
var response = jsonrpc && jsonrpc !== 1 ? {
jsonrpc: "2.0",
error: {
code: code,
message: message
},
id: id
} : {
result: null,
error: {
code: code,
message: message
},
id: id
};
if (data) {
response.error.data = data;
}
return JSON.stringify(response) + delimiter;
};
/**
* @static
*
*/
module.exports = {
formatRequest: formatRequest,
formatResponse: formatResponse,
formatError: formatError
};