@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
71 lines (70 loc) • 4.22 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 { ChildElementMixin, ChildElementOptions } from './mixins/child-element.mixin';
import { ParsedElementType } from './utilities';
export interface ElementChildParserOptions extends ChildElementOptions {
}
export interface ElementChildParser<T extends ParsedElement, Child extends ParsedElement> extends ChildElementMixin<Child> {
}
export declare class ElementChildParser<T extends ParsedElement, Child extends ParsedElement> implements ElementParser<T> {
readonly propertyKey: string;
readonly elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>);
readonly options: ElementChildParserOptions;
constructor(propertyKey: string, elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>), options: ElementChildParserOptions);
parse(xmlParser: XmlParserService, element: RxapElement, parsedElement: T): T;
private findChildElementType;
}
export interface ElementChildSerializerOptions extends ChildElementOptions {
}
export interface ElementChildSerializer<T extends ParsedElement, Child extends ParsedElement> extends ChildElementMixin<Child> {
}
export declare class ElementChildSerializer<T extends ParsedElement, Child extends ParsedElement> implements ElementSerializer<T> {
readonly propertyKey: string;
readonly elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>);
readonly options: ElementChildParserOptions;
constructor(propertyKey: string, elementTypeOrFunction: ParsedElementType<Child> | (() => ParsedElementType<Child>), options: ElementChildParserOptions);
serialize(xmlParser: XmlSerializerService, element: RxapElement, parsedElement: T): void;
}
/**
* Decorator factory that creates a decorator to parse and validate a child element of a specified type.
*
* This decorator can be applied to properties within a class to automatically handle the parsing
* and validation of child elements based on the specified `elementType`. It uses the `ElementChildParser`
* to configure the parsing process according to the provided options and any existing metadata associated
* with the property.
*
* @param elementType - The constructor of the child element type that this parser should target. This
* helps in ensuring that the parsed data conforms to the specified type.
* @param options - An optional `ElementChildOptions` object that provides additional configuration
* settings for the parser. These options are merged with any existing metadata options
* defined on the property.
* Default is an empty object.
*
* @returns A decorator function that takes a target object and a property key. The decorator function
* enhances the target object's property with parsing capabilities, ensuring that the property
* adheres to the defined child element type and options.
*
* Usage:
* - `elementType`: Pass the class (constructor function) that the property is expected to be an instance of.
* - `options`: Configuration options such as `required` which, if set to true, will enforce that the property
* must not be undefined or null after parsing.
*
* Example:
* ```typescript
* class MyComponent {
* @ElementChild(SomeChildElement, { required: true })
* child: SomeChildElement;
* }
* ```
*
* Note:
* - The decorator will automatically merge any runtime metadata options associated with the property,
* allowing for flexible and powerful configuration that can be adjusted without modifying the source code.
* - If the `required` option is set to true, the property will also be decorated with a `RequiredProperty`
* decorator to enforce its presence.
*/
export declare function ElementChild<Child extends ParsedElement>(elementType: ParsedElementType<Child> | (() => ParsedElementType<Child>), options?: ElementChildParserOptions & ElementChildSerializerOptions): (target: any, propertyKey: string) => void;