@swirepay-developer/sparo-embed-sdk 1.0.4 → 1.0.7
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 +28 -1
- package/GATEWAY-REACT-GUIDE.md +443 -237
- package/README.md +29 -13
- package/SPARO-EMBEDDING.md +58 -29
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -0
- package/dist/sparo-embed-sdk.global.js +1 -1
- package/package.json +4 -182
- package/types/index.d.ts +16 -0
- package/dist/index.cjs +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# SPARO Embed SDK
|
|
2
2
|
|
|
3
|
-
`@swirepay/sparo-embed-sdk` embeds SPARO in web apps (including gateway React) using guest-token auth and an iframe.
|
|
3
|
+
`@swirepay-developer/sparo-embed-sdk` embeds SPARO in web apps (including gateway React) using guest-token auth and an iframe.
|
|
4
4
|
|
|
5
5
|
**Bitbucket:** https://bitbucket.org/zetametrics/sparo-swirepay-frontend.git
|
|
6
6
|
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
|------|--------|
|
|
13
13
|
| **You (SDK)** | Push this repo to Bitbucket (`sdkmaster` or release branch). Bump version in `package.json` for each release. |
|
|
14
14
|
| **DevOps** | Clone repo → `npm ci` → `npm publish`. See **[DEVOPS-NPM-PUBLISH.md](./DEVOPS-NPM-PUBLISH.md)**. |
|
|
15
|
-
| **Gateway developer** |
|
|
15
|
+
| **Gateway developer** | SDK: [GATEWAY-REACT-GUIDE.md](./GATEWAY-REACT-GUIDE.md) · Middleware: [../sparo-key-backend](../sparo-key-backend) |
|
|
16
16
|
|
|
17
17
|
Until DevOps publishes, gateway will show:
|
|
18
18
|
|
|
19
|
-
`Module not found: Can't resolve '@swirepay/sparo-embed-sdk'`
|
|
19
|
+
`Module not found: Can't resolve '@swirepay-developer/sparo-embed-sdk'`
|
|
20
20
|
|
|
21
21
|
That is expected — the package is not on npm yet.
|
|
22
22
|
|
|
@@ -25,13 +25,13 @@ That is expected — the package is not on npm yet.
|
|
|
25
25
|
## Gateway install (after DevOps publish)
|
|
26
26
|
|
|
27
27
|
```bash
|
|
28
|
-
npm install @swirepay/sparo-embed-sdk
|
|
28
|
+
npm install @swirepay-developer/sparo-embed-sdk
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
```json
|
|
32
32
|
{
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@swirepay/sparo-embed-sdk": "^1.0.
|
|
34
|
+
"@swirepay-developer/sparo-embed-sdk": "^1.0.7"
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
```
|
|
@@ -40,23 +40,39 @@ Private package: use [.npmrc.example](./.npmrc.example) with org `NPM_TOKEN` (De
|
|
|
40
40
|
|
|
41
41
|
---
|
|
42
42
|
|
|
43
|
-
## Quick Start
|
|
43
|
+
## Quick Start (Java guest-token API)
|
|
44
|
+
|
|
45
|
+
Gateway Java API (URLs from **host env**, not SDK):
|
|
46
|
+
|
|
47
|
+
| Environment | Guest-token URL |
|
|
48
|
+
|-------------|-----------------|
|
|
49
|
+
| Staging | `https://staging-backend.swirepay.com/v1/sparo/guest-token` |
|
|
50
|
+
| Production | `https://backend.swirepay.com/v1/sparo/guest-token` |
|
|
44
51
|
|
|
45
52
|
```js
|
|
46
|
-
import {
|
|
53
|
+
import {
|
|
54
|
+
embed,
|
|
55
|
+
createGuestTokenFetcher,
|
|
56
|
+
} from '@swirepay-developer/sparo-embed-sdk';
|
|
47
57
|
|
|
48
58
|
const handle = embed({
|
|
49
59
|
id: 'sparo-chat',
|
|
50
|
-
sparoDomain:
|
|
60
|
+
sparoDomain: process.env.REACT_APP_SPARO_DOMAIN,
|
|
51
61
|
mountPoint: document.getElementById('sparo'),
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
62
|
+
|
|
63
|
+
fetchGuestToken: createGuestTokenFetcher(
|
|
64
|
+
process.env.REACT_APP_SPARO_GUEST_TOKEN_URL,
|
|
65
|
+
{ getAuthorization: getGatewayBearerToken },
|
|
66
|
+
),
|
|
67
|
+
|
|
68
|
+
uiConfig: { theme: 'light' },
|
|
69
|
+
onReady: () => console.log('[Sparo] ready'),
|
|
70
|
+
onError: (err) => console.error('[Sparo]', err.code, err.message),
|
|
57
71
|
});
|
|
58
72
|
```
|
|
59
73
|
|
|
74
|
+
`getGatewayBearerToken()` returns the logged-in user's gateway JWT (same as other API calls).
|
|
75
|
+
|
|
60
76
|
---
|
|
61
77
|
|
|
62
78
|
## Docs
|
package/SPARO-EMBEDDING.md
CHANGED
|
@@ -2,17 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
Embed the SPARO assistant into your application with short-lived guest JWTs, no SPARO login screen, and automatic token refresh.
|
|
4
4
|
|
|
5
|
-
- SDK: `@swirepay/sparo-embed-sdk`
|
|
5
|
+
- SDK: `@swirepay-developer/sparo-embed-sdk`
|
|
6
6
|
- SPARO backend: `v1.10.1+`
|
|
7
7
|
|
|
8
8
|
## Architecture
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
Same pattern as **Apache Superset embedded analytics**:
|
|
11
|
+
|
|
12
|
+
| Role | Superset | SPARO |
|
|
13
|
+
|------|----------|-------|
|
|
14
|
+
| React SDK | `@superset-ui/embedded-sdk` | `@swirepay-developer/sparo-embed-sdk` |
|
|
15
|
+
| Guest-token middleware | `supersetmiddleware` | **`sparo-key-backend`** |
|
|
16
|
+
| Guest-token route | `POST /guest-token` | `POST /guest-token` |
|
|
17
|
+
| Embedded app | Superset UI URL | `sparoDomain` (e.g. `https://sparo-stag.swirepay.com`) |
|
|
18
|
+
|
|
19
|
+
1. User opens your page (gateway, Cognito session).
|
|
20
|
+
2. SDK calls `fetchGuestToken()` → **sparo-key-backend** `POST /guest-token` (Cognito Bearer).
|
|
21
|
+
3. Middleware loads `SPARO_ADMIN_KEY` from AWS Secrets Manager (`swirepay-sparoai/dev/backend-env`).
|
|
22
|
+
4. Middleware calls SPARO `POST /api/guest-token`.
|
|
23
|
+
5. SDK receives `{ token, expiresIn, account }`, loads iframe, `postMessage` auth.
|
|
24
|
+
6. SDK auto-refreshes before expiry.
|
|
16
25
|
|
|
17
26
|
## Prerequisites
|
|
18
27
|
|
|
@@ -21,34 +30,50 @@ Embed the SPARO assistant into your application with short-lived guest JWTs, no
|
|
|
21
30
|
- Merchant allowlisted in SPARO.
|
|
22
31
|
- Your backend endpoint that authenticates end users and returns guest token.
|
|
23
32
|
|
|
24
|
-
## Backend
|
|
33
|
+
## Backend — Java guest-token API
|
|
25
34
|
|
|
26
|
-
|
|
35
|
+
Gateway Java validates the user and mints the SPARO guest JWT.
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
-
|
|
30
|
-
|
|
37
|
+
```http
|
|
38
|
+
GET https://staging-backend.swirepay.com/v1/sparo/guest-token
|
|
39
|
+
Authorization: Bearer <gateway-session-jwt>
|
|
40
|
+
Accept: application/json
|
|
41
|
+
```
|
|
31
42
|
|
|
32
|
-
|
|
43
|
+
Production: `https://backend.swirepay.com/v1/sparo/guest-token`
|
|
44
|
+
|
|
45
|
+
**Response** (direct or wrapped in `data`):
|
|
33
46
|
|
|
34
47
|
```json
|
|
35
48
|
{
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"ttl": 900
|
|
49
|
+
"token": "eyJhbGciOi...",
|
|
50
|
+
"expiresIn": 900,
|
|
51
|
+
"account": "account-72ec92..."
|
|
40
52
|
}
|
|
41
53
|
```
|
|
42
54
|
|
|
43
|
-
|
|
55
|
+
SDK usage:
|
|
44
56
|
|
|
45
|
-
```
|
|
46
|
-
{
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
57
|
+
```js
|
|
58
|
+
import { embed, createGuestTokenFetcher } from '@swirepay-developer/sparo-embed-sdk';
|
|
59
|
+
|
|
60
|
+
embed({
|
|
61
|
+
sparoDomain: process.env.REACT_APP_SPARO_DOMAIN,
|
|
62
|
+
mountPoint: document.getElementById('sparo'),
|
|
63
|
+
fetchGuestToken: createGuestTokenFetcher(
|
|
64
|
+
process.env.REACT_APP_SPARO_GUEST_TOKEN_URL,
|
|
65
|
+
{ getAuthorization: getGatewayBearerToken },
|
|
66
|
+
),
|
|
67
|
+
});
|
|
50
68
|
```
|
|
51
69
|
|
|
70
|
+
Two URLs (host config):
|
|
71
|
+
|
|
72
|
+
| Variable | Example |
|
|
73
|
+
|----------|---------|
|
|
74
|
+
| `REACT_APP_SPARO_GUEST_TOKEN_URL` | `https://staging-backend.swirepay.com/v1/sparo/guest-token` |
|
|
75
|
+
| `REACT_APP_SPARO_DOMAIN` | `https://sparo-stag.swirepay.com` |
|
|
76
|
+
|
|
52
77
|
## Frontend Integration
|
|
53
78
|
|
|
54
79
|
```html
|
|
@@ -56,20 +81,24 @@ Response:
|
|
|
56
81
|
```
|
|
57
82
|
|
|
58
83
|
```js
|
|
59
|
-
import {
|
|
84
|
+
import {
|
|
85
|
+
embed,
|
|
86
|
+
createGuestTokenFetcher,
|
|
87
|
+
} from '@swirepay-developer/sparo-embed-sdk';
|
|
60
88
|
|
|
61
89
|
const handle = embed({
|
|
62
90
|
id: 'sparo-chat',
|
|
63
91
|
sparoDomain: 'https://sparo.swirepay.com',
|
|
64
92
|
mountPoint: document.getElementById('sparo'),
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
},
|
|
93
|
+
|
|
94
|
+
// Java BFF: GET /sparo/token (credentials: include)
|
|
95
|
+
fetchGuestToken: createGuestTokenFetcher('/sparo/token'),
|
|
96
|
+
|
|
70
97
|
uiConfig: { theme: 'light' },
|
|
71
98
|
onReady: () => console.log('[Sparo] ready'),
|
|
72
99
|
onError: (err) => {
|
|
100
|
+
// err.code: merchant_not_enabled | invalid_token | expired_token |
|
|
101
|
+
// fetch_failed | iframe_load_failed
|
|
73
102
|
console.error('[Sparo]', err.code, err.message);
|
|
74
103
|
},
|
|
75
104
|
});
|
package/dist/index.js
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
|
|
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});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +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 +1 @@
|
|
|
1
|
-
var SparoEmbedSDK=(()=>{var
|
|
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);})();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swirepay-developer/sparo-embed-sdk",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"description": "SPARO embedding SDK for guest-token based iframe integration",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -10,7 +10,6 @@
|
|
|
10
10
|
"module": "./dist/index.mjs",
|
|
11
11
|
"types": "./types/index.d.ts",
|
|
12
12
|
"unpkg": "./dist/sparo-embed-sdk.global.js",
|
|
13
|
-
"type": "module",
|
|
14
13
|
"jsdelivr": "./dist/sparo-embed-sdk.global.js",
|
|
15
14
|
"exports": {
|
|
16
15
|
".": {
|
|
@@ -59,9 +58,9 @@
|
|
|
59
58
|
"build:sdk": "npm run build",
|
|
60
59
|
"test": "vitest run",
|
|
61
60
|
"test:watch": "vitest",
|
|
62
|
-
"lint": "eslint src",
|
|
63
|
-
"verify": "npm run lint && npm run test && npm run build",
|
|
64
|
-
"prepublishOnly": "npm run verify"
|
|
61
|
+
"lint": "node ./node_modules/eslint/bin/eslint.js src",
|
|
62
|
+
"verify": "node scripts/ensure-dev-deps.mjs && npm run lint && npm run test && npm run build",
|
|
63
|
+
"prepublishOnly": "npm run verify && node scripts/check-publish-manifest.mjs"
|
|
65
64
|
},
|
|
66
65
|
"devDependencies": {
|
|
67
66
|
"@testing-library/dom": "^10.4.0",
|
|
@@ -80,182 +79,5 @@
|
|
|
80
79
|
"eslint --fix",
|
|
81
80
|
"prettier --write"
|
|
82
81
|
]
|
|
83
|
-
},
|
|
84
|
-
"dependencies": {
|
|
85
|
-
"acorn": "^8.16.0",
|
|
86
|
-
"acorn-jsx": "^5.3.2",
|
|
87
|
-
"agent-base": "^7.1.4",
|
|
88
|
-
"ajv": "^6.15.0",
|
|
89
|
-
"ansi-regex": "^5.0.1",
|
|
90
|
-
"ansi-styles": "^4.3.0",
|
|
91
|
-
"any-promise": "^1.3.0",
|
|
92
|
-
"argparse": "^2.0.1",
|
|
93
|
-
"assertion-error": "^2.0.1",
|
|
94
|
-
"balanced-match": "^1.0.2",
|
|
95
|
-
"brace-expansion": "^1.1.14",
|
|
96
|
-
"braces": "^3.0.3",
|
|
97
|
-
"buffer-from": "^1.1.2",
|
|
98
|
-
"bundle-require": "^5.1.0",
|
|
99
|
-
"cac": "^6.7.14",
|
|
100
|
-
"callsites": "^3.1.0",
|
|
101
|
-
"chai": "^5.3.3",
|
|
102
|
-
"chalk": "^4.1.2",
|
|
103
|
-
"check-error": "^2.1.3",
|
|
104
|
-
"cli-cursor": "^5.0.0",
|
|
105
|
-
"cli-truncate": "^4.0.0",
|
|
106
|
-
"color-convert": "^2.0.1",
|
|
107
|
-
"color-name": "^1.1.4",
|
|
108
|
-
"colorette": "^2.0.20",
|
|
109
|
-
"commander": "^4.1.1",
|
|
110
|
-
"concat-map": "^0.0.1",
|
|
111
|
-
"confbox": "^0.1.8",
|
|
112
|
-
"consola": "^3.4.2",
|
|
113
|
-
"cross-spawn": "^7.0.6",
|
|
114
|
-
"cssstyle": "^4.6.0",
|
|
115
|
-
"data-urls": "^5.0.0",
|
|
116
|
-
"debug": "^4.4.3",
|
|
117
|
-
"decimal.js": "^10.6.0",
|
|
118
|
-
"deep-eql": "^5.0.2",
|
|
119
|
-
"deep-is": "^0.1.4",
|
|
120
|
-
"dequal": "^2.0.3",
|
|
121
|
-
"environment": "^1.1.0",
|
|
122
|
-
"escape-string-regexp": "^4.0.0",
|
|
123
|
-
"eslint-scope": "^8.4.0",
|
|
124
|
-
"eslint-visitor-keys": "^3.4.3",
|
|
125
|
-
"espree": "^10.4.0",
|
|
126
|
-
"esquery": "^1.7.0",
|
|
127
|
-
"esrecurse": "^4.3.0",
|
|
128
|
-
"estraverse": "^5.3.0",
|
|
129
|
-
"esutils": "^2.0.3",
|
|
130
|
-
"expect-type": "^1.3.0",
|
|
131
|
-
"fast-deep-equal": "^3.1.3",
|
|
132
|
-
"fast-json-stable-stringify": "^2.1.0",
|
|
133
|
-
"fast-levenshtein": "^2.0.6",
|
|
134
|
-
"file-entry-cache": "^8.0.0",
|
|
135
|
-
"fill-range": "^7.1.1",
|
|
136
|
-
"find-up": "^5.0.0",
|
|
137
|
-
"fix-dts-default-cjs-exports": "^1.0.1",
|
|
138
|
-
"flat-cache": "^4.0.1",
|
|
139
|
-
"flatted": "^3.4.2",
|
|
140
|
-
"get-east-asian-width": "^1.6.0",
|
|
141
|
-
"glob-parent": "^6.0.2",
|
|
142
|
-
"has-flag": "^4.0.0",
|
|
143
|
-
"html-encoding-sniffer": "^4.0.0",
|
|
144
|
-
"http-proxy-agent": "^7.0.2",
|
|
145
|
-
"https-proxy-agent": "^7.0.6",
|
|
146
|
-
"iconv-lite": "^0.6.3",
|
|
147
|
-
"ignore": "^5.3.2",
|
|
148
|
-
"import-fresh": "^3.3.1",
|
|
149
|
-
"imurmurhash": "^0.1.4",
|
|
150
|
-
"is-extglob": "^2.1.1",
|
|
151
|
-
"is-glob": "^4.0.3",
|
|
152
|
-
"is-number": "^7.0.0",
|
|
153
|
-
"is-potential-custom-element-name": "^1.0.1",
|
|
154
|
-
"isexe": "^2.0.0",
|
|
155
|
-
"jiti": "^1.21.7",
|
|
156
|
-
"joycon": "^3.1.1",
|
|
157
|
-
"js-tokens": "^4.0.0",
|
|
158
|
-
"js-yaml": "^4.1.1",
|
|
159
|
-
"json-buffer": "^3.0.1",
|
|
160
|
-
"json-schema-traverse": "^0.4.1",
|
|
161
|
-
"json-stable-stringify-without-jsonify": "^1.0.1",
|
|
162
|
-
"keyv": "^4.5.4",
|
|
163
|
-
"levn": "^0.4.1",
|
|
164
|
-
"lines-and-columns": "^1.2.4",
|
|
165
|
-
"listr2": "^8.3.3",
|
|
166
|
-
"load-tsconfig": "^0.2.5",
|
|
167
|
-
"locate-path": "^6.0.0",
|
|
168
|
-
"lodash.merge": "^4.6.2",
|
|
169
|
-
"log-update": "^6.1.0",
|
|
170
|
-
"loupe": "^3.2.1",
|
|
171
|
-
"lz-string": "^1.5.0",
|
|
172
|
-
"merge-stream": "^2.0.0",
|
|
173
|
-
"micromatch": "^4.0.8",
|
|
174
|
-
"mimic-function": "^5.0.1",
|
|
175
|
-
"minimatch": "^3.1.5",
|
|
176
|
-
"minipass": "^7.1.3",
|
|
177
|
-
"mlly": "^1.8.2",
|
|
178
|
-
"ms": "^2.1.3",
|
|
179
|
-
"mz": "^2.7.0",
|
|
180
|
-
"nanoid": "^3.3.12",
|
|
181
|
-
"natural-compare": "^1.4.0",
|
|
182
|
-
"nwsapi": "^2.2.23",
|
|
183
|
-
"object-assign": "^4.1.1",
|
|
184
|
-
"optionator": "^0.9.4",
|
|
185
|
-
"p-limit": "^3.1.0",
|
|
186
|
-
"p-locate": "^5.0.0",
|
|
187
|
-
"package-json-from-dist": "^1.0.1",
|
|
188
|
-
"parent-module": "^1.0.1",
|
|
189
|
-
"parse5": "^7.3.0",
|
|
190
|
-
"path-exists": "^4.0.0",
|
|
191
|
-
"path-key": "^3.1.1",
|
|
192
|
-
"path-scurry": "^2.0.2",
|
|
193
|
-
"pathe": "^2.0.3",
|
|
194
|
-
"pathval": "^2.0.1",
|
|
195
|
-
"picocolors": "^1.1.1",
|
|
196
|
-
"picomatch": "^2.3.2",
|
|
197
|
-
"pidtree": "^0.6.0",
|
|
198
|
-
"pirates": "^4.0.7",
|
|
199
|
-
"pkg-types": "^1.3.1",
|
|
200
|
-
"postcss": "^8.5.15",
|
|
201
|
-
"prelude-ls": "^1.2.1",
|
|
202
|
-
"pretty-format": "^27.5.1",
|
|
203
|
-
"punycode": "^2.3.1",
|
|
204
|
-
"react-is": "^17.0.2",
|
|
205
|
-
"resolve-from": "^4.0.0",
|
|
206
|
-
"restore-cursor": "^5.1.0",
|
|
207
|
-
"rfdc": "^1.4.1",
|
|
208
|
-
"rrweb-cssom": "^0.8.0",
|
|
209
|
-
"safer-buffer": "^2.1.2",
|
|
210
|
-
"saxes": "^6.0.0",
|
|
211
|
-
"shebang-command": "^2.0.0",
|
|
212
|
-
"shebang-regex": "^3.0.0",
|
|
213
|
-
"siginfo": "^2.0.0",
|
|
214
|
-
"slice-ansi": "^5.0.0",
|
|
215
|
-
"source-map": "^0.7.6",
|
|
216
|
-
"source-map-js": "^1.2.1",
|
|
217
|
-
"source-map-support": "^0.5.21",
|
|
218
|
-
"stackback": "^0.0.2",
|
|
219
|
-
"std-env": "^3.10.0",
|
|
220
|
-
"string-argv": "^0.3.2",
|
|
221
|
-
"strip-json-comments": "^3.1.1",
|
|
222
|
-
"strip-literal": "^3.1.0",
|
|
223
|
-
"sucrase": "^3.35.1",
|
|
224
|
-
"supports-color": "^7.2.0",
|
|
225
|
-
"symbol-tree": "^3.2.4",
|
|
226
|
-
"terser": "^5.47.1",
|
|
227
|
-
"thenify": "^3.3.1",
|
|
228
|
-
"thenify-all": "^1.6.0",
|
|
229
|
-
"tinybench": "^2.9.0",
|
|
230
|
-
"tinyexec": "^0.3.2",
|
|
231
|
-
"tinyglobby": "^0.2.16",
|
|
232
|
-
"tinypool": "^1.1.1",
|
|
233
|
-
"tinyrainbow": "^2.0.0",
|
|
234
|
-
"tinyspy": "^4.0.4",
|
|
235
|
-
"tldts": "^6.1.86",
|
|
236
|
-
"tldts-core": "^6.1.86",
|
|
237
|
-
"to-regex-range": "^5.0.1",
|
|
238
|
-
"tough-cookie": "^5.1.2",
|
|
239
|
-
"tr46": "^5.1.1",
|
|
240
|
-
"tree-kill": "^1.2.2",
|
|
241
|
-
"ts-interface-checker": "^0.1.13",
|
|
242
|
-
"type-check": "^0.4.0",
|
|
243
|
-
"typescript": "^4.9.5",
|
|
244
|
-
"ufo": "^1.6.4",
|
|
245
|
-
"undici-types": "^7.24.6",
|
|
246
|
-
"uri-js": "^4.4.1",
|
|
247
|
-
"vite-node": "^3.2.4",
|
|
248
|
-
"w3c-xmlserializer": "^5.0.0",
|
|
249
|
-
"webidl-conversions": "^7.0.0",
|
|
250
|
-
"whatwg-encoding": "^3.1.1",
|
|
251
|
-
"whatwg-mimetype": "^4.0.0",
|
|
252
|
-
"whatwg-url": "^14.2.0",
|
|
253
|
-
"which": "^2.0.2",
|
|
254
|
-
"why-is-node-running": "^2.3.0",
|
|
255
|
-
"word-wrap": "^1.2.5",
|
|
256
|
-
"ws": "^8.21.0",
|
|
257
|
-
"xml-name-validator": "^5.0.0",
|
|
258
|
-
"xmlchars": "^2.2.0",
|
|
259
|
-
"yocto-queue": "^0.1.0"
|
|
260
82
|
}
|
|
261
83
|
}
|
package/types/index.d.ts
CHANGED
|
@@ -44,5 +44,21 @@ export type EmbedHandle = {
|
|
|
44
44
|
};
|
|
45
45
|
|
|
46
46
|
export declare function embed(options: EmbedOptions): EmbedHandle;
|
|
47
|
+
|
|
48
|
+
export type CreateGuestTokenFetcherOptions = {
|
|
49
|
+
/** Returns gateway session JWT (called on mount and each SDK refresh). */
|
|
50
|
+
getAuthorization?: () => string | Promise<string>;
|
|
51
|
+
init?: RequestInit;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export declare function normalizeGuestTokenResponse(
|
|
55
|
+
body: unknown,
|
|
56
|
+
): GuestTokenResponse;
|
|
57
|
+
|
|
58
|
+
export declare function createGuestTokenFetcher(
|
|
59
|
+
tokenUrl: string,
|
|
60
|
+
options?: CreateGuestTokenFetcherOptions,
|
|
61
|
+
): () => Promise<GuestTokenResponse>;
|
|
62
|
+
|
|
47
63
|
export default embed;
|
|
48
64
|
|
package/dist/index.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var E=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var A=Object.prototype.hasOwnProperty;var I=(e,t)=>{for(var i in t)E(e,i,{get:t[i],enumerable:!0})},F=(e,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let c of M(t))!A.call(e,c)&&c!==i&&E(e,c,{get:()=>t[c],enumerable:!(s=L(t,c))||s.enumerable});return e};var G=e=>F(E({},"__esModule",{value:!0}),e);var P={};I(P,{default:()=>x,embed:()=>g});module.exports=G(P);function C(e){try{return new URL(e).origin}catch{throw new Error("Invalid sparoDomain. Expected absolute URL, e.g. https://sparo.swirepay.com")}}function a(e,t,i){let s=new Error(t||e);return s.code=e,typeof i=="number"&&(s.status=i),s}function H(e){if(!e||typeof e!="object")throw a("fetch_failed","fetchGuestToken must return an object");if(!e.token||typeof e.token!="string")throw a("invalid_token","Missing token in fetchGuestToken response");if(!e.account||typeof e.account!="string")throw a("invalid_token","Missing account in fetchGuestToken response");let t=Number(e.expiresIn||900);if(!Number.isFinite(t)||t<=0)throw a("invalid_token","Invalid expiresIn in fetchGuestToken response");return{token:e.token,account:e.account,expiresIn:t}}function N(e,t){let i=new URLSearchParams({embed:"true",account:t});return`${e}/embed.html?${i.toString()}`}function g(e){let{id:t="sparo-embed",sparoDomain:i,mountPoint:s,fetchGuestToken:c,uiConfig:v={},onReady:k,onError:b}=e||{};if(!s||!(s instanceof HTMLElement))throw new Error("mountPoint must be a valid HTMLElement");if(typeof c!="function")throw new Error("fetchGuestToken must be a function");let d=C(i),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 l=!0,T=!1,m=null,p=null,y=()=>{p&&(window.clearTimeout(p),p=null)},h=n=>{b&&b(n)},S=()=>{if(!l||!T||!m)return;let n=o.contentWindow;!n||typeof n.postMessage!="function"||n.postMessage({type:"sparo:auth",token:m.token,account:m.account,uiConfig:v},d)},D=n=>{y();let r=Math.max(5,n-30);p=window.setTimeout(()=>{w().catch(()=>{})},r*1e3)},w=async()=>{if(!l)return;let n;try{n=await c()}catch(f){let R=a("fetch_failed",(f==null?void 0:f.message)||"Failed to fetch guest token");throw h(R),R}let r=H(n);m=r,D(r.expiresIn);let u=N(d,r.account);!o.src||o.src!==u?o.src=u:S()},_=n=>{if(!l||n.origin!==d)return;let r=n.data;if(!(!r||typeof r!="object"||typeof r.type!="string")){if(r.type==="sparo:ready"||r.type==="SPARO_EMBED_READY"){T=!0,S(),k&&k();return}if(r.type==="sparo:error"){let u=r.code||"iframe_error",f=a(u,r.message||u,r.status);h(f)}}};return window.addEventListener("message",_),s.innerHTML="",s.appendChild(o),w().catch(n=>{h(n)}),{refresh:()=>w(),unmount:()=>{l=!1,y(),window.removeEventListener("message",_),o.parentNode===s&&s.removeChild(o)}}}var x=g;0&&(module.exports={embed});
|