mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-05-31 00:00:07 +08:00
WIP: new API which allows overriding global options
This commit is contained in:
+18
-2
@@ -110,6 +110,24 @@ var publicInterface = {
|
||||
validateDomainName: function (validate, success, failure) {
|
||||
failure(messages.DEPRECATED_VDN);
|
||||
},
|
||||
sendRequest: function (url, options, success, failure) {
|
||||
helpers.handleMissingCallbacks(success, failure);
|
||||
|
||||
options = helpers.handleMissingOptions(options, internals);
|
||||
|
||||
var headers = helpers.getMergedHeaders(url, options.headers, internals.headers);
|
||||
var onSuccess = helpers.injectCookieHandler(url, success);
|
||||
var onFail = helpers.injectCookieHandler(url, failure);
|
||||
var payload;
|
||||
|
||||
if ([ 'get', 'delete', 'head' ].indexOf(method) < 0) {
|
||||
payload = helpers.getProcessedData(options.data, options.serializer);
|
||||
} else {
|
||||
payload = params;
|
||||
}
|
||||
|
||||
return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [ url, payload, options.serializer, headers, options.timeout ]);
|
||||
},
|
||||
post: function (url, data, headers, success, failure) {
|
||||
helpers.handleMissingCallbacks(success, failure);
|
||||
|
||||
@@ -155,7 +173,6 @@ var publicInterface = {
|
||||
|
||||
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
|
||||
},
|
||||
|
||||
patch: function (url, data, headers, success, failure) {
|
||||
helpers.handleMissingCallbacks(success, failure);
|
||||
|
||||
@@ -171,7 +188,6 @@ var publicInterface = {
|
||||
|
||||
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
|
||||
},
|
||||
|
||||
delete: function (url, params, headers, success, failure) {
|
||||
helpers.handleMissingCallbacks(success, failure);
|
||||
|
||||
|
||||
+66
-7
@@ -3,6 +3,7 @@ var cookieHandler = require(pluginId + '.cookie-handler');
|
||||
var messages = require(pluginId + '.messages');
|
||||
|
||||
var validSerializers = [ 'urlencoded', 'json', 'utf8' ];
|
||||
var validHttpMethods = [ 'get', 'put', 'post', 'patch', 'head', 'delete'];
|
||||
|
||||
module.exports = {
|
||||
b64EncodeUnicode: b64EncodeUnicode,
|
||||
@@ -62,15 +63,60 @@ function onInvalidHeader(handler) {
|
||||
});
|
||||
}
|
||||
|
||||
function checkSerializer(serializer) {
|
||||
serializer = serializer || '';
|
||||
serializer = serializer.trim().toLowerCase();
|
||||
|
||||
if (validSerializers.indexOf(serializer) > -1) {
|
||||
return serializer;
|
||||
function checkForValidStringValue(list, value, onInvalidValueMessage) {
|
||||
if (getTypeOf(value) !== 'String') {
|
||||
throw new Error(onInvalidValueMessage + ' ' + list.join(', '));
|
||||
}
|
||||
|
||||
return serializer[0];
|
||||
value = value.trim().toLowerCase();
|
||||
|
||||
if (list.indexOf(value) > -1) {
|
||||
return value;
|
||||
}
|
||||
|
||||
throw new Error(onInvalidValueMessage + ' ' + list.join(', '));
|
||||
}
|
||||
|
||||
function checkKeyValuePairObject(obj, onInvalidValueMessage) {
|
||||
if (getTypeOf(obj) !== 'Object') {
|
||||
throw new Error(onInvalidValueMessage);
|
||||
}
|
||||
|
||||
var keys = Object.keys(obj);
|
||||
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i];
|
||||
|
||||
if (getTypeOf(obj[key]) !== 'String') {
|
||||
throw new Error(onInvalidValueMessage);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function checkHttpMethod(method) {
|
||||
return checkForValidStringValue(validHttpMethods, method, messages.INVALID_HTTP_METHOD);
|
||||
}
|
||||
|
||||
function checkSerializer(serializer) {
|
||||
return checkForValidStringValue(validSerializers, serializer, messages.INVALID_DATA_SERIALIZER);
|
||||
}
|
||||
|
||||
function checkTimeoutValue(timeout) {
|
||||
if (getTypeOf(timeout) !== 'Number' || timeout < 0) {
|
||||
throw new Error(messages.INVALID_TIMEOUT_VALUE);
|
||||
}
|
||||
|
||||
return timeout;
|
||||
}
|
||||
|
||||
function checkHeadersObject(headers) {
|
||||
checkKeyValuePairObject(headers, messages.INVALID_HEADERS_VALUE);
|
||||
}
|
||||
|
||||
function checkParamsObject(params) {
|
||||
checkKeyValuePairObject(params, messages.INVALID_PARAMS_VALUE);
|
||||
}
|
||||
|
||||
function resolveCookieString(headers) {
|
||||
@@ -194,3 +240,16 @@ function handleMissingCallbacks(successFn, failFn) {
|
||||
throw new Error(messages.MANDATORY_FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
function handleMissingOptions(options, globals) {
|
||||
options = options || {};
|
||||
|
||||
return {
|
||||
method: checkHttpMethod(options.method || validHttpMethods[0]);
|
||||
serializer: checkSerializer(options.serializer || globals.dataSerializer);
|
||||
timeout: checkTimeoutValue(options.timeout || globals.timeoutInSeconds);
|
||||
headers: checkHeadersObject(options.headers || {});
|
||||
params: checkParamsObject(options.params || {});
|
||||
data: options.data || null;
|
||||
};
|
||||
}
|
||||
|
||||
+6
-1
@@ -4,5 +4,10 @@ module.exports = {
|
||||
DEPRECATED_VDN: 'advanced-http: "validateDomainName" is no more supported, please see change log for further info',
|
||||
HEADER_VALUE_MUST_BE_STRING: 'advanced-http: header values must be strings',
|
||||
MANDATORY_SUCCESS: 'advanced-http: missing mandatory "onSuccess" callback function',
|
||||
MANDATORY_FAIL: 'advanced-http: missing mandatory "onFail" callback function'
|
||||
MANDATORY_FAIL: 'advanced-http: missing mandatory "onFail" callback function',
|
||||
INVALID_HTTP_METHOD: 'advanced-http: invalid HTTP method, supported methods are:',
|
||||
INVALID_DATA_SERIALIZER: 'advanced-http: invalid serializer, supported serializers are:',
|
||||
INVALID_TIMEOUT_VALUE: 'advanced-http: invalid timeout value, needs to be a positive float value',
|
||||
INVALID_HEADERS_VALUE: 'advanced-http: invalid headers object, needs to be an object with strings',
|
||||
INVALID_PARAMS_VALUE: 'advanced-http: invalid params object, needs to be an object with strings'
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user