UNPKG

@toruslabs/http-helpers

Version:
1 lines 10.6 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.HttpHelpers=t():e.HttpHelpers=t()}(self,(()=>(()=>{var e={65:function(e,t,n){var o,r;!function(){"use strict";o=function(){var e=function(){},t="undefined",n=typeof window!==t&&typeof window.navigator!==t&&/Trident\/|MSIE /.test(window.navigator.userAgent),o=["trace","debug","info","warn","error"],r={},c=null;function i(e,t){var n=e[t];if("function"==typeof n.bind)return n.bind(e);try{return Function.prototype.bind.call(n,e)}catch(t){return function(){return Function.prototype.apply.apply(n,[e,arguments])}}}function a(){console.log&&(console.log.apply?console.log.apply(console,arguments):Function.prototype.apply.apply(console.log,[console,arguments])),console.trace&&console.trace()}function s(){for(var n=this.getLevel(),r=0;r<o.length;r++){var c=o[r];this[c]=r<n?e:this.methodFactory(c,n,this.name)}if(this.log=this.debug,typeof console===t&&n<this.levels.SILENT)return"No console available for logging"}function l(e){return function(){typeof console!==t&&(s.call(this),this[e].apply(this,arguments))}}function u(o,r,c){return function(o){return"debug"===o&&(o="log"),typeof console!==t&&("trace"===o&&n?a:void 0!==console[o]?i(console,o):void 0!==console.log?i(console,"log"):e)}(o)||l.apply(this,arguments)}function f(e,n){var i,a,l,f=this,p="loglevel";function y(){var e;if(typeof window!==t&&p){try{e=window.localStorage[p]}catch(e){}if(typeof e===t)try{var n=window.document.cookie,o=encodeURIComponent(p),r=n.indexOf(o+"=");-1!==r&&(e=/^([^;]+)/.exec(n.slice(r+o.length+1))[1])}catch(e){}return void 0===f.levels[e]&&(e=void 0),e}}function d(e){var t=e;if("string"==typeof t&&void 0!==f.levels[t.toUpperCase()]&&(t=f.levels[t.toUpperCase()]),"number"==typeof t&&t>=0&&t<=f.levels.SILENT)return t;throw new TypeError("log.setLevel() called with invalid level: "+e)}"string"==typeof e?p+=":"+e:"symbol"==typeof e&&(p=void 0),f.name=e,f.levels={TRACE:0,DEBUG:1,INFO:2,WARN:3,ERROR:4,SILENT:5},f.methodFactory=n||u,f.getLevel=function(){return null!=l?l:null!=a?a:i},f.setLevel=function(e,n){return l=d(e),!1!==n&&function(e){var n=(o[e]||"silent").toUpperCase();if(typeof window!==t&&p){try{return void(window.localStorage[p]=n)}catch(e){}try{window.document.cookie=encodeURIComponent(p)+"="+n+";"}catch(e){}}}(l),s.call(f)},f.setDefaultLevel=function(e){a=d(e),y()||f.setLevel(e,!1)},f.resetLevel=function(){l=null,function(){if(typeof window!==t&&p){try{window.localStorage.removeItem(p)}catch(e){}try{window.document.cookie=encodeURIComponent(p)+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"}catch(e){}}}(),s.call(f)},f.enableAll=function(e){f.setLevel(f.levels.TRACE,e)},f.disableAll=function(e){f.setLevel(f.levels.SILENT,e)},f.rebuild=function(){if(c!==f&&(i=d(c.getLevel())),s.call(f),c===f)for(var e in r)r[e].rebuild()},i=d(c?c.getLevel():"WARN");var h=y();null!=h&&(l=d(h)),s.call(f)}(c=new f).getLogger=function(e){if("symbol"!=typeof e&&"string"!=typeof e||""===e)throw new TypeError("You must supply a name when creating a logger.");var t=r[e];return t||(t=r[e]=new f(e,c.methodFactory)),t};var p=typeof window!==t?window.log:void 0;return c.noConflict=function(){return typeof window!==t&&window.log===c&&(window.log=p),c},c.getLoggers=function(){return r},c.default=c,c},void 0===(r=o.call(t,n,t,e))||(e.exports=r)}()},744:e=>{"use strict";var t=function(e){return function(e){return!!e&&"object"==typeof e}(e)&&!function(e){var t=Object.prototype.toString.call(e);return"[object RegExp]"===t||"[object Date]"===t||function(e){return e.$$typeof===n}(e)}(e)},n="function"==typeof Symbol&&Symbol.for?Symbol.for("react.element"):60103;function o(e,t){return!1!==t.clone&&t.isMergeableObject(e)?a((n=e,Array.isArray(n)?[]:{}),e,t):e;var n}function r(e,t,n){return e.concat(t).map((function(e){return o(e,n)}))}function c(e){return Object.keys(e).concat(function(e){return Object.getOwnPropertySymbols?Object.getOwnPropertySymbols(e).filter((function(t){return Object.propertyIsEnumerable.call(e,t)})):[]}(e))}function i(e,t){try{return t in e}catch(e){return!1}}function a(e,n,s){(s=s||{}).arrayMerge=s.arrayMerge||r,s.isMergeableObject=s.isMergeableObject||t,s.cloneUnlessOtherwiseSpecified=o;var l=Array.isArray(n);return l===Array.isArray(e)?l?s.arrayMerge(e,n,s):function(e,t,n){var r={};return n.isMergeableObject(e)&&c(e).forEach((function(t){r[t]=o(e[t],n)})),c(t).forEach((function(c){(function(e,t){return i(e,t)&&!(Object.hasOwnProperty.call(e,t)&&Object.propertyIsEnumerable.call(e,t))})(e,c)||(i(e,c)&&n.isMergeableObject(t[c])?r[c]=function(e,t){if(!t.customMerge)return a;var n=t.customMerge(e);return"function"==typeof n?n:a}(c,n)(e[c],t[c],n):r[c]=o(t[c],n))})),r}(e,n,s):o(n,s)}a.all=function(e,t){if(!Array.isArray(e))throw new Error("first argument should be an array");return e.reduce((function(e,n){return a(e,n,t)}),{})};var s=a;e.exports=s}},t={};function n(o){var r=t[o];if(void 0!==r)return r.exports;var c=t[o]={exports:{}};return e[o].call(c.exports,c,c.exports,n),c.exports}n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{"use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(t,n,o){return(n=function(t){var n=function(t){if("object"!=e(t)||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var o=n.call(t,"string");if("object"!=e(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==e(n)?n:n+""}(n))in t?Object.defineProperty(t,n,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[n]=o,t}function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,o)}return n}function c(e){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?r(Object(o),!0).forEach((function(n){t(e,n,o[n])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):r(Object(o)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))}))}return e}n.r(o),n.d(o,{clearAPIKey:()=>O,clearEmbedHost:()=>v,enableSentryTracing:()=>g,gatewayAuthHeader:()=>p,gatewayEmbedHostHeader:()=>y,generateJsonRPCObject:()=>M,get:()=>C,getAPIKey:()=>T,getEmbedHost:()=>w,patch:()=>I,post:()=>L,promiseRace:()=>D,promiseTimeout:()=>x,put:()=>R,remove:()=>U,setAPIKey:()=>j,setEmbedHost:()=>m,setLogLevel:()=>S});var i=n(744),a=n.n(i),s=n(65);const l=n.n(s)().getLogger("http-helpers");l.setLevel(s.levels.INFO);let u="torus-default",f="";const p="x-api-key",y="x-embed-host";let d=null;const h=[],b=[];function g(e,t,n){d=e,h.push(...t),b.push(...n)}function m(e){f=e}function v(){f=""}function w(){return f}function j(e){u=e}function O(){u="torus-default"}function T(){return u}function S(e){l.setLevel(e)}async function E(e,t){let n=null;try{n=new URL(e)}catch{}return d&&n&&(h.includes(n.origin)||b.includes(n.pathname))?await d.startSpan({name:e,op:"http.client"},(async()=>await fetch(e,t))):fetch(e,t)}function P(){const e={};return u&&(e[p]=u),f&&(e[y]=f),e}function A(e){l.info(`Response: ${e.status} ${e.statusText}`),l.info(`Url: ${e.url}`)}const x=async(e,t)=>{let n=null;try{const o=new Promise(((t,o)=>{n=setTimeout((()=>{o(new Error(`Timed out in ${e}ms`))}),e)})),r=await Promise.race([t,o]);return null!=n&&clearTimeout(n),r}catch(e){throw null!=n&&clearTimeout(n),e}},C=async(e,t={},n={})=>{const o={mode:"cors",headers:{}};n.useAPIKey&&(o.headers=c(c({},o.headers),P())),t.method="GET";const r=a()(o,t),i=await E(e,r);if(i.ok){const e=i.headers.get("content-type");return null!=e&&e.includes("application/json")?i.json():i.text()}throw A(i),i},L=(e,t={},n={},o={})=>{const r={mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};o.useAPIKey&&(r.headers=c(c({},r.headers),P())),n.method="POST";const i=a()(r,n);return o.isUrlEncodedData?(i.body=t,"application/json; charset=utf-8"===i.headers["Content-Type"]&&delete i.headers["Content-Type"]):i.body=JSON.stringify(t),x(o.timeout||6e4,E(e,i).then((e=>{if(o.logTracingHeader&&function(e){const t=e.headers.get("x-web3-correlation-id");t&&l.info(`Request tracing with traceID = ${t}`)}(e),e.ok){const t=e.headers.get("content-type");return null!=t&&t.includes("application/json")?e.json():e.text()}throw A(e),e})))},I=async(e,t={},n={},o={})=>{const r={mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};o.useAPIKey&&(r.headers=c(c({},r.headers),P())),n.method="PATCH";const i=a()(r,n);o.isUrlEncodedData?(i.body=t,"application/json; charset=utf-8"===i.headers["Content-Type"]&&delete i.headers["Content-Type"]):i.body=JSON.stringify(t);const s=await E(e,i);if(s.ok){const e=s.headers.get("content-type");return null!=e&&e.includes("application/json")?s.json():s.text()}throw A(s),s},R=async(e,t={},n={},o={})=>{const r={mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};o.useAPIKey&&(r.headers=c(c({},r.headers),P())),n.method="PUT";const i=a()(r,n);o.isUrlEncodedData?(i.body=t,"application/json; charset=utf-8"===i.headers["Content-Type"]&&delete i.headers["Content-Type"]):i.body=JSON.stringify(t);const s=await E(e,i);if(s.ok){const e=s.headers.get("content-type");return null!=e&&e.includes("application/json")?s.json():s.text()}throw A(s),s},U=async(e,t={},n={},o={})=>{const r={mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};o.useAPIKey&&(r.headers=c(c({},r.headers),P())),n.method="DELETE";const i=a()(r,n);o.isUrlEncodedData?(i.body=t,"application/json; charset=utf-8"===i.headers["Content-Type"]&&delete i.headers["Content-Type"]):i.body=JSON.stringify(t);const s=await E(e,i);if(s.ok){const e=s.headers.get("content-type");return null!=e&&e.includes("application/json")?s.json():s.text()}throw A(s),s},M=(e,t)=>({jsonrpc:"2.0",method:e,id:10,params:t}),D=(e,t,n=6e4)=>Promise.race([C(e,t),new Promise(((e,t)=>{setTimeout((()=>{t(new Error("timed out"))}),n)}))])})(),o})()));