Skip to content

Latest commit

 

History

History
338 lines (220 loc) · 6.64 KB

README.md

File metadata and controls

338 lines (220 loc) · 6.64 KB

xml-class-transformer

xml-class-transformer

Table of contents

Interfaces

Type Aliases

Functions

Type Aliases

XmlClass

Ƭ XmlClass: () => any

Type declaration

new XmlClass(): any

The XML class's constructor should not require any arguments. This is because the xml-class-transformer needs to be able to construct them when it needs to. And if the constructor relies on the arguments then it will crash.

Note that it is okay and even recommended to give your classes a constructor like this:

class SomeXmlElement {
  ...
  constructor(seed?: SomeXmlElement) {
    Object.assign(this, seed || {})
  }
}

note that the seed argument is optional. Such a constructor gives you a way to create instances with passed values and also enable the library to construct them without passing any arguments.

Returns

any

Defined in

src/types.ts:22


XmlPrimitiveType

Ƭ XmlPrimitiveType: typeof String | typeof Number | typeof Boolean | typeof BigInt

Defined in

src/types.ts:26


XmlType

Ƭ XmlType: XmlPrimitiveType | XmlClass

Defined in

src/types.ts:32

Functions

XmlAttribute

XmlAttribute(opts): PropertyDecorator

Class property decorator. For more details on options see XmlAttributeOptions

Example

// a basic example
class SomeXmlElement {
  @XmlAttribute({ name: 'attributeName', type: () => String })
  attributeName: string;
}

Parameters

Name Type
opts XmlAttributeOptions

Returns

PropertyDecorator

Defined in

src/decorators.ts:79


XmlChardata

XmlChardata(opts): PropertyDecorator

The property will be parsed and serialized as a character data. The "type" parameter can only be a primitive type: String, Number, Boolean.

\@XmlElem({ name: 'Comment' })
class Comment {
  \@XmlChardata({ type: () => String })
  text: string;

  \@XmlAttribute({ type: () => String, name: 'lang' })
  language: string;

  constructor(d?: Comment) {
    Object.assign(this, d || {});
  }
}

classToXml(
  new Comment({
    text: 'This is awesome',
    language: 'en',
  }),
)

Output:

<Comment lang="en">This is awesome</Comment>

Parameters

Name Type
opts XmlChardataOptions

Returns

PropertyDecorator

Defined in

src/decorators.ts:162


XmlChildElem

XmlChildElem(opts): PropertyDecorator

Class property decorator.

Example

class SomeElement {
  @XmlChildElem({ type: () => String })
  stringElem: string;

  @XmlChildElem({ name: 'someOtherName', type: () => Number })
  numberElem: string;

  @XmlChildElem({ type: () => NestedElem })
  nestedElem: NestedElem;
}

Parameters

Name Type
opts XmlChildElemOptions

Returns

PropertyDecorator

Defined in

src/decorators.ts:64


XmlComments

XmlComments(): PropertyDecorator

This decorator, when used on a class property, collects all the comments from the provided XML, turns them into an array of strings and puts them into that property. And vice-versa: at serialization that array of strings gets serialized to set of comments in the resulting XML.

Example

class SomeElement {
  \@XmlComments()
  comments?: string[];
}

classToXml(
  new SomeElement({
    comments: ['some comment', 'some other comment']
  })
)

Output:

<SomeElement>
  <!-- some comment -->
  <!-- some other comment -->
</SomeElement>

Returns

PropertyDecorator

Defined in

src/decorators.ts:115


XmlElem

XmlElem(opts?): ClassDecorator

A class decorator. It can be omitted, but only if at least one Xml* property decorator is used on it's properties.

Example

@XmlElem()
class EmptyXmlElement {}

@XmlElem({ name: 'some-xml-element' })
class SomeXmlElement {
  @XmlChildElem()
  child: string;
}

@XmlElem({ xmlns: 'http://s3.amazonaws.com/doc/2006-03-01/' })
class SomeXmlElement {
  @XmlChildElem()
  child: string;
}

Parameters

Name Type
opts? XmlElemOptions

Returns

ClassDecorator

Defined in

src/decorators.ts:31


classToXml

classToXml(entity, options?): string

Parameters

Name Type
entity any
options? ClassToXmlOptions

Returns

string

Defined in

src/transform-class-to-xml.ts:6


xmlToClass

xmlToClass<T>(xml, _class): InstanceType<T>

Type parameters

Name Type
T extends XmlClass

Parameters

Name Type
xml string
_class T

Returns

InstanceType<T>

Defined in

src/transform-xml-to-class.ts:6