UNPKG

@chubbyts/chubbyts-decode-encode

Version:

A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.

178 lines (177 loc) 4.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createJsonxTypeEncoder = void 0; const fast_xml_parser_1 = require("fast-xml-parser"); const throwable_to_error_1 = require("@chubbyts/chubbyts-throwable-to-error/dist/throwable-to-error"); const index_1 = require("../index.cjs"); const jsonx_datatypes_1 = require("../jsonx-datatypes.cjs"); const index_2 = require("./index.cjs"); const encodeHtmlEntities = (string) => string.replace(/[\u00A0-\u9999<>&]/g, (i) => '&#' + i.charCodeAt(0) + ';'); const createXmlNode = () => { return { '?xml': [ { '#text': '', }, ], ':@': { '@_version': '1.0', '@_encoding': 'UTF-8', }, }; }; const createJsonxAttributes = () => { return { ':@': { '@_xsi:schemaLocation': 'http://www.datapower.com/schemas/json jsonx.xsd', '@_xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', '@_xmlns:json': 'http://www.ibm.com/xmlns/prod/2009/jsonx', }, }; }; const createNullNode = (key = undefined) => { const node = { [jsonx_datatypes_1.DATATYPE_NULL]: [], }; if (key === undefined) { return node; } return { ...node, ':@': { '@_name': key, }, }; }; const createBooleanNode = (value, key = undefined) => { const node = { [jsonx_datatypes_1.DATATYPE_BOOLEAN]: [ { '#text': value, }, ], }; if (key === undefined) { return node; } return { ...node, ':@': { '@_name': key, }, }; }; const createNumberNode = (value, key = undefined) => { const node = { [jsonx_datatypes_1.DATATYPE_NUMBER]: [ { '#text': value, }, ], }; if (key === undefined) { return node; } return { ...node, ':@': { '@_name': key, }, }; }; const createStringNode = (value, key = undefined) => { const node = { [jsonx_datatypes_1.DATATYPE_STRING]: [ { '#text': value === value.trim() ? encodeHtmlEntities(value) : `<![CDATA[${encodeHtmlEntities(value)}]]>`, }, ], }; if (key === undefined) { return node; } return { ...node, ':@': { '@_name': key, }, }; }; const createNode = (value, key = undefined) => { if ((0, index_1.isObject)(value)) { return createObjectNode(value, key); } else if ((0, index_1.isArray)(value)) { return createArrayNode(value, key); } else if ((0, index_1.isString)(value)) { return createStringNode(value, key); } else if ((0, index_1.isNumber)(value)) { return createNumberNode(value, key); } else if ((0, index_1.isBoolean)(value)) { return createBooleanNode(value, key); } else if ((0, index_1.isNull)(value)) { return createNullNode(key); } throw new Error(`Unsupported value: ${JSON.stringify(value)}`); }; const createArrayNode = (value, key = undefined) => { const elements = value.map((subValue) => createNode(subValue)); const node = { [jsonx_datatypes_1.DATATYPE_ARRAY]: elements, }; if (key === undefined) { return node; } return { ...node, ':@': { '@_name': key, }, }; }; const createObjectNode = (value, key = undefined) => { const elements = Object.entries(value).map(([subKey, subValue]) => createNode(subValue, subKey)); const node = { [jsonx_datatypes_1.DATATYPE_OBJECT]: elements, }; if (key === undefined) { return node; } return { ...node, ':@': { '@_name': key, }, }; }; const createJsonxTypeEncoder = (prettyPrint = false) => { const builder = new fast_xml_parser_1.XMLBuilder({ preserveOrder: true, ignoreAttributes: false, processEntities: false, format: prettyPrint, }); return { encode: (data) => { try { return builder.build([ createXmlNode(), { ...createNode(data), ...createJsonxAttributes(), }, ]); } catch (e) { const error = (0, throwable_to_error_1.throwableToError)(e); throw new index_2.EncodeError(error.message, error.stack); } }, contentType: 'application/jsonx+xml', }; }; exports.createJsonxTypeEncoder = createJsonxTypeEncoder;