2025-04-19 15:38:48 +08:00

48 lines
1.3 KiB
JavaScript

import { getError, getErrorByCode, isReservedErrorCode } from "./error";
import { INTERNAL_ERROR, SERVER_ERROR } from "./constants";
export function payloadId(entropy = 3) {
const date = Date.now() * Math.pow(10, entropy);
const extra = Math.floor(Math.random() * Math.pow(10, entropy));
return date + extra;
}
export function getBigIntRpcId(entropy = 6) {
return BigInt(payloadId(entropy));
}
export function formatJsonRpcRequest(method, params, id) {
return {
id: id || payloadId(),
jsonrpc: "2.0",
method,
params,
};
}
export function formatJsonRpcResult(id, result) {
return {
id,
jsonrpc: "2.0",
result,
};
}
export function formatJsonRpcError(id, error, data) {
return {
id,
jsonrpc: "2.0",
error: formatErrorMessage(error, data),
};
}
export function formatErrorMessage(error, data) {
if (typeof error === "undefined") {
return getError(INTERNAL_ERROR);
}
if (typeof error === "string") {
error = Object.assign(Object.assign({}, getError(SERVER_ERROR)), { message: error });
}
if (typeof data !== "undefined") {
error.data = data;
}
if (isReservedErrorCode(error.code)) {
error = getErrorByCode(error.code);
}
return error;
}
//# sourceMappingURL=format.js.map