import Err from '@openaddresses/batch-error'; import type { ValidateFunction } from 'ajv'; import xmljs, { type Element } from '@tak-ps/xml-js'; /** * Core XML Document support used for XML fortatted DataPackages documents * such as Iconsets or Basemaps */ export default class XMLDocument { raw: T; constructor(raw: T) { this.raw = raw; } static check(input: string, check: ValidateFunction): U { const xml = xmljs.xml2js(input, { compact: true }) check(xml); if (check.errors) throw new Err(400, null, `${check.errors[0].message} (${check.errors[0].instancePath})`); return xml as U; } to_xml(): string { return xmljs.js2xml(this.raw as Element, { compact: true }); } }