UNPKG

@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

60 lines (59 loc) 3.84 kB
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 { ChildElementOptions, ChildElementMixin } from './mixins/child-element.mixin'; import { ChildrenElementOptions, ChildrenElementMixin } from './mixins/children-element.mixin'; import { ParsedElementType } from './utilities'; export interface ElementChildrenParserOptions extends ChildElementOptions, ChildrenElementOptions { } export interface ElementChildrenParser<T extends ParsedElement, Child extends ParsedElement> extends ChildElementMixin<Child>, ChildrenElementMixin { } export type ElementWithType<Child extends ParsedElement> = { element: RxapElement; type: ParsedElementType<Child> | null; }; export declare class ElementChildrenParser<T extends ParsedElement, Child extends ParsedElement> implements ElementParser<T> { readonly propertyKey: string; readonly elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>) | null; readonly options: ElementChildrenParserOptions; constructor(propertyKey: string, elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>) | null, options: ElementChildrenParserOptions); parse(xmlParser: XmlParserService, element: RxapElement, parsedElement: T): T; private attachType; private getExtendedTypes; } export interface ElementChildrenSerializerOptions extends ChildElementOptions, ChildrenElementOptions { } export interface ElementChildrenSerializer<T extends ParsedElement, Child extends ParsedElement> extends ChildElementMixin<Child>, ChildrenElementMixin { } export declare class ElementChildrenSerializer<T extends ParsedElement, Child extends ParsedElement> implements ElementSerializer<T> { readonly propertyKey: string; readonly elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>) | null; readonly options: ElementChildrenParserOptions; constructor(propertyKey: string, elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>) | null, options: ElementChildrenParserOptions); serialize(xmlParser: XmlSerializerService, element: RxapElement, parsedElement: T): void; } /** * Decorator factory that creates a decorator to parse children elements of a specified type from a parent element. * This decorator can be applied to properties within a class to automatically handle the parsing of child elements * based on the specified element type and options. * * @param elementTyp - The type of the child elements to parse. If null, it will parse children without type checking. * @param options - Configuration options for parsing the children elements. Default is an empty object. * Options can include custom parsing rules like filtering or transformations. * * @returns A decorator function that can be applied to a property within a class. This decorator will configure * the property to automatically parse and assign children elements of the specified type. * * Example Usage: * ``` * @ElementChildren(SomeChildElement, { required: true }) * public children: SomeChildElement[]; * ``` * * The decorator modifies the target class's metadata to include a parser for the specified property. If the `required` * option is set to true, it also ensures that the property is marked as required. */ export declare function ElementChildren<Child extends ParsedElement>(elementTyp?: ParsedElementType<Child> | (() => ParsedElementType<Child>) | null, options?: ElementChildrenParserOptions & ElementChildrenSerializerOptions): (target: any, propertyKey: string) => void;