|
| 1 | +var pluginId = module.id.slice(0, module.id.lastIndexOf('.')); |
| 2 | + |
| 3 | +var cordovaProxy = require('cordova/exec/proxy'); |
| 4 | +var helpers = require(pluginId + '.helpers'); |
| 5 | + |
| 6 | +function serializeJsonData(data) { |
| 7 | + try { |
| 8 | + return JSON.stringify(data); |
| 9 | + } catch (err) { |
| 10 | + return null; |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +function serializePrimitive(key, value) { |
| 15 | + if (value === null || value === undefined) { |
| 16 | + return encodeURIComponent(key) + '='; |
| 17 | + } |
| 18 | + |
| 19 | + return encodeURIComponent(key) + '=' + encodeURIComponent(value); |
| 20 | +} |
| 21 | + |
| 22 | +function serializeArray(key, values) { |
| 23 | + return values.map(function(value) { |
| 24 | + return encodeURIComponent(key) + '[]=' + encodeURIComponent(value); |
| 25 | + }).join('&'); |
| 26 | +} |
| 27 | + |
| 28 | +function serializeParams(params) { |
| 29 | + if (params === null) return ''; |
| 30 | + |
| 31 | + return Object.keys(params).map(function(key) { |
| 32 | + if (helpers.getTypeOf(params[key]) === 'Array') { |
| 33 | + return serializeArray(key, params[key]); |
| 34 | + } |
| 35 | + |
| 36 | + return serializePrimitive(key, params[key]); |
| 37 | + }).join('&'); |
| 38 | +} |
| 39 | + |
| 40 | +function createXhrSuccessObject(xhr) { |
| 41 | + return { |
| 42 | + url: xhr.responseURL, |
| 43 | + status: xhr.status, |
| 44 | + data: helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response, |
| 45 | + headers: xhr.getAllResponseHeaders() |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +function createXhrFailureObject(xhr) { |
| 50 | + var obj = {}; |
| 51 | + |
| 52 | + obj.headers = xhr.getAllResponseHeaders(); |
| 53 | + obj.error = helpers.getTypeOf(xhr.responseText) === 'String' ? xhr.responseText : xhr.response; |
| 54 | + obj.error = obj.error || 'advanced-http: please check browser console for error messages'; |
| 55 | + |
| 56 | + if (xhr.responseURL) obj.url = xhr.responseURL; |
| 57 | + if (xhr.status) obj.status = xhr.status; |
| 58 | + |
| 59 | + return obj; |
| 60 | +} |
| 61 | + |
| 62 | +function setHeaders(xhr, headers) { |
| 63 | + Object.keys(headers).forEach(function(key) { |
| 64 | + if (key === 'Cookie') return; |
| 65 | + |
| 66 | + xhr.setRequestHeader(key, headers[key]); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +function sendRequest(method, withData, opts, success, failure) { |
| 71 | + var data = withData ? opts[1] : null; |
| 72 | + var params = withData ? null : serializeParams(opts[1]); |
| 73 | + var serializer = withData ? opts[2] : null; |
| 74 | + var headers = withData ? opts[3] : opts[2]; |
| 75 | + var timeout = withData ? opts[4] : opts[3]; |
| 76 | + var url = params ? opts[0] + '?' + params : opts[0]; |
| 77 | + |
| 78 | + var processedData = null; |
| 79 | + var xhr = new XMLHttpRequest(); |
| 80 | + |
| 81 | + xhr.open(method, url); |
| 82 | + |
| 83 | + if (headers.Cookie && headers.Cookie.length > 0) { |
| 84 | + return failure('advanced-http: custom cookies not supported on browser platform'); |
| 85 | + } |
| 86 | + |
| 87 | + switch (serializer) { |
| 88 | + case 'json': |
| 89 | + processedData = serializeJsonData(data); |
| 90 | + |
| 91 | + if (processedData === null) { |
| 92 | + return failure('advanced-http: failed serializing data'); |
| 93 | + } |
| 94 | + |
| 95 | + break; |
| 96 | + |
| 97 | + case 'utf8': |
| 98 | + xhr.setRequestHeader('Content-type', 'application/json; charset=utf8'); |
| 99 | + processedData = data.text; |
| 100 | + break; |
| 101 | + |
| 102 | + case 'urlencoded': |
| 103 | + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); |
| 104 | + processedData = serializeParams(data); |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + xhr.timeout = timeout * 1000; |
| 109 | + setHeaders(xhr, headers); |
| 110 | + |
| 111 | + xhr.onerror = xhr.ontimeout = function () { |
| 112 | + return failure(createXhrFailureObject(xhr)); |
| 113 | + }; |
| 114 | + |
| 115 | + xhr.onload = function () { |
| 116 | + if (xhr.readyState !== xhr.DONE) return; |
| 117 | + |
| 118 | + if (xhr.status < 200 || xhr.status > 299) { |
| 119 | + return failure(createXhrFailureObject(xhr)); |
| 120 | + } |
| 121 | + |
| 122 | + return success(createXhrSuccessObject(xhr)); |
| 123 | + }; |
| 124 | + |
| 125 | + xhr.send(processedData); |
| 126 | +} |
| 127 | + |
| 128 | +var browserInterface = { |
| 129 | + post: function (success, failure, opts) { |
| 130 | + return sendRequest('post', true, opts, success, failure); |
| 131 | + }, |
| 132 | + get: function (success, failure, opts) { |
| 133 | + return sendRequest('get', false, opts, success, failure); |
| 134 | + }, |
| 135 | + put: function (success, failure, opts) { |
| 136 | + return sendRequest('put', true, opts, success, failure); |
| 137 | + }, |
| 138 | + patch: function (success, failure, opts) { |
| 139 | + return sendRequest('patch', true, opts, success, failure); |
| 140 | + }, |
| 141 | + delete: function (success, failure, opts) { |
| 142 | + return sendRequest('delete', false, opts, success, failure); |
| 143 | + }, |
| 144 | + head: function (success, failure, opts) { |
| 145 | + return sendRequest('head', false, opts, success, failure); |
| 146 | + }, |
| 147 | + uploadFile: function (success, failure, opts) { |
| 148 | + return failure('advanced-http: function "uploadFile" not supported on browser platform'); |
| 149 | + }, |
| 150 | + downloadFile: function (success, failure, opts) { |
| 151 | + return failure('advanced-http: function "downloadFile" not supported on browser platform'); |
| 152 | + }, |
| 153 | + enableSSLPinning: function (success, failure, opts) { |
| 154 | + return failure('advanced-http: function "enableSSLPinning" not supported on browser platform'); |
| 155 | + }, |
| 156 | + acceptAllCerts: function (success, failure, opts) { |
| 157 | + return failure('advanced-http: function "acceptAllCerts" not supported on browser platform'); |
| 158 | + }, |
| 159 | + disableRedirect: function (success, failure, opts) { |
| 160 | + return failure('advanced-http: function "disableRedirect" not supported on browser platform'); |
| 161 | + } |
| 162 | +}; |
| 163 | + |
| 164 | +module.exports = browserInterface; |
| 165 | +cordovaProxy.add('CordovaHttpPlugin', browserInterface); |
0 commit comments