API Docs for: 0.0.8
Show:

File: client/lib/xhr.js

  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define([
  5. 'p-promise',
  6. 'components/micrajax/micrajax',
  7. './function'
  8. ], function (p, micrajax, FunctionHelpers) {
  9. 'use strict';
  10. var partial = FunctionHelpers.partial;
  11. var NodeXMLHttpRequest;
  12. try {
  13. // If embedded in node, use the xhr2 module
  14. if (typeof require !== 'undefined') {
  15. NodeXMLHttpRequest = require('xhr2');
  16. }
  17. } catch (e) {
  18. NodeXMLHttpRequest = null;
  19. }
  20. function getXHRObject(xhr) {
  21. if (xhr) {
  22. return xhr;
  23. } else if (NodeXMLHttpRequest) {
  24. return new NodeXMLHttpRequest();
  25. }
  26. // fallback to the system default
  27. }
  28. /**
  29. * Provides XHR functionality for use in either a browser or node
  30. * environment.
  31. *
  32. * @class Xhr
  33. * @static
  34. */
  35. function request(method, path, data, options) {
  36. options = options || {};
  37. var deferred = p.defer();
  38. micrajax.ajax({
  39. type: method,
  40. url: path,
  41. data: data,
  42. contentType: options.contentType || 'application/json',
  43. headers: options.headers,
  44. xhr: getXHRObject(options.xhr),
  45. success: function (data, responseText, jqXHR) {
  46. deferred.resolve(data);
  47. },
  48. error: function (jqXHR, status, responseText) {
  49. deferred.reject(responseText);
  50. }
  51. });
  52. return deferred.promise;
  53. }
  54. var XHR = {
  55. /**
  56. * Perform a GET request
  57. * @method get
  58. * @param {String} path
  59. * endpoint URL
  60. * @param {Object || String} [data]
  61. * data to send
  62. * @param {Object} [options={}]
  63. * Options
  64. * @param {String} [options.contentType]
  65. * Content type of `data`. Defaults to `application/json`
  66. * @param {Object} [options.headers]
  67. * Headers to pass with request.
  68. * @param {Object} [options.xhr]
  69. * XMLHttpRequest compatible object to use for XHR requests
  70. * @return {Promise} A promise that will be fulfilled with JSON `xhr.responseText` of the request
  71. */
  72. get: partial(request, 'GET'),
  73. /**
  74. * Perform a POST request
  75. * @method post
  76. * @param {String} path
  77. * endpoint URL
  78. * @param {Object || String} [data]
  79. * data to send
  80. * @param {Object} [options={}]
  81. * Options
  82. * @param {String} [options.contentType]
  83. * Content type of `data`. Defaults to `application/json`
  84. * @param {Object} [options.headers]
  85. * Headers to pass with request.
  86. * @param {Object} [options.xhr]
  87. * XMLHttpRequest compatible object to use for XHR requests
  88. * @return {Promise} A promise that will be fulfilled with JSON `xhr.responseText` of the request
  89. */
  90. post: partial(request, 'POST')
  91. };
  92. return XHR;
  93. });