@elbwalker/destination-web-api
Version:
API web destination for walkerOS
1 lines • 2.44 kB
JavaScript
;var Destination=(()=>{var e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,n=Object.prototype.hasOwnProperty,o={};((t,r)=>{for(var n in r)e(t,n,{get:r[n],enumerable:!0})})(o,{DestinationWebAPI:()=>f,default:()=>p,destinationWebAPI:()=>l});var a={merge:!0,shallow:!0,extend:!0};function s(e){return void 0===e||"string"==typeof e?e:JSON.stringify(e)}function i(e={}){return function(e,t={},r={}){r={...a,...r};const n=Object.entries(t).reduce(((t,[n,o])=>{const a=e[n];return r.merge&&Array.isArray(a)&&Array.isArray(o)?t[n]=o.reduce(((e,t)=>e.includes(t)?e:[...e,t]),[...a]):(r.extend||n in e)&&(t[n]=o),t}),{});return r.shallow?{...e,...n}:(Object.assign(e,n),e)}({"Content-Type":"application/json; charset=utf-8"},e)}function c(e,t,r){return function(...n){try{return e(...n)}catch(e){if(!t)return;return t(e)}finally{null==r||r()}}}function u(e,t,r={transport:"fetch"}){switch(r.transport||"fetch"){case"beacon":return function(e,t){const r=s(t),n=navigator.sendBeacon(e,r);return{ok:n,error:n?void 0:"Failed to send beacon"}}(e,t);case"xhr":return function(e,t,r={}){const n=i(r.headers),o=r.method||"POST",a=s(t);return c((()=>{const t=new XMLHttpRequest;t.open(o,e,!1);for(const e in n)t.setRequestHeader(e,n[e]);t.send(a);const r=t.status>=200&&t.status<300;return{ok:r,data:c(JSON.parse,(()=>t.response))(t.response),error:r?void 0:`${t.status} ${t.statusText}`}}),(e=>({ok:!1,error:e.message})))()}(e,t,r);default:return async function(e,t,r={}){const n=i(r.headers),o=s(t);return(a=async()=>{const t=await fetch(e,{method:r.method||"POST",headers:n,keepalive:!0,credentials:r.credentials||"same-origin",mode:r.noCors?"no-cors":"cors",body:o}),a=r.noCors?"":await t.text();return{ok:t.ok,data:a,error:t.ok?void 0:t.statusText}},c=e=>({ok:!1,error:e.message}),async function(...e){try{return await a(...e)}catch(e){if(!c)return;return await c(e)}finally{await(null==u?void 0:u())}})();var a,c,u}(e,t,r)}}var d,f={},l={type:"api",config:{},push(e,t,r,n={}){const{custom:o={},fn:a}=t,{url:s,headers:i,method:c,transform:d,transport:f="fetch"}=o;if(!s)return;const l=void 0!==n.data?n.data:e;(a||u)(s,d?d(l,t,r):JSON.stringify(l),{headers:i,method:c,transport:f})}},p=l;return d=o,((o,a,s,i)=>{if(a&&"object"==typeof a||"function"==typeof a)for(let c of r(a))n.call(o,c)||c===s||e(o,c,{get:()=>a[c],enumerable:!(i=t(a,c))||i.enumerable});return o})(e({},"__esModule",{value:!0}),d)})();