UNPKG

@a2alite/sdk

Version:

A Modular SDK (Server & Client) for Agent to Agent (A2A) protocol, with easy task lifecycle management

30 lines (29 loc) 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.jsonRpcBodyParser = jsonRpcBodyParser; const errors_ts_1 = require("../../utils/errors.js"); const types_ts_1 = require("../../types/types.js"); /** * Helper to read and parse JSON body from Node.js IncomingMessage */ async function jsonRpcBodyParser(body) { if (!body) { return (0, errors_ts_1.jsonParseError)("Empty request body"); } try { let json = JSON.parse(body); // parse the JSON body into a JSONRPCRequest let jsonRpcRequest = types_ts_1.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 (0, errors_ts_1.invalidRequestError)("Invalid JSON-RPC request"); } } catch (err) { return (0, errors_ts_1.jsonParseError)("Invalid json in the body"); } }