@hms-networks/kolibri-js-core
Version:
Kolibri Core library containing common kolibri models and utils
92 lines • 3.17 kB
JavaScript
;
/*
* Copyright 2021 HMS Industrial Networks AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: //www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.isJsonRpcFailure = exports.isJsonRpcSuccess = exports.isJsonRpcRequest = exports.isJsonRpcError = exports.isJsonRpcId = exports.INTERNAL_ERROR = exports.INVALID_PARAMS = exports.METHOD_NOT_FOUND = exports.INVALID_REQUEST = exports.PARSE_ERROR = void 0;
//
// PRE-DEFINED ERROR CODES
//
//
/** An error occurred on the server while parsing the JSON text. */
exports.PARSE_ERROR = -32700;
/** The JSON sent is not a valid Request object. */
exports.INVALID_REQUEST = -32600;
/** The method does not exist / is not available. */
exports.METHOD_NOT_FOUND = -32601;
/** Invalid method parameter(s). */
exports.INVALID_PARAMS = -32602;
/** Internal JSON-RPC error. */
exports.INTERNAL_ERROR = -32603;
//
// TYPE GUARDS (for convenience)
//
//
/** Determine if data is a properly formatted JSONRPC 2.0 ID. */
function isJsonRpcId(input) {
switch (typeof input) {
case 'string':
return true;
case 'number':
return input % 1 !== 0;
case 'object':
let isNull = input === null;
if (isNull) {
console.warn('Use of null ID in JSONRPC 2.0 is discouraged.');
return true;
}
else {
return false;
}
default:
return false;
}
}
exports.isJsonRpcId = isJsonRpcId;
function isJsonRpcError(json) {
const jsonAsError = json;
return jsonAsError.code !== undefined && jsonAsError.message !== undefined;
}
exports.isJsonRpcError = isJsonRpcError;
function isJsonRpcRequest(json) {
const request = json;
if (!request || typeof request !== 'object') {
return false;
}
if (!request.jsonrpc || request.jsonrpc !== '2.0') {
return false;
}
if (!request.method || typeof request.method !== 'string') {
return false;
}
if (request.params && typeof request.params !== 'object') {
return false;
}
if (!request.id || (typeof request.id !== 'string' && typeof request.id !== 'number')) {
return false;
}
return true;
}
exports.isJsonRpcRequest = isJsonRpcRequest;
function isJsonRpcSuccess(response) {
return response.result !== undefined;
}
exports.isJsonRpcSuccess = isJsonRpcSuccess;
function isJsonRpcFailure(response) {
const responseAsFailure = response;
return responseAsFailure.error !== undefined && isJsonRpcError(responseAsFailure.error);
}
exports.isJsonRpcFailure = isJsonRpcFailure;
//# sourceMappingURL=jsonrpc.js.map