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.
Describe a parameter and round-trip it
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 });
A floating point field.
A signed integer field, such as <channelId>0</channelId>.
A wrapper element holding repeated items, such as
<presetList><preset>…</preset><preset>…</preset></presetList>.
A nested element with fields of its own.
A text field limited to known values, such as <parity>none</parity>.
Marks a field as possibly missing: it reads as absent instead of throwing, and is not written when absent.
Elements repeated directly under the parent, such as several
<AlarmEvent> inside <AlarmEventList>. Missing elements read as [].
A text field, such as <name>Front door</name>. XML entities are escaped
on write and unescaped on read.
An unsigned 64-bit integer field, read as a bigint so no digit is lost.
An unsigned 32-bit integer field.
Describes a parameter element and its fields.
The keys of F whose codecs are marked optional.
Reads and writes one XML field: an element's text, a nested element, or a run of repeated elements.
-
decode: () => Tname: string,parent: XmlElement
Reads the value from the children of
parentnamedname. -
encode: () => stringname: string,value: T
Writes
valueas elements namedname. -
optional: boolean
truewhen the field may be missing; set by optional.
& [K in OptionalKeys<F>]?: XmlFieldValue<F[K]>
The decoded object for a record of field codecs; optional fields may be absent.
The decoded value of an XmlField.
A Baichuan parameter: a named top-level element of <body>.
-
decode: (element: XmlElement) => T
Reads the element's fields.
-
encode: (value: T) => string
Writes the element, with
version="1.1". -
fields: XmlFields
The field codecs, in the order they are written.
-
name: N
The element name.
The decoded value of an XmlParam.
Usage
import * as mod from "protocol/xml.ts";