webclient-fetch.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. return new (P || (P = Promise))(function (resolve, reject) {
  4. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  5. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  6. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  7. step((generator = generator.apply(thisArg, _arguments || [])).next());
  8. });
  9. };
  10. Object.defineProperty(exports, "__esModule", { value: true });
  11. class WebClientFetch {
  12. request(options) {
  13. return __awaiter(this, void 0, void 0, function* () {
  14. const fetchOptions = {
  15. headers: options.headers,
  16. method: options.method
  17. };
  18. if (options.body) {
  19. fetchOptions.body = options.body;
  20. }
  21. const res = yield fetch(options.url, fetchOptions);
  22. console.log('res', res);
  23. return res.text();
  24. });
  25. }
  26. requestJSON(options) {
  27. return __awaiter(this, void 0, void 0, function* () {
  28. if (!options.headers) {
  29. options.headers = {};
  30. }
  31. if (options.body) {
  32. options.headers['content-type'] = 'application/json';
  33. if (typeof options.body === 'object') {
  34. options.body = JSON.stringify(options.body);
  35. }
  36. }
  37. options.headers.accept = 'application/json';
  38. const fetchOptions = {
  39. headers: options.headers,
  40. method: options.method
  41. };
  42. if (options.formData) {
  43. fetchOptions.formData = options.formData;
  44. }
  45. if (options.body) {
  46. fetchOptions.body = options.body;
  47. }
  48. const res = yield fetch(options.url, fetchOptions);
  49. console.log('res', res);
  50. return res.json();
  51. });
  52. }
  53. }
  54. exports.default = WebClientFetch;
  55. //# sourceMappingURL=webclient-fetch.js.map