1 | <!DOCTYPE html>
|
2 | <html lang="en">
|
3 | <head>
|
4 | <meta charset="utf-8">
|
5 | <title>JSDoc: Source: adapters/json_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/json_content.js</h1>
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | <section>
|
28 | <article>
|
29 | <pre class="prettyprint source linenums"><code>'use strict';
|
30 | const Promisie = require('promisie');
|
31 |
|
32 | /**
|
33 | * Creates a formatted success response object
|
34 | * @param {*} data Any data that should be sent with the success response
|
35 | * @param {Object} options Configurable options for success response formatting
|
36 | * @param {Function} [options.formatRender=this.formatRender] Custom formatting function for json success response. This argument will be ignored if not a function
|
37 | * @return {Object} Formatted success response object
|
38 | */
|
39 | const _RENDER = function (data, options) {
|
40 | if (typeof this.formatRender === 'function' || typeof options.formatRender === 'function') return (typeof options.formatRender === 'function') ? options.formatRender(data, options) : this.formatRender(data, options);
|
41 | else {
|
42 | return {
|
43 | result: 'success',
|
44 | status: 200,
|
45 | data
|
46 | };
|
47 | }
|
48 | };
|
49 |
|
50 | /**
|
51 | * Creates a formatted error response object
|
52 | * @param {*} err Any data to be sent as part of error response
|
53 | * @param {Object} options Configurable options for error response formatting
|
54 | * @param {Function} [options.formatError=this.formatError] Custom formatting function for json error response. This argument will be ignored if not a function
|
55 | * @return {Object} Formatted error response object
|
56 | */
|
57 | const _ERROR = function (data, options) {
|
58 | if (typeof this.formatError === 'function' || typeof options.formatError === 'function') return (typeof options.formatError === 'function') ? options.formatError(data, options) : this.formatError(data, options);
|
59 | else {
|
60 | return {
|
61 | result: 'error',
|
62 | status: 500,
|
63 | data: {
|
64 | error: data
|
65 | }
|
66 | };
|
67 | }
|
68 | };
|
69 |
|
70 | /**
|
71 | * Baseline adapter class which handles wrapping response data in an object with a status code and result message. Formatting is configurable both for success and error responses
|
72 | * @type {JSON_Adapter}
|
73 | */
|
74 | const JSON_ADAPTER = class JSON_Adapter {
|
75 | /**
|
76 | * @constructor
|
77 | * @param {Object} [options={}] Configurable options for the adapter
|
78 | * @param {Function} options.formatRender Overwrites default formatting behavior for json responses
|
79 | * @param {Function} options.formatError Overwrites default formatting behavior for errors
|
80 | */
|
81 | constructor (options = {}) {
|
82 | this.formatRender = options.formatRender;
|
83 | this.formatError = options.formatError;
|
84 | }
|
85 | /**
|
86 | * Creates a formatted response
|
87 | * @param {*} data Any data that should be sent with the success response
|
88 | * @param {Object} [options={}] Configurable options for success response formatting see _RENDER for more details
|
89 | * @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
|
90 | * @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
|
91 | * @param {Boolean} [options.skip_response] If true function will resolve with the rendered template instead of sending a response
|
92 | * @param {Boolean} options.sync If true execution of render will be handled synchronously
|
93 | * @param {Function} [cb=false] An optional callback function. If this argument is not a function it will be ignored
|
94 | * @return {*} Returns the formatted json object if options.sync is true or a Promise if cb arugement is not passed
|
95 | */
|
96 | render (data, options = {}, cb = false) {
|
97 | try {
|
98 | if (typeof options === 'function') {
|
99 | cb = options;
|
100 | options = {};
|
101 | }
|
102 | let rendered = _RENDER.call(this, data, options);
|
103 | if (options.req && options.res && !options.skip_response) {
|
104 | if (options.req.query && options.req.query.callback) options.res.status(200).jsonp(rendered);
|
105 | else options.res.status(200).send(rendered);
|
106 | }
|
107 | if (options.sync !== true) {
|
108 | if (typeof cb === 'function') cb(null, rendered);
|
109 | else return Promisie.resolve(rendered);
|
110 | }
|
111 | else return rendered;
|
112 | }
|
113 | catch (e) {
|
114 | return this.error(e, options, cb);
|
115 | }
|
116 | }
|
117 | /**
|
118 | * Creates a formatted error response
|
119 | * @param {*} err Any data to be sent as part of error response
|
120 | * @param {Object} options Configurable options for error response formatting see _ERROR for more details
|
121 | * @param {Boolean} options.sync If ture execution of error will be handled synchronously
|
122 | * @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
|
123 | * @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
|
124 | * @param {Boolean} [options.skip_response] If true function will resolve with the rendered template instead of sending a response
|
125 | * @param {Function} [cb=false] An optional callback function. If this argument is not a function it will be ignored
|
126 | * @return {*} Returns the formatted json object if options.sync is true or a Promise if cb arugement is not passed
|
127 | */
|
128 | error (err, options = {}, cb = false) {
|
129 | try {
|
130 | if (typeof options === 'function') {
|
131 | cb = options;
|
132 | options = {};
|
133 | }
|
134 | let errored = _ERROR.call(this, err, options);
|
135 | if (options.req && options.res && !options.skip_response) {
|
136 | if (options.req.query && options.req.query.callback) options.res.status(500).jsonp(errored);
|
137 | else options.res.status(500).send(errored);
|
138 | }
|
139 | if (options.sync !== true) {
|
140 | if (typeof cb === 'function') cb(null, errored);
|
141 | else return Promisie.resolve(errored);
|
142 | }
|
143 | else return errored;
|
144 | }
|
145 | catch (e) {
|
146 | if (options.sync !== true) {
|
147 | if (typeof cb === 'function') cb(e);
|
148 | else return Promisie.reject(e);
|
149 | }
|
150 | else throw e;
|
151 | }
|
152 | }
|
153 | };
|
154 |
|
155 | module.exports = JSON_ADAPTER;
|
156 | </code></pre>
|
157 | </article>
|
158 | </section>
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | </div>
|
164 |
|
165 | <nav>
|
166 | <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>
|
167 | </nav>
|
168 |
|
169 | <br class="clear">
|
170 |
|
171 | <footer>
|
172 | 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)
|
173 | </footer>
|
174 |
|
175 | <script> prettyPrint(); </script>
|
176 | <script src="scripts/linenumber.js"> </script>
|
177 | </body>
|
178 | </html>
|