requester.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. import { logMessage } from './helpers';
  2. var Requester = /** @class */ (function () {
  3. function Requester(headers) {
  4. if (headers) {
  5. this._headers = headers;
  6. }
  7. }
  8. Requester.prototype.sendRequest = function (datafeedUrl, urlPath, params) {
  9. if (params !== undefined) {
  10. var paramKeys = Object.keys(params);
  11. if (paramKeys.length !== 0) {
  12. urlPath += '?';
  13. }
  14. urlPath += paramKeys.map(function (key) {
  15. return encodeURIComponent(key) + "=" + encodeURIComponent(params[key].toString());
  16. }).join('&');
  17. }
  18. logMessage('New request: ' + urlPath);
  19. // Send user cookies if the URL is on the same origin as the calling script.
  20. var options = { credentials: 'same-origin' };
  21. if (this._headers !== undefined) {
  22. options.headers = this._headers;
  23. }
  24. return fetch(datafeedUrl + "/" + urlPath, options)
  25. .then(function (response) { return response.text(); })
  26. .then(function (responseTest) { return JSON.parse(responseTest); });
  27. };
  28. return Requester;
  29. }());
  30. export { Requester };