Skip to content

Commit

Permalink
Merge pull request #1089 from cromefire/es6-1
Browse files Browse the repository at this point in the history
Migrating backdropscreensaver, filterdialog and fetchhelper to ES6
  • Loading branch information
anthonylavado committed Jun 7, 2020
2 parents 116c218 + e8062cc commit 317838c
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 275 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -132,6 +132,7 @@ module.exports = {
'Object.getOwnPropertyDescriptor',
'Object.getPrototypeOf',
'Object.keys',
'Object.entries',
'Object.getOwnPropertyNames',
'Function.name',
'Function.hasInstance',
Expand Down
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -125,6 +125,9 @@
"src/scripts/filesystem.js",
"src/scripts/imagehelper.js",
"src/scripts/inputManager.js",
"src/plugins/backdropScreensaver/plugin.js",
"src/components/filterdialog/filterdialog.js",
"src/components/fetchhelper.js",
"src/scripts/keyboardNavigation.js",
"src/scripts/settings/appSettings.js",
"src/scripts/settings/userSettings.js",
Expand Down
65 changes: 28 additions & 37 deletions src/components/fetchhelper.js
@@ -1,21 +1,19 @@
define([], function () {
'use strict';
/* eslint-disable indent */
export function getFetchPromise(request) {

function getFetchPromise(request) {

var headers = request.headers || {};
const headers = request.headers || {};

if (request.dataType === 'json') {
headers.accept = 'application/json';
}

var fetchRequest = {
const fetchRequest = {
headers: headers,
method: request.type,
credentials: 'same-origin'
};

var contentType = request.contentType;
let contentType = request.contentType;

if (request.data) {

Expand All @@ -33,12 +31,12 @@ define([], function () {
headers['Content-Type'] = contentType;
}

var url = request.url;
let url = request.url;

if (request.query) {
var paramString = paramsToString(request.query);
const paramString = paramsToString(request.query);
if (paramString) {
url += '?' + paramString;
url += `?${paramString}`;
}
}

Expand All @@ -51,62 +49,59 @@ define([], function () {

function fetchWithTimeout(url, options, timeoutMs) {

console.debug('fetchWithTimeout: timeoutMs: ' + timeoutMs + ', url: ' + url);
console.debug(`fetchWithTimeout: timeoutMs: ${timeoutMs}, url: ${url}`);

return new Promise(function (resolve, reject) {

var timeout = setTimeout(reject, timeoutMs);
const timeout = setTimeout(reject, timeoutMs);

options = options || {};
options.credentials = 'same-origin';

fetch(url, options).then(function (response) {
clearTimeout(timeout);

console.debug('fetchWithTimeout: succeeded connecting to url: ' + url);
console.debug(`fetchWithTimeout: succeeded connecting to url: ${url}`);

resolve(response);
}, function (error) {

clearTimeout(timeout);

console.debug('fetchWithTimeout: timed out connecting to url: ' + url);
console.debug(`fetchWithTimeout: timed out connecting to url: ${url}`);

reject();
reject(error);
});
});
}

/**
* @param params {Record<string, string | number | boolean>}
* @returns {string} Query string
*/
function paramsToString(params) {

var values = [];

for (var key in params) {

var value = params[key];

if (value !== null && value !== undefined && value !== '') {
values.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}
}
return values.join('&');
return Object.entries(params)
// eslint-disable-next-line no-unused-vars
.filter(([_, v]) => v !== null && v !== undefined && v !== '')
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`)
.join('&');
}

function ajax(request) {
export function ajax(request) {
if (!request) {
throw new Error('Request cannot be null');
}

request.headers = request.headers || {};

console.debug('requesting url: ' + request.url);
console.debug(`requesting url: ${request.url}`);

return getFetchPromise(request).then(function (response) {
console.debug('response status: ' + response.status + ', url: ' + request.url);
console.debug(`response status: ${response.status}, url: ${request.url}`);
if (response.status < 400) {
if (request.dataType === 'json' || request.headers.accept === 'application/json') {
return response.json();
} else if (request.dataType === 'text' || (response.headers.get('Content-Type') || '').toLowerCase().indexOf('text/') === 0) {
} else if (request.dataType === 'text' || (response.headers.get('Content-Type') || '').toLowerCase().startsWith('text/')) {
return response.text();
} else {
return response;
Expand All @@ -115,12 +110,8 @@ define([], function () {
return Promise.reject(response);
}
}, function (err) {
console.error('request failed to url: ' + request.url);
console.error(`request failed to url: ${request.url}`);
throw err;
});
}
return {
getFetchPromise: getFetchPromise,
ajax: ajax
};
});
/* eslint-enable indent */

0 comments on commit 317838c

Please sign in to comment.