@foxglove/xmlrpc
Version:
TypeScript library implementing an XMLRPC client and server with pluggable server backend
68 lines • 2.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.XmlRpcClient = void 0;
const just_fetch_1 = __importDefault(require("@foxglove/just-fetch"));
const Deserializer_1 = require("./Deserializer");
const Serializer_1 = require("./Serializer");
const XmlRpcFault_1 = require("./XmlRpcFault");
// A client for making XML-RPC method calls over HTTP(S)
class XmlRpcClient {
constructor(url, options) {
this.headers = {
"Content-Type": "text/xml",
Accept: "text/xml",
};
this.url = url;
this.encoding = options?.encoding;
if (options?.headers != undefined) {
this.headers = { ...this.headers, ...options.headers };
}
}
// Make an XML-RPC call to the server and return the response
async methodCall(method, params) {
const body = (0, Serializer_1.serializeMethodCall)(method, params, this.encoding);
const headers = this.headers;
let res;
try {
res = await (0, just_fetch_1.default)(this.url, { method: "POST", headers, body });
}
catch (err) {
if (err.message === "Failed to fetch") {
throw new Error(`XML-RPC call "${method}" to ${this.url} failed to connect`);
}
throw err;
}
if (!res.ok) {
throw new Error(`XML-RPC call "${method}" to ${this.url} returned ${res.status}: "${res.statusText}"`);
}
const resText = await res.text();
const deserializer = new Deserializer_1.Deserializer(this.encoding);
return await deserializer.deserializeMethodResponse(resText);
}
async multiMethodCall(requests) {
const res = await this.methodCall("system.multicall", [requests]);
if (!Array.isArray(res) || res.length !== requests.length) {
throw new Error(`malformed system.multicall response`);
}
const output = [];
const createFault = (fault = {}) => {
const faultString = typeof fault.faultString === "string" ? fault.faultString : undefined;
const faultCode = typeof fault.faultCode === "number" ? fault.faultCode : undefined;
return new XmlRpcFault_1.XmlRpcFault(faultString, faultCode);
};
for (const entry of res) {
if (!Array.isArray(entry) || entry.length !== 1) {
output.push(createFault(entry));
}
else {
output.push(entry[0]);
}
}
return output;
}
}
exports.XmlRpcClient = XmlRpcClient;
//# sourceMappingURL=XmlRpcClient.js.map