@frigade/js 0.4.3 → 0.4.5

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/dist/index.cjs CHANGED
@@ -1,3 +1,3 @@
1
1
  "use client";
2
- var E=Object.defineProperty,Y=Object.defineProperties,Q=Object.getOwnPropertyDescriptor,Z=Object.getOwnPropertyDescriptors,tt=Object.getOwnPropertyNames,_=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,et=Object.prototype.propertyIsEnumerable;var $=(r,s,t)=>s in r?E(r,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[s]=t,h=(r,s)=>{for(var t in s||(s={}))x.call(s,t)&&$(r,t,s[t]);if(_)for(var t of _(s))et.call(s,t)&&$(r,t,s[t]);return r},m=(r,s)=>Y(r,Z(s));var it=(r,s)=>{for(var t in s)E(r,t,{get:s[t],enumerable:!0})},st=(r,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let i of tt(s))!x.call(r,i)&&i!==t&&E(r,i,{get:()=>s[i],enumerable:!(e=Q(s,i))||e.enumerable});return r};var rt=r=>st(E({},"__esModule",{value:!0}),r);var c=(r,s,t)=>new Promise((e,i)=>{var a=o=>{try{l(t.next(o))}catch(g){i(g)}},n=o=>{try{l(t.throw(o))}catch(g){i(g)}},l=o=>o.done?e(o.value):Promise.resolve(o.value).then(a,n);l((t=t.apply(r,s)).next())});var lt={};it(lt,{Flow:()=>b,FlowStatus:()=>A,FlowType:()=>P,Frigade:()=>G,TriggerType:()=>v});module.exports=rt(lt);var N="0.2.20";var q=require("uuid");var f={};function C(r){var s,t;return`${r.__instanceId}-${r.apiKey}:${(s=r.userId)!=null?s:""}:${(t=r.groupId)!=null?t:""}`}var y=class{constructor(s){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:k(),__instanceId:Math.random().toString(12).substring(4)};let t=Object.fromEntries(Object.entries(s).filter(([e,i])=>i!=null));this.config=h(h({},this.config),t)}fetch(s,t){return c(this,null,function*(){return this.config.__readOnly?S():M(this.getAPIUrl(s),h(h({keepalive:!0},t!=null?t:{}),D(this.config.apiKey)))})}getAPIUrl(s){let t="/v1/public";return new URL(`${t}${s}`,this.config.apiUrl.replace(t,"")).toString()}getGlobalState(){let s=C(this.config);if(!f[s])throw new Error("Frigade has not yet been initialized");return f[s]}};var b=class extends y{constructor({config:t,id:e}){super(t);this._isVisible=!1;this.lastStepUpdate=new Map;this.id=e,this.init()}get isVisible(){return this._isVisible===!1?!1:this.getGlobalState().rulesGraph.isFlowVisible(this.id)}set isVisible(t){this._isVisible=t}reload(){this.init()}resyncState(t){var i,a;let e=t!=null?t:this.getStatefulFlow();this.rawData=e,this.title=(i=e==null?void 0:e.data)==null?void 0:i.title,this.subtitle=(a=e==null?void 0:e.data)==null?void 0:a.subtitle,this.isCompleted=e.$state.completed,this.isStarted=e.$state.started,this.isSkipped=e.$state.skipped,this._isVisible=e.$state.visible,e.data.steps.forEach((n,l)=>{var p;let o=this.initStepFromStatefulStep(n,l),g=(p=this.steps)==null?void 0:p.get(n.id);g&&(g.$state=o.$state)})}initStepFromStatefulStep(t,e){return m(h({},t),{flow:this,$state:m(h({},t.$state),{lastActionAt:t.$state.lastActionAt?new Date(t.$state.lastActionAt):void 0}),order:e})}init(){let t=this.getStatefulFlow();this.resyncState();let e=new Map;t.data.steps.forEach((i,a)=>{let n=this.initStepFromStatefulStep(i,a);n.start=l=>c(this,null,function*(){let o=this.steps.get(i.id);if(this.getCurrentStep().id===o.id&&o.$state.started)return;let g=u(this.getGlobalState().flowStates[this.id]);g.data.steps[o.order].$state.started=!0,g.data.steps[o.order].$state.lastActionAt=new Date,g.$state.lastActionAt=new Date,g.$state.currentStepId=o.id,g.$state.currentStepIndex=o.order,this.getGlobalState().flowStates[this.id]=g,this.resyncState(),o.$state.completed||(yield this.sendFlowStateToAPI(B,l,o.id))}),n.complete=l=>c(this,null,function*(){let o=this.steps.get(i.id);if(o.$state.completed)return;let p=this.getNumberOfCompletedSteps()+1==this.steps.size,d=u(this.getGlobalState().flowStates[this.id]);d.$state.started=!0,d.data.steps[o.order].$state.completed=!0,d.data.steps[o.order].$state.started=!0,d.data.steps[o.order].$state.lastActionAt=new Date,p||(d.$state.currentStepId=this.getStepByIndex(o.order+1).id,d.$state.currentStepIndex=o.order+1),p&&(d.$state.completed=!0),this.getGlobalState().flowStates[this.id]=d,this.resyncState(),p&&this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(K,l,o.id)}),n.reset=()=>c(this,null,function*(){let l=this.steps.get(i.id);if(!l.$state.completed)return;let o=u(this.getGlobalState().flowStates[this.id]);o.data.steps[l.order].$state.started=!1,o.data.steps[l.order].$state.completed=!1,o.data.steps[l.order].$state.lastActionAt=void 0,this.getGlobalState().flowStates[this.id]=o,this.resyncState(),yield this.sendFlowStateToAPI(H,void 0,l.id)}),n.onStateChange=l=>{let o=g=>{if(g.id!==this.id)return;let p=g.steps.get(i.id),d=this.lastStepUpdate.get(l);(p.$state.completed!==(d==null?void 0:d.$state.completed)||p.$state.started!==(d==null?void 0:d.$state.started)||p.$state.visible!==(d==null?void 0:d.$state.visible)||p.$state.blocked!==(d==null?void 0:d.$state.blocked))&&(l(p,d!=null?d:u(p)),this.lastStepUpdate.set(l,u(p)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(l,o),this.getGlobalState().onFlowStateChangeHandlers.push(o)},n.removeStateChangeHandler=l=>{let o=this.getGlobalState().onStepStateChangeHandlerWrappers.get(l);o&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(g=>g!==o))},e.set(i.id,n)}),this.steps=e,this.getGlobalState().variables&&this.getGlobalState().variables[this.id]&&Object.keys(this.getGlobalState().variables[this.id]).length>0&&this.applyVariables(this.getGlobalState().variables[this.id])}start(t){return c(this,null,function*(){if(this.isStarted||this.isCompleted)return;let e=u(this.getGlobalState().flowStates[this.id]);e.$state.started=!0,this.getGlobalState().flowStates[this.id]=e,this.resyncState(),yield this.sendFlowStateToAPI(W,t)})}complete(t){return c(this,null,function*(){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(U,t))})}skip(t){return c(this,null,function*(){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),yield this.sendFlowStateToAPI(L,t))})}forward(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e?yield e.start(t):yield this.complete(t)})}back(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&(yield e.start(t))})}restart(){return c(this,null,function*(){this.optimisticallyMarkFlowNotStarted(),yield this.sendFlowStateToAPI(V)})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){var t;return(t=this.steps.get(this.getStatefulFlow().$state.currentStepId))!=null?t:this.steps.get(Array.from(this.steps.keys())[0])}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.completed).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.visible).length}onStateChange(t){let e=(i,a)=>{i.id===this.id&&(i.isCompleted!==(a==null?void 0:a.isCompleted)||i.isStarted!==(a==null?void 0:a.isStarted)||i.isSkipped!==(a==null?void 0:a.isSkipped)||i.isVisible!==(a==null?void 0:a.isVisible)||JSON.stringify(i.steps)!==JSON.stringify(a==null?void 0:a.steps))&&t(i,a)};this.getGlobalState().onFlowStateChangeHandlerWrappers.set(t,e),this.getGlobalState().onFlowStateChangeHandlers.push(e)}removeStateChangeHandler(t){let e=this.getGlobalState().onFlowStateChangeHandlerWrappers.get(t);e&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(i=>i!==e))}applyVariables(t){var i,a;let e=n=>{let l=n.match(/\${(.*?)}/g);return l&&l.forEach(o=>{let g=o.replace("${","").replace("}","");t[g]&&(n=n.replace(o,t[g]))}),n};this.title=e((i=this.title)!=null?i:""),this.subtitle=e((a=this.subtitle)!=null?a:""),this.steps.forEach(n=>{Object.keys(n).forEach(l=>{typeof n[l]=="string"&&(n[l]=e(n[l]))})}),this.getGlobalState().variables={},this.getGlobalState().variables[this.id]=t}getStatefulFlow(){return this.getGlobalState().flowStates[this.id]}optimisticallyMarkFlowCompleted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!0,t.$state.started=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}optimisticallyMarkFlowNotStarted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!1,t.$state.started=!1,t.$state.visible=!0,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}sendFlowStateToAPI(t,e,i){return c(this,null,function*(){let a=new Date;this.getGlobalState().lastSyncDate=a;let n=yield this.fetch("/flowStates",{method:"POST",body:JSON.stringify({userId:this.config.userId,groupId:this.config.groupId,flowSlug:this.id,stepId:i!=null?i:this.getCurrentStep().id,data:e?JSON.stringify(e):{},actionType:t,createdAt:a})});a<this.getGlobalState().lastSyncDate||(yield this.getGlobalState().refreshStateFromAPI(n))})}optimisticallyMarkFlowSkipped(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.skipped=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}register(t){this.getGlobalState().rulesGraph.register(this.id,i=>{let a=this.getGlobalState().previousFlows.get(this.id);(a==null?void 0:a._isVisible)!==i&&this.getGlobalState().onFlowStateChangeHandlers.forEach(n=>{n(this,a),this.getGlobalState().previousFlows.set(this.id,F(this))}),t==null||t(i)})}unregister(){this.getGlobalState().rulesGraph.unregister(this.id)}};var H="NOT_STARTED_STEP",U="COMPLETED_FLOW",L="SKIPPED_FLOW",W="STARTED_FLOW",V="NOT_STARTED_FLOW",K="COMPLETED_STEP",B="STARTED_STEP",J="frigade-guest-key",I="guest_",at="get-cache-",O="fr-js-";function F(r){return new b({config:r.config,id:r.id})}function u(r){return JSON.parse(JSON.stringify(r))}function D(r){return{headers:{Authorization:`Bearer ${r}`,"Content-Type":"application/json","X-Frigade-SDK-Version":N,"X-Frigade-SDK-Platform":"Javascript"}}}function ot(r){return w()?window.localStorage.getItem(`${O}${r}`):null}function nt(r,s){w()&&window.localStorage.setItem(`${O}${r}`,s)}function X(){Object.keys(f).forEach(r=>{r.startsWith(at)&&delete f[r]})}function z(){w()&&Object.keys(window.localStorage).forEach(r=>{r.startsWith(O)&&window.localStorage.removeItem(r)})}var R=class{constructor(){this.queue=[];this.ttlInMS=500;this.cacheSize=5}push(s){let t=new Date;this.queue.length>=this.cacheSize&&this.queue.shift(),this.queue.push({call:s,time:t.getTime()})}hasIdenticalCall(s){let t=new Date;return this.queue=this.queue.filter(e=>t.getTime()-e.time<this.ttlInMS),this.queue.some(e=>e.call===s)}},j=new R;function M(r,s){return c(this,null,function*(){var e;if(typeof globalThis.fetch!="function")return S("- Attempted to call fetch() in an environment that doesn't support it.");if(w()&&s&&s.body&&s.method==="POST"){let i=`${r}${JSON.stringify((e=s.body)!=null?e:{})}`;if(j.hasIdenticalCall(i))return S();j.push(i)}let t;try{t=fetch(r,s),t=yield t}catch(i){return S(i)}if(!t)return S("Received an empty response");if(t.status>=400)return S(t.statusText);try{if(t.status===204)return S();let i;try{i=yield t.json()}catch(a){return S()}return i.error?S(i.error):i}catch(i){return S(i)}})}function S(r){return r&&console.log("Call to Frigade failed",r),{json:()=>({})}}function k(){if(w()){let r=ot(J);return r||(r=`${I}${(0,q.v4)()}`,nt(J,r)),r}}function w(){return typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.localStorage!="undefined"}var T=class{constructor(s){this.graph=new Map;this.registry=new Map;this.ruleOrder=[];var t,e;this.ruleOrder=(t=s==null?void 0:s.ruleOrder)!=null?t:[],this.ingestGraphData((e=s==null?void 0:s.graph)!=null?e:{})}ingestGraphData(s){this.graph=new Map(Object.entries(s)),this.fireCallbacks()}fireCallbacks(){for(let[s,t]of this.registry)typeof t=="function"&&t(this.isFlowVisible(s))}isFlowVisible(s){let t=this.graph.get(s);return t?t.visible===!1?!1:t.edges.length===0?!0:this.findRegisteredDescendant(s)==null:!0}register(s,t){this.registry.set(s,t!=null?t:()=>{}),this.fireCallbacks()}unregister(s){this.registry.delete(s),this.fireCallbacks()}getRegistry(){return this.registry}findRegisteredDescendant(s,t=s,e){let i=this.graph.get(s);if(i==null)return;if(this.registry.has(s)&&t!==s)return i;let a;if(i.edges.length>0)for(let n of i.edges){if(e!=null&&n.ruleId!==e)continue;let l=this.findRegisteredDescendant(n.head,t,e!=null?e:n.ruleId);if(l!=null&&l.visible){a=l;break}}return a}};var G=class extends y{constructor(t,e){super(h({apiKey:t},e));this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=()=>c(this,null,function*(){document.visibilityState==="visible"&&(yield this.refreshStateFromAPI())});this.init(this.config),w()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(w()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=C(this.config);f[t]&&(f[t].onFlowStateChangeHandlers=[])}}init(t){return c(this,null,function*(){return this.config=h(h({},this.config),t),this.initPromise=(()=>c(this,null,function*(){var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(I))&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})})),yield this.refreshStateFromAPI()}))(),this.initPromise})}identify(t,e){return c(this,null,function*(){this.config=m(h({},this.config),{userId:t}),yield this.initIfNeeded(),yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),yield this.resync()})}group(t,e){return c(this,null,function*(){yield this.initIfNeeded(),this.config.groupId=t,yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,properties:e})}),yield this.resync()})}track(t,e){return c(this,null,function*(){if(yield this.initIfNeeded(),!t){console.error("Event name is required to track an event");return}this.config.userId&&this.config.groupId?yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,events:[{event:t,properties:e}]})}):this.config.userId&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,events:[{event:t,properties:e}]})})),yield this.resync()})}isReady(){return!!(this.config.__instanceId&&this.config.apiKey&&this.initPromise)}getFlow(t){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows.find(e=>e.id==t)})}getFlows(){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows})}reload(){return c(this,null,function*(){z(),X(),yield this.refreshStateFromAPI(),this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}resync(){return c(this,null,function*(){this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}onStateChange(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)})}hasFailedToLoad(){return this.hasFailed}removeStateChangeHandler(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)})}initIfNeeded(){return c(this,null,function*(){return this.initPromise!==null?this.initPromise:this.init(this.config)})}refreshStateFromAPI(){return c(this,null,function*(){let t=C(this.config);if(!f[t]){let e=this,i={set:function(a,n,l){if(a[n]){let o=a[n],g=l;JSON.stringify(o)!==JSON.stringify(g)&&e.triggerEventHandlers(g,o)}return a[n]=l,!0}};if(f[t]={refreshStateFromAPI:()=>c(this,null,function*(){}),rulesGraph:new T({graph:{},ruleOrder:[]}),flowStates:new Proxy({},i),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map,variables:{}},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockFlowStates(t);return}f[t].refreshStateFromAPI=a=>c(this,null,function*(){var l,o;if(this.config.__readOnly)return;let n=a||(yield this.fetch(`/flowStates?userId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&groupId=${encodeURIComponent(this.config.groupId)}`:""}`));f[t].rulesGraph.ingestGraphData((o=(l=n.ruleGraph)==null?void 0:l.graph)!=null?o:{}),n&&n.eligibleFlows?(n.eligibleFlows.forEach(g=>{f[t].flowStates[g.flowSlug]=g,this.flows.find(p=>p.id==g.flowSlug)||this.flows.push(new b({config:this.config,id:g.flowSlug}))}),this.hasFailed=!1):this.hasFailed=!0})}yield f[t].refreshStateFromAPI()})}mockFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var a,n,l;let i=JSON.parse(this.config.__flowConfigOverrides[e]);f[t].flowStates[e]={flowSlug:e,flowName:(a=i==null?void 0:i.name)!=null?a:e,flowType:(n=i==null?void 0:i.type)!=null?n:"CHECKLIST",data:m(h({},i),{steps:((l=i==null?void 0:i.steps)!=null?l:[]).map(o=>h({id:o.id,$state:{completed:!1,started:!1,visible:!0,blocked:!1}},o))}),$state:{currentStepId:null,currentStepIndex:-1,completed:!1,started:!1,skipped:!1,visible:!0}},this.flows.push(new b({config:this.config,id:e}))})}triggerEventHandlers(t,e){return c(this,null,function*(){t&&this.flows.forEach(i=>{i.id==e.flowSlug&&this.getGlobalState().onFlowStateChangeHandlers.forEach(a=>{let n=this.getGlobalState().previousFlows.get(i.id);i.resyncState(t),a(i,n),this.getGlobalState().previousFlows.set(i.id,F(i))})})})}};var v=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(v||{}),A=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(A||{}),P=(d=>(d.ANNOUNCEMENT="ANNOUNCEMENT",d.CHECKLIST="CHECKLIST",d.FORM="FORM",d.TOUR="TOUR",d.SUPPORT="SUPPORT",d.CUSTOM="CUSTOM",d.BANNER="BANNER",d.EMBEDDED_TIP="EMBEDDED_TIP",d.NPS_SURVEY="NPS_SURVEY",d.SURVEY="SURVEY",d.CARD="CARD",d))(P||{});0&&(module.exports={Flow,FlowStatus,FlowType,Frigade,TriggerType});
2
+ var E=Object.defineProperty,z=Object.defineProperties,Q=Object.getOwnPropertyDescriptor,Z=Object.getOwnPropertyDescriptors,tt=Object.getOwnPropertyNames,_=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,et=Object.prototype.propertyIsEnumerable;var $=(a,s,t)=>s in a?E(a,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[s]=t,h=(a,s)=>{for(var t in s||(s={}))x.call(s,t)&&$(a,t,s[t]);if(_)for(var t of _(s))et.call(s,t)&&$(a,t,s[t]);return a},m=(a,s)=>z(a,Z(s));var it=(a,s)=>{for(var t in s)E(a,t,{get:s[t],enumerable:!0})},st=(a,s,t,e)=>{if(s&&typeof s=="object"||typeof s=="function")for(let i of tt(s))!x.call(a,i)&&i!==t&&E(a,i,{get:()=>s[i],enumerable:!(e=Q(s,i))||e.enumerable});return a};var at=a=>st(E({},"__esModule",{value:!0}),a);var c=(a,s,t)=>new Promise((e,i)=>{var r=o=>{try{l(t.next(o))}catch(d){i(d)}},n=o=>{try{l(t.throw(o))}catch(d){i(d)}},l=o=>o.done?e(o.value):Promise.resolve(o.value).then(r,n);l((t=t.apply(a,s)).next())});var lt={};it(lt,{Flow:()=>b,FlowStatus:()=>A,FlowType:()=>P,Frigade:()=>G,TriggerType:()=>O});module.exports=at(lt);var N="0.2.20";var q=require("uuid");var f={};function C(a){var s,t;return`${a.__instanceId}-${a.apiKey}:${(s=a.userId)!=null?s:""}:${(t=a.groupId)!=null?t:""}`}var y=class{constructor(s){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:k(),__instanceId:Math.random().toString(12).substring(4)};let t=Object.fromEntries(Object.entries(s).filter(([e,i])=>i!=null));this.config=h(h({},this.config),t)}fetch(s,t){return c(this,null,function*(){return this.config.__readOnly?S():M(this.getAPIUrl(s),h(h({keepalive:!0},t!=null?t:{}),D(this.config.apiKey)))})}getAPIUrl(s){let t="/v1/public";return new URL(`${t}${s}`,this.config.apiUrl.replace(t,"")).toString()}getGlobalState(){let s=C(this.config);if(!f[s])throw new Error("Frigade has not yet been initialized");return f[s]}};var b=class extends y{constructor({config:t,id:e}){super(t);this._isVisible=!1;this.lastStepUpdate=new Map;this.id=e,this.init()}get isVisible(){return this._isVisible===!1?!1:this.getGlobalState().rulesGraph.isFlowVisible(this.id)}set isVisible(t){this._isVisible=t}reload(){this.init()}resyncState(t){var i,r;let e=t!=null?t:this.getStatefulFlow();this.rawData=e,this.title=(i=e==null?void 0:e.data)==null?void 0:i.title,this.subtitle=(r=e==null?void 0:e.data)==null?void 0:r.subtitle,this.isCompleted=e.$state.completed,this.isStarted=e.$state.started,this.isSkipped=e.$state.skipped,this._isVisible=e.$state.visible,e.data.steps.forEach((n,l)=>{var p;let o=this.initStepFromStatefulStep(n,l),d=(p=this.steps)==null?void 0:p.get(n.id);d&&(d.$state=o.$state)})}initStepFromStatefulStep(t,e){return m(h({},t),{flow:this,$state:m(h({},t.$state),{lastActionAt:t.$state.lastActionAt?new Date(t.$state.lastActionAt):void 0}),order:e})}init(){let t=this.getStatefulFlow();this.resyncState();let e=new Map;t.data.steps.forEach((i,r)=>{let n=this.initStepFromStatefulStep(i,r);n.start=l=>c(this,null,function*(){let o=this.steps.get(i.id);if(this.getCurrentStep().id===o.id&&o.$state.started)return;let d=u(this.getGlobalState().flowStates[this.id]);d.data.steps[o.order].$state.started=!0,d.data.steps[o.order].$state.lastActionAt=new Date,d.$state.lastActionAt=new Date,d.$state.currentStepId=o.id,d.$state.currentStepIndex=o.order,this.getGlobalState().flowStates[this.id]=d,this.resyncState(),o.$state.completed||(yield this.sendFlowStateToAPI(B,l,o.id))}),n.complete=l=>c(this,null,function*(){let o=this.steps.get(i.id);if(o.$state.completed)return;let d=o.order+1===this.getNumberOfAvailableSteps(),p=u(this.getGlobalState().flowStates[this.id]);p.$state.started=!0,p.data.steps[o.order].$state.completed=!0,p.data.steps[o.order].$state.started=!0,p.data.steps[o.order].$state.lastActionAt=new Date,d||(p.$state.currentStepId=this.getStepByIndex(o.order+1).id,p.$state.currentStepIndex=o.order+1),d&&(p.$state.completed=!0),this.getGlobalState().flowStates[this.id]=p,this.resyncState(),d&&this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(K,l,o.id)}),n.reset=()=>c(this,null,function*(){let l=this.steps.get(i.id);if(!l.$state.completed)return;let o=u(this.getGlobalState().flowStates[this.id]);o.data.steps[l.order].$state.started=!1,o.data.steps[l.order].$state.completed=!1,o.data.steps[l.order].$state.lastActionAt=void 0,this.getGlobalState().flowStates[this.id]=o,this.resyncState(),yield this.sendFlowStateToAPI(H,void 0,l.id)}),n.onStateChange=l=>{let o=d=>{if(d.id!==this.id)return;let p=d.steps.get(i.id),g=this.lastStepUpdate.get(l);(p.$state.completed!==(g==null?void 0:g.$state.completed)||p.$state.started!==(g==null?void 0:g.$state.started)||p.$state.visible!==(g==null?void 0:g.$state.visible)||p.$state.blocked!==(g==null?void 0:g.$state.blocked))&&(l(p,g!=null?g:u(p)),this.lastStepUpdate.set(l,u(p)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(l,o),this.getGlobalState().onFlowStateChangeHandlers.push(o)},n.removeStateChangeHandler=l=>{let o=this.getGlobalState().onStepStateChangeHandlerWrappers.get(l);o&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(d=>d!==o))},e.set(i.id,n)}),this.steps=e,this.getGlobalState().variables&&this.getGlobalState().variables[this.id]&&Object.keys(this.getGlobalState().variables[this.id]).length>0&&this.applyVariables(this.getGlobalState().variables[this.id])}start(t){return c(this,null,function*(){if(this.isStarted||this.isCompleted)return;let e=u(this.getGlobalState().flowStates[this.id]);e.$state.started=!0,this.getGlobalState().flowStates[this.id]=e,this.resyncState(),yield this.sendFlowStateToAPI(W,t)})}complete(t){return c(this,null,function*(){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(U,t))})}skip(t){return c(this,null,function*(){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),yield this.sendFlowStateToAPI(L,t))})}forward(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e?yield e.start(t):yield this.complete(t)})}back(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&(yield e.start(t))})}restart(){return c(this,null,function*(){this.optimisticallyMarkFlowNotStarted(),yield this.sendFlowStateToAPI(V)})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){var t;return(t=this.steps.get(this.getStatefulFlow().$state.currentStepId))!=null?t:this.steps.get(Array.from(this.steps.keys())[0])}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.completed).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.visible).length}onStateChange(t){let e=(i,r)=>{i.id===this.id&&(i.isCompleted!==(r==null?void 0:r.isCompleted)||i.isStarted!==(r==null?void 0:r.isStarted)||i.isSkipped!==(r==null?void 0:r.isSkipped)||i.isVisible!==(r==null?void 0:r.isVisible)||JSON.stringify(i.steps)!==JSON.stringify(r==null?void 0:r.steps))&&t(i,r)};this.getGlobalState().onFlowStateChangeHandlerWrappers.set(t,e),this.getGlobalState().onFlowStateChangeHandlers.push(e)}removeStateChangeHandler(t){let e=this.getGlobalState().onFlowStateChangeHandlerWrappers.get(t);e&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(i=>i!==e))}applyVariables(t){var i,r;let e=n=>{let l=n.match(/\${(.*?)}/g);return l&&l.forEach(o=>{let d=o.replace("${","").replace("}","");t[d]&&(n=n.replace(o,t[d]))}),n};this.title=e((i=this.title)!=null?i:""),this.subtitle=e((r=this.subtitle)!=null?r:""),this.steps.forEach(n=>{Object.keys(n).forEach(l=>{typeof n[l]=="string"&&(n[l]=e(n[l]))})}),this.getGlobalState().variables={},this.getGlobalState().variables[this.id]=t}getStatefulFlow(){return this.getGlobalState().flowStates[this.id]}optimisticallyMarkFlowCompleted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!0,t.$state.started=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}optimisticallyMarkFlowNotStarted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!1,t.$state.started=!1,t.$state.visible=!0,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}sendFlowStateToAPI(t,e,i){return c(this,null,function*(){let r=new Date;this.getGlobalState().lastSyncDate=r;let n=yield this.fetch("/flowStates",{method:"POST",body:JSON.stringify({userId:this.config.userId,groupId:this.config.groupId,flowSlug:this.id,stepId:i!=null?i:this.getCurrentStep().id,data:e||{},actionType:t,createdAt:r})});r<this.getGlobalState().lastSyncDate||(yield this.getGlobalState().refreshStateFromAPI(n))})}optimisticallyMarkFlowSkipped(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.skipped=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}register(t){this.getGlobalState().rulesGraph.register(this.id,i=>{let r=this.getGlobalState().previousFlows.get(this.id);(r==null?void 0:r._isVisible)!==i&&this.getGlobalState().onFlowStateChangeHandlers.forEach(n=>{n(this,r),this.getGlobalState().previousFlows.set(this.id,F(this))}),t==null||t(i)})}unregister(){this.getGlobalState().rulesGraph.unregister(this.id)}};var H="NOT_STARTED_STEP",U="COMPLETED_FLOW",L="SKIPPED_FLOW",W="STARTED_FLOW",V="NOT_STARTED_FLOW",K="COMPLETED_STEP",B="STARTED_STEP",J="frigade-guest-key",I="guest_",rt="get-cache-",v="fr-js-";function F(a){return new b({config:a.config,id:a.id})}function u(a){return JSON.parse(JSON.stringify(a))}function D(a){return{headers:{Authorization:`Bearer ${a}`,"Content-Type":"application/json","X-Frigade-SDK-Version":N,"X-Frigade-SDK-Platform":"Javascript"}}}function ot(a){return w()?window.localStorage.getItem(`${v}${a}`):null}function nt(a,s){w()&&window.localStorage.setItem(`${v}${a}`,s)}function X(){Object.keys(f).forEach(a=>{a.startsWith(rt)&&delete f[a]})}function Y(){w()&&Object.keys(window.localStorage).forEach(a=>{a.startsWith(v)&&window.localStorage.removeItem(a)})}var R=class{constructor(){this.queue=[];this.ttlInMS=500;this.cacheSize=5}push(s){let t=new Date;this.queue.length>=this.cacheSize&&this.queue.shift(),this.queue.push({call:s,time:t.getTime()})}hasIdenticalCall(s){let t=new Date;return this.queue=this.queue.filter(e=>t.getTime()-e.time<this.ttlInMS),this.queue.some(e=>e.call===s)}},j=new R;function M(a,s){return c(this,null,function*(){var e;if(typeof globalThis.fetch!="function")return S("- Attempted to call fetch() in an environment that doesn't support it.");if(w()&&s&&s.body&&s.method==="POST"){let i=`${a}${JSON.stringify((e=s.body)!=null?e:{})}`;if(j.hasIdenticalCall(i))return S();j.push(i)}let t;try{t=fetch(a,s),t=yield t}catch(i){return S(i)}if(!t)return S("Received an empty response");if(t.status>=400)return S(t.statusText);try{if(t.status===204)return S();let i;try{i=yield t.json()}catch(r){return S()}return i.error?S(i.error):i}catch(i){return S(i)}})}function S(a){return a&&console.log("Call to Frigade failed",a),{json:()=>({})}}function k(){if(w()){let a=ot(J);return a||(a=`${I}${(0,q.v4)()}`,nt(J,a)),a}}function w(){return typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.localStorage!="undefined"}var T=class{constructor(s){this.graph=new Map;this.registry=new Map;this.ruleOrder=[];var t,e;this.ruleOrder=(t=s==null?void 0:s.ruleOrder)!=null?t:[],this.ingestGraphData((e=s==null?void 0:s.graph)!=null?e:{})}ingestGraphData(s){this.graph=new Map(Object.entries(s)),this.fireCallbacks()}fireCallbacks(){for(let[s,t]of this.registry)typeof t=="function"&&t(this.isFlowVisible(s))}isFlowVisible(s){let t=this.graph.get(s);return t?t.visible===!1?!1:t.edges.length===0?!0:this.findRegisteredDescendant(s)==null:!0}register(s,t){this.registry.set(s,t!=null?t:()=>{}),this.fireCallbacks()}unregister(s){this.registry.delete(s),this.fireCallbacks()}getRegistry(){return this.registry}findRegisteredDescendant(s,t=s,e){let i=this.graph.get(s);if(i==null)return;if(this.registry.has(s)&&t!==s)return i;let r;if(i.edges.length>0)for(let n of i.edges){if(e!=null&&n.ruleId!==e)continue;let l=this.findRegisteredDescendant(n.head,t,e!=null?e:n.ruleId);if(l!=null&&l.visible){r=l;break}}return r}};var G=class extends y{constructor(t,e){super(h({apiKey:t},e));this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=()=>c(this,null,function*(){document.visibilityState==="visible"&&(yield this.refreshStateFromAPI())});this.init(this.config),w()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(w()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=C(this.config);f[t]&&(f[t].onFlowStateChangeHandlers=[])}}init(t){return c(this,null,function*(){return this.config=h(h({},this.config),t),this.initPromise=(()=>c(this,null,function*(){var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(I))&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})})),yield this.refreshStateFromAPI()}))(),this.initPromise})}identify(t,e){return c(this,null,function*(){this.config=m(h({},this.config),{userId:t}),yield this.initIfNeeded(),yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),yield this.resync()})}group(t,e){return c(this,null,function*(){yield this.initIfNeeded(),this.config.groupId=t,yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,properties:e})}),yield this.resync()})}track(t,e){return c(this,null,function*(){if(yield this.initIfNeeded(),!t){console.error("Event name is required to track an event");return}this.config.userId&&this.config.groupId?yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,events:[{event:t,properties:e}]})}):this.config.userId&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,events:[{event:t,properties:e}]})})),yield this.resync()})}isReady(){return!!(this.config.__instanceId&&this.config.apiKey&&this.initPromise)}getFlow(t){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows.find(e=>e.id==t)})}getFlows(){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows})}reload(){return c(this,null,function*(){Y(),X(),yield this.refreshStateFromAPI(),this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}resync(){return c(this,null,function*(){this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}onStateChange(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)})}hasFailedToLoad(){return this.hasFailed}removeStateChangeHandler(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)})}initIfNeeded(){return c(this,null,function*(){return this.initPromise!==null?this.initPromise:this.init(this.config)})}refreshStateFromAPI(){return c(this,null,function*(){let t=C(this.config);if(!f[t]){let e=this,i={set:function(r,n,l){if(r[n]){let o=r[n],d=l;JSON.stringify(o)!==JSON.stringify(d)&&e.triggerEventHandlers(d,o)}return r[n]=l,!0}};if(f[t]={refreshStateFromAPI:()=>c(this,null,function*(){}),rulesGraph:new T({graph:{},ruleOrder:[]}),flowStates:new Proxy({},i),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map,variables:{}},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockFlowStates(t);return}f[t].refreshStateFromAPI=r=>c(this,null,function*(){var l,o;if(this.config.__readOnly)return;let n=r||(yield this.fetch(`/flowStates?userId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&groupId=${encodeURIComponent(this.config.groupId)}`:""}`));f[t].rulesGraph.ingestGraphData((o=(l=n.ruleGraph)==null?void 0:l.graph)!=null?o:{}),n&&n.eligibleFlows?(n.eligibleFlows.forEach(d=>{f[t].flowStates[d.flowSlug]=d,this.flows.find(p=>p.id==d.flowSlug)||this.flows.push(new b({config:this.config,id:d.flowSlug}))}),this.hasFailed=!1):this.hasFailed=!0})}yield f[t].refreshStateFromAPI()})}mockFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var r,n,l;let i=JSON.parse(this.config.__flowConfigOverrides[e]);f[t].flowStates[e]={flowSlug:e,flowName:(r=i==null?void 0:i.name)!=null?r:e,flowType:(n=i==null?void 0:i.type)!=null?n:"CHECKLIST",data:m(h({},i),{steps:((l=i==null?void 0:i.steps)!=null?l:[]).map(o=>h({id:o.id,$state:{completed:!1,started:!1,visible:!0,blocked:!1}},o))}),$state:{currentStepId:null,currentStepIndex:-1,completed:!1,started:!1,skipped:!1,visible:!0}},this.flows.push(new b({config:this.config,id:e}))})}triggerEventHandlers(t,e){return c(this,null,function*(){t&&this.flows.forEach(i=>{i.id==e.flowSlug&&this.getGlobalState().onFlowStateChangeHandlers.forEach(r=>{let n=this.getGlobalState().previousFlows.get(i.id);i.resyncState(t),r(i,n),this.getGlobalState().previousFlows.set(i.id,F(i))})})})}};var O=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(O||{}),A=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(A||{}),P=(g=>(g.ANNOUNCEMENT="ANNOUNCEMENT",g.CHECKLIST="CHECKLIST",g.FORM="FORM",g.TOUR="TOUR",g.SUPPORT="SUPPORT",g.CUSTOM="CUSTOM",g.BANNER="BANNER",g.EMBEDDED_TIP="EMBEDDED_TIP",g.NPS_SURVEY="NPS_SURVEY",g.SURVEY="SURVEY",g.CARD="CARD",g))(P||{});0&&(module.exports={Flow,FlowStatus,FlowType,Frigade,TriggerType});
3
3
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/rules-graph.ts","../src/core/frigade.ts","../src/core/types.ts"],"sourcesContent":["export { Frigade } from './core/frigade'\nexport { Flow } from './core/flow'\nexport { FlowStatus, type FlowStep, type FrigadeConfig, FlowType, TriggerType } from './core/types'\nexport type { RulesGraphRegistryCallback } from './core/rules-graph'\n","export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport { v4 as uuidv4 } from 'uuid'\nimport { Flow } from '../core/flow'\nimport { frigadeGlobalState } from './state'\n\nexport const NOT_STARTED_STEP = 'NOT_STARTED_STEP'\nexport const COMPLETED_FLOW = 'COMPLETED_FLOW'\nexport const SKIPPED_FLOW = 'SKIPPED_FLOW'\nexport const STARTED_FLOW = 'STARTED_FLOW'\nexport const NOT_STARTED_FLOW = 'NOT_STARTED_FLOW'\nexport const COMPLETED_STEP = 'COMPLETED_STEP'\nexport const STARTED_STEP = 'STARTED_STEP'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow({\n config: flow.config,\n id: flow.id,\n })\n return newFlow\n}\n\nexport function clone<T>(obj: T): T {\n return JSON.parse(JSON.stringify(obj))\n}\n\nexport function getHeaders(apiKey: string) {\n return {\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n 'X-Frigade-SDK-Version': VERSION_NUMBER,\n 'X-Frigade-SDK-Platform': 'Javascript',\n },\n }\n}\n\nfunction getLocalStorage(key: string) {\n if (isWeb()) {\n return window.localStorage.getItem(`${LOCAL_STORAGE_PREFIX}${key}`)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (isWeb()) {\n window.localStorage.setItem(`${LOCAL_STORAGE_PREFIX}${key}`, value)\n }\n}\n\nfunction setGlobalState(key: string, value: any) {\n frigadeGlobalState[key] = value\n}\n\nfunction getGlobalState(key: string): any {\n return frigadeGlobalState[key]\n}\n\nexport function clearCache() {\n Object.keys(frigadeGlobalState).forEach((key) => {\n if (key.startsWith(GET_CACHE_PREFIX)) {\n delete frigadeGlobalState[key]\n }\n })\n}\n\nexport function resetAllLocalStorage() {\n if (isWeb()) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith(LOCAL_STORAGE_PREFIX)) {\n window.localStorage.removeItem(key)\n }\n })\n }\n}\n\nclass CallQueue {\n private queue: {\n call: string\n time: number\n }[] = []\n private readonly ttlInMS = 500\n private readonly cacheSize = 5\n\n public push(call: string) {\n const now = new Date()\n if (this.queue.length >= this.cacheSize) {\n this.queue.shift()\n }\n this.queue.push({\n call: call,\n time: now.getTime(),\n })\n }\n\n public hasIdenticalCall(call: string) {\n const now = new Date()\n this.queue = this.queue.filter((item) => now.getTime() - item.time < this.ttlInMS)\n return this.queue.some((item) => item.call === call)\n }\n}\n\nconst callQueue = new CallQueue()\n\nexport async function gracefulFetch(url: string, options: any) {\n if (typeof globalThis.fetch !== 'function') {\n return getEmptyResponse(\n \"- Attempted to call fetch() in an environment that doesn't support it.\"\n )\n }\n\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCallDataKey = `${url}${JSON.stringify(options.body ?? {})}`\n if (callQueue.hasIdenticalCall(lastCallDataKey)) {\n return getEmptyResponse()\n }\n callQueue.push(lastCallDataKey)\n }\n\n let response\n try {\n response = fetch(url, options)\n response = await response\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse('Received an empty response')\n }\n\n if (response.status >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n try {\n if (response.status === 204) {\n return getEmptyResponse()\n }\n\n let body\n try {\n body = await response.json()\n } catch (e) {\n return getEmptyResponse()\n }\n\n if (body.error) {\n return getEmptyResponse(body.error)\n }\n return body\n } catch (e) {\n return getEmptyResponse(e)\n }\n}\n\nexport function getEmptyResponse(error?: any) {\n if (error) {\n console.log('Call to Frigade failed', error)\n }\n\n // Create empty response that contains the .json method and returns an empty object\n return {\n json: () => ({}),\n }\n}\n\nexport function generateGuestId() {\n if (isWeb()) {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n setLocalStorage(GUEST_KEY, guestId)\n }\n return guestId\n }\n}\n\nexport function fetcher(apiKey: string, path: string, options?: Record<any, any>) {\n return gracefulFetch(`//api.frigade.com/v1/public${path}`, {\n ...(options ?? {}),\n ...getHeaders(apiKey),\n })\n}\n\nexport function isWeb() {\n return (\n typeof window !== 'undefined' &&\n typeof window.document !== 'undefined' &&\n typeof window.localStorage !== 'undefined'\n )\n}\n","import { FlowStates, FlowStep, FrigadeConfig, StatefulFlow } from '../core/types'\nimport { Flow } from '../core/flow'\nimport { RulesGraph } from '../core/rules-graph'\n\nexport interface FrigadeGlobalState {\n refreshStateFromAPI: (overrideFlowStatesRaw?: FlowStates) => Promise<void>\n flowStates: Record<string, StatefulFlow>\n rulesGraph: RulesGraph\n onFlowStateChangeHandlerWrappers: Map<\n (flow: Flow, previousFlow: Flow) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onStepStateChangeHandlerWrappers: Map<\n (step: FlowStep, previousStep: FlowStep) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onFlowStateChangeHandlers: ((flow: Flow, previousFlow: Flow) => void)[]\n previousFlows: Map<string, Flow>\n variables: Record<string, Record<string, any>>\n lastSyncDate?: Date\n}\n\nexport let frigadeGlobalState: Record<string, FrigadeGlobalState> = {}\n\nexport function getGlobalStateKey(internalConfig: FrigadeConfig): string {\n return `${internalConfig.__instanceId}-${internalConfig.apiKey}:${internalConfig.userId ?? ''}:${\n internalConfig.groupId ?? ''\n }`\n}\n","import { generateGuestId, getEmptyResponse, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../core/types'\nimport { frigadeGlobalState, FrigadeGlobalState, getGlobalStateKey } from './state'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: 'https://api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(12).substring(4),\n }\n\n constructor(config: FrigadeConfig) {\n const filteredConfig = Object.fromEntries(Object.entries(config).filter(([_, v]) => v != null))\n\n this.config = {\n ...this.config,\n ...filteredConfig,\n }\n }\n\n /**\n * @ignore\n */\n public async fetch(path: string, options?: Record<any, any>) {\n if (this.config.__readOnly) {\n return getEmptyResponse()\n }\n\n return gracefulFetch(this.getAPIUrl(path), {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n\n private getAPIUrl(path: string) {\n const pathPrefix = '/v1/public'\n const apiUrl = new URL(\n `${pathPrefix}${path}`,\n this.config.apiUrl.replace(pathPrefix, '')\n ).toString()\n\n return apiUrl\n }\n\n /**\n * @ignore\n */\n protected getGlobalState(): FrigadeGlobalState {\n const globalStateKey = getGlobalStateKey(this.config)\n if (!frigadeGlobalState[globalStateKey]) {\n throw new Error('Frigade has not yet been initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import {\n FlowActionType,\n FlowStateDTO,\n FlowStates,\n FlowStep,\n FrigadeConfig,\n StatefulFlow,\n StatefulStep,\n} from './types'\nimport {\n clone,\n cloneFlow,\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n NOT_STARTED_STEP,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraphRegistryCallback } from './rules-graph'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * Ordered map of the Steps in the Flow.\n * See [Flow Step Definition](https://docs.frigade.com/v2/sdk/js/step) for more information.\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the Flow, if defined at the top level of the YAML config.\n */\n public title?: string\n /**\n * The user-facing description of the Flow, if defined at the top level of the YAML config.\n */\n public subtitle?: string\n /**\n * The metadata of the Flow.\n * @ignore\n */\n public rawData: StatefulFlow\n /**\n * Whether the Flow is completed or not.\n */\n public isCompleted: boolean\n /**\n * Whether the Flow is started or not.\n */\n public isStarted: boolean\n /**\n * Whether the Flow has been skipped or not.\n */\n public isSkipped: boolean\n /**\n * @ignore\n */\n private _isVisible: boolean = false\n /**\n * Whether the Flow is visible to the user based on the current user/group's state.\n */\n get isVisible() {\n if (this._isVisible === false) {\n return false\n }\n\n return this.getGlobalState().rulesGraph.isFlowVisible(this.id)\n }\n set isVisible(visible: boolean) {\n this._isVisible = visible\n }\n\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n\n constructor({ config, id }: { config: FrigadeConfig; id: string }) {\n super(config)\n this.id = id\n this.init()\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.init()\n }\n\n /**\n * @ignore\n */\n resyncState(overrideStatefulFlow?: StatefulFlow) {\n const statefulFlow = overrideStatefulFlow ?? this.getStatefulFlow()\n\n this.rawData = statefulFlow\n this.title = statefulFlow?.data?.title\n this.subtitle = statefulFlow?.data?.subtitle\n\n this.isCompleted = statefulFlow.$state.completed\n this.isStarted = statefulFlow.$state.started\n this.isSkipped = statefulFlow.$state.skipped\n this._isVisible = statefulFlow.$state.visible\n\n // Set\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n const existingStep = this.steps?.get(step.id)\n if (existingStep) {\n existingStep.$state = stepObj.$state\n }\n })\n }\n\n /**\n * @ignore\n */\n private initStepFromStatefulStep(step: StatefulStep, index: number) {\n return {\n ...step,\n flow: this,\n $state: {\n ...step.$state,\n lastActionAt: step.$state.lastActionAt ? new Date(step.$state.lastActionAt) : undefined,\n },\n order: index,\n } as Partial<FlowStep>\n }\n\n /**\n * @ignore\n */\n private init() {\n const statefulFlow = this.getStatefulFlow()\n\n this.resyncState()\n\n const newSteps = new Map<string, FlowStep>()\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (this.getCurrentStep().id === thisStep.id && thisStep.$state.started) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n copy.$state.lastActionAt = new Date()\n copy.$state.currentStepId = thisStep.id\n copy.$state.currentStepIndex = thisStep.order\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (!thisStep.$state.completed) {\n await this.sendFlowStateToAPI(STARTED_STEP, properties, thisStep.id)\n }\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.$state.completed) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n const copy = clone(this.getGlobalState().flowStates[this.id])\n\n copy.$state.started = true\n copy.data.steps[thisStep.order].$state.completed = true\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n\n // If there are more index, advance current step\n if (!isLastStep) {\n copy.$state.currentStepId = this.getStepByIndex(thisStep.order + 1).id\n copy.$state.currentStepIndex = thisStep.order + 1\n }\n\n if (isLastStep) {\n copy.$state.completed = true\n }\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n await this.sendFlowStateToAPI(COMPLETED_STEP, properties, thisStep.id)\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.$state.completed) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = false\n copy.data.steps[thisStep.order].$state.completed = false\n copy.data.steps[thisStep.order].$state.lastActionAt = undefined\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(NOT_STARTED_STEP, undefined, thisStep.id)\n }\n\n stepObj.onStateChange = (handler: (step: FlowStep, previousStep: FlowStep) => void) => {\n const wrapperHandler = (flow: Flow) => {\n if (flow.id !== this.id) {\n return\n }\n const thisStep = flow.steps.get(step.id)\n const previousStep = this.lastStepUpdate.get(handler)\n\n if (\n thisStep.$state.completed !== previousStep?.$state.completed ||\n thisStep.$state.started !== previousStep?.$state.started ||\n thisStep.$state.visible !== previousStep?.$state.visible ||\n thisStep.$state.blocked !== previousStep?.$state.blocked\n ) {\n handler(thisStep, previousStep ?? clone(thisStep))\n this.lastStepUpdate.set(handler, clone(thisStep))\n }\n }\n this.getGlobalState().onStepStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n stepObj.removeStateChangeHandler = (\n handler: (step: FlowStep, previousStep: FlowStep) => void\n ) => {\n const wrapperHandler = this.getGlobalState().onStepStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n newSteps.set(step.id, stepObj as FlowStep)\n })\n this.steps = newSteps\n // Check if empty object\n if (\n this.getGlobalState().variables &&\n this.getGlobalState().variables[this.id] &&\n Object.keys(this.getGlobalState().variables[this.id]).length > 0\n ) {\n this.applyVariables(this.getGlobalState().variables[this.id])\n }\n }\n\n /**\n * Marks the flow started\n */\n public async start(properties?: Record<string | number, any>) {\n if (this.isStarted || this.isCompleted) {\n return\n }\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.started = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(STARTED_FLOW, properties)\n }\n\n /**\n * Marks the flow completed\n */\n public async complete(properties?: Record<string | number, any>) {\n if (this.isCompleted) {\n return\n }\n this.optimisticallyMarkFlowCompleted()\n await this.sendFlowStateToAPI(COMPLETED_FLOW, properties)\n }\n\n /**\n * Marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n if (this.isSkipped) {\n return\n }\n this.optimisticallyMarkFlowSkipped()\n await this.sendFlowStateToAPI(SKIPPED_FLOW, properties)\n }\n\n /**\n * Navigates the flow to the next step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async forward(properties?: Record<string | number, any>) {\n const nextStep = this.getStepByIndex(this.getCurrentStepIndex() + 1)\n if (nextStep) {\n await nextStep.start(properties)\n } else {\n await this.complete(properties)\n }\n }\n\n /**\n * Navigates the flow to the previous step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async back(properties?: Record<string | number, any>) {\n const previousStep = this.getStepByIndex(this.getCurrentStepIndex() - 1)\n if (previousStep) {\n await previousStep.start(properties)\n }\n }\n\n /**\n * Restarts the flow/marks it not started\n */\n public async restart() {\n this.optimisticallyMarkFlowNotStarted()\n await this.sendFlowStateToAPI(NOT_STARTED_FLOW)\n }\n\n /**\n * Get a step by index\n * @param index\n */\n public getStepByIndex(index: number): FlowStep | undefined {\n return this.steps.get(Array.from(this.steps.keys())[index])\n }\n\n /**\n * Gets current step\n */\n public getCurrentStep(): FlowStep {\n return (\n this.steps.get(this.getStatefulFlow().$state.currentStepId) ??\n this.steps.get(Array.from(this.steps.keys())[0])\n )\n }\n\n /**\n * Get the index of the current step. Starts at 0\n */\n public getCurrentStepIndex(): number {\n const currentStep = this.getCurrentStep()\n return Array.from(this.steps.keys()).indexOf(currentStep.id)\n }\n\n /**\n * Get the number of completed steps for the current user in the current flow\n */\n public getNumberOfCompletedSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.completed).length\n }\n\n /**\n * Get the number of available steps for the current user in the current flow. This is the number of steps that are not hidden.\n */\n public getNumberOfAvailableSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.visible).length\n }\n\n /**\n * @ignore\n */\n public onStateChange(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = (flow: Flow, previousFlow: Flow) => {\n if (flow.id === this.id) {\n if (\n flow.isCompleted !== previousFlow?.isCompleted ||\n flow.isStarted !== previousFlow?.isStarted ||\n flow.isSkipped !== previousFlow?.isSkipped ||\n flow.isVisible !== previousFlow?.isVisible ||\n JSON.stringify(flow.steps) !== JSON.stringify(previousFlow?.steps)\n ) {\n handler(flow, previousFlow)\n }\n }\n }\n this.getGlobalState().onFlowStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n /**\n * @ignore\n */\n public removeStateChangeHandler(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = this.getGlobalState().onFlowStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n /**\n * @ignore\n */\n public applyVariables(variables: Record<string, any>) {\n // Replace ${variable} with the value of the variable\n const replaceVariables = (str: string) => {\n const matches = str.match(/\\${(.*?)}/g)\n if (matches) {\n matches.forEach((match) => {\n const variable = match.replace('${', '').replace('}', '')\n if (!variables[variable]) {\n return\n }\n str = str.replace(match, variables[variable])\n })\n }\n return str\n }\n\n this.title = replaceVariables(this.title ?? '')\n this.subtitle = replaceVariables(this.subtitle ?? '')\n this.steps.forEach((step) => {\n // Iterate over every string field in the step and replace variables\n Object.keys(step).forEach((key) => {\n if (typeof step[key] === 'string') {\n // @ts-ignore\n step[key] = replaceVariables(step[key])\n }\n })\n })\n\n this.getGlobalState().variables = {}\n this.getGlobalState().variables[this.id] = variables\n }\n\n /**\n * @ignore\n */\n private getStatefulFlow(): StatefulFlow {\n const userFlowStates = this.getGlobalState().flowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = true\n copy.$state.started = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowNotStarted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = false\n copy.$state.started = false\n copy.$state.visible = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n private async sendFlowStateToAPI(\n action: FlowActionType,\n data?: Record<string | number, any>,\n stepId?: string\n ) {\n const date = new Date()\n this.getGlobalState().lastSyncDate = date\n const flowStatesRaw: FlowStates = await this.fetch('/flowStates', {\n method: 'POST',\n body: JSON.stringify({\n userId: this.config.userId,\n groupId: this.config.groupId,\n flowSlug: this.id,\n stepId: stepId ?? this.getCurrentStep().id,\n data: data ? JSON.stringify(data) : {},\n actionType: action,\n createdAt: date,\n } as FlowStateDTO),\n })\n if (date < this.getGlobalState().lastSyncDate) {\n return\n }\n await this.getGlobalState().refreshStateFromAPI(flowStatesRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.skipped = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n public register(callback?: RulesGraphRegistryCallback) {\n const globalState = this.getGlobalState()\n\n globalState.rulesGraph.register(this.id, (visible) => {\n const prevFlow = this.getGlobalState().previousFlows.get(this.id)\n\n if (prevFlow?._isVisible !== visible) {\n // TODO: Store these in a hash so we can grab this flow's handler and call it\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n handler(this, prevFlow)\n this.getGlobalState().previousFlows.set(this.id, cloneFlow(this))\n })\n }\n\n callback?.(visible)\n })\n }\n\n public unregister() {\n this.getGlobalState().rulesGraph.unregister(this.id)\n }\n}\n","import { RulesGraphData } from './types'\n\nexport interface RulesGraphNode {\n visible: boolean\n edges: RulesGraphEdge[]\n}\n\nexport interface RulesGraphEdge {\n head: string\n ruleId: string\n}\n\nexport type RulesGraphRegistryCallback = (visible: boolean) => void\n\n// TODO: JSDoc this class\nexport class RulesGraph {\n private graph: Map<string, RulesGraphNode> = new Map()\n private readonly registry: Map<string, RulesGraphRegistryCallback> = new Map()\n private readonly ruleOrder: string[] = []\n\n constructor(graphData: RulesGraphData) {\n this.ruleOrder = graphData?.ruleOrder ?? []\n this.ingestGraphData(graphData?.graph ?? {})\n }\n\n ingestGraphData(graphData: Record<string, RulesGraphNode>) {\n this.graph = new Map(Object.entries(graphData))\n\n this.fireCallbacks()\n }\n\n fireCallbacks() {\n for (const [flowId, callback] of this.registry) {\n if (typeof callback === 'function') {\n callback(this.isFlowVisible(flowId))\n }\n }\n }\n\n isFlowVisible(flowId: string) {\n const currentNode = this.graph.get(flowId)\n\n // Is the flow in the graph?\n // If not, it belongs to 0 rules and can carry on about its business\n if (!currentNode) {\n return true\n }\n\n if (currentNode.visible === false) {\n return false\n }\n\n // Does the flow have edges?\n // If not, it's the king of all flows, and it does what it wants\n if (currentNode.edges.length === 0) {\n return true\n }\n\n // Are any of the flow's descendants visible and in the registry already?\n const registeredDescendant = this.findRegisteredDescendant(flowId)\n\n // If so, this flow is not visible\n if (registeredDescendant != null) {\n return false\n }\n\n // Congrats! There are no conditions in the graph that disqualify this flow from being visible\n return true\n }\n\n register(flowId: string, callback?: RulesGraphRegistryCallback) {\n this.registry.set(flowId, callback ?? (() => {}))\n\n this.fireCallbacks()\n }\n\n unregister(flowId: string) {\n this.registry.delete(flowId)\n\n this.fireCallbacks()\n }\n\n getRegistry() {\n return this.registry\n }\n\n private findRegisteredDescendant(nodeId: string, originId = nodeId, ruleId?: string) {\n const node = this.graph.get(nodeId)\n\n if (node == null) {\n return undefined\n }\n\n if (this.registry.has(nodeId) && originId !== nodeId) {\n return node\n }\n\n let result = undefined\n\n if (node.edges.length > 0) {\n for (const edge of node.edges) {\n // If we went past the end of the rule, stop searching for this rule\n if (ruleId != null && edge.ruleId !== ruleId) {\n continue\n }\n\n const ancestor = this.findRegisteredDescendant(edge.head, originId, ruleId ?? edge.ruleId)\n\n if (ancestor != null && ancestor.visible) {\n result = ancestor\n break\n }\n }\n }\n\n return result\n }\n}\n","import { FlowStates, FrigadeConfig, StatefulFlow, StatefulStep } from './types'\nimport { clearCache, cloneFlow, GUEST_PREFIX, isWeb, resetAllLocalStorage } from '../shared/utils'\nimport { Flow } from './flow'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraph } from './rules-graph'\n\nexport class Frigade extends Fetchable {\n /**\n * @ignore\n */\n private flows: Flow[] = []\n /**\n * @ignore\n */\n private initPromise: Promise<void>\n /**\n * @ignore\n */\n private hasFailed = false\n\n /**\n * @ignore\n */\n private visibilityChangeHandler = async () => {\n if (document.visibilityState === 'visible') {\n await this.refreshStateFromAPI()\n }\n }\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n this.init(this.config)\n if (isWeb()) {\n document.addEventListener('visibilitychange', this.visibilityChangeHandler)\n }\n }\n\n /**\n * @ignore\n */\n destroy() {\n if (isWeb()) {\n document.removeEventListener('visibilitychange', this.visibilityChangeHandler)\n // Remove all other event listeners\n const globalStateKey = getGlobalStateKey(this.config)\n if (frigadeGlobalState[globalStateKey]) {\n frigadeGlobalState[globalStateKey].onFlowStateChangeHandlers = []\n }\n }\n }\n\n /**\n * @ignore\n */\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n if (this.config.userId && !this.config.userId?.startsWith(GUEST_PREFIX)) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n }),\n })\n }\n await this.refreshStateFromAPI()\n })()\n\n return this.initPromise\n }\n\n /**\n * Set the current user.\n * @param userId\n * @param properties\n */\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.config = { ...this.config, userId }\n await this.initIfNeeded()\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Set the group for the current user.\n * @param groupId\n * @param properties\n */\n public async group(groupId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.groupId = groupId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Track an event for the current user (and group if set).\n * @param event\n * @param properties\n */\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n if (!event) {\n console.error('Event name is required to track an event')\n return\n }\n if (this.config.userId && this.config.groupId) {\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n } else if (this.config.userId) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n }\n await this.resync()\n }\n\n /**\n * @ignore\n */\n public isReady(): boolean {\n return Boolean(this.config.__instanceId && this.config.apiKey && this.initPromise)\n }\n\n /**\n * Get a Flow by its ID.\n * @param flowId\n */\n public async getFlow(flowId: string) {\n await this.initIfNeeded()\n\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n public async getFlows() {\n await this.initIfNeeded()\n return this.flows\n }\n\n /**\n * Reload the current state of the flows by calling the Frigade API.\n * This will trigger all event handlers.\n */\n public async reload() {\n resetAllLocalStorage()\n clearCache()\n await this.refreshStateFromAPI()\n this.initPromise = null\n await this.init(this.config)\n // Trigger all event handlers\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n private async resync() {\n this.initPromise = null\n await this.init(this.config)\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n /**\n * Event handler that captures all changes that happen to the state of the Flows.\n * @param handler\n */\n public async onStateChange(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers.push(handler)\n }\n\n /**\n * Returns true if the JS SDK failed to connect to the Frigade API.\n */\n hasFailedToLoad() {\n return this.hasFailed\n }\n\n /**\n * Removes the given handler from the list of event handlers.\n * @param handler\n */\n public async removeStateChangeHandler(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== handler)\n }\n\n /**\n * @ignore\n */\n private async initIfNeeded() {\n if (this.initPromise !== null) {\n return this.initPromise\n } else {\n return this.init(this.config)\n }\n }\n\n /**\n * @ignore\n */\n private async refreshStateFromAPI(): Promise<void> {\n const globalStateKey = getGlobalStateKey(this.config)\n\n if (!frigadeGlobalState[globalStateKey]) {\n const that = this\n\n let validator = {\n set: function (target: any, key: any, value: any) {\n if (target[key]) {\n const previousState = target[key] as StatefulFlow\n const newState = value as StatefulFlow\n if (JSON.stringify(previousState) !== JSON.stringify(newState)) {\n that.triggerEventHandlers(newState, previousState)\n }\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshStateFromAPI: async () => {},\n rulesGraph: new RulesGraph({\n graph: {},\n ruleOrder: [],\n }),\n flowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n variables: {},\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshStateFromAPI = async (\n overrideFlowStateRaw?: FlowStates\n ) => {\n if (this.config.__readOnly) {\n return\n }\n\n const flowStateRaw: FlowStates = overrideFlowStateRaw\n ? overrideFlowStateRaw\n : await this.fetch(\n `/flowStates?userId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId ? `&groupId=${encodeURIComponent(this.config.groupId)}` : ''\n }`\n )\n\n // TODO: should this also take in order and check if it has changed?\n frigadeGlobalState[globalStateKey].rulesGraph.ingestGraphData(\n flowStateRaw.ruleGraph?.graph ?? {}\n )\n\n if (flowStateRaw && flowStateRaw.eligibleFlows) {\n flowStateRaw.eligibleFlows.forEach((statefulFlow) => {\n frigadeGlobalState[globalStateKey].flowStates[statefulFlow.flowSlug] = statefulFlow\n if (!this.flows.find((flow) => flow.id == statefulFlow.flowSlug)) {\n this.flows.push(\n new Flow({\n config: this.config,\n id: statefulFlow.flowSlug,\n })\n )\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshStateFromAPI()\n }\n\n /**\n * @ignore\n */\n private mockFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].flowStates[flowId] = {\n flowSlug: flowId,\n flowName: parsed?.name ?? flowId,\n flowType: parsed?.type ?? 'CHECKLIST',\n data: {\n ...parsed,\n steps: (parsed?.steps ?? []).map((step: any): StatefulStep => {\n return {\n id: step.id,\n $state: {\n completed: false,\n started: false,\n visible: true,\n blocked: false,\n },\n ...step,\n }\n }),\n },\n $state: {\n currentStepId: null,\n currentStepIndex: -1,\n completed: false,\n started: false,\n skipped: false,\n visible: true,\n },\n } as StatefulFlow\n\n this.flows.push(\n new Flow({\n config: this.config,\n id: flowId,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(newState: StatefulFlow, previousState?: StatefulFlow) {\n if (newState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousState.flowSlug) {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n flow.resyncState(newState)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n }\n })\n }\n }\n}\n","import type { Flow } from './flow'\n\nimport { RulesGraphNode } from './rules-graph'\n\nexport enum TriggerType {\n MANUAL = 'MANUAL',\n AUTOMATIC = 'AUTOMATIC',\n}\n\nexport enum FlowStatus {\n DRAFT = 'DRAFT',\n ACTIVE = 'ACTIVE',\n ARCHIVED = 'ARCHIVED',\n}\n\nexport type StepAction =\n | 'flow.back'\n | 'flow.complete'\n | 'flow.forward'\n | 'flow.restart'\n | 'flow.skip'\n | 'flow.start'\n | 'step.complete'\n | 'step.reset'\n | 'step.start'\n | false\n\nexport interface FlowStep extends StatefulStep {\n /**\n * Order of the step in the Flow.\n */\n order: number\n\n /**\n * Name of the step when shown in a list view.\n */\n stepName?: string\n\n /**\n * Title of the step.\n */\n title?: string\n\n /**\n * Subtitle of the step.\n */\n subtitle?: string\n\n /**\n * Config for the primary button in this step\n */\n primaryButton?: {\n /**\n * Primary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Primary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Primary button title. If omitted, the primary button will not be shown.\n */\n title?: string\n\n /**\n * Primary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use primaryButton.title instead\n * @description Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * @deprecated Use primaryButton.uri instead\n * @description Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * @deprecated Use primaryButton.target instead\n * @description Primary button URI target (defaults to _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Config for the secondary button in this step\n */\n secondaryButton?: {\n /**\n * Secondary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Secondary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n title?: string\n\n /**\n * Secondary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use secondaryButton.title instead\n * @description Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * @deprecated Use secondaryButton.uri instead\n * @description Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * @deprecated Use secondaryButton.target instead\n * @description Secondary button URI target (defaults to _self)\n */\n secondaryButtonUriTarget?: string\n\n /**\n * Text on button if a back button is present.\n */\n backButtonTitle?: string\n\n /**\n * If true, the step will be marked as completed when the secondary button is clicked.\n */\n skippable?: boolean\n\n /**\n * Video url to be shown for components supporting video.\n */\n videoUri?: string\n\n /**\n * Image url to be shown for components supporting image.\n */\n imageUri?: string | null\n\n /**\n * Automatically mark the step as completed when the primary button is clicked. Default is false.\n */\n autoMarkCompleted?: boolean\n\n /**\n * @ignore\n */\n props?: any\n /**\n * Criteria that needs to be met for the step to complete.\n * Completion criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be completed.\n */\n completionCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to start.\n * Start criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be started.\n */\n startCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to be visible.\n * Visibility criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be visible.\n */\n visibilityCriteria?: string\n\n /**\n * Progress if the step is tied to another Frigade Flow through completionCriteria.\n */\n progress?: number\n\n /**\n * Whether the step is dismissible (for instance, tooltips or other non-essential steps).\n */\n dismissible?: boolean\n\n /**\n * Any other additional props defined in the YAML config.\n */\n [x: string | number | symbol]: unknown\n\n /**\n * Marks the step started.\n */\n start: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Marks the step completed.\n */\n complete: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Resets the step (useful for undoing a finished step).\n */\n reset: () => Promise<void>\n\n /**\n * Event handler called when this step's state changes.\n */\n onStateChange: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Removes the given callback from the list of event handlers.\n */\n removeStateChangeHandler: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Reference to this step's parent Flow\n */\n flow: Flow\n}\n\nexport interface FrigadeConfig {\n /**\n * Frigade API key. You can find your API key in the Frigade Dashboard under \"Developer\".\n */\n apiKey?: string\n /**\n * API url to use for all requests. Defaults to https://api.frigade.com\n */\n apiUrl?: string\n\n /**\n * User ID to use for all requests. If not provided, a Guest ID will be generated.\n */\n userId?: string\n\n /**\n * Group ID (organization) to use for all requests.\n */\n groupId?: string\n\n /**\n * @ignore Internal use only.\n * If enabled, Frigade will not send any data to the API. A user's state will be reset on page refresh.\n */\n __readOnly?: boolean\n\n /**\n * @ignore Internal use only.\n * Map of Flow ID to Flow Config for all flows in the app.\n * Configs will have to be provided in serialized JSON format rather than YAML.\n */\n __flowConfigOverrides?: Record<string, string>\n\n /**\n * @ignore Internal use only.\n */\n __instanceId?: string\n}\n\nexport interface StatefulStep {\n id: string\n $state: {\n completed: boolean\n started: boolean\n visible: boolean\n blocked: boolean\n lastActionAt?: Date\n }\n // allow any other properties\n [key: string]: any\n}\n\nexport interface StatefulFlow {\n flowSlug: string\n flowName: string\n flowType: FlowType\n data: {\n steps: StatefulStep[]\n // allow any other properties\n [key: string]: any\n }\n $state: {\n currentStepId: string\n currentStepIndex: number\n completed: boolean\n started: boolean\n skipped: boolean\n visible: boolean\n lastActionAt?: Date\n }\n}\n\nexport interface RulesGraphData {\n ruleOrder: string[]\n graph: Record<string, RulesGraphNode>\n}\n\nexport interface FlowStates {\n eligibleFlows: StatefulFlow[]\n ineligibleFlows: string[]\n ruleGraph?: RulesGraphData\n}\n\nexport enum FlowType {\n ANNOUNCEMENT = 'ANNOUNCEMENT',\n CHECKLIST = 'CHECKLIST',\n FORM = 'FORM',\n TOUR = 'TOUR',\n SUPPORT = 'SUPPORT',\n CUSTOM = 'CUSTOM',\n BANNER = 'BANNER',\n EMBEDDED_TIP = 'EMBEDDED_TIP',\n NPS_SURVEY = 'NPS_SURVEY',\n SURVEY = 'SURVEY',\n CARD = 'CARD',\n}\n\nexport type FlowActionType =\n | 'STARTED_STEP'\n | 'COMPLETED_STEP'\n | 'NOT_STARTED_STEP'\n | 'STARTED_FLOW'\n | 'COMPLETED_FLOW'\n | 'SKIPPED_FLOW'\n | 'NOT_STARTED_FLOW'\n\nexport class FlowStateDTO {\n userId: string\n groupId?: string\n flowSlug: string\n stepId?: string\n data?: string\n actionType: FlowActionType\n createdAt?: Date\n}\n"],"mappings":";48BAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,UAAAE,EAAA,eAAAC,EAAA,aAAAC,EAAA,YAAAC,EAAA,gBAAAC,IA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V,EAAA,GACDmC,GADC,CAEJ,QAAQD,EAAAC,GAAA,YAAAA,EAAQ,QAAR,KAAAD,EAAiB,CAAC,GAAG,IAAKE,GACzBpC,EAAA,CACL,GAAIoC,EAAK,GACT,OAAQ,CACN,UAAW,GACX,QAAS,GACT,QAAS,GACT,QAAS,EACX,GACGA,EAEN,CACH,GACA,OAAQ,CACN,cAAe,KACf,iBAAkB,GAClB,UAAW,GACX,QAAS,GACT,QAAS,GACT,QAAS,EACX,CACF,EAEA,KAAK,MAAM,KACT,IAAIH,EAAK,CACP,OAAQ,KAAK,OACb,GAAIpB,CACN,CAAC,CACH,CACF,CAAC,CACH,CAKc,qBAAqBc,EAAwBD,EAA8B,QAAAzB,EAAA,sBACnF0B,GACF,KAAK,MAAM,QAASb,GAAS,CACvBA,EAAK,IAAMY,EAAc,UAC3B,KAAK,eAAe,EAAE,0BAA0B,QAAST,GAAY,CACnE,IAAMC,EAAW,KAAK,eAAe,EAAE,cAAc,IAAIJ,EAAK,EAAE,EAChEA,EAAK,YAAYa,CAAQ,EACzBV,EAAQH,EAAMI,CAAQ,EACtB,KAAK,eAAe,EAAE,cAAc,IAAIJ,EAAK,GAAIK,EAAUL,CAAI,CAAC,CAClE,CAAC,CAEL,CAAC,CAEL,GACF,ECxYO,IAAKuB,OACVA,EAAA,OAAS,SACTA,EAAA,UAAY,YAFFA,OAAA,IAKAC,OACVA,EAAA,MAAQ,QACRA,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IA6SAC,OACVA,EAAA,aAAe,eACfA,EAAA,UAAY,YACZA,EAAA,KAAO,OACPA,EAAA,KAAO,OACPA,EAAA,QAAU,UACVA,EAAA,OAAS,SACTA,EAAA,OAAS,SACTA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,OAAS,SACTA,EAAA,KAAO,OAXGA,OAAA","names":["src_exports","__export","Flow","FlowStatus","FlowType","Frigade","TriggerType","__toCommonJS","VERSION_NUMBER","import_uuid","frigadeGlobalState","getGlobalStateKey","internalConfig","_a","_b","Fetchable","config","generateGuestId","filteredConfig","_","v","__spreadValues","path","options","__async","getEmptyResponse","gracefulFetch","getHeaders","pathPrefix","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","id","visible","overrideStatefulFlow","_a","_b","statefulFlow","step","index","stepObj","existingStep","__spreadProps","__spreadValues","newSteps","properties","__async","thisStep","copy","clone","STARTED_STEP","isLastStep","COMPLETED_STEP","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","STARTED_FLOW","COMPLETED_FLOW","SKIPPED_FLOW","nextStep","NOT_STARTED_FLOW","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","key","action","data","stepId","date","flowStatesRaw","callback","prevFlow","cloneFlow","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","LOCAL_STORAGE_PREFIX","cloneFlow","flow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","CallQueue","call","now","item","callQueue","gracefulFetch","url","options","__async","_a","getEmptyResponse","lastCallDataKey","response","error","body","e","generateGuestId","guestId","getLocalStorage","GUEST_KEY","GUEST_PREFIX","uuidv4","setLocalStorage","isWeb","RulesGraph","graphData","_a","_b","flowId","callback","currentNode","nodeId","originId","ruleId","node","result","edge","ancestor","Frigade","Fetchable","apiKey","config","__spreadValues","__async","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","__spreadProps","groupId","event","flowId","flow","resetAllLocalStorage","clearCache","handler","lastFlow","cloneFlow","h","that","validator","target","key","value","previousState","newState","RulesGraph","overrideFlowStateRaw","_b","flowStateRaw","statefulFlow","Flow","_c","parsed","step","TriggerType","FlowStatus","FlowType"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/rules-graph.ts","../src/core/frigade.ts","../src/core/types.ts"],"sourcesContent":["export { Frigade } from './core/frigade'\nexport { Flow } from './core/flow'\nexport { FlowStatus, type FlowStep, type FrigadeConfig, FlowType, TriggerType } from './core/types'\nexport type { RulesGraphRegistryCallback } from './core/rules-graph'\n","export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport { v4 as uuidv4 } from 'uuid'\nimport { Flow } from '../core/flow'\nimport { frigadeGlobalState } from './state'\n\nexport const NOT_STARTED_STEP = 'NOT_STARTED_STEP'\nexport const COMPLETED_FLOW = 'COMPLETED_FLOW'\nexport const SKIPPED_FLOW = 'SKIPPED_FLOW'\nexport const STARTED_FLOW = 'STARTED_FLOW'\nexport const NOT_STARTED_FLOW = 'NOT_STARTED_FLOW'\nexport const COMPLETED_STEP = 'COMPLETED_STEP'\nexport const STARTED_STEP = 'STARTED_STEP'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow({\n config: flow.config,\n id: flow.id,\n })\n return newFlow\n}\n\nexport function clone<T>(obj: T): T {\n return JSON.parse(JSON.stringify(obj))\n}\n\nexport function getHeaders(apiKey: string) {\n return {\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n 'X-Frigade-SDK-Version': VERSION_NUMBER,\n 'X-Frigade-SDK-Platform': 'Javascript',\n },\n }\n}\n\nfunction getLocalStorage(key: string) {\n if (isWeb()) {\n return window.localStorage.getItem(`${LOCAL_STORAGE_PREFIX}${key}`)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (isWeb()) {\n window.localStorage.setItem(`${LOCAL_STORAGE_PREFIX}${key}`, value)\n }\n}\n\nfunction setGlobalState(key: string, value: any) {\n frigadeGlobalState[key] = value\n}\n\nfunction getGlobalState(key: string): any {\n return frigadeGlobalState[key]\n}\n\nexport function clearCache() {\n Object.keys(frigadeGlobalState).forEach((key) => {\n if (key.startsWith(GET_CACHE_PREFIX)) {\n delete frigadeGlobalState[key]\n }\n })\n}\n\nexport function resetAllLocalStorage() {\n if (isWeb()) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith(LOCAL_STORAGE_PREFIX)) {\n window.localStorage.removeItem(key)\n }\n })\n }\n}\n\nclass CallQueue {\n private queue: {\n call: string\n time: number\n }[] = []\n private readonly ttlInMS = 500\n private readonly cacheSize = 5\n\n public push(call: string) {\n const now = new Date()\n if (this.queue.length >= this.cacheSize) {\n this.queue.shift()\n }\n this.queue.push({\n call: call,\n time: now.getTime(),\n })\n }\n\n public hasIdenticalCall(call: string) {\n const now = new Date()\n this.queue = this.queue.filter((item) => now.getTime() - item.time < this.ttlInMS)\n return this.queue.some((item) => item.call === call)\n }\n}\n\nconst callQueue = new CallQueue()\n\nexport async function gracefulFetch(url: string, options: any) {\n if (typeof globalThis.fetch !== 'function') {\n return getEmptyResponse(\n \"- Attempted to call fetch() in an environment that doesn't support it.\"\n )\n }\n\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCallDataKey = `${url}${JSON.stringify(options.body ?? {})}`\n if (callQueue.hasIdenticalCall(lastCallDataKey)) {\n return getEmptyResponse()\n }\n callQueue.push(lastCallDataKey)\n }\n\n let response\n try {\n response = fetch(url, options)\n response = await response\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse('Received an empty response')\n }\n\n if (response.status >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n try {\n if (response.status === 204) {\n return getEmptyResponse()\n }\n\n let body\n try {\n body = await response.json()\n } catch (e) {\n return getEmptyResponse()\n }\n\n if (body.error) {\n return getEmptyResponse(body.error)\n }\n return body\n } catch (e) {\n return getEmptyResponse(e)\n }\n}\n\nexport function getEmptyResponse(error?: any) {\n if (error) {\n console.log('Call to Frigade failed', error)\n }\n\n // Create empty response that contains the .json method and returns an empty object\n return {\n json: () => ({}),\n }\n}\n\nexport function generateGuestId() {\n if (isWeb()) {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n setLocalStorage(GUEST_KEY, guestId)\n }\n return guestId\n }\n}\n\nexport function fetcher(apiKey: string, path: string, options?: Record<any, any>) {\n return gracefulFetch(`//api.frigade.com/v1/public${path}`, {\n ...(options ?? {}),\n ...getHeaders(apiKey),\n })\n}\n\nexport function isWeb() {\n return (\n typeof window !== 'undefined' &&\n typeof window.document !== 'undefined' &&\n typeof window.localStorage !== 'undefined'\n )\n}\n","import { FlowStates, FlowStep, FrigadeConfig, StatefulFlow } from '../core/types'\nimport { Flow } from '../core/flow'\nimport { RulesGraph } from '../core/rules-graph'\n\nexport interface FrigadeGlobalState {\n refreshStateFromAPI: (overrideFlowStatesRaw?: FlowStates) => Promise<void>\n flowStates: Record<string, StatefulFlow>\n rulesGraph: RulesGraph\n onFlowStateChangeHandlerWrappers: Map<\n (flow: Flow, previousFlow: Flow) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onStepStateChangeHandlerWrappers: Map<\n (step: FlowStep, previousStep: FlowStep) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onFlowStateChangeHandlers: ((flow: Flow, previousFlow: Flow) => void)[]\n previousFlows: Map<string, Flow>\n variables: Record<string, Record<string, any>>\n lastSyncDate?: Date\n}\n\nexport let frigadeGlobalState: Record<string, FrigadeGlobalState> = {}\n\nexport function getGlobalStateKey(internalConfig: FrigadeConfig): string {\n return `${internalConfig.__instanceId}-${internalConfig.apiKey}:${internalConfig.userId ?? ''}:${\n internalConfig.groupId ?? ''\n }`\n}\n","import { generateGuestId, getEmptyResponse, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../core/types'\nimport { frigadeGlobalState, FrigadeGlobalState, getGlobalStateKey } from './state'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: 'https://api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(12).substring(4),\n }\n\n constructor(config: FrigadeConfig) {\n const filteredConfig = Object.fromEntries(Object.entries(config).filter(([_, v]) => v != null))\n\n this.config = {\n ...this.config,\n ...filteredConfig,\n }\n }\n\n /**\n * @ignore\n */\n public async fetch(path: string, options?: Record<any, any>) {\n if (this.config.__readOnly) {\n return getEmptyResponse()\n }\n\n return gracefulFetch(this.getAPIUrl(path), {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n\n private getAPIUrl(path: string) {\n const pathPrefix = '/v1/public'\n const apiUrl = new URL(\n `${pathPrefix}${path}`,\n this.config.apiUrl.replace(pathPrefix, '')\n ).toString()\n\n return apiUrl\n }\n\n /**\n * @ignore\n */\n protected getGlobalState(): FrigadeGlobalState {\n const globalStateKey = getGlobalStateKey(this.config)\n if (!frigadeGlobalState[globalStateKey]) {\n throw new Error('Frigade has not yet been initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import {\n FlowActionType,\n FlowStateDTO,\n FlowStates,\n FlowStep,\n FrigadeConfig,\n StatefulFlow,\n StatefulStep,\n} from './types'\nimport {\n clone,\n cloneFlow,\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n NOT_STARTED_STEP,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraphRegistryCallback } from './rules-graph'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * Ordered map of the Steps in the Flow.\n * See [Flow Step Definition](https://docs.frigade.com/v2/sdk/js/step) for more information.\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the Flow, if defined at the top level of the YAML config.\n */\n public title?: string\n /**\n * The user-facing description of the Flow, if defined at the top level of the YAML config.\n */\n public subtitle?: string\n /**\n * The metadata of the Flow.\n * @ignore\n */\n public rawData: StatefulFlow\n /**\n * Whether the Flow is completed or not.\n */\n public isCompleted: boolean\n /**\n * Whether the Flow is started or not.\n */\n public isStarted: boolean\n /**\n * Whether the Flow has been skipped or not.\n */\n public isSkipped: boolean\n /**\n * @ignore\n */\n private _isVisible: boolean = false\n /**\n * Whether the Flow is visible to the user based on the current user/group's state.\n */\n get isVisible() {\n if (this._isVisible === false) {\n return false\n }\n\n return this.getGlobalState().rulesGraph.isFlowVisible(this.id)\n }\n set isVisible(visible: boolean) {\n this._isVisible = visible\n }\n\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n\n constructor({ config, id }: { config: FrigadeConfig; id: string }) {\n super(config)\n this.id = id\n this.init()\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.init()\n }\n\n /**\n * @ignore\n */\n resyncState(overrideStatefulFlow?: StatefulFlow) {\n const statefulFlow = overrideStatefulFlow ?? this.getStatefulFlow()\n\n this.rawData = statefulFlow\n this.title = statefulFlow?.data?.title\n this.subtitle = statefulFlow?.data?.subtitle\n\n this.isCompleted = statefulFlow.$state.completed\n this.isStarted = statefulFlow.$state.started\n this.isSkipped = statefulFlow.$state.skipped\n this._isVisible = statefulFlow.$state.visible\n\n // Set\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n const existingStep = this.steps?.get(step.id)\n if (existingStep) {\n existingStep.$state = stepObj.$state\n }\n })\n }\n\n /**\n * @ignore\n */\n private initStepFromStatefulStep(step: StatefulStep, index: number) {\n return {\n ...step,\n flow: this,\n $state: {\n ...step.$state,\n lastActionAt: step.$state.lastActionAt ? new Date(step.$state.lastActionAt) : undefined,\n },\n order: index,\n } as Partial<FlowStep>\n }\n\n /**\n * @ignore\n */\n private init() {\n const statefulFlow = this.getStatefulFlow()\n\n this.resyncState()\n\n const newSteps = new Map<string, FlowStep>()\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (this.getCurrentStep().id === thisStep.id && thisStep.$state.started) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n copy.$state.lastActionAt = new Date()\n copy.$state.currentStepId = thisStep.id\n copy.$state.currentStepIndex = thisStep.order\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (!thisStep.$state.completed) {\n await this.sendFlowStateToAPI(STARTED_STEP, properties, thisStep.id)\n }\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.$state.completed) {\n return\n }\n\n const isLastStep = thisStep.order + 1 === this.getNumberOfAvailableSteps()\n const copy = clone(this.getGlobalState().flowStates[this.id])\n\n copy.$state.started = true\n copy.data.steps[thisStep.order].$state.completed = true\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n\n // If there are more index, advance current step\n if (!isLastStep) {\n copy.$state.currentStepId = this.getStepByIndex(thisStep.order + 1).id\n copy.$state.currentStepIndex = thisStep.order + 1\n }\n\n if (isLastStep) {\n copy.$state.completed = true\n }\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n await this.sendFlowStateToAPI(COMPLETED_STEP, properties, thisStep.id)\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.$state.completed) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = false\n copy.data.steps[thisStep.order].$state.completed = false\n copy.data.steps[thisStep.order].$state.lastActionAt = undefined\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(NOT_STARTED_STEP, undefined, thisStep.id)\n }\n\n stepObj.onStateChange = (handler: (step: FlowStep, previousStep: FlowStep) => void) => {\n const wrapperHandler = (flow: Flow) => {\n if (flow.id !== this.id) {\n return\n }\n const thisStep = flow.steps.get(step.id)\n const previousStep = this.lastStepUpdate.get(handler)\n\n if (\n thisStep.$state.completed !== previousStep?.$state.completed ||\n thisStep.$state.started !== previousStep?.$state.started ||\n thisStep.$state.visible !== previousStep?.$state.visible ||\n thisStep.$state.blocked !== previousStep?.$state.blocked\n ) {\n handler(thisStep, previousStep ?? clone(thisStep))\n this.lastStepUpdate.set(handler, clone(thisStep))\n }\n }\n this.getGlobalState().onStepStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n stepObj.removeStateChangeHandler = (\n handler: (step: FlowStep, previousStep: FlowStep) => void\n ) => {\n const wrapperHandler = this.getGlobalState().onStepStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n newSteps.set(step.id, stepObj as FlowStep)\n })\n this.steps = newSteps\n // Check if empty object\n if (\n this.getGlobalState().variables &&\n this.getGlobalState().variables[this.id] &&\n Object.keys(this.getGlobalState().variables[this.id]).length > 0\n ) {\n this.applyVariables(this.getGlobalState().variables[this.id])\n }\n }\n\n /**\n * Marks the flow started\n */\n public async start(properties?: Record<string | number, any>) {\n if (this.isStarted || this.isCompleted) {\n return\n }\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.started = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(STARTED_FLOW, properties)\n }\n\n /**\n * Marks the flow completed\n */\n public async complete(properties?: Record<string | number, any>) {\n if (this.isCompleted) {\n return\n }\n this.optimisticallyMarkFlowCompleted()\n await this.sendFlowStateToAPI(COMPLETED_FLOW, properties)\n }\n\n /**\n * Marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n if (this.isSkipped) {\n return\n }\n this.optimisticallyMarkFlowSkipped()\n await this.sendFlowStateToAPI(SKIPPED_FLOW, properties)\n }\n\n /**\n * Navigates the flow to the next step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async forward(properties?: Record<string | number, any>) {\n const nextStep = this.getStepByIndex(this.getCurrentStepIndex() + 1)\n if (nextStep) {\n await nextStep.start(properties)\n } else {\n await this.complete(properties)\n }\n }\n\n /**\n * Navigates the flow to the previous step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async back(properties?: Record<string | number, any>) {\n const previousStep = this.getStepByIndex(this.getCurrentStepIndex() - 1)\n if (previousStep) {\n await previousStep.start(properties)\n }\n }\n\n /**\n * Restarts the flow/marks it not started\n */\n public async restart() {\n this.optimisticallyMarkFlowNotStarted()\n await this.sendFlowStateToAPI(NOT_STARTED_FLOW)\n }\n\n /**\n * Get a step by index\n * @param index\n */\n public getStepByIndex(index: number): FlowStep | undefined {\n return this.steps.get(Array.from(this.steps.keys())[index])\n }\n\n /**\n * Gets current step\n */\n public getCurrentStep(): FlowStep {\n return (\n this.steps.get(this.getStatefulFlow().$state.currentStepId) ??\n this.steps.get(Array.from(this.steps.keys())[0])\n )\n }\n\n /**\n * Get the index of the current step. Starts at 0\n */\n public getCurrentStepIndex(): number {\n const currentStep = this.getCurrentStep()\n return Array.from(this.steps.keys()).indexOf(currentStep.id)\n }\n\n /**\n * Get the number of completed steps for the current user in the current flow\n */\n public getNumberOfCompletedSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.completed).length\n }\n\n /**\n * Get the number of available steps for the current user in the current flow. This is the number of steps that are not hidden.\n */\n public getNumberOfAvailableSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.visible).length\n }\n\n /**\n * @ignore\n */\n public onStateChange(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = (flow: Flow, previousFlow: Flow) => {\n if (flow.id === this.id) {\n if (\n flow.isCompleted !== previousFlow?.isCompleted ||\n flow.isStarted !== previousFlow?.isStarted ||\n flow.isSkipped !== previousFlow?.isSkipped ||\n flow.isVisible !== previousFlow?.isVisible ||\n JSON.stringify(flow.steps) !== JSON.stringify(previousFlow?.steps)\n ) {\n handler(flow, previousFlow)\n }\n }\n }\n this.getGlobalState().onFlowStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n /**\n * @ignore\n */\n public removeStateChangeHandler(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = this.getGlobalState().onFlowStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n /**\n * @ignore\n */\n public applyVariables(variables: Record<string, any>) {\n // Replace ${variable} with the value of the variable\n const replaceVariables = (str: string) => {\n const matches = str.match(/\\${(.*?)}/g)\n if (matches) {\n matches.forEach((match) => {\n const variable = match.replace('${', '').replace('}', '')\n if (!variables[variable]) {\n return\n }\n str = str.replace(match, variables[variable])\n })\n }\n return str\n }\n\n this.title = replaceVariables(this.title ?? '')\n this.subtitle = replaceVariables(this.subtitle ?? '')\n this.steps.forEach((step) => {\n // Iterate over every string field in the step and replace variables\n Object.keys(step).forEach((key) => {\n if (typeof step[key] === 'string') {\n // @ts-ignore\n step[key] = replaceVariables(step[key])\n }\n })\n })\n\n this.getGlobalState().variables = {}\n this.getGlobalState().variables[this.id] = variables\n }\n\n /**\n * @ignore\n */\n private getStatefulFlow(): StatefulFlow {\n const userFlowStates = this.getGlobalState().flowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = true\n copy.$state.started = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowNotStarted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = false\n copy.$state.started = false\n copy.$state.visible = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n private async sendFlowStateToAPI(\n action: FlowActionType,\n data?: Record<string | number, any>,\n stepId?: string\n ) {\n const date = new Date()\n this.getGlobalState().lastSyncDate = date\n const flowStatesRaw: FlowStates = await this.fetch('/flowStates', {\n method: 'POST',\n body: JSON.stringify({\n userId: this.config.userId,\n groupId: this.config.groupId,\n flowSlug: this.id,\n stepId: stepId ?? this.getCurrentStep().id,\n data: data ? data : {},\n actionType: action,\n createdAt: date,\n } as FlowStateDTO),\n })\n if (date < this.getGlobalState().lastSyncDate) {\n return\n }\n await this.getGlobalState().refreshStateFromAPI(flowStatesRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.skipped = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n public register(callback?: RulesGraphRegistryCallback) {\n const globalState = this.getGlobalState()\n\n globalState.rulesGraph.register(this.id, (visible) => {\n const prevFlow = this.getGlobalState().previousFlows.get(this.id)\n\n if (prevFlow?._isVisible !== visible) {\n // TODO: Store these in a hash so we can grab this flow's handler and call it\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n handler(this, prevFlow)\n this.getGlobalState().previousFlows.set(this.id, cloneFlow(this))\n })\n }\n\n callback?.(visible)\n })\n }\n\n public unregister() {\n this.getGlobalState().rulesGraph.unregister(this.id)\n }\n}\n","import { RulesGraphData } from './types'\n\nexport interface RulesGraphNode {\n visible: boolean\n edges: RulesGraphEdge[]\n}\n\nexport interface RulesGraphEdge {\n head: string\n ruleId: string\n}\n\nexport type RulesGraphRegistryCallback = (visible: boolean) => void\n\n// TODO: JSDoc this class\nexport class RulesGraph {\n private graph: Map<string, RulesGraphNode> = new Map()\n private readonly registry: Map<string, RulesGraphRegistryCallback> = new Map()\n private readonly ruleOrder: string[] = []\n\n constructor(graphData: RulesGraphData) {\n this.ruleOrder = graphData?.ruleOrder ?? []\n this.ingestGraphData(graphData?.graph ?? {})\n }\n\n ingestGraphData(graphData: Record<string, RulesGraphNode>) {\n this.graph = new Map(Object.entries(graphData))\n\n this.fireCallbacks()\n }\n\n fireCallbacks() {\n for (const [flowId, callback] of this.registry) {\n if (typeof callback === 'function') {\n callback(this.isFlowVisible(flowId))\n }\n }\n }\n\n isFlowVisible(flowId: string) {\n const currentNode = this.graph.get(flowId)\n\n // Is the flow in the graph?\n // If not, it belongs to 0 rules and can carry on about its business\n if (!currentNode) {\n return true\n }\n\n if (currentNode.visible === false) {\n return false\n }\n\n // Does the flow have edges?\n // If not, it's the king of all flows, and it does what it wants\n if (currentNode.edges.length === 0) {\n return true\n }\n\n // Are any of the flow's descendants visible and in the registry already?\n const registeredDescendant = this.findRegisteredDescendant(flowId)\n\n // If so, this flow is not visible\n if (registeredDescendant != null) {\n return false\n }\n\n // Congrats! There are no conditions in the graph that disqualify this flow from being visible\n return true\n }\n\n register(flowId: string, callback?: RulesGraphRegistryCallback) {\n this.registry.set(flowId, callback ?? (() => {}))\n\n this.fireCallbacks()\n }\n\n unregister(flowId: string) {\n this.registry.delete(flowId)\n\n this.fireCallbacks()\n }\n\n getRegistry() {\n return this.registry\n }\n\n private findRegisteredDescendant(nodeId: string, originId = nodeId, ruleId?: string) {\n const node = this.graph.get(nodeId)\n\n if (node == null) {\n return undefined\n }\n\n if (this.registry.has(nodeId) && originId !== nodeId) {\n return node\n }\n\n let result = undefined\n\n if (node.edges.length > 0) {\n for (const edge of node.edges) {\n // If we went past the end of the rule, stop searching for this rule\n if (ruleId != null && edge.ruleId !== ruleId) {\n continue\n }\n\n const ancestor = this.findRegisteredDescendant(edge.head, originId, ruleId ?? edge.ruleId)\n\n if (ancestor != null && ancestor.visible) {\n result = ancestor\n break\n }\n }\n }\n\n return result\n }\n}\n","import { FlowStates, FrigadeConfig, StatefulFlow, StatefulStep } from './types'\nimport { clearCache, cloneFlow, GUEST_PREFIX, isWeb, resetAllLocalStorage } from '../shared/utils'\nimport { Flow } from './flow'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraph } from './rules-graph'\n\nexport class Frigade extends Fetchable {\n /**\n * @ignore\n */\n private flows: Flow[] = []\n /**\n * @ignore\n */\n private initPromise: Promise<void>\n /**\n * @ignore\n */\n private hasFailed = false\n\n /**\n * @ignore\n */\n private visibilityChangeHandler = async () => {\n if (document.visibilityState === 'visible') {\n await this.refreshStateFromAPI()\n }\n }\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n this.init(this.config)\n if (isWeb()) {\n document.addEventListener('visibilitychange', this.visibilityChangeHandler)\n }\n }\n\n /**\n * @ignore\n */\n destroy() {\n if (isWeb()) {\n document.removeEventListener('visibilitychange', this.visibilityChangeHandler)\n // Remove all other event listeners\n const globalStateKey = getGlobalStateKey(this.config)\n if (frigadeGlobalState[globalStateKey]) {\n frigadeGlobalState[globalStateKey].onFlowStateChangeHandlers = []\n }\n }\n }\n\n /**\n * @ignore\n */\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n if (this.config.userId && !this.config.userId?.startsWith(GUEST_PREFIX)) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n }),\n })\n }\n await this.refreshStateFromAPI()\n })()\n\n return this.initPromise\n }\n\n /**\n * Set the current user.\n * @param userId\n * @param properties\n */\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.config = { ...this.config, userId }\n await this.initIfNeeded()\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Set the group for the current user.\n * @param groupId\n * @param properties\n */\n public async group(groupId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.groupId = groupId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Track an event for the current user (and group if set).\n * @param event\n * @param properties\n */\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n if (!event) {\n console.error('Event name is required to track an event')\n return\n }\n if (this.config.userId && this.config.groupId) {\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n } else if (this.config.userId) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n }\n await this.resync()\n }\n\n /**\n * @ignore\n */\n public isReady(): boolean {\n return Boolean(this.config.__instanceId && this.config.apiKey && this.initPromise)\n }\n\n /**\n * Get a Flow by its ID.\n * @param flowId\n */\n public async getFlow(flowId: string) {\n await this.initIfNeeded()\n\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n public async getFlows() {\n await this.initIfNeeded()\n return this.flows\n }\n\n /**\n * Reload the current state of the flows by calling the Frigade API.\n * This will trigger all event handlers.\n */\n public async reload() {\n resetAllLocalStorage()\n clearCache()\n await this.refreshStateFromAPI()\n this.initPromise = null\n await this.init(this.config)\n // Trigger all event handlers\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n private async resync() {\n this.initPromise = null\n await this.init(this.config)\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n /**\n * Event handler that captures all changes that happen to the state of the Flows.\n * @param handler\n */\n public async onStateChange(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers.push(handler)\n }\n\n /**\n * Returns true if the JS SDK failed to connect to the Frigade API.\n */\n hasFailedToLoad() {\n return this.hasFailed\n }\n\n /**\n * Removes the given handler from the list of event handlers.\n * @param handler\n */\n public async removeStateChangeHandler(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== handler)\n }\n\n /**\n * @ignore\n */\n private async initIfNeeded() {\n if (this.initPromise !== null) {\n return this.initPromise\n } else {\n return this.init(this.config)\n }\n }\n\n /**\n * @ignore\n */\n private async refreshStateFromAPI(): Promise<void> {\n const globalStateKey = getGlobalStateKey(this.config)\n\n if (!frigadeGlobalState[globalStateKey]) {\n const that = this\n\n let validator = {\n set: function (target: any, key: any, value: any) {\n if (target[key]) {\n const previousState = target[key] as StatefulFlow\n const newState = value as StatefulFlow\n if (JSON.stringify(previousState) !== JSON.stringify(newState)) {\n that.triggerEventHandlers(newState, previousState)\n }\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshStateFromAPI: async () => {},\n rulesGraph: new RulesGraph({\n graph: {},\n ruleOrder: [],\n }),\n flowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n variables: {},\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshStateFromAPI = async (\n overrideFlowStateRaw?: FlowStates\n ) => {\n if (this.config.__readOnly) {\n return\n }\n\n const flowStateRaw: FlowStates = overrideFlowStateRaw\n ? overrideFlowStateRaw\n : await this.fetch(\n `/flowStates?userId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId ? `&groupId=${encodeURIComponent(this.config.groupId)}` : ''\n }`\n )\n\n // TODO: should this also take in order and check if it has changed?\n frigadeGlobalState[globalStateKey].rulesGraph.ingestGraphData(\n flowStateRaw.ruleGraph?.graph ?? {}\n )\n\n if (flowStateRaw && flowStateRaw.eligibleFlows) {\n flowStateRaw.eligibleFlows.forEach((statefulFlow) => {\n frigadeGlobalState[globalStateKey].flowStates[statefulFlow.flowSlug] = statefulFlow\n if (!this.flows.find((flow) => flow.id == statefulFlow.flowSlug)) {\n this.flows.push(\n new Flow({\n config: this.config,\n id: statefulFlow.flowSlug,\n })\n )\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshStateFromAPI()\n }\n\n /**\n * @ignore\n */\n private mockFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].flowStates[flowId] = {\n flowSlug: flowId,\n flowName: parsed?.name ?? flowId,\n flowType: parsed?.type ?? 'CHECKLIST',\n data: {\n ...parsed,\n steps: (parsed?.steps ?? []).map((step: any): StatefulStep => {\n return {\n id: step.id,\n $state: {\n completed: false,\n started: false,\n visible: true,\n blocked: false,\n },\n ...step,\n }\n }),\n },\n $state: {\n currentStepId: null,\n currentStepIndex: -1,\n completed: false,\n started: false,\n skipped: false,\n visible: true,\n },\n } as StatefulFlow\n\n this.flows.push(\n new Flow({\n config: this.config,\n id: flowId,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(newState: StatefulFlow, previousState?: StatefulFlow) {\n if (newState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousState.flowSlug) {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n flow.resyncState(newState)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n }\n })\n }\n }\n}\n","import type { Flow } from './flow'\n\nimport { RulesGraphNode } from './rules-graph'\n\nexport enum TriggerType {\n MANUAL = 'MANUAL',\n AUTOMATIC = 'AUTOMATIC',\n}\n\nexport enum FlowStatus {\n DRAFT = 'DRAFT',\n ACTIVE = 'ACTIVE',\n ARCHIVED = 'ARCHIVED',\n}\n\nexport type StepAction =\n | 'flow.back'\n | 'flow.complete'\n | 'flow.forward'\n | 'flow.restart'\n | 'flow.skip'\n | 'flow.start'\n | 'step.complete'\n | 'step.reset'\n | 'step.start'\n | false\n\nexport interface FlowStep extends StatefulStep {\n /**\n * Order of the step in the Flow.\n */\n order: number\n\n /**\n * Name of the step when shown in a list view.\n */\n stepName?: string\n\n /**\n * Title of the step.\n */\n title?: string\n\n /**\n * Subtitle of the step.\n */\n subtitle?: string\n\n /**\n * Config for the primary button in this step\n */\n primaryButton?: {\n /**\n * Primary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Primary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Primary button title. If omitted, the primary button will not be shown.\n */\n title?: string\n\n /**\n * Primary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use primaryButton.title instead\n * @description Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * @deprecated Use primaryButton.uri instead\n * @description Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * @deprecated Use primaryButton.target instead\n * @description Primary button URI target (defaults to _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Config for the secondary button in this step\n */\n secondaryButton?: {\n /**\n * Secondary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Secondary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n title?: string\n\n /**\n * Secondary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use secondaryButton.title instead\n * @description Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * @deprecated Use secondaryButton.uri instead\n * @description Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * @deprecated Use secondaryButton.target instead\n * @description Secondary button URI target (defaults to _self)\n */\n secondaryButtonUriTarget?: string\n\n /**\n * Text on button if a back button is present.\n */\n backButtonTitle?: string\n\n /**\n * If true, the step will be marked as completed when the secondary button is clicked.\n */\n skippable?: boolean\n\n /**\n * Video url to be shown for components supporting video.\n */\n videoUri?: string\n\n /**\n * Image url to be shown for components supporting image.\n */\n imageUri?: string | null\n\n /**\n * Automatically mark the step as completed when the primary button is clicked. Default is false.\n */\n autoMarkCompleted?: boolean\n\n /**\n * @ignore\n */\n props?: any\n /**\n * Criteria that needs to be met for the step to complete.\n * Completion criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be completed.\n */\n completionCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to start.\n * Start criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be started.\n */\n startCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to be visible.\n * Visibility criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be visible.\n */\n visibilityCriteria?: string\n\n /**\n * Progress if the step is tied to another Frigade Flow through completionCriteria.\n */\n progress?: number\n\n /**\n * Whether the step is dismissible (for instance, tooltips or other non-essential steps).\n */\n dismissible?: boolean\n\n /**\n * Any other additional props defined in the YAML config.\n */\n [x: string | number | symbol]: unknown\n\n /**\n * Marks the step started.\n */\n start: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Marks the step completed.\n */\n complete: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Resets the step (useful for undoing a finished step).\n */\n reset: () => Promise<void>\n\n /**\n * Event handler called when this step's state changes.\n */\n onStateChange: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Removes the given callback from the list of event handlers.\n */\n removeStateChangeHandler: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Reference to this step's parent Flow\n */\n flow: Flow\n}\n\nexport interface FrigadeConfig {\n /**\n * Frigade API key. You can find your API key in the Frigade Dashboard under \"Developer\".\n */\n apiKey?: string\n /**\n * API url to use for all requests. Defaults to https://api.frigade.com\n */\n apiUrl?: string\n\n /**\n * User ID to use for all requests. If not provided, a Guest ID will be generated.\n */\n userId?: string\n\n /**\n * Group ID (organization) to use for all requests.\n */\n groupId?: string\n\n /**\n * @ignore Internal use only.\n * If enabled, Frigade will not send any data to the API. A user's state will be reset on page refresh.\n */\n __readOnly?: boolean\n\n /**\n * @ignore Internal use only.\n * Map of Flow ID to Flow Config for all flows in the app.\n * Configs will have to be provided in serialized JSON format rather than YAML.\n */\n __flowConfigOverrides?: Record<string, string>\n\n /**\n * @ignore Internal use only.\n */\n __instanceId?: string\n}\n\nexport interface StatefulStep {\n id: string\n $state: {\n completed: boolean\n started: boolean\n visible: boolean\n blocked: boolean\n lastActionAt?: Date\n }\n // allow any other properties\n [key: string]: any\n}\n\nexport interface StatefulFlow {\n flowSlug: string\n flowName: string\n flowType: FlowType\n data: {\n steps: StatefulStep[]\n // allow any other properties\n [key: string]: any\n }\n $state: {\n currentStepId: string\n currentStepIndex: number\n completed: boolean\n started: boolean\n skipped: boolean\n visible: boolean\n lastActionAt?: Date\n }\n}\n\nexport interface RulesGraphData {\n ruleOrder: string[]\n graph: Record<string, RulesGraphNode>\n}\n\nexport interface FlowStates {\n eligibleFlows: StatefulFlow[]\n ineligibleFlows: string[]\n ruleGraph?: RulesGraphData\n}\n\nexport enum FlowType {\n ANNOUNCEMENT = 'ANNOUNCEMENT',\n CHECKLIST = 'CHECKLIST',\n FORM = 'FORM',\n TOUR = 'TOUR',\n SUPPORT = 'SUPPORT',\n CUSTOM = 'CUSTOM',\n BANNER = 'BANNER',\n EMBEDDED_TIP = 'EMBEDDED_TIP',\n NPS_SURVEY = 'NPS_SURVEY',\n SURVEY = 'SURVEY',\n CARD = 'CARD',\n}\n\nexport type FlowActionType =\n | 'STARTED_STEP'\n | 'COMPLETED_STEP'\n | 'NOT_STARTED_STEP'\n | 'STARTED_FLOW'\n | 'COMPLETED_FLOW'\n | 'SKIPPED_FLOW'\n | 'NOT_STARTED_FLOW'\n\nexport class FlowStateDTO {\n userId: string\n groupId?: string\n flowSlug: string\n stepId?: string\n data?: any\n actionType: FlowActionType\n createdAt?: Date\n}\n"],"mappings":";48BAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,UAAAE,EAAA,eAAAC,EAAA,aAAAC,EAAA,YAAAC,EAAA,gBAAAC,IAAA,eAAAC,GAAAP,ICAO,IAAMQ,EAAiB,SCC9B,IAAAC,EAA6B,gBCqBtB,IAAIC,EAAyD,CAAC,EAE9D,SAASC,EAAkBC,EAAuC,CAxBzE,IAAAC,EAAAC,EAyBE,MAAO,GAAGF,EAAe,gBAAgBA,EAAe,WAAUC,EAAAD,EAAe,SAAf,KAAAC,EAAyB,OACzFC,EAAAF,EAAe,UAAf,KAAAE,EAA0B,IAE9B,CCxBO,IAAMC,EAAN,KAAgB,CAQrB,YAAYC,EAAuB,CAPnC,KAAO,OAAwB,CAC7B,OAAQ,GACR,OAAQ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CACH,CAKc,qBAAqBc,EAAwBD,EAA8B,QAAAzB,EAAA,sBACnF0B,GACF,KAAK,MAAM,QAASb,GAAS,CACvBA,EAAK,IAAMY,EAAc,UAC3B,KAAK,eAAe,EAAE,0BAA0B,QAAST,GAAY,CACnE,IAAMC,EAAW,KAAK,eAAe,EAAE,cAAc,IAAIJ,EAAK,EAAE,EAChEA,EAAK,YAAYa,CAAQ,EACzBV,EAAQH,EAAMI,CAAQ,EACtB,KAAK,eAAe,EAAE,cAAc,IAAIJ,EAAK,GAAIK,EAAUL,CAAI,CAAC,CAClE,CAAC,CAEL,CAAC,CAEL,GACF,ECxYO,IAAKuB,OACVA,EAAA,OAAS,SACTA,EAAA,UAAY,YAFFA,OAAA,IAKAC,OACVA,EAAA,MAAQ,QACRA,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IA6SAC,OACVA,EAAA,aAAe,eACfA,EAAA,UAAY,YACZA,EAAA,KAAO,OACPA,EAAA,KAAO,OACPA,EAAA,QAAU,UACVA,EAAA,OAAS,SACTA,EAAA,OAAS,SACTA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,OAAS,SACTA,EAAA,KAAO,OAXGA,OAAA","names":["src_exports","__export","Flow","FlowStatus","FlowType","Frigade","TriggerType","__toCommonJS","VERSION_NUMBER","import_uuid","frigadeGlobalState","getGlobalStateKey","internalConfig","_a","_b","Fetchable","config","generateGuestId","filteredConfig","_","v","__spreadValues","path","options","__async","getEmptyResponse","gracefulFetch","getHeaders","pathPrefix","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","id","visible","overrideStatefulFlow","_a","_b","statefulFlow","step","index","stepObj","existingStep","__spreadProps","__spreadValues","newSteps","properties","__async","thisStep","copy","clone","STARTED_STEP","isLastStep","COMPLETED_STEP","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","STARTED_FLOW","COMPLETED_FLOW","SKIPPED_FLOW","nextStep","NOT_STARTED_FLOW","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","key","action","data","stepId","date","flowStatesRaw","callback","prevFlow","cloneFlow","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","LOCAL_STORAGE_PREFIX","cloneFlow","flow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","CallQueue","call","now","item","callQueue","gracefulFetch","url","options","__async","_a","getEmptyResponse","lastCallDataKey","response","error","body","e","generateGuestId","guestId","getLocalStorage","GUEST_KEY","GUEST_PREFIX","uuidv4","setLocalStorage","isWeb","RulesGraph","graphData","_a","_b","flowId","callback","currentNode","nodeId","originId","ruleId","node","result","edge","ancestor","Frigade","Fetchable","apiKey","config","__spreadValues","__async","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","__spreadProps","groupId","event","flowId","flow","resetAllLocalStorage","clearCache","handler","lastFlow","cloneFlow","h","that","validator","target","key","value","previousState","newState","RulesGraph","overrideFlowStateRaw","_b","flowStateRaw","statefulFlow","Flow","_c","parsed","step","TriggerType","FlowStatus","FlowType"]}
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  "use client";
2
- var q=Object.defineProperty,X=Object.defineProperties;var z=Object.getOwnPropertyDescriptors;var O=Object.getOwnPropertySymbols;var Y=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable;var v=(a,s,t)=>s in a?q(a,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):a[s]=t,h=(a,s)=>{for(var t in s||(s={}))Y.call(s,t)&&v(a,t,s[t]);if(O)for(var t of O(s))Q.call(s,t)&&v(a,t,s[t]);return a},m=(a,s)=>X(a,z(s));var c=(a,s,t)=>new Promise((e,i)=>{var r=o=>{try{l(t.next(o))}catch(g){i(g)}},n=o=>{try{l(t.throw(o))}catch(g){i(g)}},l=o=>o.done?e(o.value):Promise.resolve(o.value).then(r,n);l((t=t.apply(a,s)).next())});var A="0.2.20";import{v4 as Z}from"uuid";var f={};function C(a){var s,t;return`${a.__instanceId}-${a.apiKey}:${(s=a.userId)!=null?s:""}:${(t=a.groupId)!=null?t:""}`}var y=class{constructor(s){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:$(),__instanceId:Math.random().toString(12).substring(4)};let t=Object.fromEntries(Object.entries(s).filter(([e,i])=>i!=null));this.config=h(h({},this.config),t)}fetch(s,t){return c(this,null,function*(){return this.config.__readOnly?S():_(this.getAPIUrl(s),h(h({keepalive:!0},t!=null?t:{}),P(this.config.apiKey)))})}getAPIUrl(s){let t="/v1/public";return new URL(`${t}${s}`,this.config.apiUrl.replace(t,"")).toString()}getGlobalState(){let s=C(this.config);if(!f[s])throw new Error("Frigade has not yet been initialized");return f[s]}};var b=class extends y{constructor({config:t,id:e}){super(t);this._isVisible=!1;this.lastStepUpdate=new Map;this.id=e,this.init()}get isVisible(){return this._isVisible===!1?!1:this.getGlobalState().rulesGraph.isFlowVisible(this.id)}set isVisible(t){this._isVisible=t}reload(){this.init()}resyncState(t){var i,r;let e=t!=null?t:this.getStatefulFlow();this.rawData=e,this.title=(i=e==null?void 0:e.data)==null?void 0:i.title,this.subtitle=(r=e==null?void 0:e.data)==null?void 0:r.subtitle,this.isCompleted=e.$state.completed,this.isStarted=e.$state.started,this.isSkipped=e.$state.skipped,this._isVisible=e.$state.visible,e.data.steps.forEach((n,l)=>{var p;let o=this.initStepFromStatefulStep(n,l),g=(p=this.steps)==null?void 0:p.get(n.id);g&&(g.$state=o.$state)})}initStepFromStatefulStep(t,e){return m(h({},t),{flow:this,$state:m(h({},t.$state),{lastActionAt:t.$state.lastActionAt?new Date(t.$state.lastActionAt):void 0}),order:e})}init(){let t=this.getStatefulFlow();this.resyncState();let e=new Map;t.data.steps.forEach((i,r)=>{let n=this.initStepFromStatefulStep(i,r);n.start=l=>c(this,null,function*(){let o=this.steps.get(i.id);if(this.getCurrentStep().id===o.id&&o.$state.started)return;let g=u(this.getGlobalState().flowStates[this.id]);g.data.steps[o.order].$state.started=!0,g.data.steps[o.order].$state.lastActionAt=new Date,g.$state.lastActionAt=new Date,g.$state.currentStepId=o.id,g.$state.currentStepIndex=o.order,this.getGlobalState().flowStates[this.id]=g,this.resyncState(),o.$state.completed||(yield this.sendFlowStateToAPI(U,l,o.id))}),n.complete=l=>c(this,null,function*(){let o=this.steps.get(i.id);if(o.$state.completed)return;let p=this.getNumberOfCompletedSteps()+1==this.steps.size,d=u(this.getGlobalState().flowStates[this.id]);d.$state.started=!0,d.data.steps[o.order].$state.completed=!0,d.data.steps[o.order].$state.started=!0,d.data.steps[o.order].$state.lastActionAt=new Date,p||(d.$state.currentStepId=this.getStepByIndex(o.order+1).id,d.$state.currentStepIndex=o.order+1),p&&(d.$state.completed=!0),this.getGlobalState().flowStates[this.id]=d,this.resyncState(),p&&this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(H,l,o.id)}),n.reset=()=>c(this,null,function*(){let l=this.steps.get(i.id);if(!l.$state.completed)return;let o=u(this.getGlobalState().flowStates[this.id]);o.data.steps[l.order].$state.started=!1,o.data.steps[l.order].$state.completed=!1,o.data.steps[l.order].$state.lastActionAt=void 0,this.getGlobalState().flowStates[this.id]=o,this.resyncState(),yield this.sendFlowStateToAPI(x,void 0,l.id)}),n.onStateChange=l=>{let o=g=>{if(g.id!==this.id)return;let p=g.steps.get(i.id),d=this.lastStepUpdate.get(l);(p.$state.completed!==(d==null?void 0:d.$state.completed)||p.$state.started!==(d==null?void 0:d.$state.started)||p.$state.visible!==(d==null?void 0:d.$state.visible)||p.$state.blocked!==(d==null?void 0:d.$state.blocked))&&(l(p,d!=null?d:u(p)),this.lastStepUpdate.set(l,u(p)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(l,o),this.getGlobalState().onFlowStateChangeHandlers.push(o)},n.removeStateChangeHandler=l=>{let o=this.getGlobalState().onStepStateChangeHandlerWrappers.get(l);o&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(g=>g!==o))},e.set(i.id,n)}),this.steps=e,this.getGlobalState().variables&&this.getGlobalState().variables[this.id]&&Object.keys(this.getGlobalState().variables[this.id]).length>0&&this.applyVariables(this.getGlobalState().variables[this.id])}start(t){return c(this,null,function*(){if(this.isStarted||this.isCompleted)return;let e=u(this.getGlobalState().flowStates[this.id]);e.$state.started=!0,this.getGlobalState().flowStates[this.id]=e,this.resyncState(),yield this.sendFlowStateToAPI(M,t)})}complete(t){return c(this,null,function*(){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(N,t))})}skip(t){return c(this,null,function*(){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),yield this.sendFlowStateToAPI(D,t))})}forward(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e?yield e.start(t):yield this.complete(t)})}back(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&(yield e.start(t))})}restart(){return c(this,null,function*(){this.optimisticallyMarkFlowNotStarted(),yield this.sendFlowStateToAPI(k)})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){var t;return(t=this.steps.get(this.getStatefulFlow().$state.currentStepId))!=null?t:this.steps.get(Array.from(this.steps.keys())[0])}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.completed).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.visible).length}onStateChange(t){let e=(i,r)=>{i.id===this.id&&(i.isCompleted!==(r==null?void 0:r.isCompleted)||i.isStarted!==(r==null?void 0:r.isStarted)||i.isSkipped!==(r==null?void 0:r.isSkipped)||i.isVisible!==(r==null?void 0:r.isVisible)||JSON.stringify(i.steps)!==JSON.stringify(r==null?void 0:r.steps))&&t(i,r)};this.getGlobalState().onFlowStateChangeHandlerWrappers.set(t,e),this.getGlobalState().onFlowStateChangeHandlers.push(e)}removeStateChangeHandler(t){let e=this.getGlobalState().onFlowStateChangeHandlerWrappers.get(t);e&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(i=>i!==e))}applyVariables(t){var i,r;let e=n=>{let l=n.match(/\${(.*?)}/g);return l&&l.forEach(o=>{let g=o.replace("${","").replace("}","");t[g]&&(n=n.replace(o,t[g]))}),n};this.title=e((i=this.title)!=null?i:""),this.subtitle=e((r=this.subtitle)!=null?r:""),this.steps.forEach(n=>{Object.keys(n).forEach(l=>{typeof n[l]=="string"&&(n[l]=e(n[l]))})}),this.getGlobalState().variables={},this.getGlobalState().variables[this.id]=t}getStatefulFlow(){return this.getGlobalState().flowStates[this.id]}optimisticallyMarkFlowCompleted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!0,t.$state.started=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}optimisticallyMarkFlowNotStarted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!1,t.$state.started=!1,t.$state.visible=!0,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}sendFlowStateToAPI(t,e,i){return c(this,null,function*(){let r=new Date;this.getGlobalState().lastSyncDate=r;let n=yield this.fetch("/flowStates",{method:"POST",body:JSON.stringify({userId:this.config.userId,groupId:this.config.groupId,flowSlug:this.id,stepId:i!=null?i:this.getCurrentStep().id,data:e?JSON.stringify(e):{},actionType:t,createdAt:r})});r<this.getGlobalState().lastSyncDate||(yield this.getGlobalState().refreshStateFromAPI(n))})}optimisticallyMarkFlowSkipped(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.skipped=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}register(t){this.getGlobalState().rulesGraph.register(this.id,i=>{let r=this.getGlobalState().previousFlows.get(this.id);(r==null?void 0:r._isVisible)!==i&&this.getGlobalState().onFlowStateChangeHandlers.forEach(n=>{n(this,r),this.getGlobalState().previousFlows.set(this.id,F(this))}),t==null||t(i)})}unregister(){this.getGlobalState().rulesGraph.unregister(this.id)}};var x="NOT_STARTED_STEP",N="COMPLETED_FLOW",D="SKIPPED_FLOW",M="STARTED_FLOW",k="NOT_STARTED_FLOW",H="COMPLETED_STEP",U="STARTED_STEP",L="frigade-guest-key",G="guest_",tt="get-cache-",R="fr-js-";function F(a){return new b({config:a.config,id:a.id})}function u(a){return JSON.parse(JSON.stringify(a))}function P(a){return{headers:{Authorization:`Bearer ${a}`,"Content-Type":"application/json","X-Frigade-SDK-Version":A,"X-Frigade-SDK-Platform":"Javascript"}}}function et(a){return w()?window.localStorage.getItem(`${R}${a}`):null}function it(a,s){w()&&window.localStorage.setItem(`${R}${a}`,s)}function V(){Object.keys(f).forEach(a=>{a.startsWith(tt)&&delete f[a]})}function K(){w()&&Object.keys(window.localStorage).forEach(a=>{a.startsWith(R)&&window.localStorage.removeItem(a)})}var T=class{constructor(){this.queue=[];this.ttlInMS=500;this.cacheSize=5}push(s){let t=new Date;this.queue.length>=this.cacheSize&&this.queue.shift(),this.queue.push({call:s,time:t.getTime()})}hasIdenticalCall(s){let t=new Date;return this.queue=this.queue.filter(e=>t.getTime()-e.time<this.ttlInMS),this.queue.some(e=>e.call===s)}},W=new T;function _(a,s){return c(this,null,function*(){var e;if(typeof globalThis.fetch!="function")return S("- Attempted to call fetch() in an environment that doesn't support it.");if(w()&&s&&s.body&&s.method==="POST"){let i=`${a}${JSON.stringify((e=s.body)!=null?e:{})}`;if(W.hasIdenticalCall(i))return S();W.push(i)}let t;try{t=fetch(a,s),t=yield t}catch(i){return S(i)}if(!t)return S("Received an empty response");if(t.status>=400)return S(t.statusText);try{if(t.status===204)return S();let i;try{i=yield t.json()}catch(r){return S()}return i.error?S(i.error):i}catch(i){return S(i)}})}function S(a){return a&&console.log("Call to Frigade failed",a),{json:()=>({})}}function $(){if(w()){let a=et(L);return a||(a=`${G}${Z()}`,it(L,a)),a}}function w(){return typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.localStorage!="undefined"}var E=class{constructor(s){this.graph=new Map;this.registry=new Map;this.ruleOrder=[];var t,e;this.ruleOrder=(t=s==null?void 0:s.ruleOrder)!=null?t:[],this.ingestGraphData((e=s==null?void 0:s.graph)!=null?e:{})}ingestGraphData(s){this.graph=new Map(Object.entries(s)),this.fireCallbacks()}fireCallbacks(){for(let[s,t]of this.registry)typeof t=="function"&&t(this.isFlowVisible(s))}isFlowVisible(s){let t=this.graph.get(s);return t?t.visible===!1?!1:t.edges.length===0?!0:this.findRegisteredDescendant(s)==null:!0}register(s,t){this.registry.set(s,t!=null?t:()=>{}),this.fireCallbacks()}unregister(s){this.registry.delete(s),this.fireCallbacks()}getRegistry(){return this.registry}findRegisteredDescendant(s,t=s,e){let i=this.graph.get(s);if(i==null)return;if(this.registry.has(s)&&t!==s)return i;let r;if(i.edges.length>0)for(let n of i.edges){if(e!=null&&n.ruleId!==e)continue;let l=this.findRegisteredDescendant(n.head,t,e!=null?e:n.ruleId);if(l!=null&&l.visible){r=l;break}}return r}};var I=class extends y{constructor(t,e){super(h({apiKey:t},e));this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=()=>c(this,null,function*(){document.visibilityState==="visible"&&(yield this.refreshStateFromAPI())});this.init(this.config),w()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(w()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=C(this.config);f[t]&&(f[t].onFlowStateChangeHandlers=[])}}init(t){return c(this,null,function*(){return this.config=h(h({},this.config),t),this.initPromise=(()=>c(this,null,function*(){var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(G))&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})})),yield this.refreshStateFromAPI()}))(),this.initPromise})}identify(t,e){return c(this,null,function*(){this.config=m(h({},this.config),{userId:t}),yield this.initIfNeeded(),yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),yield this.resync()})}group(t,e){return c(this,null,function*(){yield this.initIfNeeded(),this.config.groupId=t,yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,properties:e})}),yield this.resync()})}track(t,e){return c(this,null,function*(){if(yield this.initIfNeeded(),!t){console.error("Event name is required to track an event");return}this.config.userId&&this.config.groupId?yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,events:[{event:t,properties:e}]})}):this.config.userId&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,events:[{event:t,properties:e}]})})),yield this.resync()})}isReady(){return!!(this.config.__instanceId&&this.config.apiKey&&this.initPromise)}getFlow(t){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows.find(e=>e.id==t)})}getFlows(){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows})}reload(){return c(this,null,function*(){K(),V(),yield this.refreshStateFromAPI(),this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}resync(){return c(this,null,function*(){this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}onStateChange(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)})}hasFailedToLoad(){return this.hasFailed}removeStateChangeHandler(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)})}initIfNeeded(){return c(this,null,function*(){return this.initPromise!==null?this.initPromise:this.init(this.config)})}refreshStateFromAPI(){return c(this,null,function*(){let t=C(this.config);if(!f[t]){let e=this,i={set:function(r,n,l){if(r[n]){let o=r[n],g=l;JSON.stringify(o)!==JSON.stringify(g)&&e.triggerEventHandlers(g,o)}return r[n]=l,!0}};if(f[t]={refreshStateFromAPI:()=>c(this,null,function*(){}),rulesGraph:new E({graph:{},ruleOrder:[]}),flowStates:new Proxy({},i),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map,variables:{}},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockFlowStates(t);return}f[t].refreshStateFromAPI=r=>c(this,null,function*(){var l,o;if(this.config.__readOnly)return;let n=r||(yield this.fetch(`/flowStates?userId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&groupId=${encodeURIComponent(this.config.groupId)}`:""}`));f[t].rulesGraph.ingestGraphData((o=(l=n.ruleGraph)==null?void 0:l.graph)!=null?o:{}),n&&n.eligibleFlows?(n.eligibleFlows.forEach(g=>{f[t].flowStates[g.flowSlug]=g,this.flows.find(p=>p.id==g.flowSlug)||this.flows.push(new b({config:this.config,id:g.flowSlug}))}),this.hasFailed=!1):this.hasFailed=!0})}yield f[t].refreshStateFromAPI()})}mockFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var r,n,l;let i=JSON.parse(this.config.__flowConfigOverrides[e]);f[t].flowStates[e]={flowSlug:e,flowName:(r=i==null?void 0:i.name)!=null?r:e,flowType:(n=i==null?void 0:i.type)!=null?n:"CHECKLIST",data:m(h({},i),{steps:((l=i==null?void 0:i.steps)!=null?l:[]).map(o=>h({id:o.id,$state:{completed:!1,started:!1,visible:!0,blocked:!1}},o))}),$state:{currentStepId:null,currentStepIndex:-1,completed:!1,started:!1,skipped:!1,visible:!0}},this.flows.push(new b({config:this.config,id:e}))})}triggerEventHandlers(t,e){return c(this,null,function*(){t&&this.flows.forEach(i=>{i.id==e.flowSlug&&this.getGlobalState().onFlowStateChangeHandlers.forEach(r=>{let n=this.getGlobalState().previousFlows.get(i.id);i.resyncState(t),r(i,n),this.getGlobalState().previousFlows.set(i.id,F(i))})})})}};var B=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(B||{}),J=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(J||{}),j=(d=>(d.ANNOUNCEMENT="ANNOUNCEMENT",d.CHECKLIST="CHECKLIST",d.FORM="FORM",d.TOUR="TOUR",d.SUPPORT="SUPPORT",d.CUSTOM="CUSTOM",d.BANNER="BANNER",d.EMBEDDED_TIP="EMBEDDED_TIP",d.NPS_SURVEY="NPS_SURVEY",d.SURVEY="SURVEY",d.CARD="CARD",d))(j||{});export{b as Flow,J as FlowStatus,j as FlowType,I as Frigade,B as TriggerType};
2
+ var q=Object.defineProperty,X=Object.defineProperties;var Y=Object.getOwnPropertyDescriptors;var v=Object.getOwnPropertySymbols;var z=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable;var O=(o,s,t)=>s in o?q(o,s,{enumerable:!0,configurable:!0,writable:!0,value:t}):o[s]=t,h=(o,s)=>{for(var t in s||(s={}))z.call(s,t)&&O(o,t,s[t]);if(v)for(var t of v(s))Q.call(s,t)&&O(o,t,s[t]);return o},m=(o,s)=>X(o,Y(s));var c=(o,s,t)=>new Promise((e,i)=>{var a=r=>{try{l(t.next(r))}catch(d){i(d)}},n=r=>{try{l(t.throw(r))}catch(d){i(d)}},l=r=>r.done?e(r.value):Promise.resolve(r.value).then(a,n);l((t=t.apply(o,s)).next())});var A="0.2.20";import{v4 as Z}from"uuid";var f={};function C(o){var s,t;return`${o.__instanceId}-${o.apiKey}:${(s=o.userId)!=null?s:""}:${(t=o.groupId)!=null?t:""}`}var y=class{constructor(s){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:$(),__instanceId:Math.random().toString(12).substring(4)};let t=Object.fromEntries(Object.entries(s).filter(([e,i])=>i!=null));this.config=h(h({},this.config),t)}fetch(s,t){return c(this,null,function*(){return this.config.__readOnly?S():_(this.getAPIUrl(s),h(h({keepalive:!0},t!=null?t:{}),P(this.config.apiKey)))})}getAPIUrl(s){let t="/v1/public";return new URL(`${t}${s}`,this.config.apiUrl.replace(t,"")).toString()}getGlobalState(){let s=C(this.config);if(!f[s])throw new Error("Frigade has not yet been initialized");return f[s]}};var b=class extends y{constructor({config:t,id:e}){super(t);this._isVisible=!1;this.lastStepUpdate=new Map;this.id=e,this.init()}get isVisible(){return this._isVisible===!1?!1:this.getGlobalState().rulesGraph.isFlowVisible(this.id)}set isVisible(t){this._isVisible=t}reload(){this.init()}resyncState(t){var i,a;let e=t!=null?t:this.getStatefulFlow();this.rawData=e,this.title=(i=e==null?void 0:e.data)==null?void 0:i.title,this.subtitle=(a=e==null?void 0:e.data)==null?void 0:a.subtitle,this.isCompleted=e.$state.completed,this.isStarted=e.$state.started,this.isSkipped=e.$state.skipped,this._isVisible=e.$state.visible,e.data.steps.forEach((n,l)=>{var p;let r=this.initStepFromStatefulStep(n,l),d=(p=this.steps)==null?void 0:p.get(n.id);d&&(d.$state=r.$state)})}initStepFromStatefulStep(t,e){return m(h({},t),{flow:this,$state:m(h({},t.$state),{lastActionAt:t.$state.lastActionAt?new Date(t.$state.lastActionAt):void 0}),order:e})}init(){let t=this.getStatefulFlow();this.resyncState();let e=new Map;t.data.steps.forEach((i,a)=>{let n=this.initStepFromStatefulStep(i,a);n.start=l=>c(this,null,function*(){let r=this.steps.get(i.id);if(this.getCurrentStep().id===r.id&&r.$state.started)return;let d=u(this.getGlobalState().flowStates[this.id]);d.data.steps[r.order].$state.started=!0,d.data.steps[r.order].$state.lastActionAt=new Date,d.$state.lastActionAt=new Date,d.$state.currentStepId=r.id,d.$state.currentStepIndex=r.order,this.getGlobalState().flowStates[this.id]=d,this.resyncState(),r.$state.completed||(yield this.sendFlowStateToAPI(U,l,r.id))}),n.complete=l=>c(this,null,function*(){let r=this.steps.get(i.id);if(r.$state.completed)return;let d=r.order+1===this.getNumberOfAvailableSteps(),p=u(this.getGlobalState().flowStates[this.id]);p.$state.started=!0,p.data.steps[r.order].$state.completed=!0,p.data.steps[r.order].$state.started=!0,p.data.steps[r.order].$state.lastActionAt=new Date,d||(p.$state.currentStepId=this.getStepByIndex(r.order+1).id,p.$state.currentStepIndex=r.order+1),d&&(p.$state.completed=!0),this.getGlobalState().flowStates[this.id]=p,this.resyncState(),d&&this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(H,l,r.id)}),n.reset=()=>c(this,null,function*(){let l=this.steps.get(i.id);if(!l.$state.completed)return;let r=u(this.getGlobalState().flowStates[this.id]);r.data.steps[l.order].$state.started=!1,r.data.steps[l.order].$state.completed=!1,r.data.steps[l.order].$state.lastActionAt=void 0,this.getGlobalState().flowStates[this.id]=r,this.resyncState(),yield this.sendFlowStateToAPI(x,void 0,l.id)}),n.onStateChange=l=>{let r=d=>{if(d.id!==this.id)return;let p=d.steps.get(i.id),g=this.lastStepUpdate.get(l);(p.$state.completed!==(g==null?void 0:g.$state.completed)||p.$state.started!==(g==null?void 0:g.$state.started)||p.$state.visible!==(g==null?void 0:g.$state.visible)||p.$state.blocked!==(g==null?void 0:g.$state.blocked))&&(l(p,g!=null?g:u(p)),this.lastStepUpdate.set(l,u(p)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(l,r),this.getGlobalState().onFlowStateChangeHandlers.push(r)},n.removeStateChangeHandler=l=>{let r=this.getGlobalState().onStepStateChangeHandlerWrappers.get(l);r&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(d=>d!==r))},e.set(i.id,n)}),this.steps=e,this.getGlobalState().variables&&this.getGlobalState().variables[this.id]&&Object.keys(this.getGlobalState().variables[this.id]).length>0&&this.applyVariables(this.getGlobalState().variables[this.id])}start(t){return c(this,null,function*(){if(this.isStarted||this.isCompleted)return;let e=u(this.getGlobalState().flowStates[this.id]);e.$state.started=!0,this.getGlobalState().flowStates[this.id]=e,this.resyncState(),yield this.sendFlowStateToAPI(M,t)})}complete(t){return c(this,null,function*(){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),yield this.sendFlowStateToAPI(N,t))})}skip(t){return c(this,null,function*(){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),yield this.sendFlowStateToAPI(D,t))})}forward(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e?yield e.start(t):yield this.complete(t)})}back(t){return c(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&(yield e.start(t))})}restart(){return c(this,null,function*(){this.optimisticallyMarkFlowNotStarted(),yield this.sendFlowStateToAPI(k)})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){var t;return(t=this.steps.get(this.getStatefulFlow().$state.currentStepId))!=null?t:this.steps.get(Array.from(this.steps.keys())[0])}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.completed).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>t.$state.visible).length}onStateChange(t){let e=(i,a)=>{i.id===this.id&&(i.isCompleted!==(a==null?void 0:a.isCompleted)||i.isStarted!==(a==null?void 0:a.isStarted)||i.isSkipped!==(a==null?void 0:a.isSkipped)||i.isVisible!==(a==null?void 0:a.isVisible)||JSON.stringify(i.steps)!==JSON.stringify(a==null?void 0:a.steps))&&t(i,a)};this.getGlobalState().onFlowStateChangeHandlerWrappers.set(t,e),this.getGlobalState().onFlowStateChangeHandlers.push(e)}removeStateChangeHandler(t){let e=this.getGlobalState().onFlowStateChangeHandlerWrappers.get(t);e&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(i=>i!==e))}applyVariables(t){var i,a;let e=n=>{let l=n.match(/\${(.*?)}/g);return l&&l.forEach(r=>{let d=r.replace("${","").replace("}","");t[d]&&(n=n.replace(r,t[d]))}),n};this.title=e((i=this.title)!=null?i:""),this.subtitle=e((a=this.subtitle)!=null?a:""),this.steps.forEach(n=>{Object.keys(n).forEach(l=>{typeof n[l]=="string"&&(n[l]=e(n[l]))})}),this.getGlobalState().variables={},this.getGlobalState().variables[this.id]=t}getStatefulFlow(){return this.getGlobalState().flowStates[this.id]}optimisticallyMarkFlowCompleted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!0,t.$state.started=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}optimisticallyMarkFlowNotStarted(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.completed=!1,t.$state.started=!1,t.$state.visible=!0,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}sendFlowStateToAPI(t,e,i){return c(this,null,function*(){let a=new Date;this.getGlobalState().lastSyncDate=a;let n=yield this.fetch("/flowStates",{method:"POST",body:JSON.stringify({userId:this.config.userId,groupId:this.config.groupId,flowSlug:this.id,stepId:i!=null?i:this.getCurrentStep().id,data:e||{},actionType:t,createdAt:a})});a<this.getGlobalState().lastSyncDate||(yield this.getGlobalState().refreshStateFromAPI(n))})}optimisticallyMarkFlowSkipped(){let t=u(this.getGlobalState().flowStates[this.id]);t.$state.skipped=!0,t.$state.visible=!1,this.getGlobalState().flowStates[this.id]=t,this.resyncState()}register(t){this.getGlobalState().rulesGraph.register(this.id,i=>{let a=this.getGlobalState().previousFlows.get(this.id);(a==null?void 0:a._isVisible)!==i&&this.getGlobalState().onFlowStateChangeHandlers.forEach(n=>{n(this,a),this.getGlobalState().previousFlows.set(this.id,F(this))}),t==null||t(i)})}unregister(){this.getGlobalState().rulesGraph.unregister(this.id)}};var x="NOT_STARTED_STEP",N="COMPLETED_FLOW",D="SKIPPED_FLOW",M="STARTED_FLOW",k="NOT_STARTED_FLOW",H="COMPLETED_STEP",U="STARTED_STEP",L="frigade-guest-key",G="guest_",tt="get-cache-",R="fr-js-";function F(o){return new b({config:o.config,id:o.id})}function u(o){return JSON.parse(JSON.stringify(o))}function P(o){return{headers:{Authorization:`Bearer ${o}`,"Content-Type":"application/json","X-Frigade-SDK-Version":A,"X-Frigade-SDK-Platform":"Javascript"}}}function et(o){return w()?window.localStorage.getItem(`${R}${o}`):null}function it(o,s){w()&&window.localStorage.setItem(`${R}${o}`,s)}function V(){Object.keys(f).forEach(o=>{o.startsWith(tt)&&delete f[o]})}function K(){w()&&Object.keys(window.localStorage).forEach(o=>{o.startsWith(R)&&window.localStorage.removeItem(o)})}var T=class{constructor(){this.queue=[];this.ttlInMS=500;this.cacheSize=5}push(s){let t=new Date;this.queue.length>=this.cacheSize&&this.queue.shift(),this.queue.push({call:s,time:t.getTime()})}hasIdenticalCall(s){let t=new Date;return this.queue=this.queue.filter(e=>t.getTime()-e.time<this.ttlInMS),this.queue.some(e=>e.call===s)}},W=new T;function _(o,s){return c(this,null,function*(){var e;if(typeof globalThis.fetch!="function")return S("- Attempted to call fetch() in an environment that doesn't support it.");if(w()&&s&&s.body&&s.method==="POST"){let i=`${o}${JSON.stringify((e=s.body)!=null?e:{})}`;if(W.hasIdenticalCall(i))return S();W.push(i)}let t;try{t=fetch(o,s),t=yield t}catch(i){return S(i)}if(!t)return S("Received an empty response");if(t.status>=400)return S(t.statusText);try{if(t.status===204)return S();let i;try{i=yield t.json()}catch(a){return S()}return i.error?S(i.error):i}catch(i){return S(i)}})}function S(o){return o&&console.log("Call to Frigade failed",o),{json:()=>({})}}function $(){if(w()){let o=et(L);return o||(o=`${G}${Z()}`,it(L,o)),o}}function w(){return typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.localStorage!="undefined"}var E=class{constructor(s){this.graph=new Map;this.registry=new Map;this.ruleOrder=[];var t,e;this.ruleOrder=(t=s==null?void 0:s.ruleOrder)!=null?t:[],this.ingestGraphData((e=s==null?void 0:s.graph)!=null?e:{})}ingestGraphData(s){this.graph=new Map(Object.entries(s)),this.fireCallbacks()}fireCallbacks(){for(let[s,t]of this.registry)typeof t=="function"&&t(this.isFlowVisible(s))}isFlowVisible(s){let t=this.graph.get(s);return t?t.visible===!1?!1:t.edges.length===0?!0:this.findRegisteredDescendant(s)==null:!0}register(s,t){this.registry.set(s,t!=null?t:()=>{}),this.fireCallbacks()}unregister(s){this.registry.delete(s),this.fireCallbacks()}getRegistry(){return this.registry}findRegisteredDescendant(s,t=s,e){let i=this.graph.get(s);if(i==null)return;if(this.registry.has(s)&&t!==s)return i;let a;if(i.edges.length>0)for(let n of i.edges){if(e!=null&&n.ruleId!==e)continue;let l=this.findRegisteredDescendant(n.head,t,e!=null?e:n.ruleId);if(l!=null&&l.visible){a=l;break}}return a}};var I=class extends y{constructor(t,e){super(h({apiKey:t},e));this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=()=>c(this,null,function*(){document.visibilityState==="visible"&&(yield this.refreshStateFromAPI())});this.init(this.config),w()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(w()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=C(this.config);f[t]&&(f[t].onFlowStateChangeHandlers=[])}}init(t){return c(this,null,function*(){return this.config=h(h({},this.config),t),this.initPromise=(()=>c(this,null,function*(){var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(G))&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})})),yield this.refreshStateFromAPI()}))(),this.initPromise})}identify(t,e){return c(this,null,function*(){this.config=m(h({},this.config),{userId:t}),yield this.initIfNeeded(),yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),yield this.resync()})}group(t,e){return c(this,null,function*(){yield this.initIfNeeded(),this.config.groupId=t,yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,properties:e})}),yield this.resync()})}track(t,e){return c(this,null,function*(){if(yield this.initIfNeeded(),!t){console.error("Event name is required to track an event");return}this.config.userId&&this.config.groupId?yield this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,events:[{event:t,properties:e}]})}):this.config.userId&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,events:[{event:t,properties:e}]})})),yield this.resync()})}isReady(){return!!(this.config.__instanceId&&this.config.apiKey&&this.initPromise)}getFlow(t){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows.find(e=>e.id==t)})}getFlows(){return c(this,null,function*(){return yield this.initIfNeeded(),this.flows})}reload(){return c(this,null,function*(){K(),V(),yield this.refreshStateFromAPI(),this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}resync(){return c(this,null,function*(){this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let i=this.getGlobalState().previousFlows.get(t.id);e(t,i),this.getGlobalState().previousFlows.set(t.id,F(t))})})})}onStateChange(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)})}hasFailedToLoad(){return this.hasFailed}removeStateChangeHandler(t){return c(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)})}initIfNeeded(){return c(this,null,function*(){return this.initPromise!==null?this.initPromise:this.init(this.config)})}refreshStateFromAPI(){return c(this,null,function*(){let t=C(this.config);if(!f[t]){let e=this,i={set:function(a,n,l){if(a[n]){let r=a[n],d=l;JSON.stringify(r)!==JSON.stringify(d)&&e.triggerEventHandlers(d,r)}return a[n]=l,!0}};if(f[t]={refreshStateFromAPI:()=>c(this,null,function*(){}),rulesGraph:new E({graph:{},ruleOrder:[]}),flowStates:new Proxy({},i),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map,variables:{}},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockFlowStates(t);return}f[t].refreshStateFromAPI=a=>c(this,null,function*(){var l,r;if(this.config.__readOnly)return;let n=a||(yield this.fetch(`/flowStates?userId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&groupId=${encodeURIComponent(this.config.groupId)}`:""}`));f[t].rulesGraph.ingestGraphData((r=(l=n.ruleGraph)==null?void 0:l.graph)!=null?r:{}),n&&n.eligibleFlows?(n.eligibleFlows.forEach(d=>{f[t].flowStates[d.flowSlug]=d,this.flows.find(p=>p.id==d.flowSlug)||this.flows.push(new b({config:this.config,id:d.flowSlug}))}),this.hasFailed=!1):this.hasFailed=!0})}yield f[t].refreshStateFromAPI()})}mockFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var a,n,l;let i=JSON.parse(this.config.__flowConfigOverrides[e]);f[t].flowStates[e]={flowSlug:e,flowName:(a=i==null?void 0:i.name)!=null?a:e,flowType:(n=i==null?void 0:i.type)!=null?n:"CHECKLIST",data:m(h({},i),{steps:((l=i==null?void 0:i.steps)!=null?l:[]).map(r=>h({id:r.id,$state:{completed:!1,started:!1,visible:!0,blocked:!1}},r))}),$state:{currentStepId:null,currentStepIndex:-1,completed:!1,started:!1,skipped:!1,visible:!0}},this.flows.push(new b({config:this.config,id:e}))})}triggerEventHandlers(t,e){return c(this,null,function*(){t&&this.flows.forEach(i=>{i.id==e.flowSlug&&this.getGlobalState().onFlowStateChangeHandlers.forEach(a=>{let n=this.getGlobalState().previousFlows.get(i.id);i.resyncState(t),a(i,n),this.getGlobalState().previousFlows.set(i.id,F(i))})})})}};var B=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(B||{}),J=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(J||{}),j=(g=>(g.ANNOUNCEMENT="ANNOUNCEMENT",g.CHECKLIST="CHECKLIST",g.FORM="FORM",g.TOUR="TOUR",g.SUPPORT="SUPPORT",g.CUSTOM="CUSTOM",g.BANNER="BANNER",g.EMBEDDED_TIP="EMBEDDED_TIP",g.NPS_SURVEY="NPS_SURVEY",g.SURVEY="SURVEY",g.CARD="CARD",g))(j||{});export{b as Flow,J as FlowStatus,j as FlowType,I as Frigade,B as TriggerType};
3
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/rules-graph.ts","../src/core/frigade.ts","../src/core/types.ts"],"sourcesContent":["export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport { v4 as uuidv4 } from 'uuid'\nimport { Flow } from '../core/flow'\nimport { frigadeGlobalState } from './state'\n\nexport const NOT_STARTED_STEP = 'NOT_STARTED_STEP'\nexport const COMPLETED_FLOW = 'COMPLETED_FLOW'\nexport const SKIPPED_FLOW = 'SKIPPED_FLOW'\nexport const STARTED_FLOW = 'STARTED_FLOW'\nexport const NOT_STARTED_FLOW = 'NOT_STARTED_FLOW'\nexport const COMPLETED_STEP = 'COMPLETED_STEP'\nexport const STARTED_STEP = 'STARTED_STEP'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow({\n config: flow.config,\n id: flow.id,\n })\n return newFlow\n}\n\nexport function clone<T>(obj: T): T {\n return JSON.parse(JSON.stringify(obj))\n}\n\nexport function getHeaders(apiKey: string) {\n return {\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n 'X-Frigade-SDK-Version': VERSION_NUMBER,\n 'X-Frigade-SDK-Platform': 'Javascript',\n },\n }\n}\n\nfunction getLocalStorage(key: string) {\n if (isWeb()) {\n return window.localStorage.getItem(`${LOCAL_STORAGE_PREFIX}${key}`)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (isWeb()) {\n window.localStorage.setItem(`${LOCAL_STORAGE_PREFIX}${key}`, value)\n }\n}\n\nfunction setGlobalState(key: string, value: any) {\n frigadeGlobalState[key] = value\n}\n\nfunction getGlobalState(key: string): any {\n return frigadeGlobalState[key]\n}\n\nexport function clearCache() {\n Object.keys(frigadeGlobalState).forEach((key) => {\n if (key.startsWith(GET_CACHE_PREFIX)) {\n delete frigadeGlobalState[key]\n }\n })\n}\n\nexport function resetAllLocalStorage() {\n if (isWeb()) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith(LOCAL_STORAGE_PREFIX)) {\n window.localStorage.removeItem(key)\n }\n })\n }\n}\n\nclass CallQueue {\n private queue: {\n call: string\n time: number\n }[] = []\n private readonly ttlInMS = 500\n private readonly cacheSize = 5\n\n public push(call: string) {\n const now = new Date()\n if (this.queue.length >= this.cacheSize) {\n this.queue.shift()\n }\n this.queue.push({\n call: call,\n time: now.getTime(),\n })\n }\n\n public hasIdenticalCall(call: string) {\n const now = new Date()\n this.queue = this.queue.filter((item) => now.getTime() - item.time < this.ttlInMS)\n return this.queue.some((item) => item.call === call)\n }\n}\n\nconst callQueue = new CallQueue()\n\nexport async function gracefulFetch(url: string, options: any) {\n if (typeof globalThis.fetch !== 'function') {\n return getEmptyResponse(\n \"- Attempted to call fetch() in an environment that doesn't support it.\"\n )\n }\n\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCallDataKey = `${url}${JSON.stringify(options.body ?? {})}`\n if (callQueue.hasIdenticalCall(lastCallDataKey)) {\n return getEmptyResponse()\n }\n callQueue.push(lastCallDataKey)\n }\n\n let response\n try {\n response = fetch(url, options)\n response = await response\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse('Received an empty response')\n }\n\n if (response.status >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n try {\n if (response.status === 204) {\n return getEmptyResponse()\n }\n\n let body\n try {\n body = await response.json()\n } catch (e) {\n return getEmptyResponse()\n }\n\n if (body.error) {\n return getEmptyResponse(body.error)\n }\n return body\n } catch (e) {\n return getEmptyResponse(e)\n }\n}\n\nexport function getEmptyResponse(error?: any) {\n if (error) {\n console.log('Call to Frigade failed', error)\n }\n\n // Create empty response that contains the .json method and returns an empty object\n return {\n json: () => ({}),\n }\n}\n\nexport function generateGuestId() {\n if (isWeb()) {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n setLocalStorage(GUEST_KEY, guestId)\n }\n return guestId\n }\n}\n\nexport function fetcher(apiKey: string, path: string, options?: Record<any, any>) {\n return gracefulFetch(`//api.frigade.com/v1/public${path}`, {\n ...(options ?? {}),\n ...getHeaders(apiKey),\n })\n}\n\nexport function isWeb() {\n return (\n typeof window !== 'undefined' &&\n typeof window.document !== 'undefined' &&\n typeof window.localStorage !== 'undefined'\n )\n}\n","import { FlowStates, FlowStep, FrigadeConfig, StatefulFlow } from '../core/types'\nimport { Flow } from '../core/flow'\nimport { RulesGraph } from '../core/rules-graph'\n\nexport interface FrigadeGlobalState {\n refreshStateFromAPI: (overrideFlowStatesRaw?: FlowStates) => Promise<void>\n flowStates: Record<string, StatefulFlow>\n rulesGraph: RulesGraph\n onFlowStateChangeHandlerWrappers: Map<\n (flow: Flow, previousFlow: Flow) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onStepStateChangeHandlerWrappers: Map<\n (step: FlowStep, previousStep: FlowStep) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onFlowStateChangeHandlers: ((flow: Flow, previousFlow: Flow) => void)[]\n previousFlows: Map<string, Flow>\n variables: Record<string, Record<string, any>>\n lastSyncDate?: Date\n}\n\nexport let frigadeGlobalState: Record<string, FrigadeGlobalState> = {}\n\nexport function getGlobalStateKey(internalConfig: FrigadeConfig): string {\n return `${internalConfig.__instanceId}-${internalConfig.apiKey}:${internalConfig.userId ?? ''}:${\n internalConfig.groupId ?? ''\n }`\n}\n","import { generateGuestId, getEmptyResponse, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../core/types'\nimport { frigadeGlobalState, FrigadeGlobalState, getGlobalStateKey } from './state'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: 'https://api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(12).substring(4),\n }\n\n constructor(config: FrigadeConfig) {\n const filteredConfig = Object.fromEntries(Object.entries(config).filter(([_, v]) => v != null))\n\n this.config = {\n ...this.config,\n ...filteredConfig,\n }\n }\n\n /**\n * @ignore\n */\n public async fetch(path: string, options?: Record<any, any>) {\n if (this.config.__readOnly) {\n return getEmptyResponse()\n }\n\n return gracefulFetch(this.getAPIUrl(path), {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n\n private getAPIUrl(path: string) {\n const pathPrefix = '/v1/public'\n const apiUrl = new URL(\n `${pathPrefix}${path}`,\n this.config.apiUrl.replace(pathPrefix, '')\n ).toString()\n\n return apiUrl\n }\n\n /**\n * @ignore\n */\n protected getGlobalState(): FrigadeGlobalState {\n const globalStateKey = getGlobalStateKey(this.config)\n if (!frigadeGlobalState[globalStateKey]) {\n throw new Error('Frigade has not yet been initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import {\n FlowActionType,\n FlowStateDTO,\n FlowStates,\n FlowStep,\n FrigadeConfig,\n StatefulFlow,\n StatefulStep,\n} from './types'\nimport {\n clone,\n cloneFlow,\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n NOT_STARTED_STEP,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraphRegistryCallback } from './rules-graph'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * Ordered map of the Steps in the Flow.\n * See [Flow Step Definition](https://docs.frigade.com/v2/sdk/js/step) for more information.\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the Flow, if defined at the top level of the YAML config.\n */\n public title?: string\n /**\n * The user-facing description of the Flow, if defined at the top level of the YAML config.\n */\n public subtitle?: string\n /**\n * The metadata of the Flow.\n * @ignore\n */\n public rawData: StatefulFlow\n /**\n * Whether the Flow is completed or not.\n */\n public isCompleted: boolean\n /**\n * Whether the Flow is started or not.\n */\n public isStarted: boolean\n /**\n * Whether the Flow has been skipped or not.\n */\n public isSkipped: boolean\n /**\n * @ignore\n */\n private _isVisible: boolean = false\n /**\n * Whether the Flow is visible to the user based on the current user/group's state.\n */\n get isVisible() {\n if (this._isVisible === false) {\n return false\n }\n\n return this.getGlobalState().rulesGraph.isFlowVisible(this.id)\n }\n set isVisible(visible: boolean) {\n this._isVisible = visible\n }\n\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n\n constructor({ config, id }: { config: FrigadeConfig; id: string }) {\n super(config)\n this.id = id\n this.init()\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.init()\n }\n\n /**\n * @ignore\n */\n resyncState(overrideStatefulFlow?: StatefulFlow) {\n const statefulFlow = overrideStatefulFlow ?? this.getStatefulFlow()\n\n this.rawData = statefulFlow\n this.title = statefulFlow?.data?.title\n this.subtitle = statefulFlow?.data?.subtitle\n\n this.isCompleted = statefulFlow.$state.completed\n this.isStarted = statefulFlow.$state.started\n this.isSkipped = statefulFlow.$state.skipped\n this._isVisible = statefulFlow.$state.visible\n\n // Set\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n const existingStep = this.steps?.get(step.id)\n if (existingStep) {\n existingStep.$state = stepObj.$state\n }\n })\n }\n\n /**\n * @ignore\n */\n private initStepFromStatefulStep(step: StatefulStep, index: number) {\n return {\n ...step,\n flow: this,\n $state: {\n ...step.$state,\n lastActionAt: step.$state.lastActionAt ? new Date(step.$state.lastActionAt) : undefined,\n },\n order: index,\n } as Partial<FlowStep>\n }\n\n /**\n * @ignore\n */\n private init() {\n const statefulFlow = this.getStatefulFlow()\n\n this.resyncState()\n\n const newSteps = new Map<string, FlowStep>()\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (this.getCurrentStep().id === thisStep.id && thisStep.$state.started) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n copy.$state.lastActionAt = new Date()\n copy.$state.currentStepId = thisStep.id\n copy.$state.currentStepIndex = thisStep.order\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (!thisStep.$state.completed) {\n await this.sendFlowStateToAPI(STARTED_STEP, properties, thisStep.id)\n }\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.$state.completed) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n const copy = clone(this.getGlobalState().flowStates[this.id])\n\n copy.$state.started = true\n copy.data.steps[thisStep.order].$state.completed = true\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n\n // If there are more index, advance current step\n if (!isLastStep) {\n copy.$state.currentStepId = this.getStepByIndex(thisStep.order + 1).id\n copy.$state.currentStepIndex = thisStep.order + 1\n }\n\n if (isLastStep) {\n copy.$state.completed = true\n }\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n await this.sendFlowStateToAPI(COMPLETED_STEP, properties, thisStep.id)\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.$state.completed) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = false\n copy.data.steps[thisStep.order].$state.completed = false\n copy.data.steps[thisStep.order].$state.lastActionAt = undefined\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(NOT_STARTED_STEP, undefined, thisStep.id)\n }\n\n stepObj.onStateChange = (handler: (step: FlowStep, previousStep: FlowStep) => void) => {\n const wrapperHandler = (flow: Flow) => {\n if (flow.id !== this.id) {\n return\n }\n const thisStep = flow.steps.get(step.id)\n const previousStep = this.lastStepUpdate.get(handler)\n\n if (\n thisStep.$state.completed !== previousStep?.$state.completed ||\n thisStep.$state.started !== previousStep?.$state.started ||\n thisStep.$state.visible !== previousStep?.$state.visible ||\n thisStep.$state.blocked !== previousStep?.$state.blocked\n ) {\n handler(thisStep, previousStep ?? clone(thisStep))\n this.lastStepUpdate.set(handler, clone(thisStep))\n }\n }\n this.getGlobalState().onStepStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n stepObj.removeStateChangeHandler = (\n handler: (step: FlowStep, previousStep: FlowStep) => void\n ) => {\n const wrapperHandler = this.getGlobalState().onStepStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n newSteps.set(step.id, stepObj as FlowStep)\n })\n this.steps = newSteps\n // Check if empty object\n if (\n this.getGlobalState().variables &&\n this.getGlobalState().variables[this.id] &&\n Object.keys(this.getGlobalState().variables[this.id]).length > 0\n ) {\n this.applyVariables(this.getGlobalState().variables[this.id])\n }\n }\n\n /**\n * Marks the flow started\n */\n public async start(properties?: Record<string | number, any>) {\n if (this.isStarted || this.isCompleted) {\n return\n }\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.started = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(STARTED_FLOW, properties)\n }\n\n /**\n * Marks the flow completed\n */\n public async complete(properties?: Record<string | number, any>) {\n if (this.isCompleted) {\n return\n }\n this.optimisticallyMarkFlowCompleted()\n await this.sendFlowStateToAPI(COMPLETED_FLOW, properties)\n }\n\n /**\n * Marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n if (this.isSkipped) {\n return\n }\n this.optimisticallyMarkFlowSkipped()\n await this.sendFlowStateToAPI(SKIPPED_FLOW, properties)\n }\n\n /**\n * Navigates the flow to the next step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async forward(properties?: Record<string | number, any>) {\n const nextStep = this.getStepByIndex(this.getCurrentStepIndex() + 1)\n if (nextStep) {\n await nextStep.start(properties)\n } else {\n await this.complete(properties)\n }\n }\n\n /**\n * Navigates the flow to the previous step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async back(properties?: Record<string | number, any>) {\n const previousStep = this.getStepByIndex(this.getCurrentStepIndex() - 1)\n if (previousStep) {\n await previousStep.start(properties)\n }\n }\n\n /**\n * Restarts the flow/marks it not started\n */\n public async restart() {\n this.optimisticallyMarkFlowNotStarted()\n await this.sendFlowStateToAPI(NOT_STARTED_FLOW)\n }\n\n /**\n * Get a step by index\n * @param index\n */\n public getStepByIndex(index: number): FlowStep | undefined {\n return this.steps.get(Array.from(this.steps.keys())[index])\n }\n\n /**\n * Gets current step\n */\n public getCurrentStep(): FlowStep {\n return (\n this.steps.get(this.getStatefulFlow().$state.currentStepId) ??\n this.steps.get(Array.from(this.steps.keys())[0])\n )\n }\n\n /**\n * Get the index of the current step. Starts at 0\n */\n public getCurrentStepIndex(): number {\n const currentStep = this.getCurrentStep()\n return Array.from(this.steps.keys()).indexOf(currentStep.id)\n }\n\n /**\n * Get the number of completed steps for the current user in the current flow\n */\n public getNumberOfCompletedSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.completed).length\n }\n\n /**\n * Get the number of available steps for the current user in the current flow. This is the number of steps that are not hidden.\n */\n public getNumberOfAvailableSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.visible).length\n }\n\n /**\n * @ignore\n */\n public onStateChange(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = (flow: Flow, previousFlow: Flow) => {\n if (flow.id === this.id) {\n if (\n flow.isCompleted !== previousFlow?.isCompleted ||\n flow.isStarted !== previousFlow?.isStarted ||\n flow.isSkipped !== previousFlow?.isSkipped ||\n flow.isVisible !== previousFlow?.isVisible ||\n JSON.stringify(flow.steps) !== JSON.stringify(previousFlow?.steps)\n ) {\n handler(flow, previousFlow)\n }\n }\n }\n this.getGlobalState().onFlowStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n /**\n * @ignore\n */\n public removeStateChangeHandler(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = this.getGlobalState().onFlowStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n /**\n * @ignore\n */\n public applyVariables(variables: Record<string, any>) {\n // Replace ${variable} with the value of the variable\n const replaceVariables = (str: string) => {\n const matches = str.match(/\\${(.*?)}/g)\n if (matches) {\n matches.forEach((match) => {\n const variable = match.replace('${', '').replace('}', '')\n if (!variables[variable]) {\n return\n }\n str = str.replace(match, variables[variable])\n })\n }\n return str\n }\n\n this.title = replaceVariables(this.title ?? '')\n this.subtitle = replaceVariables(this.subtitle ?? '')\n this.steps.forEach((step) => {\n // Iterate over every string field in the step and replace variables\n Object.keys(step).forEach((key) => {\n if (typeof step[key] === 'string') {\n // @ts-ignore\n step[key] = replaceVariables(step[key])\n }\n })\n })\n\n this.getGlobalState().variables = {}\n this.getGlobalState().variables[this.id] = variables\n }\n\n /**\n * @ignore\n */\n private getStatefulFlow(): StatefulFlow {\n const userFlowStates = this.getGlobalState().flowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = true\n copy.$state.started = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowNotStarted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = false\n copy.$state.started = false\n copy.$state.visible = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n private async sendFlowStateToAPI(\n action: FlowActionType,\n data?: Record<string | number, any>,\n stepId?: string\n ) {\n const date = new Date()\n this.getGlobalState().lastSyncDate = date\n const flowStatesRaw: FlowStates = await this.fetch('/flowStates', {\n method: 'POST',\n body: JSON.stringify({\n userId: this.config.userId,\n groupId: this.config.groupId,\n flowSlug: this.id,\n stepId: stepId ?? this.getCurrentStep().id,\n data: data ? JSON.stringify(data) : {},\n actionType: action,\n createdAt: date,\n } as FlowStateDTO),\n })\n if (date < this.getGlobalState().lastSyncDate) {\n return\n }\n await this.getGlobalState().refreshStateFromAPI(flowStatesRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.skipped = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n public register(callback?: RulesGraphRegistryCallback) {\n const globalState = this.getGlobalState()\n\n globalState.rulesGraph.register(this.id, (visible) => {\n const prevFlow = this.getGlobalState().previousFlows.get(this.id)\n\n if (prevFlow?._isVisible !== visible) {\n // TODO: Store these in a hash so we can grab this flow's handler and call it\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n handler(this, prevFlow)\n this.getGlobalState().previousFlows.set(this.id, cloneFlow(this))\n })\n }\n\n callback?.(visible)\n })\n }\n\n public unregister() {\n this.getGlobalState().rulesGraph.unregister(this.id)\n }\n}\n","import { RulesGraphData } from './types'\n\nexport interface RulesGraphNode {\n visible: boolean\n edges: RulesGraphEdge[]\n}\n\nexport interface RulesGraphEdge {\n head: string\n ruleId: string\n}\n\nexport type RulesGraphRegistryCallback = (visible: boolean) => void\n\n// TODO: JSDoc this class\nexport class RulesGraph {\n private graph: Map<string, RulesGraphNode> = new Map()\n private readonly registry: Map<string, RulesGraphRegistryCallback> = new Map()\n private readonly ruleOrder: string[] = []\n\n constructor(graphData: RulesGraphData) {\n this.ruleOrder = graphData?.ruleOrder ?? []\n this.ingestGraphData(graphData?.graph ?? {})\n }\n\n ingestGraphData(graphData: Record<string, RulesGraphNode>) {\n this.graph = new Map(Object.entries(graphData))\n\n this.fireCallbacks()\n }\n\n fireCallbacks() {\n for (const [flowId, callback] of this.registry) {\n if (typeof callback === 'function') {\n callback(this.isFlowVisible(flowId))\n }\n }\n }\n\n isFlowVisible(flowId: string) {\n const currentNode = this.graph.get(flowId)\n\n // Is the flow in the graph?\n // If not, it belongs to 0 rules and can carry on about its business\n if (!currentNode) {\n return true\n }\n\n if (currentNode.visible === false) {\n return false\n }\n\n // Does the flow have edges?\n // If not, it's the king of all flows, and it does what it wants\n if (currentNode.edges.length === 0) {\n return true\n }\n\n // Are any of the flow's descendants visible and in the registry already?\n const registeredDescendant = this.findRegisteredDescendant(flowId)\n\n // If so, this flow is not visible\n if (registeredDescendant != null) {\n return false\n }\n\n // Congrats! There are no conditions in the graph that disqualify this flow from being visible\n return true\n }\n\n register(flowId: string, callback?: RulesGraphRegistryCallback) {\n this.registry.set(flowId, callback ?? (() => {}))\n\n this.fireCallbacks()\n }\n\n unregister(flowId: string) {\n this.registry.delete(flowId)\n\n this.fireCallbacks()\n }\n\n getRegistry() {\n return this.registry\n }\n\n private findRegisteredDescendant(nodeId: string, originId = nodeId, ruleId?: string) {\n const node = this.graph.get(nodeId)\n\n if (node == null) {\n return undefined\n }\n\n if (this.registry.has(nodeId) && originId !== nodeId) {\n return node\n }\n\n let result = undefined\n\n if (node.edges.length > 0) {\n for (const edge of node.edges) {\n // If we went past the end of the rule, stop searching for this rule\n if (ruleId != null && edge.ruleId !== ruleId) {\n continue\n }\n\n const ancestor = this.findRegisteredDescendant(edge.head, originId, ruleId ?? edge.ruleId)\n\n if (ancestor != null && ancestor.visible) {\n result = ancestor\n break\n }\n }\n }\n\n return result\n }\n}\n","import { FlowStates, FrigadeConfig, StatefulFlow, StatefulStep } from './types'\nimport { clearCache, cloneFlow, GUEST_PREFIX, isWeb, resetAllLocalStorage } from '../shared/utils'\nimport { Flow } from './flow'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraph } from './rules-graph'\n\nexport class Frigade extends Fetchable {\n /**\n * @ignore\n */\n private flows: Flow[] = []\n /**\n * @ignore\n */\n private initPromise: Promise<void>\n /**\n * @ignore\n */\n private hasFailed = false\n\n /**\n * @ignore\n */\n private visibilityChangeHandler = async () => {\n if (document.visibilityState === 'visible') {\n await this.refreshStateFromAPI()\n }\n }\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n this.init(this.config)\n if (isWeb()) {\n document.addEventListener('visibilitychange', this.visibilityChangeHandler)\n }\n }\n\n /**\n * @ignore\n */\n destroy() {\n if (isWeb()) {\n document.removeEventListener('visibilitychange', this.visibilityChangeHandler)\n // Remove all other event listeners\n const globalStateKey = getGlobalStateKey(this.config)\n if (frigadeGlobalState[globalStateKey]) {\n frigadeGlobalState[globalStateKey].onFlowStateChangeHandlers = []\n }\n }\n }\n\n /**\n * @ignore\n */\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n if (this.config.userId && !this.config.userId?.startsWith(GUEST_PREFIX)) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n }),\n })\n }\n await this.refreshStateFromAPI()\n })()\n\n return this.initPromise\n }\n\n /**\n * Set the current user.\n * @param userId\n * @param properties\n */\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.config = { ...this.config, userId }\n await this.initIfNeeded()\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Set the group for the current user.\n * @param groupId\n * @param properties\n */\n public async group(groupId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.groupId = groupId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Track an event for the current user (and group if set).\n * @param event\n * @param properties\n */\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n if (!event) {\n console.error('Event name is required to track an event')\n return\n }\n if (this.config.userId && this.config.groupId) {\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n } else if (this.config.userId) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n }\n await this.resync()\n }\n\n /**\n * @ignore\n */\n public isReady(): boolean {\n return Boolean(this.config.__instanceId && this.config.apiKey && this.initPromise)\n }\n\n /**\n * Get a Flow by its ID.\n * @param flowId\n */\n public async getFlow(flowId: string) {\n await this.initIfNeeded()\n\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n public async getFlows() {\n await this.initIfNeeded()\n return this.flows\n }\n\n /**\n * Reload the current state of the flows by calling the Frigade API.\n * This will trigger all event handlers.\n */\n public async reload() {\n resetAllLocalStorage()\n clearCache()\n await this.refreshStateFromAPI()\n this.initPromise = null\n await this.init(this.config)\n // Trigger all event handlers\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n private async resync() {\n this.initPromise = null\n await this.init(this.config)\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n /**\n * Event handler that captures all changes that happen to the state of the Flows.\n * @param handler\n */\n public async onStateChange(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers.push(handler)\n }\n\n /**\n * Returns true if the JS SDK failed to connect to the Frigade API.\n */\n hasFailedToLoad() {\n return this.hasFailed\n }\n\n /**\n * Removes the given handler from the list of event handlers.\n * @param handler\n */\n public async removeStateChangeHandler(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== handler)\n }\n\n /**\n * @ignore\n */\n private async initIfNeeded() {\n if (this.initPromise !== null) {\n return this.initPromise\n } else {\n return this.init(this.config)\n }\n }\n\n /**\n * @ignore\n */\n private async refreshStateFromAPI(): Promise<void> {\n const globalStateKey = getGlobalStateKey(this.config)\n\n if (!frigadeGlobalState[globalStateKey]) {\n const that = this\n\n let validator = {\n set: function (target: any, key: any, value: any) {\n if (target[key]) {\n const previousState = target[key] as StatefulFlow\n const newState = value as StatefulFlow\n if (JSON.stringify(previousState) !== JSON.stringify(newState)) {\n that.triggerEventHandlers(newState, previousState)\n }\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshStateFromAPI: async () => {},\n rulesGraph: new RulesGraph({\n graph: {},\n ruleOrder: [],\n }),\n flowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n variables: {},\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshStateFromAPI = async (\n overrideFlowStateRaw?: FlowStates\n ) => {\n if (this.config.__readOnly) {\n return\n }\n\n const flowStateRaw: FlowStates = overrideFlowStateRaw\n ? overrideFlowStateRaw\n : await this.fetch(\n `/flowStates?userId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId ? `&groupId=${encodeURIComponent(this.config.groupId)}` : ''\n }`\n )\n\n // TODO: should this also take in order and check if it has changed?\n frigadeGlobalState[globalStateKey].rulesGraph.ingestGraphData(\n flowStateRaw.ruleGraph?.graph ?? {}\n )\n\n if (flowStateRaw && flowStateRaw.eligibleFlows) {\n flowStateRaw.eligibleFlows.forEach((statefulFlow) => {\n frigadeGlobalState[globalStateKey].flowStates[statefulFlow.flowSlug] = statefulFlow\n if (!this.flows.find((flow) => flow.id == statefulFlow.flowSlug)) {\n this.flows.push(\n new Flow({\n config: this.config,\n id: statefulFlow.flowSlug,\n })\n )\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshStateFromAPI()\n }\n\n /**\n * @ignore\n */\n private mockFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].flowStates[flowId] = {\n flowSlug: flowId,\n flowName: parsed?.name ?? flowId,\n flowType: parsed?.type ?? 'CHECKLIST',\n data: {\n ...parsed,\n steps: (parsed?.steps ?? []).map((step: any): StatefulStep => {\n return {\n id: step.id,\n $state: {\n completed: false,\n started: false,\n visible: true,\n blocked: false,\n },\n ...step,\n }\n }),\n },\n $state: {\n currentStepId: null,\n currentStepIndex: -1,\n completed: false,\n started: false,\n skipped: false,\n visible: true,\n },\n } as StatefulFlow\n\n this.flows.push(\n new Flow({\n config: this.config,\n id: flowId,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(newState: StatefulFlow, previousState?: StatefulFlow) {\n if (newState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousState.flowSlug) {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n flow.resyncState(newState)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n }\n })\n }\n }\n}\n","import type { Flow } from './flow'\n\nimport { RulesGraphNode } from './rules-graph'\n\nexport enum TriggerType {\n MANUAL = 'MANUAL',\n AUTOMATIC = 'AUTOMATIC',\n}\n\nexport enum FlowStatus {\n DRAFT = 'DRAFT',\n ACTIVE = 'ACTIVE',\n ARCHIVED = 'ARCHIVED',\n}\n\nexport type StepAction =\n | 'flow.back'\n | 'flow.complete'\n | 'flow.forward'\n | 'flow.restart'\n | 'flow.skip'\n | 'flow.start'\n | 'step.complete'\n | 'step.reset'\n | 'step.start'\n | false\n\nexport interface FlowStep extends StatefulStep {\n /**\n * Order of the step in the Flow.\n */\n order: number\n\n /**\n * Name of the step when shown in a list view.\n */\n stepName?: string\n\n /**\n * Title of the step.\n */\n title?: string\n\n /**\n * Subtitle of the step.\n */\n subtitle?: string\n\n /**\n * Config for the primary button in this step\n */\n primaryButton?: {\n /**\n * Primary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Primary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Primary button title. If omitted, the primary button will not be shown.\n */\n title?: string\n\n /**\n * Primary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use primaryButton.title instead\n * @description Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * @deprecated Use primaryButton.uri instead\n * @description Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * @deprecated Use primaryButton.target instead\n * @description Primary button URI target (defaults to _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Config for the secondary button in this step\n */\n secondaryButton?: {\n /**\n * Secondary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Secondary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n title?: string\n\n /**\n * Secondary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use secondaryButton.title instead\n * @description Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * @deprecated Use secondaryButton.uri instead\n * @description Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * @deprecated Use secondaryButton.target instead\n * @description Secondary button URI target (defaults to _self)\n */\n secondaryButtonUriTarget?: string\n\n /**\n * Text on button if a back button is present.\n */\n backButtonTitle?: string\n\n /**\n * If true, the step will be marked as completed when the secondary button is clicked.\n */\n skippable?: boolean\n\n /**\n * Video url to be shown for components supporting video.\n */\n videoUri?: string\n\n /**\n * Image url to be shown for components supporting image.\n */\n imageUri?: string | null\n\n /**\n * Automatically mark the step as completed when the primary button is clicked. Default is false.\n */\n autoMarkCompleted?: boolean\n\n /**\n * @ignore\n */\n props?: any\n /**\n * Criteria that needs to be met for the step to complete.\n * Completion criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be completed.\n */\n completionCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to start.\n * Start criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be started.\n */\n startCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to be visible.\n * Visibility criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be visible.\n */\n visibilityCriteria?: string\n\n /**\n * Progress if the step is tied to another Frigade Flow through completionCriteria.\n */\n progress?: number\n\n /**\n * Whether the step is dismissible (for instance, tooltips or other non-essential steps).\n */\n dismissible?: boolean\n\n /**\n * Any other additional props defined in the YAML config.\n */\n [x: string | number | symbol]: unknown\n\n /**\n * Marks the step started.\n */\n start: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Marks the step completed.\n */\n complete: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Resets the step (useful for undoing a finished step).\n */\n reset: () => Promise<void>\n\n /**\n * Event handler called when this step's state changes.\n */\n onStateChange: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Removes the given callback from the list of event handlers.\n */\n removeStateChangeHandler: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Reference to this step's parent Flow\n */\n flow: Flow\n}\n\nexport interface FrigadeConfig {\n /**\n * Frigade API key. You can find your API key in the Frigade Dashboard under \"Developer\".\n */\n apiKey?: string\n /**\n * API url to use for all requests. Defaults to https://api.frigade.com\n */\n apiUrl?: string\n\n /**\n * User ID to use for all requests. If not provided, a Guest ID will be generated.\n */\n userId?: string\n\n /**\n * Group ID (organization) to use for all requests.\n */\n groupId?: string\n\n /**\n * @ignore Internal use only.\n * If enabled, Frigade will not send any data to the API. A user's state will be reset on page refresh.\n */\n __readOnly?: boolean\n\n /**\n * @ignore Internal use only.\n * Map of Flow ID to Flow Config for all flows in the app.\n * Configs will have to be provided in serialized JSON format rather than YAML.\n */\n __flowConfigOverrides?: Record<string, string>\n\n /**\n * @ignore Internal use only.\n */\n __instanceId?: string\n}\n\nexport interface StatefulStep {\n id: string\n $state: {\n completed: boolean\n started: boolean\n visible: boolean\n blocked: boolean\n lastActionAt?: Date\n }\n // allow any other properties\n [key: string]: any\n}\n\nexport interface StatefulFlow {\n flowSlug: string\n flowName: string\n flowType: FlowType\n data: {\n steps: StatefulStep[]\n // allow any other properties\n [key: string]: any\n }\n $state: {\n currentStepId: string\n currentStepIndex: number\n completed: boolean\n started: boolean\n skipped: boolean\n visible: boolean\n lastActionAt?: Date\n }\n}\n\nexport interface RulesGraphData {\n ruleOrder: string[]\n graph: Record<string, RulesGraphNode>\n}\n\nexport interface FlowStates {\n eligibleFlows: StatefulFlow[]\n ineligibleFlows: string[]\n ruleGraph?: RulesGraphData\n}\n\nexport enum FlowType {\n ANNOUNCEMENT = 'ANNOUNCEMENT',\n CHECKLIST = 'CHECKLIST',\n FORM = 'FORM',\n TOUR = 'TOUR',\n SUPPORT = 'SUPPORT',\n CUSTOM = 'CUSTOM',\n BANNER = 'BANNER',\n EMBEDDED_TIP = 'EMBEDDED_TIP',\n NPS_SURVEY = 'NPS_SURVEY',\n SURVEY = 'SURVEY',\n CARD = 'CARD',\n}\n\nexport type FlowActionType =\n | 'STARTED_STEP'\n | 'COMPLETED_STEP'\n | 'NOT_STARTED_STEP'\n | 'STARTED_FLOW'\n | 'COMPLETED_FLOW'\n | 'SKIPPED_FLOW'\n | 'NOT_STARTED_FLOW'\n\nexport class FlowStateDTO {\n userId: string\n groupId?: string\n flowSlug: string\n stepId?: string\n data?: string\n actionType: FlowActionType\n createdAt?: Date\n}\n"],"mappings":";0nBAAO,IAAMA,EAAiB,SCC9B,OAAS,MAAMC,MAAc,OCqBtB,IAAIC,EAAyD,CAAC,EAE9D,SAASC,EAAkBC,EAAuC,CAxBzE,IAAAC,EAAAC,EAyBE,MAAO,GAA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AS,GACT,QAAS,GACT,QAAS,EACX,GACGA,EAEN,CACH,GACA,OAAQ,CACN,cAAe,KACf,iBAAkB,GAClB,UAAW,GACX,QAAS,GACT,QAAS,GACT,QAAS,EACX,CACF,EAEA,KAAK,MAAM,KACT,IAAIH,EAAK,CACP,OAAQ,KAAK,OACb,GAAIpB,CACN,CAAC,CACH,CACF,CAAC,CACH,CAKc,qBAAqBc,EAAwBD,EAA8B,QAAAzB,EAAA,sBACnF0B,GACF,KAAK,MAAM,QAASb,GAAS,CACvBA,EAAK,IAAMY,EAAc,UAC3B,KAAK,eAAe,EAAE,0BAA0B,QAAST,GAAY,CACnE,IAAMC,EAAW,KAAK,eAAe,EAAE,cAAc,IAAIJ,EAAK,EAAE,EAChEA,EAAK,YAAYa,CAAQ,EACzBV,EAAQH,EAAMI,CAAQ,EACtB,KAAK,eAAe,EAAE,cAAc,IAAIJ,EAAK,GAAIK,EAAUL,CAAI,CAAC,CAClE,CAAC,CAEL,CAAC,CAEL,GACF,ECxYO,IAAKuB,OACVA,EAAA,OAAS,SACTA,EAAA,UAAY,YAFFA,OAAA,IAKAC,OACVA,EAAA,MAAQ,QACRA,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IA6SAC,OACVA,EAAA,aAAe,eACfA,EAAA,UAAY,YACZA,EAAA,KAAO,OACPA,EAAA,KAAO,OACPA,EAAA,QAAU,UACVA,EAAA,OAAS,SACTA,EAAA,OAAS,SACTA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,OAAS,SACTA,EAAA,KAAO,OAXGA,OAAA","names":["VERSION_NUMBER","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","_a","_b","Fetchable","config","generateGuestId","filteredConfig","_","v","__spreadValues","path","options","__async","getEmptyResponse","gracefulFetch","getHeaders","pathPrefix","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","id","visible","overrideStatefulFlow","_a","_b","statefulFlow","step","index","stepObj","existingStep","__spreadProps","__spreadValues","newSteps","properties","__async","thisStep","copy","clone","STARTED_STEP","isLastStep","COMPLETED_STEP","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","STARTED_FLOW","COMPLETED_FLOW","SKIPPED_FLOW","nextStep","NOT_STARTED_FLOW","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","key","action","data","stepId","date","flowStatesRaw","callback","prevFlow","cloneFlow","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","LOCAL_STORAGE_PREFIX","cloneFlow","flow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","CallQueue","call","now","item","callQueue","gracefulFetch","url","options","__async","_a","getEmptyResponse","lastCallDataKey","response","error","body","e","generateGuestId","guestId","getLocalStorage","GUEST_KEY","GUEST_PREFIX","uuidv4","setLocalStorage","isWeb","RulesGraph","graphData","_a","_b","flowId","callback","currentNode","nodeId","originId","ruleId","node","result","edge","ancestor","Frigade","Fetchable","apiKey","config","__spreadValues","__async","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","__spreadProps","groupId","event","flowId","flow","resetAllLocalStorage","clearCache","handler","lastFlow","cloneFlow","h","that","validator","target","key","value","previousState","newState","RulesGraph","overrideFlowStateRaw","_b","flowStateRaw","statefulFlow","Flow","_c","parsed","step","TriggerType","FlowStatus","FlowType"]}
1
+ {"version":3,"sources":["../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/rules-graph.ts","../src/core/frigade.ts","../src/core/types.ts"],"sourcesContent":["export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport { v4 as uuidv4 } from 'uuid'\nimport { Flow } from '../core/flow'\nimport { frigadeGlobalState } from './state'\n\nexport const NOT_STARTED_STEP = 'NOT_STARTED_STEP'\nexport const COMPLETED_FLOW = 'COMPLETED_FLOW'\nexport const SKIPPED_FLOW = 'SKIPPED_FLOW'\nexport const STARTED_FLOW = 'STARTED_FLOW'\nexport const NOT_STARTED_FLOW = 'NOT_STARTED_FLOW'\nexport const COMPLETED_STEP = 'COMPLETED_STEP'\nexport const STARTED_STEP = 'STARTED_STEP'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow({\n config: flow.config,\n id: flow.id,\n })\n return newFlow\n}\n\nexport function clone<T>(obj: T): T {\n return JSON.parse(JSON.stringify(obj))\n}\n\nexport function getHeaders(apiKey: string) {\n return {\n headers: {\n Authorization: `Bearer ${apiKey}`,\n 'Content-Type': 'application/json',\n 'X-Frigade-SDK-Version': VERSION_NUMBER,\n 'X-Frigade-SDK-Platform': 'Javascript',\n },\n }\n}\n\nfunction getLocalStorage(key: string) {\n if (isWeb()) {\n return window.localStorage.getItem(`${LOCAL_STORAGE_PREFIX}${key}`)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (isWeb()) {\n window.localStorage.setItem(`${LOCAL_STORAGE_PREFIX}${key}`, value)\n }\n}\n\nfunction setGlobalState(key: string, value: any) {\n frigadeGlobalState[key] = value\n}\n\nfunction getGlobalState(key: string): any {\n return frigadeGlobalState[key]\n}\n\nexport function clearCache() {\n Object.keys(frigadeGlobalState).forEach((key) => {\n if (key.startsWith(GET_CACHE_PREFIX)) {\n delete frigadeGlobalState[key]\n }\n })\n}\n\nexport function resetAllLocalStorage() {\n if (isWeb()) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith(LOCAL_STORAGE_PREFIX)) {\n window.localStorage.removeItem(key)\n }\n })\n }\n}\n\nclass CallQueue {\n private queue: {\n call: string\n time: number\n }[] = []\n private readonly ttlInMS = 500\n private readonly cacheSize = 5\n\n public push(call: string) {\n const now = new Date()\n if (this.queue.length >= this.cacheSize) {\n this.queue.shift()\n }\n this.queue.push({\n call: call,\n time: now.getTime(),\n })\n }\n\n public hasIdenticalCall(call: string) {\n const now = new Date()\n this.queue = this.queue.filter((item) => now.getTime() - item.time < this.ttlInMS)\n return this.queue.some((item) => item.call === call)\n }\n}\n\nconst callQueue = new CallQueue()\n\nexport async function gracefulFetch(url: string, options: any) {\n if (typeof globalThis.fetch !== 'function') {\n return getEmptyResponse(\n \"- Attempted to call fetch() in an environment that doesn't support it.\"\n )\n }\n\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCallDataKey = `${url}${JSON.stringify(options.body ?? {})}`\n if (callQueue.hasIdenticalCall(lastCallDataKey)) {\n return getEmptyResponse()\n }\n callQueue.push(lastCallDataKey)\n }\n\n let response\n try {\n response = fetch(url, options)\n response = await response\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse('Received an empty response')\n }\n\n if (response.status >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n try {\n if (response.status === 204) {\n return getEmptyResponse()\n }\n\n let body\n try {\n body = await response.json()\n } catch (e) {\n return getEmptyResponse()\n }\n\n if (body.error) {\n return getEmptyResponse(body.error)\n }\n return body\n } catch (e) {\n return getEmptyResponse(e)\n }\n}\n\nexport function getEmptyResponse(error?: any) {\n if (error) {\n console.log('Call to Frigade failed', error)\n }\n\n // Create empty response that contains the .json method and returns an empty object\n return {\n json: () => ({}),\n }\n}\n\nexport function generateGuestId() {\n if (isWeb()) {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n setLocalStorage(GUEST_KEY, guestId)\n }\n return guestId\n }\n}\n\nexport function fetcher(apiKey: string, path: string, options?: Record<any, any>) {\n return gracefulFetch(`//api.frigade.com/v1/public${path}`, {\n ...(options ?? {}),\n ...getHeaders(apiKey),\n })\n}\n\nexport function isWeb() {\n return (\n typeof window !== 'undefined' &&\n typeof window.document !== 'undefined' &&\n typeof window.localStorage !== 'undefined'\n )\n}\n","import { FlowStates, FlowStep, FrigadeConfig, StatefulFlow } from '../core/types'\nimport { Flow } from '../core/flow'\nimport { RulesGraph } from '../core/rules-graph'\n\nexport interface FrigadeGlobalState {\n refreshStateFromAPI: (overrideFlowStatesRaw?: FlowStates) => Promise<void>\n flowStates: Record<string, StatefulFlow>\n rulesGraph: RulesGraph\n onFlowStateChangeHandlerWrappers: Map<\n (flow: Flow, previousFlow: Flow) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onStepStateChangeHandlerWrappers: Map<\n (step: FlowStep, previousStep: FlowStep) => void,\n (flow: Flow, previousFlow: Flow) => void\n >\n onFlowStateChangeHandlers: ((flow: Flow, previousFlow: Flow) => void)[]\n previousFlows: Map<string, Flow>\n variables: Record<string, Record<string, any>>\n lastSyncDate?: Date\n}\n\nexport let frigadeGlobalState: Record<string, FrigadeGlobalState> = {}\n\nexport function getGlobalStateKey(internalConfig: FrigadeConfig): string {\n return `${internalConfig.__instanceId}-${internalConfig.apiKey}:${internalConfig.userId ?? ''}:${\n internalConfig.groupId ?? ''\n }`\n}\n","import { generateGuestId, getEmptyResponse, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../core/types'\nimport { frigadeGlobalState, FrigadeGlobalState, getGlobalStateKey } from './state'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: 'https://api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(12).substring(4),\n }\n\n constructor(config: FrigadeConfig) {\n const filteredConfig = Object.fromEntries(Object.entries(config).filter(([_, v]) => v != null))\n\n this.config = {\n ...this.config,\n ...filteredConfig,\n }\n }\n\n /**\n * @ignore\n */\n public async fetch(path: string, options?: Record<any, any>) {\n if (this.config.__readOnly) {\n return getEmptyResponse()\n }\n\n return gracefulFetch(this.getAPIUrl(path), {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n\n private getAPIUrl(path: string) {\n const pathPrefix = '/v1/public'\n const apiUrl = new URL(\n `${pathPrefix}${path}`,\n this.config.apiUrl.replace(pathPrefix, '')\n ).toString()\n\n return apiUrl\n }\n\n /**\n * @ignore\n */\n protected getGlobalState(): FrigadeGlobalState {\n const globalStateKey = getGlobalStateKey(this.config)\n if (!frigadeGlobalState[globalStateKey]) {\n throw new Error('Frigade has not yet been initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import {\n FlowActionType,\n FlowStateDTO,\n FlowStates,\n FlowStep,\n FrigadeConfig,\n StatefulFlow,\n StatefulStep,\n} from './types'\nimport {\n clone,\n cloneFlow,\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n NOT_STARTED_STEP,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraphRegistryCallback } from './rules-graph'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * Ordered map of the Steps in the Flow.\n * See [Flow Step Definition](https://docs.frigade.com/v2/sdk/js/step) for more information.\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the Flow, if defined at the top level of the YAML config.\n */\n public title?: string\n /**\n * The user-facing description of the Flow, if defined at the top level of the YAML config.\n */\n public subtitle?: string\n /**\n * The metadata of the Flow.\n * @ignore\n */\n public rawData: StatefulFlow\n /**\n * Whether the Flow is completed or not.\n */\n public isCompleted: boolean\n /**\n * Whether the Flow is started or not.\n */\n public isStarted: boolean\n /**\n * Whether the Flow has been skipped or not.\n */\n public isSkipped: boolean\n /**\n * @ignore\n */\n private _isVisible: boolean = false\n /**\n * Whether the Flow is visible to the user based on the current user/group's state.\n */\n get isVisible() {\n if (this._isVisible === false) {\n return false\n }\n\n return this.getGlobalState().rulesGraph.isFlowVisible(this.id)\n }\n set isVisible(visible: boolean) {\n this._isVisible = visible\n }\n\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n\n constructor({ config, id }: { config: FrigadeConfig; id: string }) {\n super(config)\n this.id = id\n this.init()\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.init()\n }\n\n /**\n * @ignore\n */\n resyncState(overrideStatefulFlow?: StatefulFlow) {\n const statefulFlow = overrideStatefulFlow ?? this.getStatefulFlow()\n\n this.rawData = statefulFlow\n this.title = statefulFlow?.data?.title\n this.subtitle = statefulFlow?.data?.subtitle\n\n this.isCompleted = statefulFlow.$state.completed\n this.isStarted = statefulFlow.$state.started\n this.isSkipped = statefulFlow.$state.skipped\n this._isVisible = statefulFlow.$state.visible\n\n // Set\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n const existingStep = this.steps?.get(step.id)\n if (existingStep) {\n existingStep.$state = stepObj.$state\n }\n })\n }\n\n /**\n * @ignore\n */\n private initStepFromStatefulStep(step: StatefulStep, index: number) {\n return {\n ...step,\n flow: this,\n $state: {\n ...step.$state,\n lastActionAt: step.$state.lastActionAt ? new Date(step.$state.lastActionAt) : undefined,\n },\n order: index,\n } as Partial<FlowStep>\n }\n\n /**\n * @ignore\n */\n private init() {\n const statefulFlow = this.getStatefulFlow()\n\n this.resyncState()\n\n const newSteps = new Map<string, FlowStep>()\n\n statefulFlow.data.steps.forEach((step, index) => {\n const stepObj = this.initStepFromStatefulStep(step, index)\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (this.getCurrentStep().id === thisStep.id && thisStep.$state.started) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n copy.$state.lastActionAt = new Date()\n copy.$state.currentStepId = thisStep.id\n copy.$state.currentStepIndex = thisStep.order\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (!thisStep.$state.completed) {\n await this.sendFlowStateToAPI(STARTED_STEP, properties, thisStep.id)\n }\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.$state.completed) {\n return\n }\n\n const isLastStep = thisStep.order + 1 === this.getNumberOfAvailableSteps()\n const copy = clone(this.getGlobalState().flowStates[this.id])\n\n copy.$state.started = true\n copy.data.steps[thisStep.order].$state.completed = true\n copy.data.steps[thisStep.order].$state.started = true\n copy.data.steps[thisStep.order].$state.lastActionAt = new Date()\n\n // If there are more index, advance current step\n if (!isLastStep) {\n copy.$state.currentStepId = this.getStepByIndex(thisStep.order + 1).id\n copy.$state.currentStepIndex = thisStep.order + 1\n }\n\n if (isLastStep) {\n copy.$state.completed = true\n }\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n await this.sendFlowStateToAPI(COMPLETED_STEP, properties, thisStep.id)\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.$state.completed) {\n return\n }\n\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.data.steps[thisStep.order].$state.started = false\n copy.data.steps[thisStep.order].$state.completed = false\n copy.data.steps[thisStep.order].$state.lastActionAt = undefined\n\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(NOT_STARTED_STEP, undefined, thisStep.id)\n }\n\n stepObj.onStateChange = (handler: (step: FlowStep, previousStep: FlowStep) => void) => {\n const wrapperHandler = (flow: Flow) => {\n if (flow.id !== this.id) {\n return\n }\n const thisStep = flow.steps.get(step.id)\n const previousStep = this.lastStepUpdate.get(handler)\n\n if (\n thisStep.$state.completed !== previousStep?.$state.completed ||\n thisStep.$state.started !== previousStep?.$state.started ||\n thisStep.$state.visible !== previousStep?.$state.visible ||\n thisStep.$state.blocked !== previousStep?.$state.blocked\n ) {\n handler(thisStep, previousStep ?? clone(thisStep))\n this.lastStepUpdate.set(handler, clone(thisStep))\n }\n }\n this.getGlobalState().onStepStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n stepObj.removeStateChangeHandler = (\n handler: (step: FlowStep, previousStep: FlowStep) => void\n ) => {\n const wrapperHandler = this.getGlobalState().onStepStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n newSteps.set(step.id, stepObj as FlowStep)\n })\n this.steps = newSteps\n // Check if empty object\n if (\n this.getGlobalState().variables &&\n this.getGlobalState().variables[this.id] &&\n Object.keys(this.getGlobalState().variables[this.id]).length > 0\n ) {\n this.applyVariables(this.getGlobalState().variables[this.id])\n }\n }\n\n /**\n * Marks the flow started\n */\n public async start(properties?: Record<string | number, any>) {\n if (this.isStarted || this.isCompleted) {\n return\n }\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.started = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n\n await this.sendFlowStateToAPI(STARTED_FLOW, properties)\n }\n\n /**\n * Marks the flow completed\n */\n public async complete(properties?: Record<string | number, any>) {\n if (this.isCompleted) {\n return\n }\n this.optimisticallyMarkFlowCompleted()\n await this.sendFlowStateToAPI(COMPLETED_FLOW, properties)\n }\n\n /**\n * Marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n if (this.isSkipped) {\n return\n }\n this.optimisticallyMarkFlowSkipped()\n await this.sendFlowStateToAPI(SKIPPED_FLOW, properties)\n }\n\n /**\n * Navigates the flow to the next step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async forward(properties?: Record<string | number, any>) {\n const nextStep = this.getStepByIndex(this.getCurrentStepIndex() + 1)\n if (nextStep) {\n await nextStep.start(properties)\n } else {\n await this.complete(properties)\n }\n }\n\n /**\n * Navigates the flow to the previous step if one exists. This will mark that step started, but will not complete the previous step.\n */\n public async back(properties?: Record<string | number, any>) {\n const previousStep = this.getStepByIndex(this.getCurrentStepIndex() - 1)\n if (previousStep) {\n await previousStep.start(properties)\n }\n }\n\n /**\n * Restarts the flow/marks it not started\n */\n public async restart() {\n this.optimisticallyMarkFlowNotStarted()\n await this.sendFlowStateToAPI(NOT_STARTED_FLOW)\n }\n\n /**\n * Get a step by index\n * @param index\n */\n public getStepByIndex(index: number): FlowStep | undefined {\n return this.steps.get(Array.from(this.steps.keys())[index])\n }\n\n /**\n * Gets current step\n */\n public getCurrentStep(): FlowStep {\n return (\n this.steps.get(this.getStatefulFlow().$state.currentStepId) ??\n this.steps.get(Array.from(this.steps.keys())[0])\n )\n }\n\n /**\n * Get the index of the current step. Starts at 0\n */\n public getCurrentStepIndex(): number {\n const currentStep = this.getCurrentStep()\n return Array.from(this.steps.keys()).indexOf(currentStep.id)\n }\n\n /**\n * Get the number of completed steps for the current user in the current flow\n */\n public getNumberOfCompletedSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.completed).length\n }\n\n /**\n * Get the number of available steps for the current user in the current flow. This is the number of steps that are not hidden.\n */\n public getNumberOfAvailableSteps(): number {\n return Array.from(this.steps.values()).filter((step) => step.$state.visible).length\n }\n\n /**\n * @ignore\n */\n public onStateChange(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = (flow: Flow, previousFlow: Flow) => {\n if (flow.id === this.id) {\n if (\n flow.isCompleted !== previousFlow?.isCompleted ||\n flow.isStarted !== previousFlow?.isStarted ||\n flow.isSkipped !== previousFlow?.isSkipped ||\n flow.isVisible !== previousFlow?.isVisible ||\n JSON.stringify(flow.steps) !== JSON.stringify(previousFlow?.steps)\n ) {\n handler(flow, previousFlow)\n }\n }\n }\n this.getGlobalState().onFlowStateChangeHandlerWrappers.set(handler, wrapperHandler)\n this.getGlobalState().onFlowStateChangeHandlers.push(wrapperHandler)\n }\n\n /**\n * @ignore\n */\n public removeStateChangeHandler(handler: (flow: Flow, previousFlow: Flow) => void) {\n const wrapperHandler = this.getGlobalState().onFlowStateChangeHandlerWrappers.get(handler)\n if (wrapperHandler) {\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== wrapperHandler)\n }\n }\n\n /**\n * @ignore\n */\n public applyVariables(variables: Record<string, any>) {\n // Replace ${variable} with the value of the variable\n const replaceVariables = (str: string) => {\n const matches = str.match(/\\${(.*?)}/g)\n if (matches) {\n matches.forEach((match) => {\n const variable = match.replace('${', '').replace('}', '')\n if (!variables[variable]) {\n return\n }\n str = str.replace(match, variables[variable])\n })\n }\n return str\n }\n\n this.title = replaceVariables(this.title ?? '')\n this.subtitle = replaceVariables(this.subtitle ?? '')\n this.steps.forEach((step) => {\n // Iterate over every string field in the step and replace variables\n Object.keys(step).forEach((key) => {\n if (typeof step[key] === 'string') {\n // @ts-ignore\n step[key] = replaceVariables(step[key])\n }\n })\n })\n\n this.getGlobalState().variables = {}\n this.getGlobalState().variables[this.id] = variables\n }\n\n /**\n * @ignore\n */\n private getStatefulFlow(): StatefulFlow {\n const userFlowStates = this.getGlobalState().flowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = true\n copy.$state.started = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowNotStarted() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.completed = false\n copy.$state.started = false\n copy.$state.visible = true\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n private async sendFlowStateToAPI(\n action: FlowActionType,\n data?: Record<string | number, any>,\n stepId?: string\n ) {\n const date = new Date()\n this.getGlobalState().lastSyncDate = date\n const flowStatesRaw: FlowStates = await this.fetch('/flowStates', {\n method: 'POST',\n body: JSON.stringify({\n userId: this.config.userId,\n groupId: this.config.groupId,\n flowSlug: this.id,\n stepId: stepId ?? this.getCurrentStep().id,\n data: data ? data : {},\n actionType: action,\n createdAt: date,\n } as FlowStateDTO),\n })\n if (date < this.getGlobalState().lastSyncDate) {\n return\n }\n await this.getGlobalState().refreshStateFromAPI(flowStatesRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n const copy = clone(this.getGlobalState().flowStates[this.id])\n copy.$state.skipped = true\n copy.$state.visible = false\n this.getGlobalState().flowStates[this.id] = copy\n this.resyncState()\n }\n\n public register(callback?: RulesGraphRegistryCallback) {\n const globalState = this.getGlobalState()\n\n globalState.rulesGraph.register(this.id, (visible) => {\n const prevFlow = this.getGlobalState().previousFlows.get(this.id)\n\n if (prevFlow?._isVisible !== visible) {\n // TODO: Store these in a hash so we can grab this flow's handler and call it\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n handler(this, prevFlow)\n this.getGlobalState().previousFlows.set(this.id, cloneFlow(this))\n })\n }\n\n callback?.(visible)\n })\n }\n\n public unregister() {\n this.getGlobalState().rulesGraph.unregister(this.id)\n }\n}\n","import { RulesGraphData } from './types'\n\nexport interface RulesGraphNode {\n visible: boolean\n edges: RulesGraphEdge[]\n}\n\nexport interface RulesGraphEdge {\n head: string\n ruleId: string\n}\n\nexport type RulesGraphRegistryCallback = (visible: boolean) => void\n\n// TODO: JSDoc this class\nexport class RulesGraph {\n private graph: Map<string, RulesGraphNode> = new Map()\n private readonly registry: Map<string, RulesGraphRegistryCallback> = new Map()\n private readonly ruleOrder: string[] = []\n\n constructor(graphData: RulesGraphData) {\n this.ruleOrder = graphData?.ruleOrder ?? []\n this.ingestGraphData(graphData?.graph ?? {})\n }\n\n ingestGraphData(graphData: Record<string, RulesGraphNode>) {\n this.graph = new Map(Object.entries(graphData))\n\n this.fireCallbacks()\n }\n\n fireCallbacks() {\n for (const [flowId, callback] of this.registry) {\n if (typeof callback === 'function') {\n callback(this.isFlowVisible(flowId))\n }\n }\n }\n\n isFlowVisible(flowId: string) {\n const currentNode = this.graph.get(flowId)\n\n // Is the flow in the graph?\n // If not, it belongs to 0 rules and can carry on about its business\n if (!currentNode) {\n return true\n }\n\n if (currentNode.visible === false) {\n return false\n }\n\n // Does the flow have edges?\n // If not, it's the king of all flows, and it does what it wants\n if (currentNode.edges.length === 0) {\n return true\n }\n\n // Are any of the flow's descendants visible and in the registry already?\n const registeredDescendant = this.findRegisteredDescendant(flowId)\n\n // If so, this flow is not visible\n if (registeredDescendant != null) {\n return false\n }\n\n // Congrats! There are no conditions in the graph that disqualify this flow from being visible\n return true\n }\n\n register(flowId: string, callback?: RulesGraphRegistryCallback) {\n this.registry.set(flowId, callback ?? (() => {}))\n\n this.fireCallbacks()\n }\n\n unregister(flowId: string) {\n this.registry.delete(flowId)\n\n this.fireCallbacks()\n }\n\n getRegistry() {\n return this.registry\n }\n\n private findRegisteredDescendant(nodeId: string, originId = nodeId, ruleId?: string) {\n const node = this.graph.get(nodeId)\n\n if (node == null) {\n return undefined\n }\n\n if (this.registry.has(nodeId) && originId !== nodeId) {\n return node\n }\n\n let result = undefined\n\n if (node.edges.length > 0) {\n for (const edge of node.edges) {\n // If we went past the end of the rule, stop searching for this rule\n if (ruleId != null && edge.ruleId !== ruleId) {\n continue\n }\n\n const ancestor = this.findRegisteredDescendant(edge.head, originId, ruleId ?? edge.ruleId)\n\n if (ancestor != null && ancestor.visible) {\n result = ancestor\n break\n }\n }\n }\n\n return result\n }\n}\n","import { FlowStates, FrigadeConfig, StatefulFlow, StatefulStep } from './types'\nimport { clearCache, cloneFlow, GUEST_PREFIX, isWeb, resetAllLocalStorage } from '../shared/utils'\nimport { Flow } from './flow'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/fetchable'\nimport { RulesGraph } from './rules-graph'\n\nexport class Frigade extends Fetchable {\n /**\n * @ignore\n */\n private flows: Flow[] = []\n /**\n * @ignore\n */\n private initPromise: Promise<void>\n /**\n * @ignore\n */\n private hasFailed = false\n\n /**\n * @ignore\n */\n private visibilityChangeHandler = async () => {\n if (document.visibilityState === 'visible') {\n await this.refreshStateFromAPI()\n }\n }\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n this.init(this.config)\n if (isWeb()) {\n document.addEventListener('visibilitychange', this.visibilityChangeHandler)\n }\n }\n\n /**\n * @ignore\n */\n destroy() {\n if (isWeb()) {\n document.removeEventListener('visibilitychange', this.visibilityChangeHandler)\n // Remove all other event listeners\n const globalStateKey = getGlobalStateKey(this.config)\n if (frigadeGlobalState[globalStateKey]) {\n frigadeGlobalState[globalStateKey].onFlowStateChangeHandlers = []\n }\n }\n }\n\n /**\n * @ignore\n */\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n if (this.config.userId && !this.config.userId?.startsWith(GUEST_PREFIX)) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n }),\n })\n }\n await this.refreshStateFromAPI()\n })()\n\n return this.initPromise\n }\n\n /**\n * Set the current user.\n * @param userId\n * @param properties\n */\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.config = { ...this.config, userId }\n await this.initIfNeeded()\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Set the group for the current user.\n * @param groupId\n * @param properties\n */\n public async group(groupId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.groupId = groupId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n properties,\n }),\n })\n await this.resync()\n }\n\n /**\n * Track an event for the current user (and group if set).\n * @param event\n * @param properties\n */\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n if (!event) {\n console.error('Event name is required to track an event')\n return\n }\n if (this.config.userId && this.config.groupId) {\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n } else if (this.config.userId) {\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n events: [\n {\n event,\n properties,\n },\n ],\n }),\n })\n }\n await this.resync()\n }\n\n /**\n * @ignore\n */\n public isReady(): boolean {\n return Boolean(this.config.__instanceId && this.config.apiKey && this.initPromise)\n }\n\n /**\n * Get a Flow by its ID.\n * @param flowId\n */\n public async getFlow(flowId: string) {\n await this.initIfNeeded()\n\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n public async getFlows() {\n await this.initIfNeeded()\n return this.flows\n }\n\n /**\n * Reload the current state of the flows by calling the Frigade API.\n * This will trigger all event handlers.\n */\n public async reload() {\n resetAllLocalStorage()\n clearCache()\n await this.refreshStateFromAPI()\n this.initPromise = null\n await this.init(this.config)\n // Trigger all event handlers\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n private async resync() {\n this.initPromise = null\n await this.init(this.config)\n this.flows.forEach((flow) => {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n })\n }\n\n /**\n * Event handler that captures all changes that happen to the state of the Flows.\n * @param handler\n */\n public async onStateChange(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers.push(handler)\n }\n\n /**\n * Returns true if the JS SDK failed to connect to the Frigade API.\n */\n hasFailedToLoad() {\n return this.hasFailed\n }\n\n /**\n * Removes the given handler from the list of event handlers.\n * @param handler\n */\n public async removeStateChangeHandler(handler: (flow: Flow, previousFlow?: Flow) => void) {\n await this.initIfNeeded()\n this.getGlobalState().onFlowStateChangeHandlers =\n this.getGlobalState().onFlowStateChangeHandlers.filter((h) => h !== handler)\n }\n\n /**\n * @ignore\n */\n private async initIfNeeded() {\n if (this.initPromise !== null) {\n return this.initPromise\n } else {\n return this.init(this.config)\n }\n }\n\n /**\n * @ignore\n */\n private async refreshStateFromAPI(): Promise<void> {\n const globalStateKey = getGlobalStateKey(this.config)\n\n if (!frigadeGlobalState[globalStateKey]) {\n const that = this\n\n let validator = {\n set: function (target: any, key: any, value: any) {\n if (target[key]) {\n const previousState = target[key] as StatefulFlow\n const newState = value as StatefulFlow\n if (JSON.stringify(previousState) !== JSON.stringify(newState)) {\n that.triggerEventHandlers(newState, previousState)\n }\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshStateFromAPI: async () => {},\n rulesGraph: new RulesGraph({\n graph: {},\n ruleOrder: [],\n }),\n flowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n variables: {},\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshStateFromAPI = async (\n overrideFlowStateRaw?: FlowStates\n ) => {\n if (this.config.__readOnly) {\n return\n }\n\n const flowStateRaw: FlowStates = overrideFlowStateRaw\n ? overrideFlowStateRaw\n : await this.fetch(\n `/flowStates?userId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId ? `&groupId=${encodeURIComponent(this.config.groupId)}` : ''\n }`\n )\n\n // TODO: should this also take in order and check if it has changed?\n frigadeGlobalState[globalStateKey].rulesGraph.ingestGraphData(\n flowStateRaw.ruleGraph?.graph ?? {}\n )\n\n if (flowStateRaw && flowStateRaw.eligibleFlows) {\n flowStateRaw.eligibleFlows.forEach((statefulFlow) => {\n frigadeGlobalState[globalStateKey].flowStates[statefulFlow.flowSlug] = statefulFlow\n if (!this.flows.find((flow) => flow.id == statefulFlow.flowSlug)) {\n this.flows.push(\n new Flow({\n config: this.config,\n id: statefulFlow.flowSlug,\n })\n )\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshStateFromAPI()\n }\n\n /**\n * @ignore\n */\n private mockFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].flowStates[flowId] = {\n flowSlug: flowId,\n flowName: parsed?.name ?? flowId,\n flowType: parsed?.type ?? 'CHECKLIST',\n data: {\n ...parsed,\n steps: (parsed?.steps ?? []).map((step: any): StatefulStep => {\n return {\n id: step.id,\n $state: {\n completed: false,\n started: false,\n visible: true,\n blocked: false,\n },\n ...step,\n }\n }),\n },\n $state: {\n currentStepId: null,\n currentStepIndex: -1,\n completed: false,\n started: false,\n skipped: false,\n visible: true,\n },\n } as StatefulFlow\n\n this.flows.push(\n new Flow({\n config: this.config,\n id: flowId,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(newState: StatefulFlow, previousState?: StatefulFlow) {\n if (newState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousState.flowSlug) {\n this.getGlobalState().onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlow = this.getGlobalState().previousFlows.get(flow.id)\n flow.resyncState(newState)\n handler(flow, lastFlow)\n this.getGlobalState().previousFlows.set(flow.id, cloneFlow(flow))\n })\n }\n })\n }\n }\n}\n","import type { Flow } from './flow'\n\nimport { RulesGraphNode } from './rules-graph'\n\nexport enum TriggerType {\n MANUAL = 'MANUAL',\n AUTOMATIC = 'AUTOMATIC',\n}\n\nexport enum FlowStatus {\n DRAFT = 'DRAFT',\n ACTIVE = 'ACTIVE',\n ARCHIVED = 'ARCHIVED',\n}\n\nexport type StepAction =\n | 'flow.back'\n | 'flow.complete'\n | 'flow.forward'\n | 'flow.restart'\n | 'flow.skip'\n | 'flow.start'\n | 'step.complete'\n | 'step.reset'\n | 'step.start'\n | false\n\nexport interface FlowStep extends StatefulStep {\n /**\n * Order of the step in the Flow.\n */\n order: number\n\n /**\n * Name of the step when shown in a list view.\n */\n stepName?: string\n\n /**\n * Title of the step.\n */\n title?: string\n\n /**\n * Subtitle of the step.\n */\n subtitle?: string\n\n /**\n * Config for the primary button in this step\n */\n primaryButton?: {\n /**\n * Primary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Primary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Primary button title. If omitted, the primary button will not be shown.\n */\n title?: string\n\n /**\n * Primary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use primaryButton.title instead\n * @description Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * @deprecated Use primaryButton.uri instead\n * @description Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * @deprecated Use primaryButton.target instead\n * @description Primary button URI target (defaults to _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Config for the secondary button in this step\n */\n secondaryButton?: {\n /**\n * Secondary button action. (defaults to step.complete)\n */\n action?: StepAction\n\n /**\n * Secondary button URI target (defaults to _self).\n */\n target?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n title?: string\n\n /**\n * Secondary button URI.\n */\n uri?: string\n }\n\n /**\n * @deprecated Use secondaryButton.title instead\n * @description Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * @deprecated Use secondaryButton.uri instead\n * @description Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * @deprecated Use secondaryButton.target instead\n * @description Secondary button URI target (defaults to _self)\n */\n secondaryButtonUriTarget?: string\n\n /**\n * Text on button if a back button is present.\n */\n backButtonTitle?: string\n\n /**\n * If true, the step will be marked as completed when the secondary button is clicked.\n */\n skippable?: boolean\n\n /**\n * Video url to be shown for components supporting video.\n */\n videoUri?: string\n\n /**\n * Image url to be shown for components supporting image.\n */\n imageUri?: string | null\n\n /**\n * Automatically mark the step as completed when the primary button is clicked. Default is false.\n */\n autoMarkCompleted?: boolean\n\n /**\n * @ignore\n */\n props?: any\n /**\n * Criteria that needs to be met for the step to complete.\n * Completion criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be completed.\n */\n completionCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to start.\n * Start criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be started.\n */\n startCriteria?: string\n\n /**\n * Criteria that needs to be met for the step to be visible.\n * Visibility criteria uses Frigade's [Targeting Engine](https://docs.frigade.com/v2/platform/targeting) to determine if the step should be visible.\n */\n visibilityCriteria?: string\n\n /**\n * Progress if the step is tied to another Frigade Flow through completionCriteria.\n */\n progress?: number\n\n /**\n * Whether the step is dismissible (for instance, tooltips or other non-essential steps).\n */\n dismissible?: boolean\n\n /**\n * Any other additional props defined in the YAML config.\n */\n [x: string | number | symbol]: unknown\n\n /**\n * Marks the step started.\n */\n start: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Marks the step completed.\n */\n complete: (properties?: Record<string | number, any>) => Promise<void>\n\n /**\n * Resets the step (useful for undoing a finished step).\n */\n reset: () => Promise<void>\n\n /**\n * Event handler called when this step's state changes.\n */\n onStateChange: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Removes the given callback from the list of event handlers.\n */\n removeStateChangeHandler: (callback: (step: FlowStep, previousStep?: FlowStep) => void) => void\n\n /**\n * Reference to this step's parent Flow\n */\n flow: Flow\n}\n\nexport interface FrigadeConfig {\n /**\n * Frigade API key. You can find your API key in the Frigade Dashboard under \"Developer\".\n */\n apiKey?: string\n /**\n * API url to use for all requests. Defaults to https://api.frigade.com\n */\n apiUrl?: string\n\n /**\n * User ID to use for all requests. If not provided, a Guest ID will be generated.\n */\n userId?: string\n\n /**\n * Group ID (organization) to use for all requests.\n */\n groupId?: string\n\n /**\n * @ignore Internal use only.\n * If enabled, Frigade will not send any data to the API. A user's state will be reset on page refresh.\n */\n __readOnly?: boolean\n\n /**\n * @ignore Internal use only.\n * Map of Flow ID to Flow Config for all flows in the app.\n * Configs will have to be provided in serialized JSON format rather than YAML.\n */\n __flowConfigOverrides?: Record<string, string>\n\n /**\n * @ignore Internal use only.\n */\n __instanceId?: string\n}\n\nexport interface StatefulStep {\n id: string\n $state: {\n completed: boolean\n started: boolean\n visible: boolean\n blocked: boolean\n lastActionAt?: Date\n }\n // allow any other properties\n [key: string]: any\n}\n\nexport interface StatefulFlow {\n flowSlug: string\n flowName: string\n flowType: FlowType\n data: {\n steps: StatefulStep[]\n // allow any other properties\n [key: string]: any\n }\n $state: {\n currentStepId: string\n currentStepIndex: number\n completed: boolean\n started: boolean\n skipped: boolean\n visible: boolean\n lastActionAt?: Date\n }\n}\n\nexport interface RulesGraphData {\n ruleOrder: string[]\n graph: Record<string, RulesGraphNode>\n}\n\nexport interface FlowStates {\n eligibleFlows: StatefulFlow[]\n ineligibleFlows: string[]\n ruleGraph?: RulesGraphData\n}\n\nexport enum FlowType {\n ANNOUNCEMENT = 'ANNOUNCEMENT',\n CHECKLIST = 'CHECKLIST',\n FORM = 'FORM',\n TOUR = 'TOUR',\n SUPPORT = 'SUPPORT',\n CUSTOM = 'CUSTOM',\n BANNER = 'BANNER',\n EMBEDDED_TIP = 'EMBEDDED_TIP',\n NPS_SURVEY = 'NPS_SURVEY',\n SURVEY = 'SURVEY',\n CARD = 'CARD',\n}\n\nexport type FlowActionType =\n | 'STARTED_STEP'\n | 'COMPLETED_STEP'\n | 'NOT_STARTED_STEP'\n | 'STARTED_FLOW'\n | 'COMPLETED_FLOW'\n | 'SKIPPED_FLOW'\n | 'NOT_STARTED_FLOW'\n\nexport class FlowStateDTO {\n userId: string\n groupId?: string\n flowSlug: string\n stepId?: string\n data?: any\n actionType: FlowActionType\n createdAt?: Date\n}\n"],"mappings":";0nBAAO,IAAMA,EAAiB,SCC9B,OAAS,MAAMC,MAAc,OCqBtB,IAAIC,EAAyD,CAAC,EAE9D,SAASC,EAAkBC,EAAuC,CAxBzE,IAAAC,EAAAC,EAyBE,MAAO,GAAGF,EAAe,gBAAgBA,EAAe,WAAUC,EAAAD,EAAe,SAAf,KAAAC,EAAyB,OACzFC,EAAAF,EAAe,UAAf,KAAAE,EAA0B,IAE9B,CCxBO,IAAMC,EAAN,KAAgB,CAQrB,YAAYC,EAAuB,CAPnC,KAAO,OAAwB,CAC7B,OAAQ,GACR,OAAQ,oCACR,OAAQC,EAAgB,EACxB,aAAc,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,CAAC,CACtD,EAGE,IAAMC,EAAiB,OAAO,YAAY,OAAO,QAAQF,CAAM,EAAE,OAAO,CAAC,CAACG,EAAGC,CAAC,IAAMA,GAAK,IAAI,CAAC,EAE9F,KAAK,OAASC,IAAA,GACT,KAAK,QACLH,EAEP,CAKa,MAAMI,EAAcC,EAA4B,QAAAC,EAAA,sBAC3D,OAAI,KAAK,OAAO,WACPC,EAAiB,EAGnBC,EAAc,KAAK,UAAUJ,CAAI,EAAGD,IAAA,CACzC,UAAW,IACPE,GAAA,KAAAA,EAAW,CAAC,GACbI,EAAW,KAAK,OAAO,MAAM,EACjC,CACH,GAEQ,UAAUL,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,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IA6SAC,OACVA,EAAA,aAAe,eACfA,EAAA,UAAY,YACZA,EAAA,KAAO,OACPA,EAAA,KAAO,OACPA,EAAA,QAAU,UACVA,EAAA,OAAS,SACTA,EAAA,OAAS,SACTA,EAAA,aAAe,eACfA,EAAA,WAAa,aACbA,EAAA,OAAS,SACTA,EAAA,KAAO,OAXGA,OAAA","names":["VERSION_NUMBER","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","_a","_b","Fetchable","config","generateGuestId","filteredConfig","_","v","__spreadValues","path","options","__async","getEmptyResponse","gracefulFetch","getHeaders","pathPrefix","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","id","visible","overrideStatefulFlow","_a","_b","statefulFlow","step","index","stepObj","existingStep","__spreadProps","__spreadValues","newSteps","properties","__async","thisStep","copy","clone","STARTED_STEP","isLastStep","COMPLETED_STEP","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","STARTED_FLOW","COMPLETED_FLOW","SKIPPED_FLOW","nextStep","NOT_STARTED_FLOW","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","key","action","data","stepId","date","flowStatesRaw","callback","prevFlow","cloneFlow","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","LOCAL_STORAGE_PREFIX","cloneFlow","flow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","CallQueue","call","now","item","callQueue","gracefulFetch","url","options","__async","_a","getEmptyResponse","lastCallDataKey","response","error","body","e","generateGuestId","guestId","getLocalStorage","GUEST_KEY","GUEST_PREFIX","uuidv4","setLocalStorage","isWeb","RulesGraph","graphData","_a","_b","flowId","callback","currentNode","nodeId","originId","ruleId","node","result","edge","ancestor","Frigade","Fetchable","apiKey","config","__spreadValues","__async","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","__spreadProps","groupId","event","flowId","flow","resetAllLocalStorage","clearCache","handler","lastFlow","cloneFlow","h","that","validator","target","key","value","previousState","newState","RulesGraph","overrideFlowStateRaw","_b","flowStateRaw","statefulFlow","Flow","_c","parsed","step","TriggerType","FlowStatus","FlowType"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frigade/js",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "description": "The official Javascript SDK for Frigade.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",