geturl.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. import { arrayify } from "@ethersproject/bytes";
  12. export function getUrl(href, options) {
  13. return __awaiter(this, void 0, void 0, function* () {
  14. if (options == null) {
  15. options = {};
  16. }
  17. const request = {
  18. method: (options.method || "GET"),
  19. headers: (options.headers || {}),
  20. body: (options.body || undefined),
  21. };
  22. if (options.skipFetchSetup !== true) {
  23. request.mode = "cors"; // no-cors, cors, *same-origin
  24. request.cache = "no-cache"; // *default, no-cache, reload, force-cache, only-if-cached
  25. request.credentials = "same-origin"; // include, *same-origin, omit
  26. request.redirect = "follow"; // manual, *follow, error
  27. request.referrer = "client"; // no-referrer, *client
  28. }
  29. ;
  30. if (options.fetchOptions != null) {
  31. const opts = options.fetchOptions;
  32. if (opts.mode) {
  33. request.mode = (opts.mode);
  34. }
  35. if (opts.cache) {
  36. request.cache = (opts.cache);
  37. }
  38. if (opts.credentials) {
  39. request.credentials = (opts.credentials);
  40. }
  41. if (opts.redirect) {
  42. request.redirect = (opts.redirect);
  43. }
  44. if (opts.referrer) {
  45. request.referrer = opts.referrer;
  46. }
  47. }
  48. const response = yield fetch(href, request);
  49. const body = yield response.arrayBuffer();
  50. const headers = {};
  51. if (response.headers.forEach) {
  52. response.headers.forEach((value, key) => {
  53. headers[key.toLowerCase()] = value;
  54. });
  55. }
  56. else {
  57. ((response.headers).keys)().forEach((key) => {
  58. headers[key.toLowerCase()] = response.headers.get(key);
  59. });
  60. }
  61. return {
  62. headers: headers,
  63. statusCode: response.status,
  64. statusMessage: response.statusText,
  65. body: arrayify(new Uint8Array(body)),
  66. };
  67. });
  68. }
  69. //# sourceMappingURL=geturl.js.map