UNPKG

@elbwalker/destination-web-api

Version:

API web destination for walkerOS

1 lines 1.98 kB
var t={merge:!0,shallow:!0,extend:!0};function e(t){return void 0===t||"string"==typeof t?t:JSON.stringify(t)}function r(e={}){return function(e,r={},n={}){n={...t,...n};const o=Object.entries(r).reduce(((t,[r,o])=>{const s=e[r];return n.merge&&Array.isArray(s)&&Array.isArray(o)?t[r]=o.reduce(((t,e)=>t.includes(e)?t:[...t,e]),[...s]):(n.extend||r in e)&&(t[r]=o),t}),{});return n.shallow?{...e,...o}:(Object.assign(e,o),e)}({"Content-Type":"application/json; charset=utf-8"},e)}function n(t,e){return function(...r){try{return t(...r)}catch(t){if(!e)return;return e(t)}}}function o(t,o,s={transport:"fetch"}){switch(s.transport||"fetch"){case"beacon":return function(t,r){const n=e(r),o=navigator.sendBeacon(t,n);return{ok:o,error:o?void 0:"Failed to send beacon"}}(t,o);case"xhr":return function(t,o,s={}){const a=r(s.headers),c=s.method||"POST",i=e(o);return n((()=>{const e=new XMLHttpRequest;e.open(c,t,!1);for(const t in a)e.setRequestHeader(t,a[t]);e.send(i);const r=e.status>=200&&e.status<300;return{ok:r,data:n(JSON.parse,(()=>e.response))(e.response),error:r?void 0:`${e.status} ${e.statusText}`}}),(t=>({ok:!1,error:t.message})))()}(t,o,s);default:return async function(t,n,o={}){const s=r(o.headers),a=e(n);return(c=async()=>{const e=await fetch(t,{method:o.method||"POST",headers:s,keepalive:!0,credentials:o.credentials||"same-origin",mode:o.noCors?"no-cors":"cors",body:a}),r=o.noCors?"":await e.text();return{ok:e.ok,data:r,error:e.ok?void 0:e.statusText}},i=t=>({ok:!1,error:t.message}),async function(...t){try{return await c(...t)}catch(t){if(!i)return;return await i(t)}})();var c,i}(t,o,s)}}var s={},a={type:"api",config:{},push(t,e,r,n={}){const{custom:s={},fn:a}=e,{url:c,headers:i,method:u,transform:d,transport:f="fetch"}=s;if(!c)return;const h=void 0!==n.data?n.data:t;(a||o)(c,d?d(h,e,r):JSON.stringify(h),{headers:i,method:u,transport:f})}},c=a;export{s as DestinationWebAPI,c as default,a as destinationWebAPI};//# sourceMappingURL=index.mjs.map