1.1.12 • Published 4 days ago

fastify-json-to-xml v1.1.12

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days ago

fastify-json-to-xml

GitHub release npm version Build status Coverage status code style: Prettier

Fastify plugin to serialise JSON responses into XML

Overview

The fastify-json-to-xml plugin adds an onSend hook that supports serialising 'application/json' responses into XML if:

  • The Accept HTTP request header only includes 'application/xml'
  • The Accept HTTP request header explicitly includes the 'application/xml' media type before 'application/json'

Installation

Install using npm:

npm i fastify-json-to-xml

Example usage

const Fastify = require("fastify");
const jsonToXml = require("fastify-json-to-xml");

const server = Fastify();
server.register(jsonToXml);

server.get("/", (_req, res) => {
	res.send({ example: "I'm an example value!" });
});

server.listen(3000, (err) => {
	if (err) throw err;
	console.log("Server listening on 3000");
});

Make an HTTP GET request to the route above, with application/xml in the accept HTTP request header, and the results will look like so:

<?xml version="1.0" encoding="UTF-8"?><response><example>I'm an example value!</example></response>

Replacing invalid XML characters in output

By default, this plugin will throw an error if the JSON response it is transforming has characters that XML considers invalid, such as "$". Set replaceInvalidChars: true in the plugin options and they will be replaced with the Unicode replacement character (U+FFFD) instead, and the plugin will not throw an error.

For example:

{ "$test-key": "test-value" }

Will become:

<?xml version="1.0" encoding="UTF-8"?><response><�test-key>test-value</�test-key></response>'

Contributing

Contributions are welcome, and any help is greatly appreciated!

See the contributing guide for details on how to get started. Please adhere to this project's Code of Conduct when contributing.

Acknowledgements

License

fastify-json-to-xml is licensed under the MIT license.

1.1.12

4 days ago

1.1.11

3 months ago

1.1.9

5 months ago

1.1.10

4 months ago

1.1.8

6 months ago

1.1.7

8 months ago

1.1.6

9 months ago

1.1.5

10 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

1 year ago

1.1.1

1 year ago

1.0.2

1 year ago

1.1.0

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago