fetch-result-please.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name in all)
  7. __defProp(target, name, { get: all[name], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var fetch_result_please_exports = {};
  19. __export(fetch_result_please_exports, {
  20. DetailedError: () => DetailedError,
  21. fetchRP: () => fetchRP
  22. });
  23. module.exports = __toCommonJS(fetch_result_please_exports);
  24. const nullBodyResponses = /* @__PURE__ */ new Set([101, 204, 205, 304]);
  25. async function fetchRP(fetchRes) {
  26. const _fetchRes = await fetchRes;
  27. const hasBody = (_fetchRes.body || _fetchRes._bodyInit) && !nullBodyResponses.has(_fetchRes.status);
  28. if (hasBody) {
  29. const responseType = detectResponseType(_fetchRes);
  30. _fetchRes._data = await _fetchRes[responseType]();
  31. }
  32. if (!_fetchRes.ok) {
  33. throw new DetailedError(`${_fetchRes.status} ${_fetchRes.statusText}`, {
  34. statusCode: _fetchRes?.status,
  35. detail: {
  36. data: _fetchRes?._data,
  37. statusText: _fetchRes?.statusText
  38. }
  39. });
  40. }
  41. return _fetchRes._data;
  42. }
  43. class DetailedError extends Error {
  44. /**
  45. * Additional `message` that will be logged AND returned to client
  46. */
  47. detail;
  48. /**
  49. * Additional `code` that will be logged AND returned to client
  50. */
  51. code;
  52. /**
  53. * Additional value that will be logged AND NOT returned to client
  54. */
  55. log;
  56. /**
  57. * Optionally set the status code to return, in a web server context
  58. */
  59. statusCode;
  60. constructor(message, options = {}) {
  61. super(message);
  62. this.name = "DetailedError";
  63. this.log = options.log;
  64. this.detail = options.detail;
  65. this.code = options.code;
  66. this.statusCode = options.statusCode;
  67. }
  68. }
  69. const jsonRegex = /^application\/(?:[\w!#$%&*.^`~-]*\+)?json(?:;.+)?$/i;
  70. function detectResponseType(response) {
  71. const _contentType = response.headers.get("content-type");
  72. if (!_contentType) {
  73. return "text";
  74. }
  75. const contentType = _contentType.split(";").shift();
  76. if (jsonRegex.test(contentType)) {
  77. return "json";
  78. }
  79. return "text";
  80. }
  81. // Annotate the CommonJS export names for ESM import in node:
  82. 0 && (module.exports = {
  83. DetailedError,
  84. fetchRP
  85. });