1 | <!DOCTYPE html>
|
2 | <html lang="en">
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <title>JSDoc: Source: adapters/xml_content.js</title>
|
6 |
|
7 | <script src="scripts/prettify/prettify.js"> </script>
|
8 | <script src="scripts/prettify/lang-css.js"> </script>
|
9 | |
10 |
|
11 |
|
12 | <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
13 | <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
14 | </head>
|
15 |
|
16 | <body>
|
17 |
|
18 | <div id="main">
|
19 |
|
20 | <h1 class="page-title">Source: adapters/xml_content.js</h1>
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | <section>
|
28 | <article>
|
29 | <pre class="prettyprint source linenums"><code>'use strict';
|
30 | const path = require('path');
|
31 | const convert = require('js2xmlparser').parse;
|
32 | const JSON_Adapter = require(path.join(__dirname, './json_content'));
|
33 |
|
34 | /**
|
35 | * Creates a formatted success message and converts to XML
|
36 | * @param {*} data Any data to be included in XML response
|
37 | * @param {Object} options Configurable options for XML response generation
|
38 | * @param {Boolean} [options.skip_conversion=this.skip_conversion] If true rendering is skipped and the data argument is immediately returned
|
39 | * @param {string} [options.xml_root=this.xml_root] Defines the value of the XML document root tag. If this.xml_root is not set and options.xml_root is not provided a TypeError will be thrown
|
40 | * @param {Object} [options.xml_configuration=this.xml_configuration] Options for the convert function see js2xmlparser documentation for full details
|
41 | * @return {string} Returns the rendered XML string or the data argument if options.skip_conversion is true
|
42 | */
|
43 | const _RENDER = function (data, options) {
|
44 | let skip_conversion = (typeof options.skip_conversion === 'boolean') ? options.skip_conversion : this.skip_conversion;
|
45 | if (skip_conversion) return data;
|
46 | let xml_root = options.xml_root || this.xml_root;
|
47 | if (typeof xml_root !== 'string') throw new TypeError('xml_root must be a string, please provide options.xml_root or set this.xml_root');
|
48 | let xml_configuration = options.xml_configuration || this.xml_configuration;
|
49 | let xml_data = {
|
50 | result: 'success',
|
51 | status: 200,
|
52 | data
|
53 | };
|
54 | return convert(xml_root, xml_data, (xml_configuration && typeof xml_configuration === 'object') ? xml_configuration : undefined);
|
55 | };
|
56 |
|
57 | /**
|
58 | * Creates a formatted error response message and converts to XML
|
59 | * @param {*} err Error data to be included in error response. If err is an instanceof Error or has a .message property it is assumed that the .message property is the only thing to include in error response
|
60 | * @param {Object} options Configurable options for error XML formatting
|
61 | * @param {Boolean} [options.skip_conversion=this.skip_conversion] If true rendering is skipped and the data argument is immediately returned
|
62 | * @param {string} [options.xml_root=this.xml_root] Defines the value of the XML document root tag. If this.xml_root is not set and options.xml_root is not provided a TypeError will be thrown
|
63 | * @param {Object} options.xml_configuration Options for the convert function see js2xmlparser documentation for full details
|
64 | * @return {string} Returns the rendered XML string or the err argument if options.skip_conversion is true
|
65 | */
|
66 | const _ERROR = function (err, options) {
|
67 | let skip_conversion = (typeof options.skip_conversion === 'boolean') ? options.skip_conversion : this.skip_conversion;
|
68 | if (skip_conversion) return err;
|
69 | let xml_root = options.xml_root || this.xml_root;
|
70 | if (typeof xml_root !== 'string') throw new TypeError('xml_root must be a string, please provide options.xml_root or set this.xml_root');
|
71 | let xml_configuration = options.xml_configuration || this.xml_configuration;
|
72 | let xml_data = {
|
73 | result: 'error',
|
74 | status: 500,
|
75 | data: {
|
76 | error: (err instanceof Error || typeof err.message === 'string') ? err.message : err
|
77 | }
|
78 | };
|
79 | return convert(xml_root, xml_data, (xml_configuration && typeof xml_configuration === 'object') ? xml_configuration : undefined);
|
80 | };
|
81 |
|
82 | /**
|
83 | * XML response adapter class which handles wrapping response data in object with status code and result message and converting into XML string.
|
84 | * @type {XML_Adapter}
|
85 | * @extends {JSON_Adapter}
|
86 | */
|
87 | const XML_ADAPTER = class XML_Adapter extends JSON_Adapter {
|
88 | /**
|
89 | * @constructor
|
90 | * @param {Object} [options={}] Configurable options for XML adapter
|
91 | * @param {Boolean} options.skip_conversion If true render will just return data that is passed to it
|
92 | * @param {string} options.xml_root The value that should be used in generating the root tag of the converted XML document
|
93 | * @param {Object} options.xml_configuration Options that will be passed to the XML conversion function
|
94 | */
|
95 | constructor (options = {}) {
|
96 | super(options);
|
97 | this.skip_conversion = options.skip_conversion;
|
98 | this.xml_root = options.xml_root;
|
99 | this.xml_configuration = options.xml_configuration;
|
100 | }
|
101 | /**
|
102 | * Creates a formatted XML response
|
103 | * @param {*} data Any data that should be sent with the success response
|
104 | * @param {Object} [options={}] Configurable options for the XML success response formatting see _RENDER for more details
|
105 | * @param {Function} [options.formatRender=_RENDER] Custom formatting function for XML rendering. It is recommended that the default value for this property is used and only custom options for the XML rendering are passed
|
106 | * @param {Function} [cb=false] Optional callback function. If argument is not a function it will be ignored
|
107 | * @param {Object} [options.req] Express request object. If options.req and options.res are defined the express .render method will be used to render template
|
108 | * @param {Object} [options.res] Express response object. If options.res and options.req are defined the express .render method will be used to render template
|
109 | * @param {Boolean} [options.skip_response] If true function will resolve with the rendered template instead of sending a response
|
110 | * @return {*} Returns the formatted XML string if options.sync is true or a Promise if cb arugement is not passed
|
111 | */
|
112 | render (data, options = {}, cb = false) {
|
113 | if (typeof options === 'function') {
|
114 | cb = options;
|
115 | options = {};
|
116 | }
|
117 | options.formatRender = (typeof options.formatRender === 'function') ? options.formatRender : _RENDER.bind(this);
|
118 | return super.render(data, options, cb);
|
119 | }
|
120 | /**
|
121 | * Creates a formatted XML error response
|
122 | * @param {*} [err={}] Any data that should be sent with the error response
|
123 | * @param {Object} [options={}] Configurable options for the XML error response formatting see _ERROR for more details
|
124 | * @param {Function} [options.formatError=_ERROR] Custom formatting function for XML rendering. It is recommended that the default value for this property is used and only custom options for the XML rendering are passed
|
125 | * @param {Object} [options.req] Express request object. If options.req and options.res are defined the express .render method will be used to render template
|
126 | * @param {Object} [options.res] Express response object. If options.res and options.req are defined the express .render method will be used to render template
|
127 | * @param {Boolean} [options.skip_response] If true function will resolve with the rendered template instead of sending a response
|
128 | * @param {Function} [cb=false] Optional callback function. If argument is not a function it will be ignored
|
129 | * @return {*} Returns the formatted XML string if options.sync is true or a Promise if cb arugement is not passed
|
130 | */
|
131 | error (err = {}, options = {}, cb = false) {
|
132 | if (typeof options === 'function') {
|
133 | cb = options;
|
134 | options = {};
|
135 | }
|
136 | options.formatError = (typeof options.formatError === 'function') ? options.formatError : _ERROR.bind(this);
|
137 | return super.error(err, options, cb);
|
138 | }
|
139 | };
|
140 |
|
141 | module.exports = XML_ADAPTER;
|
142 | </code></pre>
|
143 | </article>
|
144 | </section>
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | </div>
|
150 |
|
151 | <nav>
|
152 | <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="CONTENT_ADAPTER_INTERFACE.html">CONTENT_ADAPTER_INTERFACE</a></li><li><a href="HTML_ADAPTER.html">HTML_ADAPTER</a></li><li><a href="JSON_ADAPTER.html">JSON_ADAPTER</a></li><li><a href="XML_ADAPTER.html">XML_ADAPTER</a></li></ul><h3>Global</h3><ul><li><a href="global.html#_ERROR">_ERROR</a></li><li><a href="global.html#_RENDER">_RENDER</a></li><li><a href="global.html#findValidViewFromPaths">findValidViewFromPaths</a></li></ul>
|
153 | </nav>
|
154 |
|
155 | <br class="clear">
|
156 |
|
157 | <footer>
|
158 | Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Thu Dec 22 2016 14:40:43 GMT-0500 (EST)
|
159 | </footer>
|
160 |
|
161 | <script> prettyPrint(); </script>
|
162 | <script src="scripts/linenumber.js"> </script>
|
163 | </body>
|
164 | </html>
|