@swirepay-developer/sparo-embed-sdk 1.0.7 → 1.0.8

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.8
4
+
5
+ - `account` is optional in guest-token responses — SDK no longer requires or derives it from JWT.
6
+ - Iframe URL uses `embed=true` only; `account` query param added only when provided.
7
+ - `postMessage` auth omits `account` when not in token response.
8
+
3
9
  ## 1.0.7
4
10
 
5
11
  - `createGuestTokenFetcher` calls gateway Java API `GET /v1/sparo/guest-token` with `Authorization: Bearer <gateway-jwt>`.
@@ -90,7 +90,7 @@ yarn add @swirepay-developer/sparo-embed-sdk
90
90
  ```json
91
91
  {
92
92
  "dependencies": {
93
- "@swirepay-developer/sparo-embed-sdk": "^1.0.7"
93
+ "@swirepay-developer/sparo-embed-sdk": "^1.0.8"
94
94
  }
95
95
  }
96
96
  ```
@@ -520,7 +520,7 @@ Authorization: Bearer <gateway-session-jwt>
520
520
  Accept: application/json
521
521
  ```
522
522
 
523
- **Response `200`:**
523
+ **Response `200`:** (either direct payload, or wrapped in `entity`)
524
524
 
525
525
  ```json
526
526
  {
@@ -530,7 +530,30 @@ Accept: application/json
530
530
  }
531
531
  ```
532
532
 
533
- Wrapped responses (`{ "data": { ... } }`) are handled automatically by `createGuestTokenFetcher`.
533
+ Java wrapper example (also supported):
534
+
535
+ ```json
536
+ {
537
+ "message": "OK",
538
+ "entity": {
539
+ "token": "eyJhbGciOi...",
540
+ "expiresIn": 600,
541
+ "account": "account-72ec92..."
542
+ },
543
+ "responseCode": 200,
544
+ "status": "SUCCESS"
545
+ }
546
+ ```
547
+
548
+ Wrapped responses (`{ "data": { ... } }` or `{ "entity": { ... } }`) are handled automatically by `createGuestTokenFetcher` / `normalizeGuestTokenResponse`.
549
+
550
+ ### Refresh behavior (no refresh token policy)
551
+
552
+ - The guest JWT is **short-lived** (`expiresIn` seconds).
553
+ - The SDK **automatically re-calls** the same Java API before expiry (approximately `expiresIn - 30s`), and re-authenticates the iframe.
554
+ - Gateway React should **not** build a separate timer if it uses `embed()` — just provide `fetchGuestToken`.
555
+
556
+ > Backend best practice: include `account` explicitly in the payload. If omitted, the SDK can infer it from the JWT `account` claim (or the previous token), but explicit is preferred.
534
557
 
535
558
  | HTTP | SDK `err.code` | UI suggestion |
536
559
  |------|----------------|---------------|
package/README.md CHANGED
@@ -31,7 +31,7 @@ npm install @swirepay-developer/sparo-embed-sdk
31
31
  ```json
32
32
  {
33
33
  "dependencies": {
34
- "@swirepay-developer/sparo-embed-sdk": "^1.0.7"
34
+ "@swirepay-developer/sparo-embed-sdk": "^1.0.8"
35
35
  }
36
36
  }
37
37
  ```
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var _=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var I=(e,n)=>{for(var s in n)_(e,s,{get:n[s],enumerable:!0})},z=(e,n,s,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let f of M(n))!F.call(e,f)&&f!==s&&_(e,f,{get:()=>n[f],enumerable:!(r=D(n,f))||r.enumerable});return e};var N=e=>z(_({},"__esModule",{value:!0}),e);var U={};I(U,{createGuestTokenFetcher:()=>v,default:()=>G,embed:()=>y,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 d(e,n,s){let r=new Error(n||e);return r.code=e,typeof s=="number"&&(r.status=s),r}function P(e){if(!e||typeof e!="object")throw d("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw d("invalid_token","Missing token in fetchGuestToken response");if(!e.account||typeof e.account!="string")throw d("invalid_token","Missing account in fetchGuestToken response");let n=Number(e.expiresIn||900);if(!Number.isFinite(n)||n<=0)throw d("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:e.account,expiresIn:n}}function H(e,n){let s=new URLSearchParams({embed:"true",account:n});return`${e}/embed.html?${s.toString()}`}function y(e){let{id:n="sparo-embed",sparoDomain:s,mountPoint:r,fetchGuestToken:f,uiConfig:g={},onReady:T,onError:m}=e||{};if(!r||!(r instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof f!="function")throw new Error("fetchGuestToken must be a function");let a=O(s),o=document.createElement("iframe");o.id=n,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,u=!1,l=null,h=null,x=()=>{h&&(window.clearTimeout(h),h=null)},w=t=>{m&&m(b(t))},b=t=>t&&typeof t=="object"&&t.code?t:d("fetch_failed",(t==null?void 0:t.message)||"SPARO embed error"),R=()=>{if(!c||!u||!l)return;let t=o.contentWindow;!t||typeof t.postMessage!="function"||t.postMessage({type:"sparo:auth",token:l.token,account:l.account,uiConfig:g},a)},L=t=>{x();let i=Math.max(5,t-30);h=window.setTimeout(()=>{k().catch(()=>{})},i*1e3)},k=async()=>{if(!c)return;let t;try{t=await f()}catch(j){let A=b(j);throw w(A),A}let i=P(t);l=i,L(i.expiresIn);let p=H(a,i.account);!o.src||o.src!==p?o.src=p:R()},S=t=>{if(!c||t.origin!==a)return;let i=t.data;if(!(!i||typeof i!="object"||typeof i.type!="string")){if(i.type==="sparo:ready"||i.type==="SPARO_EMBED_READY"){u=!0,R(),T&&T();return}if(i.type==="sparo:error"){let p=i.code||"iframe_error";if(p==="expired_token"){k().catch(()=>{});return}w(d(p,i.message||p,i.status))}}};return o.addEventListener("error",()=>{w(d("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),r.innerHTML="",r.appendChild(o),k().catch(t=>{w(t)}),{refresh:()=>k(),unmount:()=>{c=!1,x(),window.removeEventListener("message",S),o.parentNode===r&&r.removeChild(o)}}}var G=y;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 v(e,n={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:r={}}=n,f=r.method||"GET",g=r.credentials??(s?"omit":"include");return async function(){let m=new Headers(r.headers||{});if(s){let u=await s();u&&m.set("Authorization",u.startsWith("Bearer ")?u:`Bearer ${u}`)}m.has("Accept")||m.set("Accept","application/json");let a=await fetch(e,{...r,method:f,credentials:g,headers:m}),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 u=E(c)||c,l=new Error(u.message||u.error||c.message||c.error||`token fetch failed (${a.status})`);throw l.code=u.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"),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 v(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,a,u){let r=new Error(a||e);return r.code=e,typeof u=="number"&&(r.status=u),r}function L(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");if(!e.account||typeof e.account!="string")throw m("invalid_token","Missing account in fetchGuestToken response");let a=Number(e.expiresIn||900);if(!Number.isFinite(a)||a<=0)throw m("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:e.account,expiresIn:a}}function j(e,a){let u=new URLSearchParams({embed:"true",account:a});return`${e}/embed.html?${u.toString()}`}function S(e){let{id:a="sparo-embed",sparoDomain:u,mountPoint:r,fetchGuestToken:p,uiConfig:E={},onReady:g,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 s=v(u),n=document.createElement("iframe");n.id=a,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 i=!0,c=!1,f=null,h=null,_=()=>{h&&(window.clearTimeout(h),h=null)},w=t=>{l&&l(y(t))},y=t=>t&&typeof t=="object"&&t.code?t:m("fetch_failed",(t==null?void 0:t.message)||"SPARO embed error"),x=()=>{if(!i||!c||!f)return;let t=n.contentWindow;!t||typeof t.postMessage!="function"||t.postMessage({type:"sparo:auth",token:f.token,account:f.account,uiConfig:E},s)},A=t=>{_();let o=Math.max(5,t-30);h=window.setTimeout(()=>{k().catch(()=>{})},o*1e3)},k=async()=>{if(!i)return;let t;try{t=await p()}catch(G){let R=y(G);throw w(R),R}let o=L(t);f=o,A(o.expiresIn);let d=j(s,o.account);!n.src||n.src!==d?n.src=d:x()},b=t=>{if(!i||t.origin!==s)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(),g&&g();return}if(o.type==="sparo:error"){let d=o.code||"iframe_error";if(d==="expired_token"){k().catch(()=>{});return}w(m(d,o.message||d,o.status))}}};return n.addEventListener("error",()=>{w(m("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",b),r.innerHTML="",r.appendChild(n),k().catch(t=>{w(t)}),{refresh:()=>k(),unmount:()=>{i=!1,_(),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,a={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:u,init:r={}}=a,p=r.method||"GET",E=r.credentials??(u?"omit":"include");return async function(){let l=new Headers(r.headers||{});if(u){let c=await u();c&&l.set("Authorization",c.startsWith("Bearer ")?c:`Bearer ${c}`)}l.has("Accept")||l.set("Accept","application/json");let s=await fetch(e,{...r,method:p,credentials:E,headers:l}),n=s.headers.get("content-type")||"",i={};if(n.includes("application/json")?i=await s.json().catch(()=>({})):s.ok||(i={message:(await s.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!s.ok){let c=T(i)||i,f=new Error(c.message||c.error||i.message||i.error||`token fetch failed (${s.status})`);throw f.code=c.code||i.code||(s.status===403?"merchant_not_enabled":s.status===401?"invalid_token":"fetch_failed"),f.status=s.status,f}return T(i)}}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"),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 _=Object.defineProperty;var D=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var F=Object.prototype.hasOwnProperty;var I=(e,t)=>{for(var s in t)_(e,s,{get:t[s],enumerable:!0})},z=(e,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let f of M(t))!F.call(e,f)&&f!==s&&_(e,f,{get:()=>t[f],enumerable:!(r=D(t,f))||r.enumerable});return e};var N=e=>z(_({},"__esModule",{value:!0}),e);var U={};I(U,{createGuestTokenFetcher:()=>v,default:()=>G,embed:()=>y,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 d(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 d("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw d("invalid_token","Missing token in fetchGuestToken response");if(!e.account||typeof e.account!="string")throw d("invalid_token","Missing account in fetchGuestToken response");let t=Number(e.expiresIn||900);if(!Number.isFinite(t)||t<=0)throw d("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:e.account,expiresIn:t}}function H(e,t){let s=new URLSearchParams({embed:"true",account:t});return`${e}/embed.html?${s.toString()}`}function y(e){let{id:t="sparo-embed",sparoDomain:s,mountPoint:r,fetchGuestToken:f,uiConfig:g={},onReady:T,onError:m}=e||{};if(!r||!(r instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof f!="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,u=!1,l=null,h=null,x=()=>{h&&(window.clearTimeout(h),h=null)},w=n=>{m&&m(b(n))},b=n=>n&&typeof n=="object"&&n.code?n:d("fetch_failed",n?.message||"SPARO embed error"),R=()=>{if(!c||!u||!l)return;let n=o.contentWindow;!n||typeof n.postMessage!="function"||n.postMessage({type:"sparo:auth",token:l.token,account:l.account,uiConfig:g},a)},L=n=>{x();let i=Math.max(5,n-30);h=window.setTimeout(()=>{k().catch(()=>{})},i*1e3)},k=async()=>{if(!c)return;let n;try{n=await f()}catch(j){let A=b(j);throw w(A),A}let i=P(n);l=i,L(i.expiresIn);let p=H(a,i.account);!o.src||o.src!==p?o.src=p: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"){u=!0,R(),T&&T();return}if(i.type==="sparo:error"){let p=i.code||"iframe_error";if(p==="expired_token"){k().catch(()=>{});return}w(d(p,i.message||p,i.status))}}};return o.addEventListener("error",()=>{w(d("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),r.innerHTML="",r.appendChild(o),k().catch(n=>{w(n)}),{refresh:()=>k(),unmount:()=>{c=!1,x(),window.removeEventListener("message",S),o.parentNode===r&&r.removeChild(o)}}}var G=y;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 v(e,t={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:r={}}=t,f=r.method||"GET",g=r.credentials??(s?"omit":"include");return async function(){let m=new Headers(r.headers||{});if(s){let u=await s();u&&m.set("Authorization",u.startsWith("Bearer ")?u:`Bearer ${u}`)}m.has("Accept")||m.set("Accept","application/json");let a=await fetch(e,{...r,method:f,credentials:g,headers:m}),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 u=E(c)||c,l=new Error(u.message||u.error||c.message||c.error||`token fetch failed (${a.status})`);throw l.code=u.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"),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);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swirepay-developer/sparo-embed-sdk",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "SPARO embedding SDK for guest-token based iframe integration",
5
5
  "private": false,
6
6
  "license": "UNLICENSED",
package/types/index.d.ts CHANGED
@@ -9,7 +9,7 @@ export type UiConfig = {
9
9
  export type GuestTokenResponse = {
10
10
  token: string;
11
11
  expiresIn: number;
12
- account: string;
12
+ account?: string;
13
13
  };
14
14
 
15
15
  export type SparoEmbedErrorCode =