@healthier-business/hub-sdk 0.1.0 → 0.1.1
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/README.md +2 -0
- package/dist/cjs/browser/index.d.ts +2 -2
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/browser/index.d.ts +2 -2
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/iife/hb-embed.min.js +1 -1
- package/package.json +3 -7
package/README.md
CHANGED
|
@@ -12,6 +12,8 @@ Give it your API key and it does the rest: it gets and renews tokens, retries sa
|
|
|
12
12
|
npm install @healthier-business/hub-sdk
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
+
Full documentation, with every method, the embed flow and the sandbox: <https://dev.hbcompliance.co.uk/sdk-overview>
|
|
16
|
+
|
|
15
17
|
## Quickstart
|
|
16
18
|
|
|
17
19
|
```ts
|
|
@@ -4,6 +4,6 @@ export { embedErrorHint, isEmbedSession, PROTOCOL_VERSION, type EmbedErrorCode,
|
|
|
4
4
|
/** `HealthierBusiness.mount(element, options)` for the browser. It holds no API key and cannot make API calls. */
|
|
5
5
|
export declare const HealthierBusiness: {
|
|
6
6
|
readonly mount: typeof mount;
|
|
7
|
-
readonly version: "0.1.
|
|
7
|
+
readonly version: "0.1.1";
|
|
8
8
|
};
|
|
9
|
-
export declare const version = "0.1.
|
|
9
|
+
export declare const version = "0.1.1";
|
package/dist/cjs/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.
|
|
1
|
+
export declare const VERSION = "0.1.1";
|
package/dist/cjs/version.js
CHANGED
|
@@ -4,6 +4,6 @@ export { embedErrorHint, isEmbedSession, PROTOCOL_VERSION, type EmbedErrorCode,
|
|
|
4
4
|
/** `HealthierBusiness.mount(element, options)` for the browser. It holds no API key and cannot make API calls. */
|
|
5
5
|
export declare const HealthierBusiness: {
|
|
6
6
|
readonly mount: typeof mount;
|
|
7
|
-
readonly version: "0.1.
|
|
7
|
+
readonly version: "0.1.1";
|
|
8
8
|
};
|
|
9
|
-
export declare const version = "0.1.
|
|
9
|
+
export declare const version = "0.1.1";
|
package/dist/esm/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const VERSION = "0.1.
|
|
1
|
+
export declare const VERSION = "0.1.1";
|
package/dist/esm/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.1.
|
|
1
|
+
export const VERSION = '0.1.1';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var HealthierBusiness=(function(w){"use strict";const U={token_used:"The launch token was already used. A token works once: create or refresh the session and mount again.",token_invalid:"The launch token is not valid for this session. Make sure getSession returns the session your server just created or refreshed.",token_expired:"The launch token expired (it lasts 5 minutes and is renewed on refresh). Create or refresh the session and mount again.",frame_invalid:"The frame was not opened by the SDK. Show it only with HealthierBusiness.mount(), not with your own iframe.",origin_not_allowed:"This website is not the one the session was created for, or it is not registered. Add it under Developer > Embed in Hub and pass the same origin when your server creates the session.",not_allowed:"The content can only be opened through the SDK from the website the session was created for.",session_expired:"The session timed out (60 minutes without a refresh, 12 hours at most). Create a new session.",session_finished:"The session is complete. Create a new session if the person needs it again.",not_found:"The session does not exist (any more). Create a new session.",network:"The frame could not reach the Hub. Check the connection and try again.",frame_timeout:"The frame did not start. Common causes: the session expired or finished, the website is not allowed to show it, or the session was created for another origin.",origin_mismatch:"The session was created for a different origin than this page. Pass this page's origin when your server creates the session.",session_failed:"Your server route did not return a session. Check its response in the network tab.",session_changed:"Refreshing returned a different session. Your route must call hb.embed.refresh(sessionId) for a refresh.",token_refresh_failed:"Renewing the launch token failed twice. Check that your refresh route works and that the person is still signed in.",init_failed:"The frame could not be started with the token."};function S(a){return U[a]}function V(a){if(typeof a!="object"||a===null)return!1;const e=a;return e.hb!==1?!1:e.type==="ready"?typeof e.nonce=="string"&&e.nonce.length>0&&e.nonce.length<=200:e.type==="event"?typeof e.name=="string":e.type==="resize"?typeof e.height=="number"&&Number.isFinite(e.height):e.type==="scroll-top"}function W(a){if(typeof a!="object"||a===null)return!1;const e=a;return typeof e.sessionId=="string"&&(e.kind==="course"||e.kind==="test"||e.kind==="oh_form")&&typeof e.launchToken=="string"&&typeof e.frameUrl=="string"&&(typeof e.expiresAt=="string"||e.expiresAt===null)&&typeof e.sessionExpiresAt=="string"&&typeof e.allowedOrigin=="string"}const C="allow-scripts allow-same-origin allow-forms allow-downloads allow-popups allow-popups-to-escape-sandbox",$="fullscreen; picture-in-picture; encrypted-media",Y={course:"Course",test:"Test",oh_form:"Occupational health form"},K=100,X=3e4;function M(a){let e;try{e=new URL(a.frameUrl)}catch{throw new Error("The session has no valid frameUrl.")}const m=e.hostname==="localhost"||e.hostname==="127.0.0.1"||e.hostname.endsWith(".localhost")||e.hostname.endsWith(".test");if(e.protocol!=="https:"&&!(e.protocol==="http:"&&m))throw new Error("The frame must be served over https.");return e.origin}function H(a){const{endpoint:e,kind:m,courseId:c,formShortcode:x,fetchInit:l}=a;if(!e||!m)throw new Error("mount needs either getSession, or endpoint and kind.");return async({reason:h,sessionId:o})=>{const f=await fetch(e,{credentials:"same-origin",...l,method:"POST",headers:{"content-type":"application/json",...l==null?void 0:l.headers},body:JSON.stringify(h==="refresh"?{action:"refresh",sessionId:o}:{action:"create",kind:m,courseId:c,formShortcode:x})}),n=await f.json().catch(()=>({}));if(!f.ok)throw new Error(typeof n.message=="string"?n.message:`Your server answered HTTP ${f.status}.`);return n}}function z(a,e){var L;const m=a.ownerDocument,c=m.defaultView,x=(L=e.getSession)!=null?L:H(e),l=new Map;let h=!1,o,f="",n,g,y,R=0,I=!1,D,A;const P=new Promise((s,t)=>{D=s,A=t});P.catch(()=>{});const b=(s,t)=>{var d,T,p,_,v,i,u,E,B;if(h)return;const r=t!=null?t:{};switch((d=e.onEvent)==null||d.call(e,{name:s,data:t}),s){case"ready":(T=e.onReady)==null||T.call(e);break;case"progress":(p=e.onProgress)==null||p.call(e,r);break;case"completed":(_=e.onCompleted)==null||_.call(e,r);break;case"passed":(v=e.onPassed)==null||v.call(e,r);break;case"failed":(i=e.onFailed)==null||i.call(e,r);break;case"submitted":(u=e.onSubmitted)==null||u.call(e,r);break;case"error":(E=e.onError)==null||E.call(e,r);break}(B=l.get(s))==null||B.forEach(q=>q(r))},k=(s,t)=>{c.clearTimeout(g),A(new Error(t)),b("error",{code:s,message:t||S(s)})},j=()=>{var s;c.clearTimeout(g),g=c.setTimeout(()=>k("frame_timeout",S("frame_timeout")),(s=e.readyTimeoutMs)!=null?s:2e4)},N=s=>{var t;(t=n==null?void 0:n.contentWindow)==null||t.postMessage(s,f)},O=async s=>{const t=await x(s);if(!W(t))throw new Error("getSession must return the session your server got from the SDK.");return t},J=()=>y||(y=(async()=>{let s;for(let t=0;t<2;t++)try{const r=await O({reason:"refresh",sessionId:o==null?void 0:o.sessionId});if(h)return;if(r.sessionId!==(o==null?void 0:o.sessionId)||M(r)!==f){k("session_changed","The refresh returned a different session. Your route must call hb.embed.refresh(sessionId).");return}o=r,N({hb:1,type:"token",token:r.launchToken});return}catch(r){if(s=r,h)return;await new Promise(d=>setTimeout(d,1500))}h||b("error",{code:"token_refresh_failed",message:s instanceof Error?s.message:"Could not renew the session."})})().finally(()=>{y=void 0}),y),F=s=>{var r,d,T,p,_,v;if(h||!n||s.source!==n.contentWindow||s.origin!==f||!V(s.data))return;const t=s.data;if(t.type==="ready")R+=1,I=!1,c.clearTimeout(g),(async()=>{try{let i=o==null?void 0:o.launchToken;const u=o!=null&&o.expiresAt?Date.parse(o.expiresAt)<=Date.now():!1;if(R>1||u){const E=await O({reason:"refresh",sessionId:o==null?void 0:o.sessionId});if(h)return;o=E,i=E.launchToken}N({hb:1,type:"init",nonce:t.nonce,token:i}),j()}catch(i){k("init_failed",i instanceof Error?i.message:"Could not start the frame.")}})();else if(t.type==="resize"){if(e.height===void 0||e.height==="auto"){const i=Math.min(X,Math.max((r=e.minHeight)!=null?r:320,Math.ceil(t.height)));n&&(n.style.height=`${Math.max(K,i)}px`)}}else if(t.type==="scroll-top"){if(n&&((d=e.onScrollTop)==null?void 0:d.call(e,n))!==!1&&e.scrollIntoView!==!1&&n.getBoundingClientRect().top<0){const i=typeof c.matchMedia=="function"&&c.matchMedia("(prefers-reduced-motion: reduce)").matches;n.scrollIntoView({block:"start",behavior:i?"auto":"smooth"})}}else if(t.type==="event"){if(t.name==="token-expiring"){b("token-expiring",t.data),J();return}if(t.name==="ready"){if(c.clearTimeout(g),I)return;I=!0,D()}else if(t.name==="error"){const i=(T=t.data)!=null?T:{},u=typeof i.code=="string"?i.code:"error";c.clearTimeout(g),I||A(new Error((_=(p=i.message)!=null?p:S(u))!=null?_:u)),b("error",{...typeof t.data=="object"&&t.data?t.data:{},code:u,message:(v=i.message)!=null?v:S(u)});return}b(t.name,t.data)}};return c.addEventListener("message",F),(async()=>{var s,t;try{const r=await O({reason:"initial"});if(h)return;if(f=M(r),r.allowedOrigin!==c.location.origin){k("origin_mismatch",`This session was created for ${r.allowedOrigin} but this page is ${c.location.origin}. Pass the right origin when your server creates the session.`);return}o=r,n=m.createElement("iframe"),n.setAttribute("sandbox",C),n.setAttribute("allow",$),n.setAttribute("allowfullscreen",""),n.setAttribute("referrerpolicy","origin"),n.setAttribute("title",(s=e.title)!=null?s:Y[r.kind]),n.setAttribute("scrolling",typeof e.height=="number"?"auto":"no"),n.style.cssText="display:block;width:100%;border:0;background:#fff;color-scheme:light;",n.style.height=`${typeof e.height=="number"?e.height:(t=e.minHeight)!=null?t:320}px`,j(),n.src=r.frameUrl,a.appendChild(n)}catch(r){k("session_failed",r instanceof Error?r.message:"Could not get a session from your server.")}})(),{destroy(){h||(h=!0,c.removeEventListener("message",F),c.clearTimeout(g),l.clear(),n==null||n.remove(),n=void 0)},on(s,t){var d;const r=(d=l.get(s))!=null?d:new Set;return r.add(t),l.set(s,r),()=>r.delete(t)},off(s,t){var r;(r=l.get(s))==null||r.delete(t)},ready:P,get session(){return o},get destroyed(){return h}}}const G="0.1.
|
|
1
|
+
var HealthierBusiness=(function(w){"use strict";const U={token_used:"The launch token was already used. A token works once: create or refresh the session and mount again.",token_invalid:"The launch token is not valid for this session. Make sure getSession returns the session your server just created or refreshed.",token_expired:"The launch token expired (it lasts 5 minutes and is renewed on refresh). Create or refresh the session and mount again.",frame_invalid:"The frame was not opened by the SDK. Show it only with HealthierBusiness.mount(), not with your own iframe.",origin_not_allowed:"This website is not the one the session was created for, or it is not registered. Add it under Developer > Embed in Hub and pass the same origin when your server creates the session.",not_allowed:"The content can only be opened through the SDK from the website the session was created for.",session_expired:"The session timed out (60 minutes without a refresh, 12 hours at most). Create a new session.",session_finished:"The session is complete. Create a new session if the person needs it again.",not_found:"The session does not exist (any more). Create a new session.",network:"The frame could not reach the Hub. Check the connection and try again.",frame_timeout:"The frame did not start. Common causes: the session expired or finished, the website is not allowed to show it, or the session was created for another origin.",origin_mismatch:"The session was created for a different origin than this page. Pass this page's origin when your server creates the session.",session_failed:"Your server route did not return a session. Check its response in the network tab.",session_changed:"Refreshing returned a different session. Your route must call hb.embed.refresh(sessionId) for a refresh.",token_refresh_failed:"Renewing the launch token failed twice. Check that your refresh route works and that the person is still signed in.",init_failed:"The frame could not be started with the token."};function S(a){return U[a]}function V(a){if(typeof a!="object"||a===null)return!1;const e=a;return e.hb!==1?!1:e.type==="ready"?typeof e.nonce=="string"&&e.nonce.length>0&&e.nonce.length<=200:e.type==="event"?typeof e.name=="string":e.type==="resize"?typeof e.height=="number"&&Number.isFinite(e.height):e.type==="scroll-top"}function W(a){if(typeof a!="object"||a===null)return!1;const e=a;return typeof e.sessionId=="string"&&(e.kind==="course"||e.kind==="test"||e.kind==="oh_form")&&typeof e.launchToken=="string"&&typeof e.frameUrl=="string"&&(typeof e.expiresAt=="string"||e.expiresAt===null)&&typeof e.sessionExpiresAt=="string"&&typeof e.allowedOrigin=="string"}const C="allow-scripts allow-same-origin allow-forms allow-downloads allow-popups allow-popups-to-escape-sandbox",$="fullscreen; picture-in-picture; encrypted-media",Y={course:"Course",test:"Test",oh_form:"Occupational health form"},K=100,X=3e4;function M(a){let e;try{e=new URL(a.frameUrl)}catch{throw new Error("The session has no valid frameUrl.")}const m=e.hostname==="localhost"||e.hostname==="127.0.0.1"||e.hostname.endsWith(".localhost")||e.hostname.endsWith(".test");if(e.protocol!=="https:"&&!(e.protocol==="http:"&&m))throw new Error("The frame must be served over https.");return e.origin}function H(a){const{endpoint:e,kind:m,courseId:c,formShortcode:x,fetchInit:l}=a;if(!e||!m)throw new Error("mount needs either getSession, or endpoint and kind.");return async({reason:h,sessionId:o})=>{const f=await fetch(e,{credentials:"same-origin",...l,method:"POST",headers:{"content-type":"application/json",...l==null?void 0:l.headers},body:JSON.stringify(h==="refresh"?{action:"refresh",sessionId:o}:{action:"create",kind:m,courseId:c,formShortcode:x})}),n=await f.json().catch(()=>({}));if(!f.ok)throw new Error(typeof n.message=="string"?n.message:`Your server answered HTTP ${f.status}.`);return n}}function z(a,e){var L;const m=a.ownerDocument,c=m.defaultView,x=(L=e.getSession)!=null?L:H(e),l=new Map;let h=!1,o,f="",n,g,y,R=0,I=!1,D,A;const P=new Promise((s,t)=>{D=s,A=t});P.catch(()=>{});const b=(s,t)=>{var d,T,p,_,v,i,u,E,B;if(h)return;const r=t!=null?t:{};switch((d=e.onEvent)==null||d.call(e,{name:s,data:t}),s){case"ready":(T=e.onReady)==null||T.call(e);break;case"progress":(p=e.onProgress)==null||p.call(e,r);break;case"completed":(_=e.onCompleted)==null||_.call(e,r);break;case"passed":(v=e.onPassed)==null||v.call(e,r);break;case"failed":(i=e.onFailed)==null||i.call(e,r);break;case"submitted":(u=e.onSubmitted)==null||u.call(e,r);break;case"error":(E=e.onError)==null||E.call(e,r);break}(B=l.get(s))==null||B.forEach(q=>q(r))},k=(s,t)=>{c.clearTimeout(g),A(new Error(t)),b("error",{code:s,message:t||S(s)})},j=()=>{var s;c.clearTimeout(g),g=c.setTimeout(()=>k("frame_timeout",S("frame_timeout")),(s=e.readyTimeoutMs)!=null?s:2e4)},N=s=>{var t;(t=n==null?void 0:n.contentWindow)==null||t.postMessage(s,f)},O=async s=>{const t=await x(s);if(!W(t))throw new Error("getSession must return the session your server got from the SDK.");return t},J=()=>y||(y=(async()=>{let s;for(let t=0;t<2;t++)try{const r=await O({reason:"refresh",sessionId:o==null?void 0:o.sessionId});if(h)return;if(r.sessionId!==(o==null?void 0:o.sessionId)||M(r)!==f){k("session_changed","The refresh returned a different session. Your route must call hb.embed.refresh(sessionId).");return}o=r,N({hb:1,type:"token",token:r.launchToken});return}catch(r){if(s=r,h)return;await new Promise(d=>setTimeout(d,1500))}h||b("error",{code:"token_refresh_failed",message:s instanceof Error?s.message:"Could not renew the session."})})().finally(()=>{y=void 0}),y),F=s=>{var r,d,T,p,_,v;if(h||!n||s.source!==n.contentWindow||s.origin!==f||!V(s.data))return;const t=s.data;if(t.type==="ready")R+=1,I=!1,c.clearTimeout(g),(async()=>{try{let i=o==null?void 0:o.launchToken;const u=o!=null&&o.expiresAt?Date.parse(o.expiresAt)<=Date.now():!1;if(R>1||u){const E=await O({reason:"refresh",sessionId:o==null?void 0:o.sessionId});if(h)return;o=E,i=E.launchToken}N({hb:1,type:"init",nonce:t.nonce,token:i}),j()}catch(i){k("init_failed",i instanceof Error?i.message:"Could not start the frame.")}})();else if(t.type==="resize"){if(e.height===void 0||e.height==="auto"){const i=Math.min(X,Math.max((r=e.minHeight)!=null?r:320,Math.ceil(t.height)));n&&(n.style.height=`${Math.max(K,i)}px`)}}else if(t.type==="scroll-top"){if(n&&((d=e.onScrollTop)==null?void 0:d.call(e,n))!==!1&&e.scrollIntoView!==!1&&n.getBoundingClientRect().top<0){const i=typeof c.matchMedia=="function"&&c.matchMedia("(prefers-reduced-motion: reduce)").matches;n.scrollIntoView({block:"start",behavior:i?"auto":"smooth"})}}else if(t.type==="event"){if(t.name==="token-expiring"){b("token-expiring",t.data),J();return}if(t.name==="ready"){if(c.clearTimeout(g),I)return;I=!0,D()}else if(t.name==="error"){const i=(T=t.data)!=null?T:{},u=typeof i.code=="string"?i.code:"error";c.clearTimeout(g),I||A(new Error((_=(p=i.message)!=null?p:S(u))!=null?_:u)),b("error",{...typeof t.data=="object"&&t.data?t.data:{},code:u,message:(v=i.message)!=null?v:S(u)});return}b(t.name,t.data)}};return c.addEventListener("message",F),(async()=>{var s,t;try{const r=await O({reason:"initial"});if(h)return;if(f=M(r),r.allowedOrigin!==c.location.origin){k("origin_mismatch",`This session was created for ${r.allowedOrigin} but this page is ${c.location.origin}. Pass the right origin when your server creates the session.`);return}o=r,n=m.createElement("iframe"),n.setAttribute("sandbox",C),n.setAttribute("allow",$),n.setAttribute("allowfullscreen",""),n.setAttribute("referrerpolicy","origin"),n.setAttribute("title",(s=e.title)!=null?s:Y[r.kind]),n.setAttribute("scrolling",typeof e.height=="number"?"auto":"no"),n.style.cssText="display:block;width:100%;border:0;background:#fff;color-scheme:light;",n.style.height=`${typeof e.height=="number"?e.height:(t=e.minHeight)!=null?t:320}px`,j(),n.src=r.frameUrl,a.appendChild(n)}catch(r){k("session_failed",r instanceof Error?r.message:"Could not get a session from your server.")}})(),{destroy(){h||(h=!0,c.removeEventListener("message",F),c.clearTimeout(g),l.clear(),n==null||n.remove(),n=void 0)},on(s,t){var d;const r=(d=l.get(s))!=null?d:new Set;return r.add(t),l.set(s,r),()=>r.delete(t)},off(s,t){var r;(r=l.get(s))==null||r.delete(t)},ready:P,get session(){return o},get destroyed(){return h}}}const G="0.1.1";return w.SANDBOX=C,w.mount=z,w.sessionFetcher=H,w.version=G,Object.defineProperty(w,Symbol.toStringTag,{value:"Module"}),w})({});
|
package/package.json
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@healthier-business/hub-sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "The official JavaScript and TypeScript SDK for the Healthier Business Hub API v2: automatic tokens, typed resources for every portal, signed webhooks and embeddable courses, tests and OH forms.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Sanchit Patil",
|
|
7
|
-
"
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/Healthier-Business/hb-sdk.git"
|
|
10
|
-
},
|
|
11
|
-
"homepage": "https://github.com/Healthier-Business/hb-sdk#readme",
|
|
7
|
+
"homepage": "https://dev.hbcompliance.co.uk/sdk-overview",
|
|
12
8
|
"bugs": {
|
|
13
|
-
"url": "https://
|
|
9
|
+
"url": "https://dev.hbcompliance.co.uk/sdk-faq-and-support"
|
|
14
10
|
},
|
|
15
11
|
"keywords": [
|
|
16
12
|
"healthier-business",
|