Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

public-interface.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. cordova.define("cordova-plugin-advanced-http.public-interface", function(require, exports, module) {
  2. module.exports = function init(exec, cookieHandler, urlUtil, helpers, globalConfigs, errorCodes, ponyfills) {
  3. var publicInterface = {
  4. getBasicAuthHeader: getBasicAuthHeader,
  5. useBasicAuth: useBasicAuth,
  6. getHeaders: getHeaders,
  7. setHeader: setHeader,
  8. getDataSerializer: getDataSerializer,
  9. setDataSerializer: setDataSerializer,
  10. setCookie: setCookie,
  11. clearCookies: clearCookies,
  12. removeCookies: removeCookies,
  13. getCookieString: getCookieString,
  14. getRequestTimeout: getRequestTimeout,
  15. setRequestTimeout: setRequestTimeout,
  16. getFollowRedirect: getFollowRedirect,
  17. setFollowRedirect: setFollowRedirect,
  18. setServerTrustMode: setServerTrustMode,
  19. setClientAuthMode: setClientAuthMode,
  20. sendRequest: sendRequest,
  21. post: post,
  22. put: put,
  23. patch: patch,
  24. get: get,
  25. delete: del,
  26. head: head,
  27. options: options,
  28. uploadFile: uploadFile,
  29. downloadFile: downloadFile,
  30. ErrorCode: errorCodes,
  31. ponyfills: ponyfills
  32. };
  33. function getBasicAuthHeader(username, password) {
  34. return { 'Authorization': 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password) };
  35. }
  36. function useBasicAuth(username, password) {
  37. this.setHeader('*', 'Authorization', 'Basic ' + helpers.b64EncodeUnicode(username + ':' + password));
  38. }
  39. function getHeaders(host) {
  40. return globalConfigs.headers[host || '*'] || null;
  41. }
  42. function setHeader() {
  43. // this one is for being backward compatible
  44. var host = '*';
  45. var header = arguments[0];
  46. var value = arguments[1];
  47. if (arguments.length === 3) {
  48. host = arguments[0];
  49. header = arguments[1];
  50. value = arguments[2];
  51. }
  52. helpers.checkForBlacklistedHeaderKey(header);
  53. helpers.checkForInvalidHeaderValue(value);
  54. globalConfigs.headers[host] = globalConfigs.headers[host] || {};
  55. if (value === null) {
  56. delete globalConfigs.headers[host][header];
  57. } else {
  58. globalConfigs.headers[host][header] = value;
  59. }
  60. }
  61. function getDataSerializer() {
  62. return globalConfigs.serializer;
  63. }
  64. function setDataSerializer(serializer) {
  65. globalConfigs.serializer = helpers.checkSerializer(serializer);
  66. }
  67. function setCookie(url, cookie, options) {
  68. cookieHandler.setCookie(url, cookie, options);
  69. }
  70. function clearCookies() {
  71. cookieHandler.clearCookies();
  72. }
  73. function removeCookies(url, callback) {
  74. cookieHandler.removeCookies(url, callback);
  75. }
  76. function getCookieString(url) {
  77. return cookieHandler.getCookieString(url);
  78. }
  79. function getRequestTimeout() {
  80. return globalConfigs.timeout;
  81. }
  82. function setRequestTimeout(timeout) {
  83. globalConfigs.timeout = helpers.checkTimeoutValue(timeout);
  84. }
  85. function getFollowRedirect() {
  86. return globalConfigs.followRedirect;
  87. }
  88. function setFollowRedirect(follow) {
  89. globalConfigs.followRedirect = helpers.checkFollowRedirectValue(follow);
  90. }
  91. function setServerTrustMode(mode, success, failure) {
  92. helpers.handleMissingCallbacks(success, failure);
  93. return exec(success, failure, 'CordovaHttpPlugin', 'setServerTrustMode', [helpers.checkSSLCertMode(mode)]);
  94. }
  95. function setClientAuthMode() {
  96. var mode = arguments[0];
  97. var options = null;
  98. var success = arguments[1];
  99. var failure = arguments[2];
  100. if (arguments.length === 4) {
  101. options = arguments[1];
  102. success = arguments[2];
  103. failure = arguments[3];
  104. }
  105. mode = helpers.checkClientAuthMode(mode);
  106. options = helpers.checkClientAuthOptions(mode, options);
  107. helpers.handleMissingCallbacks(success, failure);
  108. return exec(success, failure, 'CordovaHttpPlugin', 'setClientAuthMode', [mode, options.alias, options.rawPkcs, options.pkcsPassword]);
  109. }
  110. function sendRequest(url, options, success, failure) {
  111. helpers.handleMissingCallbacks(success, failure);
  112. options = helpers.handleMissingOptions(options, globalConfigs);
  113. url = urlUtil.appendQueryParamsString(url, urlUtil.serializeQueryParams(options.params, true));
  114. var headers = helpers.getMergedHeaders(url, options.headers, globalConfigs.headers);
  115. var onFail = helpers.injectCookieHandler(url, failure);
  116. var onSuccess = helpers.injectCookieHandler(url, helpers.injectRawResponseHandler(options.responseType, success, failure));
  117. switch (options.method) {
  118. case 'post':
  119. case 'put':
  120. case 'patch':
  121. return helpers.processData(options.data, options.serializer, function (data) {
  122. exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, data, options.serializer, headers, options.timeout, options.followRedirect, options.responseType]);
  123. });
  124. case 'upload':
  125. var fileOptions = helpers.checkUploadFileOptions(options.filePath, options.name);
  126. return exec(onSuccess, onFail, 'CordovaHttpPlugin', 'uploadFiles', [url, headers, fileOptions.filePaths, fileOptions.names, options.timeout, options.followRedirect, options.responseType]);
  127. case 'download':
  128. var filePath = helpers.checkDownloadFilePath(options.filePath);
  129. var onDownloadSuccess = helpers.injectCookieHandler(url, helpers.injectFileEntryHandler(success));
  130. return exec(onDownloadSuccess, onFail, 'CordovaHttpPlugin', 'downloadFile', [url, headers, filePath, options.timeout, options.followRedirect]);
  131. default:
  132. return exec(onSuccess, onFail, 'CordovaHttpPlugin', options.method, [url, headers, options.timeout, options.followRedirect, options.responseType]);
  133. }
  134. }
  135. function post(url, data, headers, success, failure) {
  136. return publicInterface.sendRequest(url, { method: 'post', data: data, headers: headers }, success, failure);
  137. };
  138. function put(url, data, headers, success, failure) {
  139. return publicInterface.sendRequest(url, { method: 'put', data: data, headers: headers }, success, failure);
  140. }
  141. function patch(url, data, headers, success, failure) {
  142. return publicInterface.sendRequest(url, { method: 'patch', data: data, headers: headers }, success, failure);
  143. }
  144. function get(url, params, headers, success, failure) {
  145. return publicInterface.sendRequest(url, { method: 'get', params: params, headers: headers }, success, failure);
  146. };
  147. function del(url, params, headers, success, failure) {
  148. return publicInterface.sendRequest(url, { method: 'delete', params: params, headers: headers }, success, failure);
  149. }
  150. function head(url, params, headers, success, failure) {
  151. return publicInterface.sendRequest(url, { method: 'head', params: params, headers: headers }, success, failure);
  152. }
  153. function options(url, params, headers, success, failure) {
  154. return publicInterface.sendRequest(url, { method: 'options', params: params, headers: headers }, success, failure);
  155. };
  156. function uploadFile(url, params, headers, filePath, name, success, failure) {
  157. return publicInterface.sendRequest(url, { method: 'upload', params: params, headers: headers, filePath: filePath, name: name }, success, failure);
  158. }
  159. function downloadFile(url, params, headers, filePath, success, failure) {
  160. return publicInterface.sendRequest(url, { method: 'download', params: params, headers: headers, filePath: filePath }, success, failure);
  161. }
  162. return publicInterface;
  163. }
  164. });