- WIP: implement data pre-processor for #101

- implement checks for #101
- add some specs
This commit is contained in:
Sefa Ilkimen
2019-11-10 06:40:31 +01:00
parent cc3e70771c
commit 867b8ea202
7 changed files with 191 additions and 24 deletions

View File

@@ -14,8 +14,11 @@ 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')(jsUtil, cookieHandler, messages, base64, errorCodes);
var dependencyValidator = require(pluginId + '.dependency-validator')(window.FormData, window.console, messages);
var helpers = require(pluginId + '.helpers')(window, jsUtil, cookieHandler, messages, base64, errorCodes, dependencyValidator);
var urlUtil = require(pluginId + '.url-util')(jsUtil);
var publicInterface = require(pluginId + '.public-interface')(exec, cookieHandler, urlUtil, helpers, globalConfigs, errorCodes);
dependencyValidator.logWarnings();
module.exports = publicInterface;

View File

@@ -0,0 +1,22 @@
module.exports = function init(FormData, console, messages) {
var interface = {
checkFormDataApi: checkFormDataApi,
logWarnings: logWarnings,
};
return interface;
function logWarnings() {
if (!FormData) {
console.warn(messages.MISSING_FORMDATA_API);
} else if (!FormData.prototype || !FormData.prototype.entries) {
console.warn(messages.MISSING_FORMDATA_ENTRIES_API);
}
}
function checkFormDataApi() {
if (!FormData || !FormData.prototype || !FormData.prototype.entries) {
throw new Error(messages.MISSING_FORMDATA_ENTRIES_API);
}
}
};

View File

@@ -1,5 +1,5 @@
module.exports = function init(jsUtil, cookieHandler, messages, base64, errorCodes) {
var validSerializers = ['urlencoded', 'json', 'utf8'];
module.exports = function init(global, jsUtil, cookieHandler, messages, base64, errorCodes, dependencyValidator) {
var validSerializers = ['urlencoded', 'json', 'utf8', 'multipart'];
var validCertModes = ['default', 'nocheck', 'pinned', 'legacy'];
var validClientAuthModes = ['none', 'systemstore', 'buffer'];
var validHttpMethods = ['get', 'put', 'post', 'patch', 'head', 'delete', 'upload', 'download'];
@@ -270,7 +270,7 @@ module.exports = function init(jsUtil, cookieHandler, messages, base64, errorCod
entry.isFile = rawEntry.isFile;
entry.name = rawEntry.name;
entry.fullPath = rawEntry.fullPath;
entry.filesystem = new FileSystem(rawEntry.filesystemName || (rawEntry.filesystem == window.PERSISTENT ? 'persistent' : 'temporary'));
entry.filesystem = new FileSystem(rawEntry.filesystemName || (rawEntry.filesystem == global.PERSISTENT ? 'persistent' : 'temporary'));
entry.nativeURL = rawEntry.nativeURL;
return entry;
@@ -363,16 +363,31 @@ module.exports = function init(jsUtil, cookieHandler, messages, base64, errorCod
return ['String'];
case 'urlencoded':
return ['Object'];
default:
case 'json':
return ['Array', 'Object'];
default:
return [];
}
}
function getAllowedInstanceType(dataSerializer) {
return dataSerializer === 'multipart' ? 'FormData' : null;
}
function getProcessedData(data, dataSerializer) {
var currentDataType = jsUtil.getTypeOf(data);
var allowedDataTypes = getAllowedDataTypes(dataSerializer);
var allowedInstanceType = getAllowedInstanceType(dataSerializer);
if (allowedDataTypes.indexOf(currentDataType) === -1) {
if (allowedInstanceType && !global[allowedInstanceType]) {
throw new Error(messages.INSTANCE_TYPE_NOT_SUPPORTED + ' ' + allowedInstanceType);
}
if (allowedInstanceType && !(data instanceof global[allowedInstanceType])) {
throw new Error(messages.INSTANCE_TYPE_MISMATCH_DATA + ' ' + allowedInstanceType);
}
if (!allowedInstanceType && allowedDataTypes.indexOf(currentDataType) === -1) {
throw new Error(messages.TYPE_MISMATCH_DATA + ' ' + allowedDataTypes.join(', '));
}
@@ -380,6 +395,12 @@ module.exports = function init(jsUtil, cookieHandler, messages, base64, errorCod
data = { text: data };
}
if (dataSerializer === 'multipart') {
dependencyValidator.checkFormDataApi();
// TODO
}
return data;
}

View File

@@ -2,6 +2,8 @@ module.exports = {
ADDING_COOKIES_NOT_SUPPORTED: 'advanced-http: "setHeader" does not support adding cookies, please use "setCookie" function instead',
EMPTY_FILE_PATHS: 'advanced-http: "filePaths" option array must not be empty, <filePaths: string[]>',
EMPTY_NAMES: 'advanced-http: "names" option array must not be empty, <names: string[]>',
INSTANCE_TYPE_MISMATCH_DATA: 'advanced-http: "data" option is configured to support only following instance types:',
INSTANCE_TYPE_NOT_SUPPORTED: 'advanced-http: this webview does not support following Web API which is needed for this plugin:',
INVALID_CLIENT_AUTH_ALIAS: 'advanced-http: invalid client certificate alias, needs to be a string or undefined, <alias: string | undefined>',
INVALID_CLIENT_AUTH_MODE: 'advanced-http: invalid client certificate authentication mode, supported modes are:',
INVALID_CLIENT_AUTH_OPTIONS: 'advanced-http: invalid client certificate authentication options, needs to be an dictionary style object',
@@ -17,8 +19,10 @@ module.exports = {
INVALID_TIMEOUT_VALUE: 'advanced-http: invalid timeout value, needs to be a positive numeric value, <timeout: number>',
MANDATORY_FAIL: 'advanced-http: missing mandatory "onFail" callback function',
MANDATORY_SUCCESS: 'advanced-http: missing mandatory "onSuccess" callback function',
MISSING_FORMDATA_API: 'advanced-http: FormData API is not supported in this webview. If you want to use "multipart/form-data" requests, you need to load a polyfill library before loading this plugin. Check out https://github.com/silkimen/cordova-plugin-advanced-http/wiki/FormData-API-requirements for more info.',
MISSING_FORMDATA_ENTRIES_API: 'advanced-http: This webview does not implement FormData API specification correctly, FormData.entries() is missing. If you want to use "multipart/form-data" requests, you need to load a polyfill library before loading this plugin. Check out https://github.com/silkimen/cordova-plugin-advanced-http/wiki/FormData-API-requirements for more info.',
POST_PROCESSING_FAILED: 'advanced-http: an error occured during post processing response:',
TYPE_MISMATCH_DATA: 'advanced-http: "data" option supports only following data types:',
TYPE_MISMATCH_DATA: 'advanced-http: "data" option is configured to support only following data types:',
TYPE_MISMATCH_FILE_PATHS: 'advanced-http: "filePaths" option needs to be an string array, <filePaths: string[]>',
TYPE_MISMATCH_HEADERS: 'advanced-http: "headers" option needs to be an dictionary style object with string values, <headers: {[key: string]: string}>',
TYPE_MISMATCH_NAMES: 'advanced-http: "names" option needs to be an string array, <names: string[]>',