Skip to content

Commit

Permalink
chore: update eslint & config to latest
Browse files Browse the repository at this point in the history
Replace `var` with `const` and `let`.

Signed-off-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
bajtos committed Oct 12, 2019
1 parent c74146d commit 67fc40b
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 97 deletions.
4 changes: 2 additions & 2 deletions lib/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ module.exports = cloneAllProperties;
function cloneAllProperties(data, err) {
data.name = err.name;
data.message = err.message;
for (var p in err) {
for (const p in err) {
if ((p in data)) continue;
data[p] = err[p];
}
// stack is appended last to ensure order is the same for response
data.stack = err.stack;
};
}
24 changes: 12 additions & 12 deletions lib/content-negotiation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
// License text available at https://opensource.org/licenses/MIT

'use strict';
var accepts = require('accepts');
var debug = require('debug')('strong-error-handler:http-response');
var sendJson = require('./send-json');
var sendHtml = require('./send-html');
var sendXml = require('./send-xml');
var util = require('util');
const accepts = require('accepts');
const debug = require('debug')('strong-error-handler:http-response');
const sendJson = require('./send-json');
const sendHtml = require('./send-html');
const sendXml = require('./send-xml');
const util = require('util');

module.exports = negotiateContentProducer;

Expand All @@ -23,14 +23,14 @@ module.exports = negotiateContentProducer;
* @returns {Function} Operation function with signature `fn(res, data)`
*/
function negotiateContentProducer(req, logWarning, options) {
var SUPPORTED_TYPES = [
const SUPPORTED_TYPES = [
'application/json', 'json',
'text/html', 'html',
'text/xml', 'xml',
];

options = options || {};
var defaultType = 'json';
let defaultType = 'json';

// checking if user provided defaultType is supported
if (options.defaultType) {
Expand All @@ -52,9 +52,9 @@ function negotiateContentProducer(req, logWarning, options) {
// Accepts: */*, application/json, text/html ---> will resolve as application/json
// eg. Chrome accepts defaults to `text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*`
// In this case `resolvedContentType` will result as: `text/html` due to the order given
var resolvedContentType = accepts(req).types(SUPPORTED_TYPES);
const resolvedContentType = accepts(req).types(SUPPORTED_TYPES);
debug('Resolved content-type', resolvedContentType);
var contentType = resolvedContentType || defaultType;
let contentType = resolvedContentType || defaultType;

if (options.negotiateContentType === false) {
if (SUPPORTED_TYPES.indexOf(options.defaultType) > -1) {
Expand All @@ -71,13 +71,13 @@ function negotiateContentProducer(req, logWarning, options) {
// to receive _format from user's url param to overide the content type
// req.query (eg /api/Users/1?_format=json will overide content negotiation
// https://github.com/strongloop/strong-remoting/blob/ac3093dcfbb787977ca0229b0f672703859e52e1/lib/http-context.js#L643-L645
var query = req.query || {};
const query = req.query || {};
if (query._format) {
if (SUPPORTED_TYPES.indexOf(query._format) > -1) {
contentType = query._format;
} else {
// format passed through query but not supported
var msg = util.format('Response _format "%s" is not supported' +
const msg = util.format('Response _format "%s" is not supported' +
'used "%s" instead"', query._format, defaultType);
logWarning(msg);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/data-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

'use strict';

var cloneAllProperties = require('../lib/clone.js');
var httpStatus = require('http-status');
const cloneAllProperties = require('../lib/clone.js');
const httpStatus = require('http-status');

module.exports = buildResponseData;

Expand Down Expand Up @@ -44,7 +44,7 @@ function buildResponseData(err, options) {
fillSafeFields(data, err, safeFields);

return data;
};
}

function serializeArrayOfErrors(errors, options) {
const details = errors.map(e => buildResponseData(e, options));
Expand Down
24 changes: 12 additions & 12 deletions lib/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

'use strict';

var path = require('path');
var SG = require('strong-globalize');
const path = require('path');
const SG = require('strong-globalize');
SG.SetRootDir(path.resolve(__dirname, '..'));
var buildResponseData = require('./data-builder');
var debug = require('debug')('strong-error-handler');
var format = require('util').format;
var logToConsole = require('./logger');
var negotiateContentProducer = require('./content-negotiation');
const buildResponseData = require('./data-builder');
const debug = require('debug')('strong-error-handler');
const format = require('util').format;
const logToConsole = require('./logger');
const negotiateContentProducer = require('./content-negotiation');

function noop() {
}
Expand All @@ -29,13 +29,13 @@ function createStrongErrorHandler(options) {
debug('Initializing with options %j', options);

// Log all errors via console.error (enabled by default)
var logError = options.log !== false ? logToConsole : noop;
const logError = options.log !== false ? logToConsole : noop;

return function strongErrorHandler(err, req, res, next) {
logError(req, err);
writeErrorToResponse(err, req, res, options);
};
};
}

/**
* Writes thrown error to response
Expand All @@ -59,20 +59,20 @@ function writeErrorToResponse(err, req, res, options) {
if (!err.status && !err.statusCode && res.statusCode >= 400)
err.statusCode = res.statusCode;

var data = buildResponseData(err, options);
const data = buildResponseData(err, options);
debug('Response status %s data %j', data.statusCode, data);

res.setHeader('X-Content-Type-Options', 'nosniff');
res.statusCode = data.statusCode;

var sendResponse = negotiateContentProducer(req, warn, options);
const sendResponse = negotiateContentProducer(req, warn, options);
sendResponse(res, data);

function warn(msg) {
res.header('X-Warning', msg);
debug(msg);
}
};
}

exports = module.exports = createStrongErrorHandler;
exports.writeErrorToResponse = writeErrorToResponse;
8 changes: 4 additions & 4 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

'use strict';

var format = require('util').format;
var g = require('strong-globalize')();
const format = require('util').format;
const g = require('strong-globalize')();

module.exports = function logToConsole(req, err) {
if (!Array.isArray(err)) {
Expand All @@ -15,9 +15,9 @@ module.exports = function logToConsole(req, err) {
return;
}

var errMsg = g.f('Unhandled array of errors for request %s %s\n',
const errMsg = g.f('Unhandled array of errors for request %s %s\n',
req.method, req.url);
var errors = err.map(formatError).join('\n');
const errors = err.map(formatError).join('\n');
console.error(errMsg, errors);
};

Expand Down
20 changes: 10 additions & 10 deletions lib/send-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
// License text available at https://opensource.org/licenses/MIT

'use strict';
var ejs = require('ejs');
var fs = require('fs');
var path = require('path');
const ejs = require('ejs');
const fs = require('fs');
const path = require('path');

var assetDir = path.resolve(__dirname, '../views');
var compiledTemplates = {
const assetDir = path.resolve(__dirname, '../views');
const compiledTemplates = {
// loading default template and stylesheet
default: loadDefaultTemplates(),
};

module.exports = sendHtml;

function sendHtml(res, data, options) {
var toRender = {options: {}, data: data};
const toRender = {options: {}, data: data};
// TODO: ability to call non-default template functions from options
var body = compiledTemplates.default(toRender);
const body = compiledTemplates.default(toRender);
sendReponse(res, body);
}

Expand All @@ -30,14 +30,14 @@ function sendHtml(res, data, options) {
* @returns {Function} render function with signature fn(data);
*/
function compileTemplate(filepath) {
var options = {cache: true, filename: filepath};
var fileContent = fs.readFileSync(filepath, 'utf8');
const options = {cache: true, filename: filepath};
const fileContent = fs.readFileSync(filepath, 'utf8');
return ejs.compile(fileContent, options);
}

// loads and cache default error templates
function loadDefaultTemplates() {
var defaultTemplate = path.resolve(assetDir, 'default-error.ejs');
const defaultTemplate = path.resolve(assetDir, 'default-error.ejs');
return compileTemplate(defaultTemplate);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/send-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

'use strict';

var safeStringify = require('fast-safe-stringify');
const safeStringify = require('fast-safe-stringify');

module.exports = function sendJson(res, data) {
var content = safeStringify({error: data});
const content = safeStringify({error: data});
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.end(content, 'utf-8');
};
4 changes: 2 additions & 2 deletions lib/send-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

'use strict';

var js2xmlparser = require('js2xmlparser');
const js2xmlparser = require('js2xmlparser');

module.exports = function sendXml(res, data) {
var content = js2xmlparser.parse('error', data);
const content = js2xmlparser.parse('error', data);
res.setHeader('Content-Type', 'text/xml; charset=utf-8');
res.end(content, 'utf-8');
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
},
"devDependencies": {
"chai": "^4.1.2",
"eslint": "^4.19.1",
"eslint-config-loopback": "^10.0.0",
"eslint": "^6.5.1",
"eslint-config-loopback": "^13.1.0",
"express": "^4.16.3",
"mocha": "^5.2.0",
"supertest": "^3.1.0"
Expand Down
Loading

0 comments on commit 67fc40b

Please sign in to comment.