@unireq/xml
v1.0.2
Published
XML parser policy for unireq
Readme
@unireq/xml
XML parsing and serialization built on top of fast-xml-parser. Drop-in policies for SOAP-style or mixed JSON/XML APIs.
Installation
pnpm add @unireq/xmlQuick Start
import { client } from '@unireq/core';
import { http, headers } from '@unireq/http';
import { xml } from '@unireq/xml';
const api = client(
http('https://api.example.com'),
headers({ accept: 'application/xml' }),
xml({ ignoreAttributes: false, attributeNamePrefix: '@_' }),
);
const res = await api.get('/orders/123');
console.log(res.data.order.total);Features
| Symbol | Description |
| --- | --- |
| xml(options?) | Response policy that parses XML to JS objects |
| xmlBody(data, options?) | Body descriptor for XML serialization |
| XMLParserOptions | Re-exported parser configuration types |
| XMLBuilderOptions | Re-exported builder configuration types |
Serializing Requests
import { xmlBody } from '@unireq/xml';
await api.post('/invoices', xmlBody(
{ invoice: { id: 42, total: 99.9 } },
{ format: true, indentBy: ' ' },
));JSON/XML Negotiation
import { client, either } from '@unireq/core';
import { http, parse } from '@unireq/http';
import { xml } from '@unireq/xml';
const api = client(
http('https://hybrid.example.com'),
either(
(ctx) => ctx.headers.accept?.includes('json') ?? false,
parse.json(),
xml(),
),
);Tips
- Error handling: Wrap with
retryif malformed XML is possible - Streaming: Policy buffers full body before parsing
- Namespaces: Use
ignoreNameSpace/removeNSPrefixfor SOAP
Documentation
Full documentation available at unireq.dev
License
MIT
