Push.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPushStatus = getPushStatus;
  6. exports.send = send;
  7. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  8. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  9. function _interopRequireDefault(e) {
  10. return e && e.__esModule ? e : {
  11. default: e
  12. };
  13. }
  14. /**
  15. * Contains functions to deal with Push in Parse.
  16. *
  17. * @class Parse.Push
  18. * @static
  19. * @hideconstructor
  20. */
  21. /**
  22. * Sends a push notification.
  23. * **Available in Cloud Code only.**
  24. *
  25. * See {@link https://docs.parseplatform.org/js/guide/#push-notifications Push Notification Guide}
  26. *
  27. * @function send
  28. * @name Parse.Push.send
  29. * @param {object} data - The data of the push notification. Valid fields
  30. * are:
  31. * <ol>
  32. * <li>channels - An Array of channels to push to.</li>
  33. * <li>push_time - A Date object for when to send the push.</li>
  34. * <li>expiration_time - A Date object for when to expire
  35. * the push.</li>
  36. * <li>expiration_interval - The seconds from now to expire the push.</li>
  37. * <li>where - A Parse.Query over Parse.Installation that is used to match
  38. * a set of installations to push to.</li>
  39. * <li>data - The data to send as part of the push.</li>
  40. * <ol>
  41. * @param {object} options Valid options
  42. * are:<ul>
  43. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  44. * be used for this request.
  45. * </ul>
  46. * @returns {Promise} A promise that is fulfilled when the push request
  47. * completes and returns `pushStatusId`.
  48. */
  49. function send(data, options = {}) {
  50. if (data.where && data.where instanceof _ParseQuery.default) {
  51. data.where = data.where.toJSON().where;
  52. }
  53. if (data.push_time && typeof data.push_time === 'object') {
  54. data.push_time = data.push_time.toJSON();
  55. }
  56. if (data.expiration_time && typeof data.expiration_time === 'object') {
  57. data.expiration_time = data.expiration_time.toJSON();
  58. }
  59. if (data.expiration_time && data.expiration_interval) {
  60. throw new Error('expiration_time and expiration_interval cannot both be set.');
  61. }
  62. const pushOptions = {
  63. useMasterKey: true
  64. };
  65. if (options.hasOwnProperty('useMasterKey')) {
  66. pushOptions.useMasterKey = options.useMasterKey;
  67. }
  68. return _CoreManager.default.getPushController().send(data, pushOptions);
  69. }
  70. /**
  71. * Gets push status by Id
  72. *
  73. * @function getPushStatus
  74. * @name Parse.Push.getPushStatus
  75. * @param {string} pushStatusId The Id of Push Status.
  76. * @param {object} options Valid options
  77. * are:<ul>
  78. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  79. * be used for this request.
  80. * </ul>
  81. * @returns {Parse.Object} Status of Push.
  82. */
  83. function getPushStatus(pushStatusId, options = {}) {
  84. const pushOptions = {
  85. useMasterKey: true
  86. };
  87. if (options.hasOwnProperty('useMasterKey')) {
  88. pushOptions.useMasterKey = options.useMasterKey;
  89. }
  90. const query = new _ParseQuery.default('_PushStatus');
  91. return query.get(pushStatusId, pushOptions);
  92. }
  93. const DefaultController = {
  94. async send(data, options) {
  95. var _response$_headers;
  96. options.returnStatus = true;
  97. const response = await _CoreManager.default.getRESTController().request('POST', 'push', data, options);
  98. return (_response$_headers = response._headers) === null || _response$_headers === void 0 ? void 0 : _response$_headers['X-Parse-Push-Status-Id'];
  99. }
  100. };
  101. _CoreManager.default.setPushController(DefaultController);