Skip to content

Commit 6b9ed72

Browse files
author
Sefa Ilkimen
committed
WIP: new API which allows overriding global options
1 parent 1719334 commit 6b9ed72

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

www/advanced-http.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ var publicInterface = {
110110
validateDomainName: function (validate, success, failure) {
111111
failure(messages.DEPRECATED_VDN);
112112
},
113+
sendRequest: function (url, options, success, failure) {
114+
helpers.handleMissingCallbacks(success, failure);
115+
116+
options = helpers.handleMissingOptions(options, internals);
117+
118+
var headers = helpers.getMergedHeaders(url, options.headers, internals.headers);
119+
var onSuccess = helpers.injectCookieHandler(url, success);
120+
var onFail = helpers.injectCookieHandler(url, failure);
121+
var payload;
122+
123+
if ([ 'get', 'delete', 'head' ].indexOf(method) < 0) {
124+
payload = helpers.getProcessedData(options.data, options.serializer);
125+
} else {
126+
payload = params;
127+
}
128+
129+
return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [ url, payload, options.serializer, headers, options.timeout ]);
130+
},
113131
post: function (url, data, headers, success, failure) {
114132
helpers.handleMissingCallbacks(success, failure);
115133

@@ -155,7 +173,6 @@ var publicInterface = {
155173

156174
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'put', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
157175
},
158-
159176
patch: function (url, data, headers, success, failure) {
160177
helpers.handleMissingCallbacks(success, failure);
161178

@@ -171,7 +188,6 @@ var publicInterface = {
171188

172189
return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'patch', [url, data, internals.dataSerializer, headers, internals.timeoutInSeconds]);
173190
},
174-
175191
delete: function (url, params, headers, success, failure) {
176192
helpers.handleMissingCallbacks(success, failure);
177193

www/helpers.js

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var cookieHandler = require(pluginId + '.cookie-handler');
33
var messages = require(pluginId + '.messages');
44

55
var validSerializers = [ 'urlencoded', 'json', 'utf8' ];
6+
var validHttpMethods = [ 'get', 'put', 'post', 'patch', 'head', 'delete'];
67

78
module.exports = {
89
b64EncodeUnicode: b64EncodeUnicode,
@@ -62,15 +63,60 @@ function onInvalidHeader(handler) {
6263
});
6364
}
6465

66+
function checkForValidStringValue(list, value, onInvalidValueMessage) {
67+
if (getTypeOf(value) !== 'String') {
68+
throw new Error(onInvalidValueMessage + ' ' + list.join(', '));
69+
}
70+
71+
value = value.trim().toLowerCase();
72+
73+
if (list.indexOf(value) > -1) {
74+
return value;
75+
}
76+
77+
throw new Error(onInvalidValueMessage + ' ' + list.join(', '));
78+
}
79+
80+
function checkKeyValuePairObject(obj, onInvalidValueMessage) {
81+
if (getTypeOf(obj) !== 'Object') {
82+
throw new Error(onInvalidValueMessage);
83+
}
84+
85+
var keys = Object.keys(obj);
86+
87+
for (var i = 0; i < keys.length; i++) {
88+
var key = keys[i];
89+
90+
if (getTypeOf(obj[key]) !== 'String') {
91+
throw new Error(onInvalidValueMessage);
92+
}
93+
}
94+
95+
return obj;
96+
}
97+
98+
function checkHttpMethod(method) {
99+
return checkForValidStringValue(validHttpMethods, method, messages.INVALID_HTTP_METHOD);
100+
}
101+
65102
function checkSerializer(serializer) {
66-
serializer = serializer || '';
67-
serializer = serializer.trim().toLowerCase();
103+
return checkForValidStringValue(validSerializers, serializer, messages.INVALID_DATA_SERIALIZER);
104+
}
68105

69-
if (validSerializers.indexOf(serializer) > -1) {
70-
return serializer;
106+
function checkTimeoutValue(timeout) {
107+
if (getTypeOf(timeout) !== 'Number' || timeout < 0) {
108+
throw new Error(messages.INVALID_TIMEOUT_VALUE);
71109
}
72110

73-
return serializer[0];
111+
return timeout;
112+
}
113+
114+
function checkHeadersObject(headers) {
115+
checkKeyValuePairObject(headers, messages.INVALID_HEADERS_VALUE);
116+
}
117+
118+
function checkParamsObject(params) {
119+
checkKeyValuePairObject(params, messages.INVALID_PARAMS_VALUE);
74120
}
75121

76122
function resolveCookieString(headers) {
@@ -194,3 +240,16 @@ function handleMissingCallbacks(successFn, failFn) {
194240
throw new Error(messages.MANDATORY_FAIL);
195241
}
196242
}
243+
244+
function handleMissingOptions(options, globals) {
245+
options = options || {};
246+
247+
return {
248+
method: checkHttpMethod(options.method || validHttpMethods[0]);
249+
serializer: checkSerializer(options.serializer || globals.dataSerializer);
250+
timeout: checkTimeoutValue(options.timeout || globals.timeoutInSeconds);
251+
headers: checkHeadersObject(options.headers || {});
252+
params: checkParamsObject(options.params || {});
253+
data: options.data || null;
254+
};
255+
}

www/messages.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@ module.exports = {
44
DEPRECATED_VDN: 'advanced-http: "validateDomainName" is no more supported, please see change log for further info',
55
HEADER_VALUE_MUST_BE_STRING: 'advanced-http: header values must be strings',
66
MANDATORY_SUCCESS: 'advanced-http: missing mandatory "onSuccess" callback function',
7-
MANDATORY_FAIL: 'advanced-http: missing mandatory "onFail" callback function'
7+
MANDATORY_FAIL: 'advanced-http: missing mandatory "onFail" callback function',
8+
INVALID_HTTP_METHOD: 'advanced-http: invalid HTTP method, supported methods are:',
9+
INVALID_DATA_SERIALIZER: 'advanced-http: invalid serializer, supported serializers are:',
10+
INVALID_TIMEOUT_VALUE: 'advanced-http: invalid timeout value, needs to be a positive float value',
11+
INVALID_HEADERS_VALUE: 'advanced-http: invalid headers object, needs to be an object with strings',
12+
INVALID_PARAMS_VALUE: 'advanced-http: invalid params object, needs to be an object with strings'
813
};

0 commit comments

Comments
 (0)