Typed XML codecs for Baichuan parameters.

Every Baichuan body is a <body> holding one element per parameter, such as <RtspPort version="1.1">. A parameter is described once, with field codecs, and that description both builds request XML and reads reply XML:

xmlParam("RtspPort", { rtspPort: int(), enable: int() })
  encode  { rtspPort: 554, enable: 1 }  ->  <RtspPort version="1.1"><rtspPort>554</rtspPort>...
  decode  <RtspPort>...</RtspPort>      ->  { rtspPort: 554, enable: 1 }

Field codecs: int, uint, u64, float, text, oneOf, obj, list, repeated and optional. Fields are written in declaration order. Reading ignores elements the description does not name and throws on a missing field that is not optional.

Examples

Describe a parameter and round-trip it

import { assertEquals, assertStringIncludes } from "@std/assert";
import { parse } from "@std/xml";
import { int, xmlParam } from "@hertzg/reolink-api/protocol/xml";

const rtspPort = xmlParam("RtspPort", { rtspPort: int(), enable: int() });

const xml = rtspPort.encode({ rtspPort: 554, enable: 1 });
assertStringIncludes(xml, "<rtspPort>554</rtspPort>");
assertEquals(rtspPort.decode(parse(xml).root), { rtspPort: 554, enable: 1 });

Functions

f
float(): XmlField<number>

A floating point field.

f
int(): XmlField<number>

A signed integer field, such as <channelId>0</channelId>.

f
list<T>(
itemName: string,
item: XmlField<T>
): XmlField<T[]>

A wrapper element holding repeated items, such as <presetList><preset>…</preset><preset>…</preset></presetList>.

f
oneOf<V extends string>(...values: V[]): XmlField<V>

A text field limited to known values, such as <parity>none</parity>.

f
optional<T>(field: XmlField<T>): XmlField<T> & { optional: true; }

Marks a field as possibly missing: it reads as absent instead of throwing, and is not written when absent.

f
repeated<T>(item: XmlField<T>): XmlField<T[]>

Elements repeated directly under the parent, such as several <AlarmEvent> inside <AlarmEventList>. Missing elements read as [].

f
text(): XmlField<string>

A text field, such as <name>Front door</name>. XML entities are escaped on write and unescaped on read.

f
u64(): XmlField<bigint>

An unsigned 64-bit integer field, read as a bigint so no digit is lost.

f
uint(): XmlField<number>

An unsigned 32-bit integer field.

Type Aliases

T
AnyXmlParam = XmlParam<string, any>

Any XmlParam, whatever its value. any for the same variance reason as XmlFields.

T
XmlField<T> = { encode: (
name: string,
value: T
) => string
; decode: (
name: string,
parent: XmlElement
) => T
; optional?: boolean; }

Reads and writes one XML field: an element's text, a nested element, or a run of repeated elements.

T
XmlFields = Record<string, XmlField<any>>

A record of named field codecs, as passed to obj and xmlParam.

T
XmlParam<N extends string, T> = { name: N; fields: XmlFields; encode: (value: T) => string; decode: (element: XmlElement) => T; }

A Baichuan parameter: a named top-level element of <body>.