@swirepay-developer/sparo-embed-sdk 1.0.8 → 1.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sparo-embed-sdk.global.js +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var y=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var I=(e,t)=>{for(var s in t)y(e,s,{get:t[s],enumerable:!0})},z=(e,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of M(t))!F.call(e,u)&&u!==s&&y(e,u,{get:()=>t[u],enumerable:!(r=D(t,u))||r.enumerable});return e};var N=e=>z(y({},"__esModule",{value:!0}),e);var U={};I(U,{createGuestTokenFetcher:()=>L,default:()=>G,embed:()=>_,normalizeGuestTokenResponse:()=>E});module.exports=N(U);function O(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function p(e,t,s){let r=new Error(t||e);return r.code=e,typeof s=="number"&&(r.status=s),r}function P(e){if(!e||typeof e!="object")throw p("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw p("invalid_token","Missing token in fetchGuestToken response");let t=Number(e.expiresIn||900);if(!Number.isFinite(t)||t<=0)throw p("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,expiresIn:t}}function H(e,t){let s=new URLSearchParams({embed:"true"});return typeof t=="string"&&t&&s.set("account",t),`${e}/embed.html?${s.toString()}`}function _(e){let{id:t="sparo-embed",sparoDomain:s,mountPoint:r,fetchGuestToken:u,uiConfig:k={},onReady:g,onError:d}=e||{};if(!r||!(r instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof u!="function")throw new Error("fetchGuestToken must be a function");let a=O(s),o=document.createElement("iframe");o.id=t,o.title="SPARO Assistant",o.style.width="100%",o.style.height="100%",o.style.border="0",o.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals"),o.setAttribute("referrerpolicy","no-referrer");let c=!0,f=!1,l=null,h=null,x=()=>{h&&(window.clearTimeout(h),h=null)},T=n=>{d&&d(b(n))},b=n=>n&&typeof n=="object"&&n.code?n:p("fetch_failed",(n==null?void 0:n.message)||"SPARO embed error"),R=()=>{if(!c||!f||!l)return;let n=o.contentWindow;!n||typeof n.postMessage!="function"||n.postMessage({type:"sparo:auth",token:l.token,...l.account?{account:l.account}:{},uiConfig:k},a)},v=n=>{x();let i=Math.max(5,n-30);h=window.setTimeout(()=>{w().catch(()=>{})},i*1e3)},w=async()=>{if(!c)return;let n;try{n=await u()}catch(j){let A=b(j);throw T(A),A}let i=P(n);l=i,v(i.expiresIn);let m=H(a,i.account);!o.src||o.src!==m?o.src=m:R()},S=n=>{if(!c||n.origin!==a)return;let i=n.data;if(!(!i||typeof i!="object"||typeof i.type!="string")){if(i.type==="sparo:ready"||i.type==="SPARO_EMBED_READY"){f=!0,R(),g&&g();return}if(i.type==="sparo:error"){let m=i.code||"iframe_error";if(m==="expired_token"){w().catch(()=>{});return}T(p(m,i.message||m,i.status))}}};return o.addEventListener("error",()=>{T(p("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),r.innerHTML="",r.appendChild(o),w().catch(()=>{}),{refresh:()=>w(),unmount:()=>{c=!1,x(),window.removeEventListener("message",S),o.parentNode===r&&r.removeChild(o)}}}var G=_;function E(e){return!e||typeof e!="object"?e:e.data&&typeof e.data=="object"&&e.data.token?e.data:e.entity&&typeof e.entity=="object"&&e.entity.token?e.entity:e}function L(e,t={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:r={}}=t,u=r.method||"GET",k=r.credentials??(s?"omit":"include");return async function(){let d=new Headers(r.headers||{});if(s){let f=await s();f&&d.set("Authorization",f.startsWith("Bearer ")?f:`Bearer ${f}`)}d.has("Accept")||d.set("Accept","application/json");let a=await fetch(e,{...r,method:u,credentials:k,headers:d}),o=a.headers.get("content-type")||"",c={};if(o.includes("application/json")?c=await a.json().catch(()=>({})):a.ok||(c={message:(await a.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!a.ok){let f=E(c)||c,l=new Error(f.message||f.error||c.message||c.error||`token fetch failed (${a.status})`);throw l.code=f.code||c.code||(a.status===403?"merchant_not_enabled":a.status===401?"invalid_token":"fetch_failed"),l.status=a.status,l}return E(c)}}0&&(module.exports={createGuestTokenFetcher,embed,normalizeGuestTokenResponse});
|
|
1
|
+
var y=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var I=(e,t)=>{for(var s in t)y(e,s,{get:t[s],enumerable:!0})},z=(e,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of M(t))!F.call(e,u)&&u!==s&&y(e,u,{get:()=>t[u],enumerable:!(r=D(t,u))||r.enumerable});return e};var N=e=>z(y({},"__esModule",{value:!0}),e);var U={};I(U,{createGuestTokenFetcher:()=>L,default:()=>G,embed:()=>_,normalizeGuestTokenResponse:()=>E});module.exports=N(U);function O(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function p(e,t,s){let r=new Error(t||e);return r.code=e,typeof s=="number"&&(r.status=s),r}function P(e){if(!e||typeof e!="object")throw p("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw p("invalid_token","Missing token in fetchGuestToken response");let t=Number(e.expiresIn||900);if(!Number.isFinite(t)||t<=0)throw p("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,expiresIn:t}}function H(e,t){let s=new URLSearchParams({embed:"true"});return typeof t=="string"&&t&&s.set("account",t),`${e}/embed.html?${s.toString()}`}function _(e){let{id:t="sparo-embed",sparoDomain:s,mountPoint:r,fetchGuestToken:u,uiConfig:k={},onReady:g,onError:d}=e||{};if(!r||!(r instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof u!="function")throw new Error("fetchGuestToken must be a function");let a=O(s),o=document.createElement("iframe");o.id=t,o.title="SPARO Assistant",o.style.width="100%",o.style.height="100%",o.style.border="0",o.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-downloads"),o.setAttribute("referrerpolicy","no-referrer");let c=!0,f=!1,l=null,h=null,x=()=>{h&&(window.clearTimeout(h),h=null)},T=n=>{d&&d(b(n))},b=n=>n&&typeof n=="object"&&n.code?n:p("fetch_failed",(n==null?void 0:n.message)||"SPARO embed error"),R=()=>{if(!c||!f||!l)return;let n=o.contentWindow;!n||typeof n.postMessage!="function"||n.postMessage({type:"sparo:auth",token:l.token,...l.account?{account:l.account}:{},uiConfig:k},a)},v=n=>{x();let i=Math.max(5,n-30);h=window.setTimeout(()=>{w().catch(()=>{})},i*1e3)},w=async()=>{if(!c)return;let n;try{n=await u()}catch(j){let A=b(j);throw T(A),A}let i=P(n);l=i,v(i.expiresIn);let m=H(a,i.account);!o.src||o.src!==m?o.src=m:R()},S=n=>{if(!c||n.origin!==a)return;let i=n.data;if(!(!i||typeof i!="object"||typeof i.type!="string")){if(i.type==="sparo:ready"||i.type==="SPARO_EMBED_READY"){f=!0,R(),g&&g();return}if(i.type==="sparo:error"){let m=i.code||"iframe_error";if(m==="expired_token"){w().catch(()=>{});return}T(p(m,i.message||m,i.status))}}};return o.addEventListener("error",()=>{T(p("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),r.innerHTML="",r.appendChild(o),w().catch(()=>{}),{refresh:()=>w(),unmount:()=>{c=!1,x(),window.removeEventListener("message",S),o.parentNode===r&&r.removeChild(o)}}}var G=_;function E(e){return!e||typeof e!="object"?e:e.data&&typeof e.data=="object"&&e.data.token?e.data:e.entity&&typeof e.entity=="object"&&e.entity.token?e.entity:e}function L(e,t={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:r={}}=t,u=r.method||"GET",k=r.credentials??(s?"omit":"include");return async function(){let d=new Headers(r.headers||{});if(s){let f=await s();f&&d.set("Authorization",f.startsWith("Bearer ")?f:`Bearer ${f}`)}d.has("Accept")||d.set("Accept","application/json");let a=await fetch(e,{...r,method:u,credentials:k,headers:d}),o=a.headers.get("content-type")||"",c={};if(o.includes("application/json")?c=await a.json().catch(()=>({})):a.ok||(c={message:(await a.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!a.ok){let f=E(c)||c,l=new Error(f.message||f.error||c.message||c.error||`token fetch failed (${a.status})`);throw l.code=f.code||c.code||(a.status===403?"merchant_not_enabled":a.status===401?"invalid_token":"fetch_failed"),l.status=a.status,l}return E(c)}}0&&(module.exports={createGuestTokenFetcher,embed,normalizeGuestTokenResponse});
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
function L(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function m(e,s,f){let r=new Error(s||e);return r.code=e,typeof f=="number"&&(r.status=f),r}function v(e){if(!e||typeof e!="object")throw m("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw m("invalid_token","Missing token in fetchGuestToken response");let s=Number(e.expiresIn||900);if(!Number.isFinite(s)||s<=0)throw m("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,expiresIn:s}}function j(e,s){let f=new URLSearchParams({embed:"true"});return typeof s=="string"&&s&&f.set("account",s),`${e}/embed.html?${f.toString()}`}function S(e){let{id:s="sparo-embed",sparoDomain:f,mountPoint:r,fetchGuestToken:p,uiConfig:E={},onReady:k,onError:l}=e||{};if(!r||!(r instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof p!="function")throw new Error("fetchGuestToken must be a function");let i=L(f),n=document.createElement("iframe");n.id=s,n.title="SPARO Assistant",n.style.width="100%",n.style.height="100%",n.style.border="0",n.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals"),n.setAttribute("referrerpolicy","no-referrer");let a=!0,c=!1,u=null,h=null,y=()=>{h&&(window.clearTimeout(h),h=null)},g=t=>{l&&l(_(t))},_=t=>t&&typeof t=="object"&&t.code?t:m("fetch_failed",(t==null?void 0:t.message)||"SPARO embed error"),x=()=>{if(!a||!c||!u)return;let t=n.contentWindow;!t||typeof t.postMessage!="function"||t.postMessage({type:"sparo:auth",token:u.token,...u.account?{account:u.account}:{},uiConfig:E},i)},A=t=>{y();let o=Math.max(5,t-30);h=window.setTimeout(()=>{w().catch(()=>{})},o*1e3)},w=async()=>{if(!a)return;let t;try{t=await p()}catch(G){let R=_(G);throw g(R),R}let o=v(t);u=o,A(o.expiresIn);let d=j(i,o.account);!n.src||n.src!==d?n.src=d:x()},b=t=>{if(!a||t.origin!==i)return;let o=t.data;if(!(!o||typeof o!="object"||typeof o.type!="string")){if(o.type==="sparo:ready"||o.type==="SPARO_EMBED_READY"){c=!0,x(),k&&k();return}if(o.type==="sparo:error"){let d=o.code||"iframe_error";if(d==="expired_token"){w().catch(()=>{});return}g(m(d,o.message||d,o.status))}}};return n.addEventListener("error",()=>{g(m("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",b),r.innerHTML="",r.appendChild(n),w().catch(()=>{}),{refresh:()=>w(),unmount:()=>{a=!1,y(),window.removeEventListener("message",b),n.parentNode===r&&r.removeChild(n)}}}var D=S;function T(e){return!e||typeof e!="object"?e:e.data&&typeof e.data=="object"&&e.data.token?e.data:e.entity&&typeof e.entity=="object"&&e.entity.token?e.entity:e}function M(e,s={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:f,init:r={}}=s,p=r.method||"GET",E=r.credentials??(f?"omit":"include");return async function(){let l=new Headers(r.headers||{});if(f){let c=await f();c&&l.set("Authorization",c.startsWith("Bearer ")?c:`Bearer ${c}`)}l.has("Accept")||l.set("Accept","application/json");let i=await fetch(e,{...r,method:p,credentials:E,headers:l}),n=i.headers.get("content-type")||"",a={};if(n.includes("application/json")?a=await i.json().catch(()=>({})):i.ok||(a={message:(await i.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!i.ok){let c=T(a)||a,u=new Error(c.message||c.error||a.message||a.error||`token fetch failed (${i.status})`);throw u.code=c.code||a.code||(i.status===403?"merchant_not_enabled":i.status===401?"invalid_token":"fetch_failed"),u.status=i.status,u}return T(a)}}export{M as createGuestTokenFetcher,D as default,S as embed,T as normalizeGuestTokenResponse};
|
|
1
|
+
function L(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function m(e,s,f){let r=new Error(s||e);return r.code=e,typeof f=="number"&&(r.status=f),r}function v(e){if(!e||typeof e!="object")throw m("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw m("invalid_token","Missing token in fetchGuestToken response");let s=Number(e.expiresIn||900);if(!Number.isFinite(s)||s<=0)throw m("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,expiresIn:s}}function j(e,s){let f=new URLSearchParams({embed:"true"});return typeof s=="string"&&s&&f.set("account",s),`${e}/embed.html?${f.toString()}`}function S(e){let{id:s="sparo-embed",sparoDomain:f,mountPoint:r,fetchGuestToken:p,uiConfig:E={},onReady:k,onError:l}=e||{};if(!r||!(r instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof p!="function")throw new Error("fetchGuestToken must be a function");let i=L(f),n=document.createElement("iframe");n.id=s,n.title="SPARO Assistant",n.style.width="100%",n.style.height="100%",n.style.border="0",n.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-downloads"),n.setAttribute("referrerpolicy","no-referrer");let a=!0,c=!1,u=null,h=null,y=()=>{h&&(window.clearTimeout(h),h=null)},g=t=>{l&&l(_(t))},_=t=>t&&typeof t=="object"&&t.code?t:m("fetch_failed",(t==null?void 0:t.message)||"SPARO embed error"),x=()=>{if(!a||!c||!u)return;let t=n.contentWindow;!t||typeof t.postMessage!="function"||t.postMessage({type:"sparo:auth",token:u.token,...u.account?{account:u.account}:{},uiConfig:E},i)},A=t=>{y();let o=Math.max(5,t-30);h=window.setTimeout(()=>{w().catch(()=>{})},o*1e3)},w=async()=>{if(!a)return;let t;try{t=await p()}catch(G){let R=_(G);throw g(R),R}let o=v(t);u=o,A(o.expiresIn);let d=j(i,o.account);!n.src||n.src!==d?n.src=d:x()},b=t=>{if(!a||t.origin!==i)return;let o=t.data;if(!(!o||typeof o!="object"||typeof o.type!="string")){if(o.type==="sparo:ready"||o.type==="SPARO_EMBED_READY"){c=!0,x(),k&&k();return}if(o.type==="sparo:error"){let d=o.code||"iframe_error";if(d==="expired_token"){w().catch(()=>{});return}g(m(d,o.message||d,o.status))}}};return n.addEventListener("error",()=>{g(m("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",b),r.innerHTML="",r.appendChild(n),w().catch(()=>{}),{refresh:()=>w(),unmount:()=>{a=!1,y(),window.removeEventListener("message",b),n.parentNode===r&&r.removeChild(n)}}}var D=S;function T(e){return!e||typeof e!="object"?e:e.data&&typeof e.data=="object"&&e.data.token?e.data:e.entity&&typeof e.entity=="object"&&e.entity.token?e.entity:e}function M(e,s={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:f,init:r={}}=s,p=r.method||"GET",E=r.credentials??(f?"omit":"include");return async function(){let l=new Headers(r.headers||{});if(f){let c=await f();c&&l.set("Authorization",c.startsWith("Bearer ")?c:`Bearer ${c}`)}l.has("Accept")||l.set("Accept","application/json");let i=await fetch(e,{...r,method:p,credentials:E,headers:l}),n=i.headers.get("content-type")||"",a={};if(n.includes("application/json")?a=await i.json().catch(()=>({})):i.ok||(a={message:(await i.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!i.ok){let c=T(a)||a,u=new Error(c.message||c.error||a.message||a.error||`token fetch failed (${i.status})`);throw u.code=c.code||a.code||(i.status===403?"merchant_not_enabled":i.status===401?"invalid_token":"fetch_failed"),u.status=i.status,u}return T(a)}}export{M as createGuestTokenFetcher,D as default,S as embed,T as normalizeGuestTokenResponse};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var SparoEmbedSDK=(()=>{var y=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var I=(e,t)=>{for(var s in t)y(e,s,{get:t[s],enumerable:!0})},z=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of M(t))!F.call(e,u)&&u!==s&&y(e,u,{get:()=>t[u],enumerable:!(n=D(t,u))||n.enumerable});return e};var N=e=>z(y({},"__esModule",{value:!0}),e);var U={};I(U,{createGuestTokenFetcher:()=>L,default:()=>G,embed:()=>_,normalizeGuestTokenResponse:()=>E});function O(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function p(e,t,s){let n=new Error(t||e);return n.code=e,typeof s=="number"&&(n.status=s),n}function P(e){if(!e||typeof e!="object")throw p("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw p("invalid_token","Missing token in fetchGuestToken response");let t=Number(e.expiresIn||900);if(!Number.isFinite(t)||t<=0)throw p("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,expiresIn:t}}function H(e,t){let s=new URLSearchParams({embed:"true"});return typeof t=="string"&&t&&s.set("account",t),`${e}/embed.html?${s.toString()}`}function _(e){let{id:t="sparo-embed",sparoDomain:s,mountPoint:n,fetchGuestToken:u,uiConfig:k={},onReady:g,onError:d}=e||{};if(!n||!(n instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof u!="function")throw new Error("fetchGuestToken must be a function");let a=O(s),o=document.createElement("iframe");o.id=t,o.title="SPARO Assistant",o.style.width="100%",o.style.height="100%",o.style.border="0",o.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals"),o.setAttribute("referrerpolicy","no-referrer");let c=!0,f=!1,l=null,h=null,x=()=>{h&&(window.clearTimeout(h),h=null)},T=r=>{d&&d(b(r))},b=r=>r&&typeof r=="object"&&r.code?r:p("fetch_failed",r?.message||"SPARO embed error"),R=()=>{if(!c||!f||!l)return;let r=o.contentWindow;!r||typeof r.postMessage!="function"||r.postMessage({type:"sparo:auth",token:l.token,...l.account?{account:l.account}:{},uiConfig:k},a)},v=r=>{x();let i=Math.max(5,r-30);h=window.setTimeout(()=>{w().catch(()=>{})},i*1e3)},w=async()=>{if(!c)return;let r;try{r=await u()}catch(j){let A=b(j);throw T(A),A}let i=P(r);l=i,v(i.expiresIn);let m=H(a,i.account);!o.src||o.src!==m?o.src=m:R()},S=r=>{if(!c||r.origin!==a)return;let i=r.data;if(!(!i||typeof i!="object"||typeof i.type!="string")){if(i.type==="sparo:ready"||i.type==="SPARO_EMBED_READY"){f=!0,R(),g&&g();return}if(i.type==="sparo:error"){let m=i.code||"iframe_error";if(m==="expired_token"){w().catch(()=>{});return}T(p(m,i.message||m,i.status))}}};return o.addEventListener("error",()=>{T(p("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),n.innerHTML="",n.appendChild(o),w().catch(()=>{}),{refresh:()=>w(),unmount:()=>{c=!1,x(),window.removeEventListener("message",S),o.parentNode===n&&n.removeChild(o)}}}var G=_;function E(e){return!e||typeof e!="object"?e:e.data&&typeof e.data=="object"&&e.data.token?e.data:e.entity&&typeof e.entity=="object"&&e.entity.token?e.entity:e}function L(e,t={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:n={}}=t,u=n.method||"GET",k=n.credentials??(s?"omit":"include");return async function(){let d=new Headers(n.headers||{});if(s){let f=await s();f&&d.set("Authorization",f.startsWith("Bearer ")?f:`Bearer ${f}`)}d.has("Accept")||d.set("Accept","application/json");let a=await fetch(e,{...n,method:u,credentials:k,headers:d}),o=a.headers.get("content-type")||"",c={};if(o.includes("application/json")?c=await a.json().catch(()=>({})):a.ok||(c={message:(await a.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!a.ok){let f=E(c)||c,l=new Error(f.message||f.error||c.message||c.error||`token fetch failed (${a.status})`);throw l.code=f.code||c.code||(a.status===403?"merchant_not_enabled":a.status===401?"invalid_token":"fetch_failed"),l.status=a.status,l}return E(c)}}return N(U);})();
|
|
1
|
+
var SparoEmbedSDK=(()=>{var y=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var I=(e,t)=>{for(var s in t)y(e,s,{get:t[s],enumerable:!0})},z=(e,t,s,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of M(t))!F.call(e,u)&&u!==s&&y(e,u,{get:()=>t[u],enumerable:!(n=D(t,u))||n.enumerable});return e};var N=e=>z(y({},"__esModule",{value:!0}),e);var U={};I(U,{createGuestTokenFetcher:()=>L,default:()=>G,embed:()=>_,normalizeGuestTokenResponse:()=>E});function O(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function p(e,t,s){let n=new Error(t||e);return n.code=e,typeof s=="number"&&(n.status=s),n}function P(e){if(!e||typeof e!="object")throw p("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw p("invalid_token","Missing token in fetchGuestToken response");let t=Number(e.expiresIn||900);if(!Number.isFinite(t)||t<=0)throw p("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,expiresIn:t}}function H(e,t){let s=new URLSearchParams({embed:"true"});return typeof t=="string"&&t&&s.set("account",t),`${e}/embed.html?${s.toString()}`}function _(e){let{id:t="sparo-embed",sparoDomain:s,mountPoint:n,fetchGuestToken:u,uiConfig:k={},onReady:g,onError:d}=e||{};if(!n||!(n instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof u!="function")throw new Error("fetchGuestToken must be a function");let a=O(s),o=document.createElement("iframe");o.id=t,o.title="SPARO Assistant",o.style.width="100%",o.style.height="100%",o.style.border="0",o.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-downloads"),o.setAttribute("referrerpolicy","no-referrer");let c=!0,f=!1,l=null,h=null,x=()=>{h&&(window.clearTimeout(h),h=null)},T=r=>{d&&d(b(r))},b=r=>r&&typeof r=="object"&&r.code?r:p("fetch_failed",r?.message||"SPARO embed error"),R=()=>{if(!c||!f||!l)return;let r=o.contentWindow;!r||typeof r.postMessage!="function"||r.postMessage({type:"sparo:auth",token:l.token,...l.account?{account:l.account}:{},uiConfig:k},a)},v=r=>{x();let i=Math.max(5,r-30);h=window.setTimeout(()=>{w().catch(()=>{})},i*1e3)},w=async()=>{if(!c)return;let r;try{r=await u()}catch(j){let A=b(j);throw T(A),A}let i=P(r);l=i,v(i.expiresIn);let m=H(a,i.account);!o.src||o.src!==m?o.src=m:R()},S=r=>{if(!c||r.origin!==a)return;let i=r.data;if(!(!i||typeof i!="object"||typeof i.type!="string")){if(i.type==="sparo:ready"||i.type==="SPARO_EMBED_READY"){f=!0,R(),g&&g();return}if(i.type==="sparo:error"){let m=i.code||"iframe_error";if(m==="expired_token"){w().catch(()=>{});return}T(p(m,i.message||m,i.status))}}};return o.addEventListener("error",()=>{T(p("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),n.innerHTML="",n.appendChild(o),w().catch(()=>{}),{refresh:()=>w(),unmount:()=>{c=!1,x(),window.removeEventListener("message",S),o.parentNode===n&&n.removeChild(o)}}}var G=_;function E(e){return!e||typeof e!="object"?e:e.data&&typeof e.data=="object"&&e.data.token?e.data:e.entity&&typeof e.entity=="object"&&e.entity.token?e.entity:e}function L(e,t={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:n={}}=t,u=n.method||"GET",k=n.credentials??(s?"omit":"include");return async function(){let d=new Headers(n.headers||{});if(s){let f=await s();f&&d.set("Authorization",f.startsWith("Bearer ")?f:`Bearer ${f}`)}d.has("Accept")||d.set("Accept","application/json");let a=await fetch(e,{...n,method:u,credentials:k,headers:d}),o=a.headers.get("content-type")||"",c={};if(o.includes("application/json")?c=await a.json().catch(()=>({})):a.ok||(c={message:(await a.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!a.ok){let f=E(c)||c,l=new Error(f.message||f.error||c.message||c.error||`token fetch failed (${a.status})`);throw l.code=f.code||c.code||(a.status===403?"merchant_not_enabled":a.status===401?"invalid_token":"fetch_failed"),l.status=a.status,l}return E(c)}}return N(U);})();
|