@rxap/xml-parser
Version:
Provides a set of decorators and services for parsing and serializing XML documents into TypeScript classes. It simplifies the process of mapping XML elements and attributes to class properties, handling data validation, and serializing objects back into
64 lines (63 loc) • 4.38 kB
TypeScript
import { RxapElement } from '../element';
import { ParsedElement } from '../elements/parsed-element';
import { XmlParserService } from '../xml-parser.service';
import { XmlSerializerService } from '../xml-serializer.service';
import { ElementParser } from './element.parser';
import { ElementSerializer } from './element.serializer';
import { ChildrenElementOptions, ChildrenElementMixin } from './mixins/children-element.mixin';
import { TagElementOptions, TagElementMixin } from './mixins/tag-element.mixin';
import { TextContentElementOptions, TextContentElementMixin } from './mixins/text-content-element.mixin';
export interface ElementChildrenTextContentParserOptions<Value> extends TextContentElementOptions<Value, Value[]>, TagElementOptions, ChildrenElementOptions {
}
/**
* Asserts that the provided `options` object conforms to the `ElementChildrenTextContentOptions` type.
*
* This function checks if the given `options` object meets the criteria of being an instance of `ElementChildrenTextContentOptions` by verifying the presence of a `tag` property, which is essential for identifying the object as a valid `ElementChildrenTextContentOptions`.
*
* @param options - The object to be validated against the `ElementChildrenTextContentOptions` type.
* @throws {Error} Throws an error if the `options` object does not have the required `tag` property, indicating that it is not a valid `ElementChildrenTextContentOptions`.
*
*/
export declare function AssertElementChildrenTextContentOptions(options: any): asserts options is ElementChildrenTextContentParserOptions<any>;
export interface ElementChildrenTextContentParser<T extends ParsedElement, Value> extends TextContentElementMixin<Value, Value[]>, TagElementMixin, ChildrenElementMixin {
}
export declare class ElementChildrenTextContentParser<T extends ParsedElement, Value> implements ElementParser<T> {
readonly propertyKey: string;
readonly options: ElementChildrenTextContentParserOptions<Value>;
constructor(propertyKey: string, options: ElementChildrenTextContentParserOptions<Value>);
parse(xmlParser: XmlParserService, element: RxapElement, parsedElement: T): T;
}
export interface ElementChildrenTextContentSerializerOptions<Value> extends TextContentElementOptions<Value, Value[]>, TagElementOptions, ChildrenElementOptions {
}
export interface ElementChildrenTextContentSerializer<T extends ParsedElement, Value> extends TextContentElementMixin<Value, Value[]>, TagElementMixin, ChildrenElementMixin {
}
export declare class ElementChildrenTextContentSerializer<T extends ParsedElement, Value> implements ElementSerializer<T> {
readonly propertyKey: string;
readonly options: ElementChildrenTextContentParserOptions<Value>;
constructor(propertyKey: string, options: ElementChildrenTextContentParserOptions<Value>);
serialize(xmlParser: XmlSerializerService, element: RxapElement, parsedElement: T): void;
}
/**
* Decorator factory that creates a decorator to parse the text content of children elements of a specified XML/HTML tag.
*
* This decorator can be applied to properties within classes that are intended to parse XML/HTML data. It configures
* the parsing behavior based on the provided options or defaults derived from the property name if no options are specified.
*
* @param {Partial<ElementChildrenTextContentParserOptions<Value>> | string} optionsOrString - Configuration options for the decorator,
* or a string specifying the tag name of the element whose children's text content should be parsed.
* If a string is provided, it is used as the tag name. If an object is provided, it can specify various parsing options.
* If omitted, the tag name is derived by dasherizing the property name.
*
* @returns {Function} A class property decorator that configures the parser for the specified property.
*
* ### Usage
*
* ```typescript
* @ElementChildrenTextContent({ tag: 'item', required: true })
* items: string[];
* ```
*
* In the above example, the decorator will configure the parser to parse the text content of children of `<item>` elements.
* The `required` option specifies that at least one `<item>` element must be present.
*/
export declare function ElementChildrenTextContent<Value>(optionsOrString?: Partial<ElementChildrenTextContentParserOptions<Value>> | string): (target: any, propertyKey: string) => void;