@a2alite/sdk
Version:
A Modular SDK (Server & Client) for Agent to Agent (A2A) protocol, with easy task lifecycle management
28 lines (27 loc) • 961 B
JavaScript
import { jsonParseError, invalidRequestError } from "../../utils/errors.js";
import { JSONRPCRequestSchema, } from "../../types/types.js";
/**
* Helper to read and parse JSON body from Node.js IncomingMessage
*/
async function jsonRpcBodyParser(body) {
if (!body) {
return jsonParseError("Empty request body");
}
try {
let json = JSON.parse(body);
// parse the JSON body into a JSONRPCRequest
let jsonRpcRequest = JSONRPCRequestSchema.safeParse(json);
if (jsonRpcRequest.success) {
// If validation succeeds, return the parsed JSON-RPC request
return jsonRpcRequest.data;
}
else {
// If validation fails, return a JSONParseError with the validation issues
return invalidRequestError("Invalid JSON-RPC request");
}
}
catch (err) {
return jsonParseError("Invalid json in the body");
}
}
export { jsonRpcBodyParser };