UNPKG

@adyen/adyen-web

Version:

[![npm](https://img.shields.io/npm/v/@adyen/adyen-web.svg)](https://www.npmjs.com/package/@adyen/adyen-web)

3 lines (2 loc) 1.4 kB
import{DEFAULT_HTTP_TIMEOUT as e,FALLBACK_CONTEXT as r}from"../config.js";import o from"../Errors/AdyenCheckoutError.js";function t(t,n){const{headers:s=[],errorLevel:i="warn",errorCode:c,loadingContext:l=r,method:d="GET",path:u,timeout:f=e}=t,m={method:d,mode:"cors",cache:"default",credentials:"same-origin",headers:{Accept:"application/json, text/plain, */*","Content-Type":"POST"===d?"application/json":"text/plain",...s},redirect:"follow",referrerPolicy:"no-referrer-when-downgrade",...AbortSignal?.timeout&&{signal:AbortSignal?.timeout(f)},...n&&{body:JSON.stringify(n)}},g=`${l}${u}`;return fetch(g,m).then(async e=>{if(204===e.status)return;const r=await e.json();if(e.ok)return r;if(function(e){return e&&e.errorCode&&e.errorType&&e.message&&e.status}(r))return void a({message:r.message,level:i,cause:r,code:c});a({message:t.errorMessage||`Service at ${g} is not available`,level:i,cause:r,code:c})}).catch(e=>{if(e instanceof o)throw e;a({message:t.errorMessage||`Call to ${g} failed. Error= ${e}`,level:i,cause:e,code:c})})}function a({message:e,level:r,cause:t,code:a}){switch(r){case"silent":break;case"info":case"warn":case"error":console[r](e);break;default:throw new o("NETWORK_ERROR",e,{cause:t,code:a})}}function n(e,r){return t({...e,method:"GET"},r)}function s(e,r){return t({...e,method:"POST"},r)}export{t as http,n as httpGet,s as httpPost}; //# sourceMappingURL=http.js.map