Skip to content

Commit

Permalink
Fix silkimen#201: browser implementation broken due to broken dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Sefa Ilkimen committed Apr 8, 2019
1 parent 4b964e8 commit 70c8b9b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 50 deletions.
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<js-module src="www/cookie-handler.js" name="cookie-handler"/>
<js-module src="www/global-configs.js" name="global-configs"/>
<js-module src="www/helpers.js" name="helpers"/>
<js-module src="www/js-util.js" name="js-util"/>
<js-module src="www/local-storage-store.js" name="local-storage-store"/>
<js-module src="www/lodash.js" name="lodash"/>
<js-module src="www/messages.js" name="messages"/>
Expand Down
8 changes: 4 additions & 4 deletions src/browser/cordova-http-plugin.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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]);
}

Expand All @@ -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())
};
}
Expand All @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions test/js-mocha-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};
Expand Down Expand Up @@ -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({
Expand Down
5 changes: 3 additions & 2 deletions www/advanced-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
45 changes: 10 additions & 35 deletions www/helpers.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
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'];
var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'upload', 'download'];

return {
b64EncodeUnicode: b64EncodeUnicode,
getTypeOf: getTypeOf,
checkSerializer: checkSerializer,
checkSSLCertMode: checkSSLCertMode,
checkClientAuthMode: checkClientAuthMode,
Expand Down Expand Up @@ -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(', '));
}

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -97,15 +96,15 @@ 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);
}

return value;
}

function checkTimeoutValue(timeout) {
if (getTypeOf(timeout) !== 'Number' || timeout < 0) {
if (jsUtil.getTypeOf(timeout) !== 'Number' || timeout < 0) {
throw new Error(messages.INVALID_TIMEOUT_VALUE);
}

Expand Down Expand Up @@ -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':
Expand All @@ -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) {
Expand All @@ -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);
}
}
Expand All @@ -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 || ''
};
Expand Down
26 changes: 26 additions & 0 deletions www/js-util.js
Original file line number Diff line number Diff line change
@@ -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';
}
}
}

10 changes: 5 additions & 5 deletions www/url-util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function init(helpers) {
module.exports = function init(jsUtil) {
return {
parseUrl: parseUrl,
appendQueryParamsString: appendQueryParamsString,
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 70c8b9b

Please sign in to comment.