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

public-interface.js 7.1KB

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