@swirepay-developer/sparo-embed-sdk 1.0.9 → 1.0.10

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,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.10
4
+
5
+ - "Speak your question": the SPARO iframe now gets `allow="microphone; clipboard-write"` so SPARO's mic button can appear. Change it with the new `allow` option (`''` grants nothing). The host page must also not block the microphone in its own `Permissions-Policy` / `Feature-Policy` header (e.g. `microphone 'self' https://sparo.swirepay.com`).
6
+ - SPARO's `sparo:token_expired` message (sent shortly before or after a guest token runs out, e.g. after the tab slept) now fetches and sends a fresh token, so chats don't end with "session timed out".
7
+ - Invoices, payment links and transfers in SPARO chat no longer ask the dashboard user to "Connect Swirepay" again.
8
+ - `embed()` forwards `gatewayToken` in `sparo:auth` (to the `sparoDomain` origin only; never stored).
9
+ - `createGuestTokenFetcher` fills `gatewayToken` with the `getAuthorization` JWT, read fresh on every refresh. A `gatewayToken` returned by the guest-token API wins. Turn off with `forwardGatewayToken: false`.
10
+
3
11
  ## 1.0.9
4
12
 
5
13
  - Add `allow-downloads` to iframe sandbox so CSV, Excel, PNG, and other exports work inside the embed.
@@ -249,6 +249,9 @@ export async function fetchSparoGuestToken() {
249
249
  token: payload.token,
250
250
  expiresIn: payload.expiresIn,
251
251
  account: payload.account,
252
+ // The dashboard user's Swirepay JWT. Without it, invoices / payment links / transfers in the
253
+ // chat ask the user to "Connect Swirepay" again. (createGuestTokenFetcher adds this for you.)
254
+ gatewayToken: token,
252
255
  };
253
256
  }
254
257
  ```
@@ -400,6 +403,9 @@ const handle = embed({
400
403
  showSuggestions: true,
401
404
  hideSidebar: false,
402
405
  },
406
+ allow: 'microphone; clipboard-write', // iframe `allow` (default shown; '' = none). The page's own
407
+ // Permissions-Policy / Feature-Policy header must not set microphone 'none',
408
+ // or SPARO's "speak your question" mic stays hidden.
403
409
  onReady: () => {}, // iframe authenticated and ready
404
410
  onError: (err) => {}, // err.code + err.message
405
411
  });
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 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});
1
+ var b=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var O=(e,n)=>{for(var s in n)b(e,s,{get:n[s],enumerable:!0})},z=(e,n,s,o)=>{if(n&&typeof n=="object"||typeof n=="function")for(let u of M(n))!I.call(e,u)&&u!==s&&b(e,u,{get:()=>n[u],enumerable:!(o=F(n,u))||o.enumerable});return e};var N=e=>z(b({},"__esModule",{value:!0}),e);var B={};O(B,{createGuestTokenFetcher:()=>j,default:()=>G,embed:()=>x,normalizeGuestTokenResponse:()=>y});module.exports=N(B);var P="microphone; clipboard-write";function U(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function w(e,n,s){let o=new Error(n||e);return o.code=e,typeof s=="number"&&(o.status=s),o}function H(e){if(!e||typeof e!="object")throw w("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw w("invalid_token","Missing token in fetchGuestToken response");let n=Number(e.expiresIn||900);if(!Number.isFinite(n)||n<=0)throw w("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,gatewayToken:typeof e.gatewayToken=="string"?e.gatewayToken:void 0,expiresIn:n}}function C(e,n){let s=new URLSearchParams({embed:"true"});return typeof n=="string"&&n&&s.set("account",n),`${e}/embed.html?${s.toString()}`}function x(e){let{id:n="sparo-embed",sparoDomain:s,mountPoint:o,fetchGuestToken:u,uiConfig:T={},allow:g=P,onReady:E,onError:p}=e||{};if(!o||!(o instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof u!="function")throw new Error("fetchGuestToken must be a function");let l=U(s),t=document.createElement("iframe");t.id=n,t.title="SPARO Assistant",t.style.width="100%",t.style.height="100%",t.style.border="0",t.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-downloads"),t.setAttribute("referrerpolicy","no-referrer"),typeof g=="string"&&g.trim()&&t.setAttribute("allow",g.trim());let d=!0,f=!1,a=null,c=null,m=()=>{c&&(window.clearTimeout(c),c=null)},_=r=>{p&&p(A(r))},A=r=>r&&typeof r=="object"&&r.code?r:w("fetch_failed",(r==null?void 0:r.message)||"SPARO embed error"),R=()=>{if(!d||!f||!a)return;let r=t.contentWindow;!r||typeof r.postMessage!="function"||r.postMessage({type:"sparo:auth",token:a.token,...a.account?{account:a.account}:{},...a.gatewayToken?{gatewayToken:a.gatewayToken}:{},uiConfig:T},l)},v=r=>{m();let i=Math.max(5,r-30);c=window.setTimeout(()=>{k().catch(()=>{})},i*1e3)},k=async()=>{if(!d)return;let r;try{r=await u()}catch(D){let L=A(D);throw _(L),L}let i=H(r);a=i,v(i.expiresIn);let h=C(l,i.account);!t.src||t.src!==h?t.src=h:R()},S=r=>{if(!d||r.origin!==l)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(),E&&E();return}if(i.type==="sparo:token_expired"){k().catch(()=>{});return}if(i.type==="sparo:error"){let h=i.code||"iframe_error";if(h==="expired_token"){k().catch(()=>{});return}_(w(h,i.message||h,i.status))}}};return t.addEventListener("error",()=>{_(w("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),o.innerHTML="",o.appendChild(t),k().catch(()=>{}),{refresh:()=>k(),unmount:()=>{d=!1,m(),window.removeEventListener("message",S),t.parentNode===o&&o.removeChild(t)}}}var G=x;function y(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 j(e,n={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:o={},forwardGatewayToken:u=!0}=n,T=o.method||"GET",g=o.credentials??(s?"omit":"include");return async function(){let p=new Headers(o.headers||{}),l="";if(s){let c=await s();typeof c=="string"&&c.trim()&&(l=c.trim().replace(/^Bearer\s+/i,""),p.set("Authorization",`Bearer ${l}`))}p.has("Accept")||p.set("Accept","application/json");let t=await fetch(e,{...o,method:T,credentials:g,headers:p}),d=t.headers.get("content-type")||"",f={};if(d.includes("application/json")?f=await t.json().catch(()=>({})):t.ok||(f={message:(await t.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!t.ok){let c=y(f)||f,m=new Error(c.message||c.error||f.message||f.error||`token fetch failed (${t.status})`);throw m.code=c.code||f.code||(t.status===403?"merchant_not_enabled":t.status===401?"invalid_token":"fetch_failed"),m.status=t.status,m}let a=y(f);return u&&l&&a&&typeof a=="object"&&!(typeof a.gatewayToken=="string"&&a.gatewayToken)?{...a,gatewayToken:l}:a}}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 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
+ var j="microphone; clipboard-write";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 h(e,s,c){let o=new Error(s||e);return o.code=e,typeof c=="number"&&(o.status=c),o}function D(e){if(!e||typeof e!="object")throw h("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw h("invalid_token","Missing token in fetchGuestToken response");let s=Number(e.expiresIn||900);if(!Number.isFinite(s)||s<=0)throw h("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,gatewayToken:typeof e.gatewayToken=="string"?e.gatewayToken:void 0,expiresIn:s}}function F(e,s){let c=new URLSearchParams({embed:"true"});return typeof s=="string"&&s&&c.set("account",s),`${e}/embed.html?${c.toString()}`}function S(e){let{id:s="sparo-embed",sparoDomain:c,mountPoint:o,fetchGuestToken:k,uiConfig:y={},allow:w=j,onReady:T,onError:l}=e||{};if(!o||!(o instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof k!="function")throw new Error("fetchGuestToken must be a function");let u=v(c),t=document.createElement("iframe");t.id=s,t.title="SPARO Assistant",t.style.width="100%",t.style.height="100%",t.style.border="0",t.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-downloads"),t.setAttribute("referrerpolicy","no-referrer"),typeof w=="string"&&w.trim()&&t.setAttribute("allow",w.trim());let p=!0,f=!1,i=null,a=null,d=()=>{a&&(window.clearTimeout(a),a=null)},E=n=>{l&&l(b(n))},b=n=>n&&typeof n=="object"&&n.code?n:h("fetch_failed",(n==null?void 0:n.message)||"SPARO embed error"),x=()=>{if(!p||!f||!i)return;let n=t.contentWindow;!n||typeof n.postMessage!="function"||n.postMessage({type:"sparo:auth",token:i.token,...i.account?{account:i.account}:{},...i.gatewayToken?{gatewayToken:i.gatewayToken}:{},uiConfig:y},u)},L=n=>{d();let r=Math.max(5,n-30);a=window.setTimeout(()=>{g().catch(()=>{})},r*1e3)},g=async()=>{if(!p)return;let n;try{n=await k()}catch(G){let R=b(G);throw E(R),R}let r=D(n);i=r,L(r.expiresIn);let m=F(u,r.account);!t.src||t.src!==m?t.src=m:x()},A=n=>{if(!p||n.origin!==u)return;let r=n.data;if(!(!r||typeof r!="object"||typeof r.type!="string")){if(r.type==="sparo:ready"||r.type==="SPARO_EMBED_READY"){f=!0,x(),T&&T();return}if(r.type==="sparo:token_expired"){g().catch(()=>{});return}if(r.type==="sparo:error"){let m=r.code||"iframe_error";if(m==="expired_token"){g().catch(()=>{});return}E(h(m,r.message||m,r.status))}}};return t.addEventListener("error",()=>{E(h("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",A),o.innerHTML="",o.appendChild(t),g().catch(()=>{}),{refresh:()=>g(),unmount:()=>{p=!1,d(),window.removeEventListener("message",A),t.parentNode===o&&o.removeChild(t)}}}var M=S;function _(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 I(e,s={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:c,init:o={},forwardGatewayToken:k=!0}=s,y=o.method||"GET",w=o.credentials??(c?"omit":"include");return async function(){let l=new Headers(o.headers||{}),u="";if(c){let a=await c();typeof a=="string"&&a.trim()&&(u=a.trim().replace(/^Bearer\s+/i,""),l.set("Authorization",`Bearer ${u}`))}l.has("Accept")||l.set("Accept","application/json");let t=await fetch(e,{...o,method:y,credentials:w,headers:l}),p=t.headers.get("content-type")||"",f={};if(p.includes("application/json")?f=await t.json().catch(()=>({})):t.ok||(f={message:(await t.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!t.ok){let a=_(f)||f,d=new Error(a.message||a.error||f.message||f.error||`token fetch failed (${t.status})`);throw d.code=a.code||f.code||(t.status===403?"merchant_not_enabled":t.status===401?"invalid_token":"fetch_failed"),d.status=t.status,d}let i=_(f);return k&&u&&i&&typeof i=="object"&&!(typeof i.gatewayToken=="string"&&i.gatewayToken)?{...i,gatewayToken:u}:i}}export{I as createGuestTokenFetcher,M as default,S as embed,_ 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 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);})();
1
+ var SparoEmbedSDK=(()=>{var b=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var I=Object.prototype.hasOwnProperty;var O=(e,r)=>{for(var s in r)b(e,s,{get:r[s],enumerable:!0})},z=(e,r,s,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let u of M(r))!I.call(e,u)&&u!==s&&b(e,u,{get:()=>r[u],enumerable:!(o=F(r,u))||o.enumerable});return e};var N=e=>z(b({},"__esModule",{value:!0}),e);var B={};O(B,{createGuestTokenFetcher:()=>j,default:()=>G,embed:()=>x,normalizeGuestTokenResponse:()=>y});var P="microphone; clipboard-write";function U(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function w(e,r,s){let o=new Error(r||e);return o.code=e,typeof s=="number"&&(o.status=s),o}function H(e){if(!e||typeof e!="object")throw w("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw w("invalid_token","Missing token in fetchGuestToken response");let r=Number(e.expiresIn||900);if(!Number.isFinite(r)||r<=0)throw w("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:typeof e.account=="string"?e.account:void 0,gatewayToken:typeof e.gatewayToken=="string"?e.gatewayToken:void 0,expiresIn:r}}function C(e,r){let s=new URLSearchParams({embed:"true"});return typeof r=="string"&&r&&s.set("account",r),`${e}/embed.html?${s.toString()}`}function x(e){let{id:r="sparo-embed",sparoDomain:s,mountPoint:o,fetchGuestToken:u,uiConfig:T={},allow:g=P,onReady:E,onError:p}=e||{};if(!o||!(o instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof u!="function")throw new Error("fetchGuestToken must be a function");let l=U(s),t=document.createElement("iframe");t.id=r,t.title="SPARO Assistant",t.style.width="100%",t.style.height="100%",t.style.border="0",t.setAttribute("sandbox","allow-scripts allow-same-origin allow-forms allow-popups allow-modals allow-downloads"),t.setAttribute("referrerpolicy","no-referrer"),typeof g=="string"&&g.trim()&&t.setAttribute("allow",g.trim());let d=!0,f=!1,a=null,c=null,m=()=>{c&&(window.clearTimeout(c),c=null)},_=n=>{p&&p(A(n))},A=n=>n&&typeof n=="object"&&n.code?n:w("fetch_failed",n?.message||"SPARO embed error"),R=()=>{if(!d||!f||!a)return;let n=t.contentWindow;!n||typeof n.postMessage!="function"||n.postMessage({type:"sparo:auth",token:a.token,...a.account?{account:a.account}:{},...a.gatewayToken?{gatewayToken:a.gatewayToken}:{},uiConfig:T},l)},v=n=>{m();let i=Math.max(5,n-30);c=window.setTimeout(()=>{k().catch(()=>{})},i*1e3)},k=async()=>{if(!d)return;let n;try{n=await u()}catch(D){let L=A(D);throw _(L),L}let i=H(n);a=i,v(i.expiresIn);let h=C(l,i.account);!t.src||t.src!==h?t.src=h:R()},S=n=>{if(!d||n.origin!==l)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(),E&&E();return}if(i.type==="sparo:token_expired"){k().catch(()=>{});return}if(i.type==="sparo:error"){let h=i.code||"iframe_error";if(h==="expired_token"){k().catch(()=>{});return}_(w(h,i.message||h,i.status))}}};return t.addEventListener("error",()=>{_(w("iframe_load_failed","Unable to load SPARO embed iframe"))}),window.addEventListener("message",S),o.innerHTML="",o.appendChild(t),k().catch(()=>{}),{refresh:()=>k(),unmount:()=>{d=!1,m(),window.removeEventListener("message",S),t.parentNode===o&&o.removeChild(t)}}}var G=x;function y(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 j(e,r={}){if(!e||typeof e!="string")throw new Error("createGuestTokenFetcher(tokenUrl) requires a URL string");let{getAuthorization:s,init:o={},forwardGatewayToken:u=!0}=r,T=o.method||"GET",g=o.credentials??(s?"omit":"include");return async function(){let p=new Headers(o.headers||{}),l="";if(s){let c=await s();typeof c=="string"&&c.trim()&&(l=c.trim().replace(/^Bearer\s+/i,""),p.set("Authorization",`Bearer ${l}`))}p.has("Accept")||p.set("Accept","application/json");let t=await fetch(e,{...o,method:T,credentials:g,headers:p}),d=t.headers.get("content-type")||"",f={};if(d.includes("application/json")?f=await t.json().catch(()=>({})):t.ok||(f={message:(await t.text().catch(()=>"")).slice(0,200)||"Non-JSON error response"}),!t.ok){let c=y(f)||f,m=new Error(c.message||c.error||f.message||f.error||`token fetch failed (${t.status})`);throw m.code=c.code||f.code||(t.status===403?"merchant_not_enabled":t.status===401?"invalid_token":"fetch_failed"),m.status=t.status,m}let a=y(f);return u&&l&&a&&typeof a=="object"&&!(typeof a.gatewayToken=="string"&&a.gatewayToken)?{...a,gatewayToken:l}:a}}return N(B);})();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swirepay-developer/sparo-embed-sdk",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
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
@@ -10,6 +10,8 @@ export type GuestTokenResponse = {
10
10
  token: string;
11
11
  expiresIn: number;
12
12
  account?: string;
13
+ /** The user's Swirepay gateway JWT; lets SPARO create invoices / payment links / transfers without "Connect Swirepay". */
14
+ gatewayToken?: string;
13
15
  };
14
16
 
15
17
  export type SparoEmbedErrorCode =
@@ -34,6 +36,12 @@ export type EmbedOptions = {
34
36
  mountPoint: HTMLElement;
35
37
  fetchGuestToken: () => Promise<GuestTokenResponse>;
36
38
  uiConfig?: UiConfig;
39
+ /**
40
+ * The iframe's `allow` attribute. Default `'microphone; clipboard-write'` (microphone powers
41
+ * "speak your question"). The host page's own Permissions-Policy / Feature-Policy header must
42
+ * not block these. Pass `''` to grant nothing.
43
+ */
44
+ allow?: string;
37
45
  onReady?: () => void;
38
46
  onError?: (error: SparoEmbedError) => void;
39
47
  };
@@ -49,6 +57,11 @@ export type CreateGuestTokenFetcherOptions = {
49
57
  /** Returns gateway session JWT (called on mount and each SDK refresh). */
50
58
  getAuthorization?: () => string | Promise<string>;
51
59
  init?: RequestInit;
60
+ /**
61
+ * Also pass the getAuthorization JWT to SPARO as `gatewayToken` (default true), so chat can
62
+ * create invoices, payment links and transfers with the dashboard's own sign-in.
63
+ */
64
+ forwardGatewayToken?: boolean;
52
65
  };
53
66
 
54
67
  export declare function normalizeGuestTokenResponse(