UNPKG

@hms-networks/kolibri-js-core

Version:

Kolibri Core library containing common kolibri models and utils

84 lines 2.66 kB
/* * 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. */ // // PRE-DEFINED ERROR CODES // // /** An error occurred on the server while parsing the JSON text. */ export var PARSE_ERROR = -32700; /** The JSON sent is not a valid Request object. */ export var INVALID_REQUEST = -32600; /** The method does not exist / is not available. */ export var METHOD_NOT_FOUND = -32601; /** Invalid method parameter(s). */ export var INVALID_PARAMS = -32602; /** Internal JSON-RPC error. */ export var INTERNAL_ERROR = -32603; // // TYPE GUARDS (for convenience) // // /** Determine if data is a properly formatted JSONRPC 2.0 ID. */ export function isJsonRpcId(input) { switch (typeof input) { case 'string': return true; case 'number': return input % 1 !== 0; case 'object': var 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; } } export function isJsonRpcError(json) { var jsonAsError = json; return jsonAsError.code !== undefined && jsonAsError.message !== undefined; } export function isJsonRpcRequest(json) { var 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; } export function isJsonRpcSuccess(response) { return response.result !== undefined; } export function isJsonRpcFailure(response) { var responseAsFailure = response; return responseAsFailure.error !== undefined && isJsonRpcError(responseAsFailure.error); } //# sourceMappingURL=jsonrpc.js.map