@shko.online/dataverse-odata
Version:
This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you can build some javascript library which consumes the output of this library.
52 lines (50 loc) • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getFetchXmlFromParser = void 0;
var _atMostOnce = require("./validators/atMostOnce");
var _differentFromEmptyString = require("./validators/differentFromEmptyString");
const option = 'fetchXml';
/**
* Parses the {@link ODataFetch.fetchXml fetchXml} query
* @returns Returns `false` when the parse has an error
*/
const getFetchXmlFromParser = (parser, result) => {
const value = parser.getAll(option);
if (value.length === 0) {
return true;
}
if (!(0, _atMostOnce.atMostOnce)(option, value, result) || !(0, _differentFromEmptyString.differentFromEmptyString)(value, result)) {
return false;
}
const fetchXml = value[0];
const serializer = new DOMParser();
const fetchXmlDocument = serializer.parseFromString(fetchXml, 'text/xml');
if (fetchXmlDocument.documentElement.tagName === 'parsererror') {
result.error = {
code: '0x80040201',
message: 'Invalid XML.'
};
return false;
}
const entity = fetchXmlDocument.evaluate('fetch/entity', fetchXmlDocument, null, XPathResult.ANY_TYPE, null).iterateNext();
if (fetchXmlDocument.documentElement.children.length != 1 || !entity || !entity.getAttribute('name')) {
result.error = {
code: '0x80041102',
message: 'Entity Name was not specified in FetchXml String.'
};
return false;
}
const invalidAttribute = fetchXmlDocument.evaluate('fetch/entity/*[not(self::filter or self::order or self::link-entity or self::attribute or self::all-attributes or self::no-attrs)]', fetchXmlDocument, null, XPathResult.ANY_TYPE, null).iterateNext();
if (invalidAttribute) {
result.error = {
code: '0x8004111c',
message: `Invalid Child Node, valid nodes are filter, order, link-entity, attribute, all-attributes, no-attrs. NodeName = ${invalidAttribute.tagName} NodeXml = ${invalidAttribute.outerHTML}`
};
return false;
}
result.fetchXml = fetchXmlDocument;
return true;
};
exports.getFetchXmlFromParser = getFetchXmlFromParser;