@swirepay-developer/sparo-embed-sdk 1.0.8 → 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 +12 -0
- package/GATEWAY-REACT-GUIDE.md +6 -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/types/index.d.ts +13 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
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
|
+
|
|
11
|
+
## 1.0.9
|
|
12
|
+
|
|
13
|
+
- Add `allow-downloads` to iframe sandbox so CSV, Excel, PNG, and other exports work inside the embed.
|
|
14
|
+
|
|
3
15
|
## 1.0.8
|
|
4
16
|
|
|
5
17
|
- `account` is optional in guest-token responses — SDK no longer requires or derives it from JWT.
|
package/GATEWAY-REACT-GUIDE.md
CHANGED
|
@@ -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
|
|
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
|
|
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
|
|
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
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(
|