serverless-spy
Version:
CDK-based library for writing elegant integration tests on AWS serverless architecture and an additional web console to monitor events in real time.
58 lines (57 loc) • 1.98 kB
JavaScript
import { getValueFromTextNode } from "@smithy/smithy-client";
import { XMLParser } from "fast-xml-parser";
import { collectBodyString } from "../common";
export const parseXmlBody = (streamBody, context) => collectBodyString(streamBody, context).then((encoded) => {
if (encoded.length) {
const parser = new XMLParser({
attributeNamePrefix: "",
htmlEntities: true,
ignoreAttributes: false,
ignoreDeclaration: true,
parseTagValue: false,
trimValues: false,
tagValueProcessor: (_, val) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
});
parser.addEntity("#xD", "\r");
parser.addEntity("#10", "\n");
let parsedObj;
try {
parsedObj = parser.parse(encoded, true);
}
catch (e) {
if (e && typeof e === "object") {
Object.defineProperty(e, "$responseBodyText", {
value: encoded,
});
}
throw e;
}
const textNodeName = "#text";
const key = Object.keys(parsedObj)[0];
const parsedObjToReturn = parsedObj[key];
if (parsedObjToReturn[textNodeName]) {
parsedObjToReturn[key] = parsedObjToReturn[textNodeName];
delete parsedObjToReturn[textNodeName];
}
return getValueFromTextNode(parsedObjToReturn);
}
return {};
});
export const parseXmlErrorBody = async (errorBody, context) => {
const value = await parseXmlBody(errorBody, context);
if (value.Error) {
value.Error.message = value.Error.message ?? value.Error.Message;
}
return value;
};
export const loadRestXmlErrorCode = (output, data) => {
if (data?.Error?.Code !== undefined) {
return data.Error.Code;
}
if (data?.Code !== undefined) {
return data.Code;
}
if (output.statusCode == 404) {
return "NotFound";
}
};