diff --git a/plugin.xml b/plugin.xml index 127bef2..1cf262d 100644 --- a/plugin.xml +++ b/plugin.xml @@ -11,6 +11,7 @@ + diff --git a/src/browser/cordova-http-plugin.js b/src/browser/cordova-http-plugin.js index 6d56a48..5c6a672 100644 --- a/src/browser/cordova-http-plugin.js +++ b/src/browser/cordova-http-plugin.js @@ -1,7 +1,7 @@ var pluginId = module.id.slice(0, module.id.lastIndexOf('.')); var cordovaProxy = require('cordova/exec/proxy'); -var helpers = require(pluginId + '.helpers'); +var jsUtil = require(pluginId + '.js-util'); function serializeJsonData(data) { try { @@ -29,7 +29,7 @@ function serializeParams(params) { if (params === null) return ''; return Object.keys(params).map(function(key) { - if (helpers.getTypeOf(params[key]) === 'Array') { + if (jsUtil.getTypeOf(params[key]) === 'Array') { return serializeArray(key, params[key]); } @@ -56,7 +56,7 @@ function createXhrSuccessObject(xhr) { return { url: xhr.responseURL, status: xhr.status, - data: helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response, + data: jsUtil.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response, headers: deserializeResponseHeaders(xhr.getAllResponseHeaders()) }; } @@ -65,7 +65,7 @@ function createXhrFailureObject(xhr) { var obj = {}; obj.headers = xhr.getAllResponseHeaders(); - obj.error = helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response; + obj.error = jsUtil.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response; obj.error = obj.error || 'advanced-http: please check browser console for error messages'; if (xhr.responseURL) obj.url = xhr.responseURL; diff --git a/test/js-mocha-specs.js b/test/js-mocha-specs.js index d631793..f9ed41a 100644 --- a/test/js-mocha-specs.js +++ b/test/js-mocha-specs.js @@ -10,12 +10,13 @@ describe('Advanced HTTP public interface', function () { const getDependenciesBlueprint = () => { const messages = require('../www/messages'); const globalConfigs = require('../www/global-configs'); + const jsUtil = require('../www/js-util'); const ToughCookie = require('../www/umd-tough-cookie'); const lodash = require('../www/lodash'); const WebStorageCookieStore = require('../www/local-storage-store')(ToughCookie, lodash); const cookieHandler = require('../www/cookie-handler')(null, ToughCookie, WebStorageCookieStore); - const helpers = require('../www/helpers')(cookieHandler, messages); - const urlUtil = require('../www/url-util')(helpers); + const helpers = require('../www/helpers')(jsUtil, cookieHandler, messages); + const urlUtil = require('../www/url-util')(jsUtil); return { exec: noop, cookieHandler, urlUtil: urlUtil, helpers, globalConfigs }; }; @@ -131,8 +132,8 @@ describe('Advanced HTTP public interface', function () { }); describe('URL util', function () { - const helpers = require('../www/helpers')(null, null); - const util = require('../www/url-util')(helpers); + const jsUtil = require('../www/js-util'); + const util = require('../www/url-util')(jsUtil); it('parses URL with protocol, hostname and path correctly', () => { util.parseUrl('http://ilkimen.net/test').should.include({ diff --git a/www/advanced-http.js b/www/advanced-http.js index a60aa3c..b6732dc 100644 --- a/www/advanced-http.js +++ b/www/advanced-http.js @@ -7,12 +7,13 @@ var pluginId = module.id.slice(0, module.id.lastIndexOf('.')); var exec = require('cordova/exec'); var messages = require(pluginId + '.messages'); var globalConfigs = require(pluginId + '.global-configs'); +var jsUtil = require(pluginId + '.js-util'); var ToughCookie = require(pluginId + '.tough-cookie'); var lodash = require(pluginId + '.lodash'); var WebStorageCookieStore = require(pluginId + '.local-storage-store')(ToughCookie, lodash); var cookieHandler = require(pluginId + '.cookie-handler')(window.localStorage, ToughCookie, WebStorageCookieStore); -var helpers = require(pluginId + '.helpers')(cookieHandler, messages); -var urlUtil = require(pluginId + '.url-util')(helpers); +var helpers = require(pluginId + '.helpers')(jsUtil, cookieHandler, messages); +var urlUtil = require(pluginId + '.url-util')(jsUtil); var publicInterface = require(pluginId + '.public-interface')(exec, cookieHandler, urlUtil, helpers, globalConfigs); module.exports = publicInterface; diff --git a/www/helpers.js b/www/helpers.js index 9f33c27..2120570 100644 --- a/www/helpers.js +++ b/www/helpers.js @@ -1,4 +1,4 @@ -module.exports = function init(cookieHandler, messages) { +module.exports = function init(jsUtil, cookieHandler, messages) { var validSerializers = ['urlencoded', 'json', 'utf8']; var validCertModes = ['default', 'nocheck', 'pinned', 'legacy']; var validClientAuthModes = ['none', 'systemstore', 'file']; @@ -6,7 +6,6 @@ module.exports = function init(cookieHandler, messages) { return { b64EncodeUnicode: b64EncodeUnicode, - getTypeOf: getTypeOf, checkSerializer: checkSerializer, checkSSLCertMode: checkSSLCertMode, checkClientAuthMode: checkClientAuthMode, @@ -43,7 +42,7 @@ module.exports = function init(cookieHandler, messages) { } function checkForValidStringValue(list, value, onInvalidValueMessage) { - if (getTypeOf(value) !== 'String') { + if (jsUtil.getTypeOf(value) !== 'String') { throw new Error(onInvalidValueMessage + ' ' + list.join(', ')); } @@ -57,14 +56,14 @@ module.exports = function init(cookieHandler, messages) { } function checkKeyValuePairObject(obj, allowedChildren, onInvalidValueMessage) { - if (getTypeOf(obj) !== 'Object') { + if (jsUtil.getTypeOf(obj) !== 'Object') { throw new Error(onInvalidValueMessage); } var keys = Object.keys(obj); for (var i = 0; i < keys.length; i++) { - if (allowedChildren.indexOf(getTypeOf(obj[keys[i]])) === -1) { + if (allowedChildren.indexOf(jsUtil.getTypeOf(obj[keys[i]])) === -1) { throw new Error(onInvalidValueMessage); } } @@ -97,7 +96,7 @@ module.exports = function init(cookieHandler, messages) { } function checkForInvalidHeaderValue(value) { - if (getTypeOf(value) !== 'String') { + if (jsUtil.getTypeOf(value) !== 'String') { throw new Error(messages.INVALID_HEADERS_VALUE); } @@ -105,7 +104,7 @@ module.exports = function init(cookieHandler, messages) { } function checkTimeoutValue(timeout) { - if (getTypeOf(timeout) !== 'Number' || timeout < 0) { + if (jsUtil.getTypeOf(timeout) !== 'Number' || timeout < 0) { throw new Error(messages.INVALID_TIMEOUT_VALUE); } @@ -180,30 +179,6 @@ module.exports = function init(cookieHandler, messages) { return mergedHeaders; } - // typeof is not working reliably in JS - function getTypeOf(object) { - switch (Object.prototype.toString.call(object)) { - case '[object Array]': - return 'Array'; - case '[object Boolean]': - return 'Boolean'; - case '[object Function]': - return 'Function'; - case '[object Null]': - return 'Null'; - case '[object Number]': - return 'Number'; - case '[object Object]': - return 'Object'; - case '[object String]': - return 'String'; - case '[object Undefined]': - return 'Undefined'; - default: - return 'Unknown'; - } - } - function getAllowedDataTypes(dataSerializer) { switch (dataSerializer) { case 'utf8': @@ -216,7 +191,7 @@ module.exports = function init(cookieHandler, messages) { } function getProcessedData(data, dataSerializer) { - var currentDataType = getTypeOf(data); + var currentDataType = jsUtil.getTypeOf(data); var allowedDataTypes = getAllowedDataTypes(dataSerializer); if (allowedDataTypes.indexOf(currentDataType) === -1) { @@ -231,11 +206,11 @@ module.exports = function init(cookieHandler, messages) { } function handleMissingCallbacks(successFn, failFn) { - if (getTypeOf(successFn) !== 'Function') { + if (jsUtil.getTypeOf(successFn) !== 'Function') { throw new Error(messages.MANDATORY_SUCCESS); } - if (getTypeOf(failFn) !== 'Function') { + if (jsUtil.getTypeOf(failFn) !== 'Function') { throw new Error(messages.MANDATORY_FAIL); } } @@ -249,7 +224,7 @@ module.exports = function init(cookieHandler, messages) { timeout: checkTimeoutValue(options.timeout || globals.timeout), headers: checkHeadersObject(options.headers || {}), params: checkParamsObject(options.params || {}), - data: getTypeOf(options.data) === 'Undefined' ? null : options.data, + data: jsUtil.getTypeOf(options.data) === 'Undefined' ? null : options.data, filePath: options.filePath || '', name: options.name || '' }; diff --git a/www/js-util.js b/www/js-util.js new file mode 100644 index 0000000..a86da8b --- /dev/null +++ b/www/js-util.js @@ -0,0 +1,26 @@ +module.exports = { + // typeof is not working reliably in JS + getTypeOf: function (object) { + switch (Object.prototype.toString.call(object)) { + case '[object Array]': + return 'Array'; + case '[object Boolean]': + return 'Boolean'; + case '[object Function]': + return 'Function'; + case '[object Null]': + return 'Null'; + case '[object Number]': + return 'Number'; + case '[object Object]': + return 'Object'; + case '[object String]': + return 'String'; + case '[object Undefined]': + return 'Undefined'; + default: + return 'Unknown'; + } + } +} + diff --git a/www/url-util.js b/www/url-util.js index 802af01..043d94c 100644 --- a/www/url-util.js +++ b/www/url-util.js @@ -1,4 +1,4 @@ -module.exports = function init(helpers) { +module.exports = function init(jsUtil) { return { parseUrl: parseUrl, appendQueryParamsString: appendQueryParamsString, @@ -48,10 +48,10 @@ module.exports = function init(helpers) { var identifier = parentKey.length ? parentKey + '[' + key + ']' : key; - if (helpers.getTypeOf(object[key]) === 'Array') { + if (jsUtil.getTypeOf(object[key]) === 'Array') { parts.push(serializeArray(identifier, object[key], encode)); continue; - } else if (helpers.getTypeOf(object[key]) === 'Object') { + } else if (jsUtil.getTypeOf(object[key]) === 'Object') { parts.push(serializeObject(identifier, object[key], encode)); continue; } @@ -66,10 +66,10 @@ module.exports = function init(helpers) { var parts = []; for (var i = 0; i < array.length; ++i) { - if (helpers.getTypeOf(array[i]) === 'Array') { + if (jsUtil.getTypeOf(array[i]) === 'Array') { parts.push(serializeArray(parentKey + '[]', array[i], encode)); continue; - } else if (helpers.getTypeOf(array[i]) === 'Object') { + } else if (jsUtil.getTypeOf(array[i]) === 'Object') { parts.push(serializeObject(parentKey + '[]' + array[i], encode)); continue; }