XML responses
@navios/adapter-xml builds XML responses, such as RSS and Atom feeds or sitemaps, with JSX. Despite its name it is not an HTTP adapter: it is a JSX runtime, a renderer and a plugin, and it works with the same code on Fastify and on Bun.
Setup
@navios/server of the same version is a peer dependency.
Point TypeScript's JSX transform at the package. Files that contain JSX use the .tsx extension.
These two options are enough. The JSX types, including the ones for lower-case tags such as <rss>, are exported by @navios/adapter-xml/jsx-runtime, which TypeScript loads through jsxImportSource, so no types entry is needed. A setup that does not use jsxImportSource gets the same types as a global JSX namespace from "types": ["@navios/adapter-xml/jsx"].
Then add the plugin:
xmlPlugin(options?) takes defaults for every XML response of the application:
Serving XML
Declare the endpoint with response: 'text' ('stream' and 'blob' work too) and return xml(tree, options?) from its handler:
The options of xml() are the plugin options plus status (default 200), and they win over the plugin's defaults. Other response headers and cookies are set as usual with ctx.reply.header() and ctx.reply.cookie().
What happens to the returned value:
xml()only wraps the tree: it returnsresult('xml', ...), a result of the custom response kindxml. The plugin registers a response encoder for that kind withapi.transport(httpTransport).encoder('xml', ...). The encoder renders the tree after the handler returns, with the request context, and sends the result as text with the XML content type. Routes that return anything else are not touched.- The whole document is rendered before the response starts, whatever the declared response kind, so a client never receives half a document.
- An error thrown by a component is handled like an error thrown by the handler: an
HttpExceptionbecomes its status, anything else a 500 problem response. See Errors.
xmlPlugin() is required. Without it nothing renders the value xml() returns: the request fails with an error that says no encoder is registered for the kind xml, and is answered with a 500 problem response.
Without the plugin
renderToXml returns a string, which the handler of a response: 'text' endpoint can return directly. Pass it the request context, and the container when the tree has class components:
Components
Components are not run when the JSX expression is evaluated. They run when the tree is rendered, top down, and siblings run in parallel.
Function components
A function component receives its props and, as second argument, the RequestContext of the request being served. It may be asynchronous.
Class components
A class component is registered with @Component() and implements render(ctx). The DI container builds a new instance for every place the component appears in a tree. Props are the constructor argument, field initialisers can inject() singletons, and render(ctx) receives the RequestContext.
With a props schema (any Standard Schema), the props are validated when the component is built and the parsed value is passed to the constructor:
A class can be registered without decorator syntax: Component()(LatestPosts) or Component({ schema })(Description).
Per-request data in components
Components are never request-scoped. A class component is a transient built against the root container, so inject() in it reaches singletons only, and injecting a request-scoped service fails with a scope mismatch error. Everything that belongs to the request comes from ctx:
Per-request state explains context keys, request values and ctx.use.
Tags
Lower-case JSX tags are written as they are, with any attributes. defineTag creates a tag component, which is useful for namespaced names and for checking attributes:
The schema can be any Standard Schema that validates synchronously. Invalid attributes throw when the tag is rendered, with an error naming the tag and the attribute. The attributes are written as given; the schema's output is not used.
CDATA and raw XML
Text and attribute values are escaped by the renderer. CData writes its content in a CDATA section and splits content that contains ]]> into several sections.
DangerouslyInsertRawXml writes its content without any escaping. Use it only with content you trust.
Rendering outside a request
renderToXml(tree, options?) renders a tree to a string, for a job that writes a sitemap to disk or for a test.
The options are declaration, encoding and pretty, as for the plugin, plus container, the DI container that builds class components, and ctx, the RequestContext passed to components.
It throws MissingContainerError when the tree contains a class component and no container was given, and MissingContextError when a component uses a context that was not given.