Format XML responses

This commit is contained in:
Gregory Schier
2024-02-22 01:00:02 -08:00
parent ca4d9b1ad6
commit 321b013ce6
5 changed files with 40 additions and 6 deletions

View File

@@ -1,3 +1,5 @@
import xmlFormat from 'xml-formatter';
export function tryFormatJson(text: string, pretty = true): string {
try {
if (pretty) return JSON.stringify(JSON.parse(text), null, 2);
@@ -6,3 +8,11 @@ export function tryFormatJson(text: string, pretty = true): string {
return text;
}
}
export function tryFormatXml(text: string): string {
try {
return xmlFormat(text, { throwOnFailure: true, strictMode: false });
} catch (_) {
return text;
}
}