@frigade/js 0.2.35 → 0.2.36

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 et=Object.create;var U=Object.defineProperty;var it=Object.getOwnPropertyDescriptor;var st=Object.getOwnPropertyNames;var ot=Object.getPrototypeOf,rt=Object.prototype.hasOwnProperty;var at=(s,r)=>{for(var t in r)U(s,t,{get:r[t],enumerable:!0})},B=(s,r,t,e)=>{if(r&&typeof r=="object"||typeof r=="function")for(let i of st(r))!rt.call(s,i)&&i!==t&&U(s,i,{get:()=>r[i],enumerable:!(e=it(r,i))||e.enumerable});return s};var nt=(s,r,t)=>(t=s!=null?et(ot(s)):{},B(r||!s||!s.__esModule?U(t,"default",{value:s,enumerable:!0}):t,s)),lt=s=>B(U({},"__esModule",{value:!0}),s);var St={};at(St,{Flow:()=>F,FlowStatus:()=>P,FlowType:()=>G,Frigade:()=>L,TriggerType:()=>v});module.exports=lt(St);var v=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(v||{}),P=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(P||{}),G=(p=>(p.ANNOUNCEMENT="ANNOUNCEMENT",p.CHECKLIST="CHECKLIST",p.FORM="FORM",p.TOUR="TOUR",p.SUPPORT="SUPPORT",p.CUSTOM="CUSTOM",p.BANNER="BANNER",p.EMBEDDED_TIP="EMBEDDED_TIP",p.NPS_SURVEY="NPS_SURVEY",p.SURVEY="SURVEY",p.CARD="CARD",p))(G||{});var $="0.2.20";var Q=nt(require("cross-fetch"),1),Z=require("uuid");var c={};function R(s){return`${s.__instanceId}-${s.apiKey}:${s.userId??""}:${s.groupId??""}`}var E=class{constructor(r){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:X(),__instanceId:Math.random().toString(36).substring(7)};let t=Object.fromEntries(Object.entries(r).filter(([e,i])=>i!=null));this.config={...this.config,...t}}async fetch(r,t){return this.config.__readOnly?u():Y(`${this.config.apiUrl}${r}`,{keepalive:!0,...t??{},...j(this.config.apiKey)})}getGlobalState(){let r=R(this.config);if(!c[r])throw new Error("Frigade not initialized");return c[r]}};var F=class extends E{constructor(t,e){super(t);this.isVisible=!1;this.isTargeted=!1;this.lastStepUpdate=new Map;this.lastUsedVariables={};this.flowDataRaw=e,this.initFromRawData(e)}reload(){this.initFromRawData(this.flowDataRaw)}initFromRawData(t){let e=JSON.parse(t.data),i=e.steps??e.data??[];this.id=t.slug,this.rawData=t,this.configYmlAsJson=e,this.title=this.configYmlAsJson.title,this.subtitle=this.configYmlAsJson.subtitle;let o=this.getUserFlowState();if(!o)return;this.userFlowStateRaw=o,this.isCompleted=o.flowState==D,this.isStarted=o.flowState==A,this.isSkipped=o.flowState==N;let g=this.isCompleted||this.isSkipped,n=t.targetingLogic&&o.shouldTrigger===!1;this.isVisible=!g&&!n,this.flowDataRaw.active===!1&&(this.isVisible=!1),this.isTargeted=t.targetingLogic?n===!1:!0;let T=new Map;i.forEach((f,C)=>{let p=o.stepStates[f.id],I={...f,isCompleted:p.actionType==y,isStarted:p.actionType==b,isHidden:p.hidden,isBlocked:p.blocked,lastActionAt:p.lastActionAt?new Date(p.lastActionAt):void 0,order:C};I.start=async l=>{let a=this.steps.get(f.id);if(this.getCurrentStep().id===a.id&&a.isStarted)return;a.isStarted=!0;let S=w(this.getGlobalState().userFlowStates[this.id]);S.stepStates[a.id].actionType=b,S.stepStates[a.id].lastActionAt=new Date().toISOString(),S.lastStepId=a.id,this.getGlobalState().userFlowStates[this.id]=S,a.isCompleted||(await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:a.id,data:l??{},createdAt:new Date().toISOString(),actionType:b})}),await this.refreshUserFlowState());let h=this.getUserFlowState();a.isCompleted=h.stepStates[a.id].actionType==y,a.isStarted=h.stepStates[a.id].actionType==b,a.lastActionAt=new Date},I.complete=async l=>{let a=this.steps.get(f.id);if(a.isCompleted)return;let h=this.getNumberOfCompletedSteps()+1==this.steps.size;a.isCompleted=!0,this.isStarted=!0;let d=w(this.getGlobalState().userFlowStates[this.id]);d.stepStates[a.id].actionType=y,d.stepStates[a.id].lastActionAt=new Date().toISOString(),d.flowState=h?D:A;let O=Array.from(this.steps.keys())[C+1];if(O){d.lastStepId=O,d.stepStates[O].actionType=b;let K=new Date;d.stepStates[O].lastActionAt=K.toISOString(),this.steps.get(O).isStarted=!0,this.steps.get(O).lastActionAt=K}h&&this.optimisticallyMarkFlowCompleted(),this.getGlobalState().userFlowStates[this.id]=d,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:a.id,data:l??{},createdAt:new Date().toISOString(),actionType:y})}),await this.refreshUserFlowState();let V=this.getUserFlowState();a.isCompleted=V.stepStates[a.id].actionType==y,a.isStarted=V.stepStates[a.id].actionType==b,a.lastActionAt=new Date},I.reset=async()=>{let l=this.steps.get(f.id);if(!l.isCompleted)return;l.isCompleted=!1,l.isStarted=!1,l.lastActionAt=void 0;let a=w(this.getGlobalState().userFlowStates[this.id]);a.stepStates[l.id].actionType=H,a.stepStates[l.id].lastActionAt=void 0,a.flowState=A,this.getGlobalState().userFlowStates[this.id]=a,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:l.id,data:{},createdAt:new Date().toISOString(),actionType:H})}),await this.refreshUserFlowState();let S=this.getUserFlowState();l.isCompleted=S.stepStates[l.id].actionType==y,l.isStarted=S.stepStates[l.id].actionType==b,l.lastActionAt=void 0},I.onStateChange=l=>{let a=S=>{if(S.id!==this.id)return;let h=S.steps.get(f.id),d=this.lastStepUpdate.get(l);(h.isCompleted!==(d==null?void 0:d.isCompleted)||h.isStarted!==(d==null?void 0:d.isStarted)||h.isHidden!==(d==null?void 0:d.isHidden)||h.isBlocked!==(d==null?void 0:d.isBlocked))&&(l(h,d??w(h)),this.lastStepUpdate.set(l,w(h)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(l,a),this.getGlobalState().onFlowStateChangeHandlers.push(a)},I.removeStateChangeHandler=l=>{let a=this.getGlobalState().onStepStateChangeHandlerWrappers.get(l);a&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(S=>S!==a))},T.set(f.id,I)}),this.steps=T,Object.keys(this.lastUsedVariables).length>0&&this.applyVariables(this.lastUsedVariables)}async start(t){if(this.isStarted||this.isCompleted)return;this.isStarted=!0;let e=w(this.getGlobalState().userFlowStates[this.id]);e.flowState=A,this.getGlobalState().userFlowStates[this.id]=e,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:A})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}async complete(t){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:D})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}optimisticallyMarkFlowCompleted(){this.isStarted=!0,this.isCompleted=!0;let t=w(this.getGlobalState().userFlowStates[this.id]);t.flowState=D,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}optimisticallyMarkFlowSkipped(){this.isSkipped=!0;let t=w(this.getGlobalState().userFlowStates[this.id]);t.flowState=N,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}async skip(t){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:N})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}async forward(t){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e&&await e.start(t)}async back(t){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&await e.start(t)}async restart(){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:z})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let t=Array.from(this.steps.keys()).find(i=>this.steps.get(i).isCompleted===!1&&this.steps.get(i).isHidden===!1);Array.from(this.steps.keys()).forEach(i=>{var o;this.steps.get(i).isStarted&&this.steps.get(i).lastActionAt&&this.steps.get(i).lastActionAt>(((o=this.steps.get(t))==null?void 0:o.lastActionAt)??new Date(0))&&(t=i)});let e=t??Array.from(this.steps.keys())[0];return this.steps.get(e)}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>!t.isHidden).length}onStateChange(t){let e=(i,o)=>{(i.id===this.id&&(i.isCompleted!==o.isCompleted||i.isStarted!==o.isStarted||i.isSkipped!==o.isSkipped||i.isVisible!==o.isVisible)||JSON.stringify(i.steps)!==JSON.stringify(o.steps))&&t(i,o)};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){let e=i=>{let o=i.match(/\${(.*?)}/g);return o&&o.forEach(g=>{let n=g.replace("${","").replace("}","");t[n]&&(i=i.replace(g,t[n]))}),i};this.title=e(this.title??""),this.subtitle=e(this.subtitle??""),this.steps.forEach(i=>{Object.keys(i).forEach(o=>{typeof i[o]=="string"&&(i[o]=e(i[o]))})}),this.lastUsedVariables=t}getUserFlowState(){return this.getGlobalState().userFlowStates[this.id]}async refreshUserFlowState(){await this.getGlobalState().refreshUserFlowStates()}};var H="NOT_STARTED_STEP",D="COMPLETED_FLOW",N="SKIPPED_FLOW",A="STARTED_FLOW",z="NOT_STARTED_FLOW",y="COMPLETED_STEP",b="STARTED_STEP",dt="frigade-last-call-at-",gt="frigade-last-call-data-",q="frigade-guest-key",k="guest_",pt="get-cache-";var ct=1e3,W="fr-js-";function _(s){let r=new F(s.config,s.rawData);return r.isCompleted=s.isCompleted,r.isStarted=s.isStarted,r.isSkipped=s.isSkipped,r.isVisible=s.isVisible,r.steps=s.steps,r}function w(s){return JSON.parse(JSON.stringify(s))}function j(s){return{headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json","X-Frigade-SDK-Version":$,"X-Frigade-SDK-Platform":"Javascript"}}}function x(s){return m()?window.localStorage.getItem(`${W}${s}`):null}function M(s,r){m()&&window.localStorage.setItem(`${W}${s}`,r)}function J(){Object.keys(c).forEach(s=>{s.startsWith(pt)&&delete c[s]})}function tt(){m()&&Object.keys(window.localStorage).forEach(s=>{s.startsWith(W)&&window.localStorage.removeItem(s)})}async function Y(s,r){let t=dt+s,e=gt+s;if(m()&&r&&r.body&&r.method==="POST"){let o=x(t),g=x(e);if(o&&g&&g==r.body){let n=new Date(o);if(new Date().getTime()-n.getTime()<ct)return u()}M(t,new Date().toISOString()),M(e,r.body),J()}let i;try{i=(0,Q.default)(s,r),i=await i}catch(o){return u(o)}if(!i)return u("Received an empty response");if(i.status>=400)return u(i.statusText);try{if(i.status===204||i.status===201)return u();let o=await i.json();return o.error?u(o.error):o}catch(o){return u(o)}}function u(s){return s&&console.log("Call to Frigade failed",s),{json:()=>({})}}function X(){if(m()){let s=x(q);return s||(s=`${k}${(0,Z.v4)()}`,M(q,s)),s}}function m(){return typeof window<"u"&&typeof window.document<"u"&&typeof window.localStorage<"u"}var L=class extends E{constructor(t,e){super({apiKey:t,...e});this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=async()=>{document.visibilityState==="visible"&&(await this.refreshFlows(),await this.refreshUserFlowStates(),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,_(t))})}))};this.init(this.config),m()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(m()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=R(this.config);c[t]&&(c[t].onFlowStateChangeHandlers=[])}}async init(t){return this.config={...this.config,...t},this.initPromise=(async()=>{var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(k))&&await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})}),await this.refreshUserFlowStates(),await this.refreshFlows()})(),this.initPromise}async identify(t,e){this.config={...this.config,userId:t},await this.initIfNeeded(),await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),await this.resync()}async group(t,e){await this.initIfNeeded(),this.config.groupId=t,await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,properties:e})}),await this.resync()}async track(t,e){if(await this.initIfNeeded(),!t){console.error("Event name is required to track an event");return}this.config.userId&&this.config.groupId?await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,events:[{event:t,properties:e}]})}):this.config.userId&&await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,events:[{event:t,properties:e}]})}),await this.resync()}isReady(){return!!(this.config.__instanceId&&this.config.apiKey&&this.initPromise)}async getFlow(t){return await this.initIfNeeded(),this.flows.find(e=>e.id==t)}async getFlows(){return await this.initIfNeeded(),this.flows}async reload(){tt(),J(),await this.refreshUserFlowStates(),await this.refreshFlows(),this.initPromise=null,await 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,_(t))})})}async resync(){this.initPromise=null,await 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,_(t))})})}async onStateChange(t){await this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)}hasFailedToLoad(){return this.hasFailed}async removeStateChangeHandler(t){await this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)}async initIfNeeded(){return this.initPromise!==null?this.initPromise:this.init(this.config)}async refreshUserFlowStates(){let t=R(this.config);if(!c[t]){let e=this,i={set:function(o,g,n){return o[g]&&o[g].flowState&&(JSON.stringify(o[g].flowState)!==JSON.stringify(n==null?void 0:n.flowState)||JSON.stringify(o[g].stepStates)!==JSON.stringify(n==null?void 0:n.stepStates)||JSON.stringify(o[g].shouldTrigger)!==JSON.stringify(n==null?void 0:n.shouldTrigger))&&e.triggerEventHandlers(o[g]),o[g]=n,!0}};if(c[t]={refreshUserFlowStates:async()=>{},userFlowStates:new Proxy({},i),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockUserFlowStates(t);return}c[t].refreshUserFlowStates=async()=>{if(this.config.__readOnly)return;let o=await this.fetch(`/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`:""}`);o&&o.data?(o.data.forEach(n=>{let T=!1,f=c[t].userFlowStates[n.flowId];f&&f.shouldTrigger==!1&&n.shouldTrigger==!0&&(T=!0),c[t].userFlowStates[n.flowId]=n,T&&this.flows.forEach(C=>{C.id==n.flowId&&(C.reload(),this.triggerEventHandlers(c[t].userFlowStates[C.id]))})}),this.hasFailed=!1):this.hasFailed=!0}}await c[t].refreshUserFlowStates()}async refreshFlows(){if(this.flows=[],this.config.__flowConfigOverrides){this.mockFlowConfigs();return}let t=await this.fetch("/flows");t&&t.data?t.data.forEach(i=>{this.flows.push(new F(this.config,i)),this.getGlobalState().previousFlows.set(i.slug,_(this.flows[this.flows.length-1]))}):this.hasFailed=!0}mockFlowConfigs(){Object.keys(this.config.__flowConfigOverrides).forEach(t=>{this.flows.push(new F(this.config,{id:-1,name:"",description:"",data:this.config.__flowConfigOverrides[t],createdAt:new Date().toISOString(),modifiedAt:new Date().toISOString(),slug:t,targetingLogic:"",type:"CHECKLIST",triggerType:"MANUAL",status:"ACTIVE",version:1,active:!0}))})}mockUserFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var o;let i=JSON.parse(this.config.__flowConfigOverrides[e]);c[t].userFlowStates[e]={flowId:e,flowState:"NOT_STARTED_FLOW",lastStepId:null,userId:this.config.userId,foreignUserId:this.config.userId,stepStates:((o=i==null?void 0:i.steps)==null?void 0:o.reduce((g,n)=>(g[n.id]={stepId:n.id,flowSlug:e,actionType:"NOT_STARTED_STEP",createdAt:new Date().toISOString(),blocked:!1,hidden:!1},g),{}))??{},shouldTrigger:!1}})}async triggerEventHandlers(t){t&&this.flows.forEach(e=>{e.id==t.flowId&&this.getGlobalState().onFlowStateChangeHandlers.forEach(i=>{let o=this.getGlobalState().previousFlows.get(e.id);i(e,o),this.getGlobalState().previousFlows.set(e.id,_(e))})})}};0&&(module.exports={Flow,FlowStatus,FlowType,Frigade,TriggerType});
2
+ var dt=Object.create;var v=Object.defineProperty,gt=Object.defineProperties,ct=Object.getOwnPropertyDescriptor,ht=Object.getOwnPropertyDescriptors,St=Object.getOwnPropertyNames,z=Object.getOwnPropertySymbols,pt=Object.getPrototypeOf,Q=Object.prototype.hasOwnProperty,ft=Object.prototype.propertyIsEnumerable;var q=(i,a,t)=>a in i?v(i,a,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[a]=t,F=(i,a)=>{for(var t in a||(a={}))Q.call(a,t)&&q(i,t,a[t]);if(z)for(var t of z(a))ft.call(a,t)&&q(i,t,a[t]);return i},N=(i,a)=>gt(i,ht(a));var wt=(i,a)=>{for(var t in a)v(i,t,{get:a[t],enumerable:!0})},Z=(i,a,t,e)=>{if(a&&typeof a=="object"||typeof a=="function")for(let s of St(a))!Q.call(i,s)&&s!==t&&v(i,s,{get:()=>a[s],enumerable:!(e=ct(a,s))||e.enumerable});return i};var ut=(i,a,t)=>(t=i!=null?dt(pt(i)):{},Z(a||!i||!i.__esModule?v(t,"default",{value:i,enumerable:!0}):t,i)),Ft=i=>Z(v({},"__esModule",{value:!0}),i);var l=(i,a,t)=>new Promise((e,s)=>{var o=h=>{try{r(t.next(h))}catch(f){s(f)}},d=h=>{try{r(t.throw(h))}catch(f){s(f)}},r=h=>h.done?e(h.value):Promise.resolve(h.value).then(o,d);r((t=t.apply(i,a)).next())});var Ct={};wt(Ct,{Flow:()=>y,FlowStatus:()=>H,FlowType:()=>x,Frigade:()=>k,TriggerType:()=>L});module.exports=Ft(Ct);var L=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(L||{}),H=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(H||{}),x=(S=>(S.ANNOUNCEMENT="ANNOUNCEMENT",S.CHECKLIST="CHECKLIST",S.FORM="FORM",S.TOUR="TOUR",S.SUPPORT="SUPPORT",S.CUSTOM="CUSTOM",S.BANNER="BANNER",S.EMBEDDED_TIP="EMBEDDED_TIP",S.NPS_SURVEY="NPS_SURVEY",S.SURVEY="SURVEY",S.CARD="CARD",S))(x||{});var tt="0.2.20";var rt=ut(require("cross-fetch"),1),nt=require("uuid");var p={};function P(i){var a,t;return`${i.__instanceId}-${i.apiKey}:${(a=i.userId)!=null?a:""}:${(t=i.groupId)!=null?t:""}`}var R=class{constructor(a){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:st(),__instanceId:Math.random().toString(36).substring(7)};let t=Object.fromEntries(Object.entries(a).filter(([e,s])=>s!=null));this.config=F(F({},this.config),t)}fetch(a,t){return l(this,null,function*(){return this.config.__readOnly?b():it(`${this.config.apiUrl}${a}`,F(F({keepalive:!0},t!=null?t:{}),et(this.config.apiKey)))})}getGlobalState(){let a=P(this.config);if(!p[a])throw new Error("Frigade not initialized");return p[a]}};var y=class extends R{constructor(t,e){super(t);this.isVisible=!1;this.isTargeted=!1;this.lastStepUpdate=new Map;this.lastUsedVariables={};this.flowDataRaw=e,this.initFromRawData(e)}reload(){this.initFromRawData(this.flowDataRaw)}initFromRawData(t){var f,O;let e=JSON.parse(t.data),s=(O=(f=e.steps)!=null?f:e.data)!=null?O:[];this.id=t.slug,this.rawData=t,this.configYmlAsJson=e,this.title=this.configYmlAsJson.title,this.subtitle=this.configYmlAsJson.subtitle;let o=this.getUserFlowState();if(!o)return;this.userFlowStateRaw=o,this.isCompleted=o.flowState==G,this.isStarted=o.flowState==D,this.isSkipped=o.flowState==M;let d=this.isCompleted||this.isSkipped,r=t.targetingLogic&&o.shouldTrigger===!1;this.isVisible=!d&&!r,this.flowDataRaw.active===!1&&(this.isVisible=!1),this.isTargeted=t.targetingLogic?r===!1:!0;let h=new Map;s.forEach((S,j)=>{let E=o.stepStates[S.id],A=N(F({},S),{isCompleted:E.actionType==I,isStarted:E.actionType==T,isHidden:E.hidden,isBlocked:E.blocked,lastActionAt:E.lastActionAt?new Date(E.lastActionAt):void 0,order:j});A.start=g=>l(this,null,function*(){let n=this.steps.get(S.id);if(this.getCurrentStep().id===n.id&&n.isStarted)return;n.isStarted=!0;let w=m(this.getGlobalState().userFlowStates[this.id]);w.stepStates[n.id].actionType=T,w.stepStates[n.id].lastActionAt=new Date().toISOString(),w.lastStepId=n.id,this.getGlobalState().userFlowStates[this.id]=w,n.isCompleted||(yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:n.id,data:g!=null?g:{},createdAt:new Date().toISOString(),actionType:T})}),yield this.refreshUserFlowState());let u=this.getUserFlowState();n.isCompleted=u.stepStates[n.id].actionType==I,n.isStarted=u.stepStates[n.id].actionType==T,n.lastActionAt=new Date}),A.complete=g=>l(this,null,function*(){let n=this.steps.get(S.id);if(n.isCompleted)return;let u=this.getNumberOfCompletedSteps()+1==this.steps.size;n.isCompleted=!0,this.isStarted=!0;let c=m(this.getGlobalState().userFlowStates[this.id]);c.stepStates[n.id].actionType=I,c.stepStates[n.id].lastActionAt=new Date().toISOString(),c.flowState=u?G:D;let _=Array.from(this.steps.keys())[j+1];if(_){c.lastStepId=_,c.stepStates[_].actionType=T;let X=new Date;c.stepStates[_].lastActionAt=X.toISOString(),this.steps.get(_).isStarted=!0,this.steps.get(_).lastActionAt=X}u&&this.optimisticallyMarkFlowCompleted(),this.getGlobalState().userFlowStates[this.id]=c,yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:n.id,data:g!=null?g:{},createdAt:new Date().toISOString(),actionType:I})}),yield this.refreshUserFlowState();let Y=this.getUserFlowState();n.isCompleted=Y.stepStates[n.id].actionType==I,n.isStarted=Y.stepStates[n.id].actionType==T,n.lastActionAt=new Date}),A.reset=()=>l(this,null,function*(){let g=this.steps.get(S.id);if(!g.isCompleted)return;g.isCompleted=!1,g.isStarted=!1,g.lastActionAt=void 0;let n=m(this.getGlobalState().userFlowStates[this.id]);n.stepStates[g.id].actionType=W,n.stepStates[g.id].lastActionAt=void 0,n.flowState=D,this.getGlobalState().userFlowStates[this.id]=n,yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:g.id,data:{},createdAt:new Date().toISOString(),actionType:W})}),yield this.refreshUserFlowState();let w=this.getUserFlowState();g.isCompleted=w.stepStates[g.id].actionType==I,g.isStarted=w.stepStates[g.id].actionType==T,g.lastActionAt=void 0}),A.onStateChange=g=>{let n=w=>{if(w.id!==this.id)return;let u=w.steps.get(S.id),c=this.lastStepUpdate.get(g);(u.isCompleted!==(c==null?void 0:c.isCompleted)||u.isStarted!==(c==null?void 0:c.isStarted)||u.isHidden!==(c==null?void 0:c.isHidden)||u.isBlocked!==(c==null?void 0:c.isBlocked))&&(g(u,c!=null?c:m(u)),this.lastStepUpdate.set(g,m(u)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(g,n),this.getGlobalState().onFlowStateChangeHandlers.push(n)},A.removeStateChangeHandler=g=>{let n=this.getGlobalState().onStepStateChangeHandlerWrappers.get(g);n&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(w=>w!==n))},h.set(S.id,A)}),this.steps=h,Object.keys(this.lastUsedVariables).length>0&&this.applyVariables(this.lastUsedVariables)}start(t){return l(this,null,function*(){if(this.isStarted||this.isCompleted)return;this.isStarted=!0;let e=m(this.getGlobalState().userFlowStates[this.id]);e.flowState=D,this.getGlobalState().userFlowStates[this.id]=e,yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t!=null?t:{},createdAt:new Date().toISOString(),actionType:D})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)})}complete(t){return l(this,null,function*(){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t!=null?t:{},createdAt:new Date().toISOString(),actionType:G})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))})}optimisticallyMarkFlowCompleted(){this.isStarted=!0,this.isCompleted=!0;let t=m(this.getGlobalState().userFlowStates[this.id]);t.flowState=G,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}optimisticallyMarkFlowSkipped(){this.isSkipped=!0;let t=m(this.getGlobalState().userFlowStates[this.id]);t.flowState=M,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}skip(t){return l(this,null,function*(){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t!=null?t:{},createdAt:new Date().toISOString(),actionType:M})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))})}forward(t){return l(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e&&(yield e.start(t))})}back(t){return l(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&(yield e.start(t))})}restart(){return l(this,null,function*(){yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:at})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let t=Array.from(this.steps.keys()).find(s=>this.steps.get(s).isCompleted===!1&&this.steps.get(s).isHidden===!1);Array.from(this.steps.keys()).forEach(s=>{var o,d;this.steps.get(s).isStarted&&this.steps.get(s).lastActionAt&&this.steps.get(s).lastActionAt>((d=(o=this.steps.get(t))==null?void 0:o.lastActionAt)!=null?d:new Date(0))&&(t=s)});let e=t!=null?t:Array.from(this.steps.keys())[0];return this.steps.get(e)}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>!t.isHidden).length}onStateChange(t){let e=(s,o)=>{(s.id===this.id&&(s.isCompleted!==o.isCompleted||s.isStarted!==o.isStarted||s.isSkipped!==o.isSkipped||s.isVisible!==o.isVisible)||JSON.stringify(s.steps)!==JSON.stringify(o.steps))&&t(s,o)};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(s=>s!==e))}applyVariables(t){var s,o;let e=d=>{let r=d.match(/\${(.*?)}/g);return r&&r.forEach(h=>{let f=h.replace("${","").replace("}","");t[f]&&(d=d.replace(h,t[f]))}),d};this.title=e((s=this.title)!=null?s:""),this.subtitle=e((o=this.subtitle)!=null?o:""),this.steps.forEach(d=>{Object.keys(d).forEach(r=>{typeof d[r]=="string"&&(d[r]=e(d[r]))})}),this.lastUsedVariables=t}getUserFlowState(){return this.getGlobalState().userFlowStates[this.id]}refreshUserFlowState(){return l(this,null,function*(){yield this.getGlobalState().refreshUserFlowStates()})}};var W="NOT_STARTED_STEP",G="COMPLETED_FLOW",M="SKIPPED_FLOW",D="STARTED_FLOW",at="NOT_STARTED_FLOW",I="COMPLETED_STEP",T="STARTED_STEP",bt="frigade-last-call-at-",mt="frigade-last-call-data-",ot="frigade-guest-key",K="guest_",yt="get-cache-";var Tt=1e3,B="fr-js-";function U(i){let a=new y(i.config,i.rawData);return a.isCompleted=i.isCompleted,a.isStarted=i.isStarted,a.isSkipped=i.isSkipped,a.isVisible=i.isVisible,a.steps=i.steps,a}function m(i){return JSON.parse(JSON.stringify(i))}function et(i){return{headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","X-Frigade-SDK-Version":tt,"X-Frigade-SDK-Platform":"Javascript"}}}function J(i){return C()?window.localStorage.getItem(`${B}${i}`):null}function V(i,a){C()&&window.localStorage.setItem(`${B}${i}`,a)}function $(){Object.keys(p).forEach(i=>{i.startsWith(yt)&&delete p[i]})}function lt(){C()&&Object.keys(window.localStorage).forEach(i=>{i.startsWith(B)&&window.localStorage.removeItem(i)})}function it(i,a){return l(this,null,function*(){let t=bt+i,e=mt+i;if(C()&&a&&a.body&&a.method==="POST"){let o=J(t),d=J(e);if(o&&d&&d==a.body){let r=new Date(o);if(new Date().getTime()-r.getTime()<Tt)return b()}V(t,new Date().toISOString()),V(e,a.body),$()}let s;try{s=(0,rt.default)(i,a),s=yield s}catch(o){return b(o)}if(!s)return b("Received an empty response");if(s.status>=400)return b(s.statusText);try{if(s.status===204||s.status===201)return b();let o=yield s.json();return o.error?b(o.error):o}catch(o){return b(o)}})}function b(i){return i&&console.log("Call to Frigade failed",i),{json:()=>({})}}function st(){if(C()){let i=J(ot);return i||(i=`${K}${(0,nt.v4)()}`,V(ot,i)),i}}function C(){return typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.localStorage!="undefined"}var k=class extends R{constructor(t,e){super(F({apiKey:t},e));this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=()=>l(this,null,function*(){document.visibilityState==="visible"&&(yield this.refreshFlows(),yield this.refreshUserFlowStates(),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let s=this.getGlobalState().previousFlows.get(t.id);e(t,s),this.getGlobalState().previousFlows.set(t.id,U(t))})}))});this.init(this.config),C()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(C()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=P(this.config);p[t]&&(p[t].onFlowStateChangeHandlers=[])}}init(t){return l(this,null,function*(){return this.config=F(F({},this.config),t),this.initPromise=(()=>l(this,null,function*(){var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(K))&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})})),yield this.refreshUserFlowStates(),yield this.refreshFlows()}))(),this.initPromise})}identify(t,e){return l(this,null,function*(){this.config=N(F({},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 l(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 l(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 l(this,null,function*(){return yield this.initIfNeeded(),this.flows.find(e=>e.id==t)})}getFlows(){return l(this,null,function*(){return yield this.initIfNeeded(),this.flows})}reload(){return l(this,null,function*(){lt(),$(),yield this.refreshUserFlowStates(),yield this.refreshFlows(),this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let s=this.getGlobalState().previousFlows.get(t.id);e(t,s),this.getGlobalState().previousFlows.set(t.id,U(t))})})})}resync(){return l(this,null,function*(){this.initPromise=null,yield this.init(this.config),this.flows.forEach(t=>{this.getGlobalState().onFlowStateChangeHandlers.forEach(e=>{let s=this.getGlobalState().previousFlows.get(t.id);e(t,s),this.getGlobalState().previousFlows.set(t.id,U(t))})})})}onStateChange(t){return l(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)})}hasFailedToLoad(){return this.hasFailed}removeStateChangeHandler(t){return l(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)})}initIfNeeded(){return l(this,null,function*(){return this.initPromise!==null?this.initPromise:this.init(this.config)})}refreshUserFlowStates(){return l(this,null,function*(){let t=P(this.config);if(!p[t]){let e=this,s={set:function(o,d,r){return o[d]&&o[d].flowState&&(JSON.stringify(o[d].flowState)!==JSON.stringify(r==null?void 0:r.flowState)||JSON.stringify(o[d].stepStates)!==JSON.stringify(r==null?void 0:r.stepStates)||JSON.stringify(o[d].shouldTrigger)!==JSON.stringify(r==null?void 0:r.shouldTrigger))&&e.triggerEventHandlers(o[d]),o[d]=r,!0}};if(p[t]={refreshUserFlowStates:()=>l(this,null,function*(){}),userFlowStates:new Proxy({},s),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockUserFlowStates(t);return}p[t].refreshUserFlowStates=()=>l(this,null,function*(){if(this.config.__readOnly)return;let o=yield this.fetch(`/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`:""}`);o&&o.data?(o.data.forEach(r=>{let h=!1,f=p[t].userFlowStates[r.flowId];f&&f.shouldTrigger==!1&&r.shouldTrigger==!0&&(h=!0),p[t].userFlowStates[r.flowId]=r,h&&this.flows.forEach(O=>{O.id==r.flowId&&(O.reload(),this.triggerEventHandlers(p[t].userFlowStates[O.id]))})}),this.hasFailed=!1):this.hasFailed=!0})}yield p[t].refreshUserFlowStates()})}refreshFlows(){return l(this,null,function*(){if(this.flows=[],this.config.__flowConfigOverrides){this.mockFlowConfigs();return}let t=yield this.fetch("/flows");t&&t.data?t.data.forEach(s=>{this.flows.push(new y(this.config,s)),this.getGlobalState().previousFlows.set(s.slug,U(this.flows[this.flows.length-1]))}):this.hasFailed=!0})}mockFlowConfigs(){Object.keys(this.config.__flowConfigOverrides).forEach(t=>{this.flows.push(new y(this.config,{id:-1,name:"",description:"",data:this.config.__flowConfigOverrides[t],createdAt:new Date().toISOString(),modifiedAt:new Date().toISOString(),slug:t,targetingLogic:"",type:"CHECKLIST",triggerType:"MANUAL",status:"ACTIVE",version:1,active:!0}))})}mockUserFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var o,d;let s=JSON.parse(this.config.__flowConfigOverrides[e]);p[t].userFlowStates[e]={flowId:e,flowState:"NOT_STARTED_FLOW",lastStepId:null,userId:this.config.userId,foreignUserId:this.config.userId,stepStates:(d=(o=s==null?void 0:s.steps)==null?void 0:o.reduce((r,h)=>(r[h.id]={stepId:h.id,flowSlug:e,actionType:"NOT_STARTED_STEP",createdAt:new Date().toISOString(),blocked:!1,hidden:!1},r),{}))!=null?d:{},shouldTrigger:!1}})}triggerEventHandlers(t){return l(this,null,function*(){t&&this.flows.forEach(e=>{e.id==t.flowId&&this.getGlobalState().onFlowStateChangeHandlers.forEach(s=>{let o=this.getGlobalState().previousFlows.get(e.id);s(e,o),this.getGlobalState().previousFlows.set(e.id,U(e))})})})}};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/types.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export { Frigade } from './core/frigade'\nexport { Flow } from './core/flow'\nexport { FlowStatus, FlowStep, FrigadeConfig, FlowType, TriggerType } from './core/types'\n","export interface FlowDataRaw {\n id: number\n name: string\n description: string\n data: string\n createdAt: string\n modifiedAt: string\n slug: string\n targetingLogic: string\n type: FlowType\n triggerType: TriggerType\n status: FlowStatus\n version: number\n active: boolean\n}\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 interface FlowStep {\n /**\n * Unique identifier for the step.\n */\n id: string\n\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 * Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * Primary button URI target (either _blank or _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * Secondary button URI target (either _blank or _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 * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isCompleted: boolean\n\n /**\n * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isStarted: boolean\n\n /**\n * Whether the step is blocked (can't be accessed yet) based on `startCriteria`.\n */\n isBlocked: boolean\n\n /**\n * Whether the step is hidden (not shown in the list view) based on `visibilityCriteria`.\n */\n isHidden: boolean\n\n /**\n * Last state update.\n */\n lastActionAt?: Date\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 for this given 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\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 UserFlowState {\n flowId: string\n flowState: 'COMPLETED_FLOW' | 'STARTED_FLOW' | 'SKIPPED_FLOW' | 'NOT_STARTED_FLOW'\n lastStepId: string\n userId: string\n foreignUserId: string\n stepStates: Record<string, UserFlowStepState>\n shouldTrigger: boolean\n}\n\nexport interface UserFlowStepState {\n stepId: string\n flowSlug: string\n actionType: 'NOT_STARTED_STEP' | 'STARTED_STEP' | 'COMPLETED_STEP'\n createdAt: string\n blocked: boolean\n hidden: boolean\n lastActionAt?: string\n}\n\nexport interface InternalConfig {\n apiKey: string\n userId?: string\n organizationId?: string\n __instanceId?: string\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","export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\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'\nexport type StepActionType = 'STARTED_STEP' | 'COMPLETED_STEP' | 'NOT_STARTED_STEP'\nexport type UserFlowStatus = 'NOT_STARTED_FLOW' | 'STARTED_FLOW' | 'COMPLETED_FLOW' | 'SKIPPED_FLOW'\nconst LAST_POST_CALL_AT = 'frigade-last-call-at-'\nconst LAST_POST_CALL_DATA = 'frigade-last-call-data-'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst GET_CACHE_TTL_MS = 1000\nconst POST_CACHE_TTL_MS = 1000\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow(flow.config, flow.rawData)\n newFlow.isCompleted = flow.isCompleted\n newFlow.isStarted = flow.isStarted\n newFlow.isSkipped = flow.isSkipped\n newFlow.isVisible = flow.isVisible\n newFlow.steps = flow.steps\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\nexport async function gracefulFetch(url: string, options: any) {\n const lastCallAtKey = LAST_POST_CALL_AT + url\n const lastCallDataKey = LAST_POST_CALL_DATA + url\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCall = getLocalStorage(lastCallAtKey)\n const lastCallData = getLocalStorage(lastCallDataKey)\n if (lastCall && lastCallData && lastCallData == options.body) {\n const lastCallDate = new Date(lastCall)\n const now = new Date()\n const diff = now.getTime() - lastCallDate.getTime()\n // Throttle consecutive POST calls to 1 second\n if (diff < POST_CACHE_TTL_MS) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n clearCache()\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 || response.status === 201) {\n return getEmptyResponse()\n }\n\n const body = await response.json()\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 { FlowStep, FrigadeConfig, UserFlowState } from '../core/types'\nimport { Flow } from '../core/flow'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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}\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(36).substring(7),\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.config.apiUrl}${path}`, {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\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 not initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import { FlowDataRaw, FlowStep, FrigadeConfig, UserFlowState } from './types'\nimport {\n clone,\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'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object.\n * @ignore\n */\n public configYmlAsJson: any\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: FlowDataRaw\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 * Whether the Flow is visible to the user based on the current user/group's state.\n */\n public isVisible: boolean = false\n /**\n * Whether the Flow targeting logic/audience matches the current user/group.\n */\n public isTargeted: boolean = false\n /**\n * @ignore\n */\n private readonly flowDataRaw: FlowDataRaw\n /**\n * @ignore\n */\n private userFlowStateRaw?: UserFlowState\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n /**\n * @ignore\n */\n private lastUsedVariables = {}\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private initFromRawData(flowDataRaw: FlowDataRaw) {\n const flowDataYml = JSON.parse(flowDataRaw.data)\n const steps = flowDataYml.steps ?? flowDataYml.data ?? []\n this.id = flowDataRaw.slug\n this.rawData = flowDataRaw\n this.configYmlAsJson = flowDataYml\n this.title = this.configYmlAsJson.title\n this.subtitle = this.configYmlAsJson.subtitle\n\n const userFlowState = this.getUserFlowState()\n if (!userFlowState) {\n return\n }\n this.userFlowStateRaw = userFlowState\n\n this.isCompleted = userFlowState.flowState == COMPLETED_FLOW\n this.isStarted = userFlowState.flowState == STARTED_FLOW\n this.isSkipped = userFlowState.flowState == SKIPPED_FLOW\n const hasCompleted = this.isCompleted || this.isSkipped\n const targetingShouldHideFlow =\n flowDataRaw.targetingLogic && userFlowState.shouldTrigger === false\n this.isVisible = !hasCompleted && !targetingShouldHideFlow\n if (this.flowDataRaw.active === false) {\n this.isVisible = false\n }\n this.isTargeted = !flowDataRaw.targetingLogic ? true : targetingShouldHideFlow === false\n\n const newSteps = new Map<string, FlowStep>()\n\n steps.forEach((step, index) => {\n const userFlowStateStep = userFlowState.stepStates[step.id]\n const stepObj = {\n ...step,\n isCompleted: userFlowStateStep.actionType == COMPLETED_STEP,\n isStarted: userFlowStateStep.actionType == STARTED_STEP,\n isHidden: userFlowStateStep.hidden,\n isBlocked: userFlowStateStep.blocked,\n lastActionAt: userFlowStateStep.lastActionAt\n ? new Date(userFlowStateStep.lastActionAt)\n : undefined,\n order: index,\n } as FlowStep\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.isStarted) {\n return\n }\n\n thisStep.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.lastStepId = thisStep.id\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n if (!thisStep.isCompleted) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n await this.refreshUserFlowState()\n }\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.isCompleted) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n\n thisStep.isCompleted = true\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n\n copy.stepStates[thisStep.id].actionType = COMPLETED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.flowState = isLastStep ? COMPLETED_FLOW : STARTED_FLOW\n\n const nextStepId = Array.from(this.steps.keys())[index + 1]\n if (nextStepId) {\n copy.lastStepId = nextStepId\n copy.stepStates[nextStepId].actionType = STARTED_STEP\n const lastAction = new Date()\n copy.stepStates[nextStepId].lastActionAt = lastAction.toISOString()\n this.steps.get(nextStepId).isStarted = true\n this.steps.get(nextStepId).lastActionAt = lastAction\n }\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n // if all steps are now completed, mark flow completed\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.isCompleted) {\n return\n }\n\n thisStep.isCompleted = false\n thisStep.isStarted = false\n thisStep.lastActionAt = undefined\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = NOT_STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = undefined\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = undefined\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.isCompleted !== previousStep?.isCompleted ||\n thisStep.isStarted !== previousStep?.isStarted ||\n thisStep.isHidden !== previousStep?.isHidden ||\n thisStep.isBlocked !== previousStep?.isBlocked\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)\n })\n this.steps = newSteps\n // Check if empty object\n if (Object.keys(this.lastUsedVariables).length > 0) {\n this.applyVariables(this.lastUsedVariables)\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\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n this.isStarted = true\n this.isCompleted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = COMPLETED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n this.isSkipped = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = SKIPPED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\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\n this.optimisticallyMarkFlowSkipped()\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: SKIPPED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 }\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 await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 let maybeCurrentStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false && this.steps.get(key).isHidden === false\n )\n Array.from(this.steps.keys()).forEach((key) => {\n if (\n this.steps.get(key).isStarted &&\n this.steps.get(key).lastActionAt &&\n this.steps.get(key).lastActionAt >\n (this.steps.get(maybeCurrentStepId)?.lastActionAt ?? new Date(0))\n ) {\n maybeCurrentStepId = key\n }\n })\n\n const currentStepId = maybeCurrentStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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.isCompleted).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.isHidden).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 (\n (flow.id === this.id &&\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 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.lastUsedVariables = variables\n }\n\n /**\n * @ignore\n */\n private getUserFlowState(): UserFlowState {\n const userFlowStates = this.getGlobalState().userFlowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private async refreshUserFlowState() {\n await this.getGlobalState().refreshUserFlowStates()\n }\n}\n","import {\n FlowDataRaw,\n FlowStatus,\n FlowType,\n FrigadeConfig,\n TriggerType,\n UserFlowState,\n} 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'\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.refreshFlows()\n await this.refreshUserFlowStates()\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\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.refreshUserFlowStates()\n await this.refreshFlows()\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.refreshUserFlowStates()\n await this.refreshFlows()\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 refreshUserFlowStates(): 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 (\n target[key] &&\n target[key].flowState &&\n (JSON.stringify(target[key].flowState) !== JSON.stringify(value?.flowState) ||\n JSON.stringify(target[key].stepStates) !== JSON.stringify(value?.stepStates) ||\n JSON.stringify(target[key].shouldTrigger) !== JSON.stringify(value?.shouldTrigger))\n ) {\n that.triggerEventHandlers(target[key])\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshUserFlowStates: async () => {},\n userFlowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockUserFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n if (this.config.__readOnly) {\n return\n }\n\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId\n ? `&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`\n : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n let shouldReload = false\n const before = frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId]\n\n // Special case: for flows that show up based on targeting logic/rules, we need to check if the flow should be triggered\n if (before && before.shouldTrigger == false && userFlowState.shouldTrigger == true) {\n shouldReload = true\n }\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n if (shouldReload) {\n this.flows.forEach((flow) => {\n if (flow.id == userFlowState.flowId) {\n flow.reload()\n this.triggerEventHandlers(\n frigadeGlobalState[globalStateKey].userFlowStates[flow.id]\n )\n }\n })\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n /**\n * @ignore\n */\n private async refreshFlows() {\n this.flows = []\n\n if (this.config.__flowConfigOverrides) {\n this.mockFlowConfigs()\n return\n }\n\n const flowDataRaw = await this.fetch('/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowDatas = flowDataRaw.data as FlowDataRaw[]\n flowDatas.forEach((flowData) => {\n this.flows.push(new Flow(this.config, flowData))\n this.getGlobalState().previousFlows.set(\n flowData.slug,\n cloneFlow(this.flows[this.flows.length - 1])\n )\n })\n } else {\n this.hasFailed = true\n }\n }\n\n /**\n * @ignore\n */\n private mockFlowConfigs() {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n this.flows.push(\n new Flow(this.config, {\n id: -1,\n name: '',\n description: '',\n data: this.config.__flowConfigOverrides[flowId],\n createdAt: new Date().toISOString(),\n modifiedAt: new Date().toISOString(),\n slug: flowId,\n targetingLogic: '',\n type: FlowType.CHECKLIST,\n triggerType: TriggerType.MANUAL,\n status: FlowStatus.ACTIVE,\n version: 1,\n active: true,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private mockUserFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].userFlowStates[flowId] = {\n flowId,\n flowState: 'NOT_STARTED_FLOW',\n lastStepId: null,\n userId: this.config.userId,\n foreignUserId: this.config.userId,\n stepStates:\n parsed?.steps?.reduce((acc, step) => {\n acc[step.id] = {\n stepId: step.id,\n flowSlug: flowId,\n actionType: 'NOT_STARTED_STEP',\n createdAt: new Date().toISOString(),\n blocked: false,\n hidden: false,\n }\n return acc\n }, {}) ?? {},\n shouldTrigger: false,\n }\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(previousUserFlowState: UserFlowState) {\n if (previousUserFlowState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousUserFlowState.flowId) {\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}\n"],"mappings":";0jBAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,UAAAE,EAAA,eAAAC,EAAA,aAAAC,EAAA,YAAAC,EAAA,gBAAAC,IAAA,eAAAC,GAAAP,ICgBO,IAAKQ,OACVA,EAAA,OAAS,SACTA,EAAA,UAAY,YAFFA,OAAA,IAKAC,OACVA,EAAA,MAAQ,QACRA,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IAiPAC,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,ICtQL,IAA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AO,OACpB,cAAe,KAAK,OAAO,OAC3B,aACEN,EAAAsB,GAAA,YAAAA,EAAQ,QAAR,YAAAtB,EAAe,OAAO,CAACuB,EAAKC,KAC1BD,EAAIC,EAAK,EAAE,EAAI,CACb,OAAQA,EAAK,GACb,SAAUlB,EACV,WAAY,mBACZ,UAAW,IAAI,KAAK,EAAE,YAAY,EAClC,QAAS,GACT,OAAQ,EACV,EACOiB,GACN,CAAC,KAAM,CAAC,EACb,cAAe,EACjB,CACF,CAAC,CACH,CAKA,MAAc,qBAAqBE,EAAsC,CACnEA,GACF,KAAK,MAAM,QAASjC,GAAS,CACvBA,EAAK,IAAMiC,EAAsB,QACnC,KAAK,eAAe,EAAE,0BAA0B,QAAShC,GAAY,CACnE,IAAMC,EAAW,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,EAAE,EAChEC,EAAQD,EAAME,CAAQ,EACtB,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,GAAIG,EAAUH,CAAI,CAAC,CAClE,CAAC,CAEL,CAAC,CAEL,CACF","names":["src_exports","__export","Flow","FlowStatus","FlowType","Frigade","TriggerType","__toCommonJS","TriggerType","FlowStatus","FlowType","VERSION_NUMBER","import_cross_fetch","import_uuid","frigadeGlobalState","getGlobalStateKey","internalConfig","Fetchable","config","generateGuestId","filteredConfig","_","v","path","options","getEmptyResponse","gracefulFetch","getHeaders","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","flowDataRaw","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","hasCompleted","targetingShouldHideFlow","newSteps","step","index","userFlowStateStep","stepObj","COMPLETED_STEP","STARTED_STEP","properties","thisStep","copy","clone","updatedUserFlowState","isLastStep","nextStepId","lastAction","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","nextStep","NOT_STARTED_FLOW","maybeCurrentStepId","key","_a","currentStepId","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","POST_CACHE_TTL_MS","LOCAL_STORAGE_PREFIX","cloneFlow","flow","newFlow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","gracefulFetch","url","options","lastCallAtKey","LAST_POST_CALL_AT","lastCallDataKey","LAST_POST_CALL_DATA","lastCall","getLocalStorage","lastCallData","lastCallDate","POST_CACHE_TTL_MS","getEmptyResponse","setLocalStorage","response","fetch","error","body","e","generateGuestId","guestId","GUEST_KEY","GUEST_PREFIX","uuidv4","isWeb","Frigade","Fetchable","apiKey","config","flow","handler","lastFlow","cloneFlow","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","groupId","event","flowId","resetAllLocalStorage","clearCache","h","that","validator","target","key","value","userFlowStatesRaw","userFlowState","shouldReload","before","flowDataRaw","flowData","Flow","parsed","acc","step","previousUserFlowState"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/core/types.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export { Frigade } from './core/frigade'\nexport { Flow } from './core/flow'\nexport { FlowStatus, FlowStep, FrigadeConfig, FlowType, TriggerType } from './core/types'\n","export interface FlowDataRaw {\n id: number\n name: string\n description: string\n data: string\n createdAt: string\n modifiedAt: string\n slug: string\n targetingLogic: string\n type: FlowType\n triggerType: TriggerType\n status: FlowStatus\n version: number\n active: boolean\n}\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 interface FlowStep {\n /**\n * Unique identifier for the step.\n */\n id: string\n\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 * Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * Primary button URI target (either _blank or _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * Secondary button URI target (either _blank or _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 * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isCompleted: boolean\n\n /**\n * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isStarted: boolean\n\n /**\n * Whether the step is blocked (can't be accessed yet) based on `startCriteria`.\n */\n isBlocked: boolean\n\n /**\n * Whether the step is hidden (not shown in the list view) based on `visibilityCriteria`.\n */\n isHidden: boolean\n\n /**\n * Last state update.\n */\n lastActionAt?: Date\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 for this given 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\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 UserFlowState {\n flowId: string\n flowState: 'COMPLETED_FLOW' | 'STARTED_FLOW' | 'SKIPPED_FLOW' | 'NOT_STARTED_FLOW'\n lastStepId: string\n userId: string\n foreignUserId: string\n stepStates: Record<string, UserFlowStepState>\n shouldTrigger: boolean\n}\n\nexport interface UserFlowStepState {\n stepId: string\n flowSlug: string\n actionType: 'NOT_STARTED_STEP' | 'STARTED_STEP' | 'COMPLETED_STEP'\n createdAt: string\n blocked: boolean\n hidden: boolean\n lastActionAt?: string\n}\n\nexport interface InternalConfig {\n apiKey: string\n userId?: string\n organizationId?: string\n __instanceId?: string\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","export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\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'\nexport type StepActionType = 'STARTED_STEP' | 'COMPLETED_STEP' | 'NOT_STARTED_STEP'\nexport type UserFlowStatus = 'NOT_STARTED_FLOW' | 'STARTED_FLOW' | 'COMPLETED_FLOW' | 'SKIPPED_FLOW'\nconst LAST_POST_CALL_AT = 'frigade-last-call-at-'\nconst LAST_POST_CALL_DATA = 'frigade-last-call-data-'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst GET_CACHE_TTL_MS = 1000\nconst POST_CACHE_TTL_MS = 1000\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow(flow.config, flow.rawData)\n newFlow.isCompleted = flow.isCompleted\n newFlow.isStarted = flow.isStarted\n newFlow.isSkipped = flow.isSkipped\n newFlow.isVisible = flow.isVisible\n newFlow.steps = flow.steps\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\nexport async function gracefulFetch(url: string, options: any) {\n const lastCallAtKey = LAST_POST_CALL_AT + url\n const lastCallDataKey = LAST_POST_CALL_DATA + url\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCall = getLocalStorage(lastCallAtKey)\n const lastCallData = getLocalStorage(lastCallDataKey)\n if (lastCall && lastCallData && lastCallData == options.body) {\n const lastCallDate = new Date(lastCall)\n const now = new Date()\n const diff = now.getTime() - lastCallDate.getTime()\n // Throttle consecutive POST calls to 1 second\n if (diff < POST_CACHE_TTL_MS) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n clearCache()\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 || response.status === 201) {\n return getEmptyResponse()\n }\n\n const body = await response.json()\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 { FlowStep, FrigadeConfig, UserFlowState } from '../core/types'\nimport { Flow } from '../core/flow'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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}\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(36).substring(7),\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.config.apiUrl}${path}`, {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\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 not initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import { FlowDataRaw, FlowStep, FrigadeConfig, UserFlowState } from './types'\nimport {\n clone,\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'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object.\n * @ignore\n */\n public configYmlAsJson: any\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: FlowDataRaw\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 * Whether the Flow is visible to the user based on the current user/group's state.\n */\n public isVisible: boolean = false\n /**\n * Whether the Flow targeting logic/audience matches the current user/group.\n */\n public isTargeted: boolean = false\n /**\n * @ignore\n */\n private readonly flowDataRaw: FlowDataRaw\n /**\n * @ignore\n */\n private userFlowStateRaw?: UserFlowState\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n /**\n * @ignore\n */\n private lastUsedVariables = {}\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private initFromRawData(flowDataRaw: FlowDataRaw) {\n const flowDataYml = JSON.parse(flowDataRaw.data)\n const steps = flowDataYml.steps ?? flowDataYml.data ?? []\n this.id = flowDataRaw.slug\n this.rawData = flowDataRaw\n this.configYmlAsJson = flowDataYml\n this.title = this.configYmlAsJson.title\n this.subtitle = this.configYmlAsJson.subtitle\n\n const userFlowState = this.getUserFlowState()\n if (!userFlowState) {\n return\n }\n this.userFlowStateRaw = userFlowState\n\n this.isCompleted = userFlowState.flowState == COMPLETED_FLOW\n this.isStarted = userFlowState.flowState == STARTED_FLOW\n this.isSkipped = userFlowState.flowState == SKIPPED_FLOW\n const hasCompleted = this.isCompleted || this.isSkipped\n const targetingShouldHideFlow =\n flowDataRaw.targetingLogic && userFlowState.shouldTrigger === false\n this.isVisible = !hasCompleted && !targetingShouldHideFlow\n if (this.flowDataRaw.active === false) {\n this.isVisible = false\n }\n this.isTargeted = !flowDataRaw.targetingLogic ? true : targetingShouldHideFlow === false\n\n const newSteps = new Map<string, FlowStep>()\n\n steps.forEach((step, index) => {\n const userFlowStateStep = userFlowState.stepStates[step.id]\n const stepObj = {\n ...step,\n isCompleted: userFlowStateStep.actionType == COMPLETED_STEP,\n isStarted: userFlowStateStep.actionType == STARTED_STEP,\n isHidden: userFlowStateStep.hidden,\n isBlocked: userFlowStateStep.blocked,\n lastActionAt: userFlowStateStep.lastActionAt\n ? new Date(userFlowStateStep.lastActionAt)\n : undefined,\n order: index,\n } as FlowStep\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.isStarted) {\n return\n }\n\n thisStep.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.lastStepId = thisStep.id\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n if (!thisStep.isCompleted) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n await this.refreshUserFlowState()\n }\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.isCompleted) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n\n thisStep.isCompleted = true\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n\n copy.stepStates[thisStep.id].actionType = COMPLETED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.flowState = isLastStep ? COMPLETED_FLOW : STARTED_FLOW\n\n const nextStepId = Array.from(this.steps.keys())[index + 1]\n if (nextStepId) {\n copy.lastStepId = nextStepId\n copy.stepStates[nextStepId].actionType = STARTED_STEP\n const lastAction = new Date()\n copy.stepStates[nextStepId].lastActionAt = lastAction.toISOString()\n this.steps.get(nextStepId).isStarted = true\n this.steps.get(nextStepId).lastActionAt = lastAction\n }\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n // if all steps are now completed, mark flow completed\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.isCompleted) {\n return\n }\n\n thisStep.isCompleted = false\n thisStep.isStarted = false\n thisStep.lastActionAt = undefined\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = NOT_STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = undefined\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = undefined\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.isCompleted !== previousStep?.isCompleted ||\n thisStep.isStarted !== previousStep?.isStarted ||\n thisStep.isHidden !== previousStep?.isHidden ||\n thisStep.isBlocked !== previousStep?.isBlocked\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)\n })\n this.steps = newSteps\n // Check if empty object\n if (Object.keys(this.lastUsedVariables).length > 0) {\n this.applyVariables(this.lastUsedVariables)\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\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n this.isStarted = true\n this.isCompleted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = COMPLETED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n this.isSkipped = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = SKIPPED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\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\n this.optimisticallyMarkFlowSkipped()\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: SKIPPED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 }\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 await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 let maybeCurrentStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false && this.steps.get(key).isHidden === false\n )\n Array.from(this.steps.keys()).forEach((key) => {\n if (\n this.steps.get(key).isStarted &&\n this.steps.get(key).lastActionAt &&\n this.steps.get(key).lastActionAt >\n (this.steps.get(maybeCurrentStepId)?.lastActionAt ?? new Date(0))\n ) {\n maybeCurrentStepId = key\n }\n })\n\n const currentStepId = maybeCurrentStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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.isCompleted).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.isHidden).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 (\n (flow.id === this.id &&\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 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.lastUsedVariables = variables\n }\n\n /**\n * @ignore\n */\n private getUserFlowState(): UserFlowState {\n const userFlowStates = this.getGlobalState().userFlowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private async refreshUserFlowState() {\n await this.getGlobalState().refreshUserFlowStates()\n }\n}\n","import {\n FlowDataRaw,\n FlowStatus,\n FlowType,\n FrigadeConfig,\n TriggerType,\n UserFlowState,\n} 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'\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.refreshFlows()\n await this.refreshUserFlowStates()\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\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.refreshUserFlowStates()\n await this.refreshFlows()\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.refreshUserFlowStates()\n await this.refreshFlows()\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 refreshUserFlowStates(): 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 (\n target[key] &&\n target[key].flowState &&\n (JSON.stringify(target[key].flowState) !== JSON.stringify(value?.flowState) ||\n JSON.stringify(target[key].stepStates) !== JSON.stringify(value?.stepStates) ||\n JSON.stringify(target[key].shouldTrigger) !== JSON.stringify(value?.shouldTrigger))\n ) {\n that.triggerEventHandlers(target[key])\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshUserFlowStates: async () => {},\n userFlowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockUserFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n if (this.config.__readOnly) {\n return\n }\n\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId\n ? `&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`\n : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n let shouldReload = false\n const before = frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId]\n\n // Special case: for flows that show up based on targeting logic/rules, we need to check if the flow should be triggered\n if (before && before.shouldTrigger == false && userFlowState.shouldTrigger == true) {\n shouldReload = true\n }\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n if (shouldReload) {\n this.flows.forEach((flow) => {\n if (flow.id == userFlowState.flowId) {\n flow.reload()\n this.triggerEventHandlers(\n frigadeGlobalState[globalStateKey].userFlowStates[flow.id]\n )\n }\n })\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n /**\n * @ignore\n */\n private async refreshFlows() {\n this.flows = []\n\n if (this.config.__flowConfigOverrides) {\n this.mockFlowConfigs()\n return\n }\n\n const flowDataRaw = await this.fetch('/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowDatas = flowDataRaw.data as FlowDataRaw[]\n flowDatas.forEach((flowData) => {\n this.flows.push(new Flow(this.config, flowData))\n this.getGlobalState().previousFlows.set(\n flowData.slug,\n cloneFlow(this.flows[this.flows.length - 1])\n )\n })\n } else {\n this.hasFailed = true\n }\n }\n\n /**\n * @ignore\n */\n private mockFlowConfigs() {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n this.flows.push(\n new Flow(this.config, {\n id: -1,\n name: '',\n description: '',\n data: this.config.__flowConfigOverrides[flowId],\n createdAt: new Date().toISOString(),\n modifiedAt: new Date().toISOString(),\n slug: flowId,\n targetingLogic: '',\n type: FlowType.CHECKLIST,\n triggerType: TriggerType.MANUAL,\n status: FlowStatus.ACTIVE,\n version: 1,\n active: true,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private mockUserFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].userFlowStates[flowId] = {\n flowId,\n flowState: 'NOT_STARTED_FLOW',\n lastStepId: null,\n userId: this.config.userId,\n foreignUserId: this.config.userId,\n stepStates:\n parsed?.steps?.reduce((acc, step) => {\n acc[step.id] = {\n stepId: step.id,\n flowSlug: flowId,\n actionType: 'NOT_STARTED_STEP',\n createdAt: new Date().toISOString(),\n blocked: false,\n hidden: false,\n }\n return acc\n }, {}) ?? {},\n shouldTrigger: false,\n }\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(previousUserFlowState: UserFlowState) {\n if (previousUserFlowState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousUserFlowState.flowId) {\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}\n"],"mappings":";kmCAAA,IAAAA,GAAA,GAAAC,GAAAD,GAAA,UAAAE,EAAA,eAAAC,EAAA,aAAAC,EAAA,YAAAC,EAAA,gBAAAC,IAAA,eAAAC,GAAAP,ICgBO,IAAKQ,OACVA,EAAA,OAAS,SACTA,EAAA,UAAY,YAFFA,OAAA,IAKAC,OACVA,EAAA,MAAQ,QACRA,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IAiPAC,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,ICtQL,IAAMC,GAAiB,SCC9B,IAAAC,GAAkB,6BAClBC,GAA6B,gBCgBtB,IAAIC,EAAyD,CAAC,EAE9D,SAASC,EAAkBC,EAAuC,CApBzE,IAAAC,EAAAC,EAqBE,MAAO,GAAGF,EAAe,gBAAgBA,EAAe,WAAUC,EAAAD,EAAe,SAAf,KAAAC,EAAyB,OACzFC,EAAAF,EAAe,UA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AMA,EACN,eAAgB,GAChB,iBACA,qBACA,gBACA,QAAS,EACT,OAAQ,EACV,CAAC,CACH,CACF,CAAC,CACH,CAKQ,mBAAmBV,EAAwB,CACjD,OAAO,KAAK,KAAK,OAAO,qBAAqB,EAAE,QAASU,GAAW,CAtZvE,IAAAP,EAAAuB,EAuZM,IAAMC,EAAS,KAAK,MAAM,KAAK,OAAO,sBAAsBjB,CAAM,CAAC,EACnER,EAAmBF,CAAc,EAAE,eAAeU,CAAM,EAAI,CAC1D,OAAAA,EACA,UAAW,mBACX,WAAY,KACZ,OAAQ,KAAK,OAAO,OACpB,cAAe,KAAK,OAAO,OAC3B,YACEgB,GAAAvB,EAAAwB,GAAA,YAAAA,EAAQ,QAAR,YAAAxB,EAAe,OAAO,CAACyB,EAAKC,KAC1BD,EAAIC,EAAK,EAAE,EAAI,CACb,OAAQA,EAAK,GACb,SAAUnB,EACV,WAAY,mBACZ,UAAW,IAAI,KAAK,EAAE,YAAY,EAClC,QAAS,GACT,OAAQ,EACV,EACOkB,GACN,CAAC,KAVJ,KAAAF,EAUU,CAAC,EACb,cAAe,EACjB,CACF,CAAC,CACH,CAKc,qBAAqBI,EAAsC,QAAApC,EAAA,sBACnEoC,GACF,KAAK,MAAM,QAASnC,GAAS,CACvBA,EAAK,IAAMmC,EAAsB,QACnC,KAAK,eAAe,EAAE,0BAA0B,QAASlC,GAAY,CACnE,IAAMC,EAAW,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,EAAE,EAChEC,EAAQD,EAAME,CAAQ,EACtB,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,GAAIG,EAAUH,CAAI,CAAC,CAClE,CAAC,CAEL,CAAC,CAEL,GACF","names":["src_exports","__export","Flow","FlowStatus","FlowType","Frigade","TriggerType","__toCommonJS","TriggerType","FlowStatus","FlowType","VERSION_NUMBER","import_cross_fetch","import_uuid","frigadeGlobalState","getGlobalStateKey","internalConfig","_a","_b","Fetchable","config","generateGuestId","filteredConfig","_","v","__spreadValues","path","options","__async","getEmptyResponse","gracefulFetch","getHeaders","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","flowDataRaw","_a","_b","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","hasCompleted","targetingShouldHideFlow","newSteps","step","index","userFlowStateStep","stepObj","__spreadProps","__spreadValues","COMPLETED_STEP","STARTED_STEP","properties","__async","thisStep","copy","clone","updatedUserFlowState","isLastStep","nextStepId","lastAction","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","nextStep","NOT_STARTED_FLOW","maybeCurrentStepId","key","currentStepId","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","POST_CACHE_TTL_MS","LOCAL_STORAGE_PREFIX","cloneFlow","flow","newFlow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","gracefulFetch","url","options","__async","lastCallAtKey","LAST_POST_CALL_AT","lastCallDataKey","LAST_POST_CALL_DATA","lastCall","getLocalStorage","lastCallData","lastCallDate","POST_CACHE_TTL_MS","getEmptyResponse","setLocalStorage","response","fetch","error","body","e","generateGuestId","guestId","GUEST_KEY","GUEST_PREFIX","uuidv4","isWeb","Frigade","Fetchable","apiKey","config","__spreadValues","__async","flow","handler","lastFlow","cloneFlow","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","__spreadProps","groupId","event","flowId","resetAllLocalStorage","clearCache","h","that","validator","target","key","value","userFlowStatesRaw","userFlowState","shouldReload","before","flowDataRaw","flowData","Flow","_b","parsed","acc","step","previousUserFlowState"]}
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  "use client";
2
- var v=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(v||{}),P=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(P||{}),G=(p=>(p.ANNOUNCEMENT="ANNOUNCEMENT",p.CHECKLIST="CHECKLIST",p.FORM="FORM",p.TOUR="TOUR",p.SUPPORT="SUPPORT",p.CUSTOM="CUSTOM",p.BANNER="BANNER",p.EMBEDDED_TIP="EMBEDDED_TIP",p.NPS_SURVEY="NPS_SURVEY",p.SURVEY="SURVEY",p.CARD="CARD",p))(G||{});var K="0.2.20";import q from"cross-fetch";import{v4 as Q}from"uuid";var c={};function R(o){return`${o.__instanceId}-${o.apiKey}:${o.userId??""}:${o.groupId??""}`}var E=class{constructor(n){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:j(),__instanceId:Math.random().toString(36).substring(7)};let t=Object.fromEntries(Object.entries(n).filter(([e,i])=>i!=null));this.config={...this.config,...t}}async fetch(n,t){return this.config.__readOnly?u():$(`${this.config.apiUrl}${n}`,{keepalive:!0,...t??{},...B(this.config.apiKey)})}getGlobalState(){let n=R(this.config);if(!c[n])throw new Error("Frigade not initialized");return c[n]}};var b=class extends E{constructor(t,e){super(t);this.isVisible=!1;this.isTargeted=!1;this.lastStepUpdate=new Map;this.lastUsedVariables={};this.flowDataRaw=e,this.initFromRawData(e)}reload(){this.initFromRawData(this.flowDataRaw)}initFromRawData(t){let e=JSON.parse(t.data),i=e.steps??e.data??[];this.id=t.slug,this.rawData=t,this.configYmlAsJson=e,this.title=this.configYmlAsJson.title,this.subtitle=this.configYmlAsJson.subtitle;let s=this.getUserFlowState();if(!s)return;this.userFlowStateRaw=s,this.isCompleted=s.flowState==D,this.isStarted=s.flowState==A,this.isSkipped=s.flowState==U;let g=this.isCompleted||this.isSkipped,a=t.targetingLogic&&s.shouldTrigger===!1;this.isVisible=!g&&!a,this.flowDataRaw.active===!1&&(this.isVisible=!1),this.isTargeted=t.targetingLogic?a===!1:!0;let T=new Map;i.forEach((f,C)=>{let p=s.stepStates[f.id],I={...f,isCompleted:p.actionType==y,isStarted:p.actionType==F,isHidden:p.hidden,isBlocked:p.blocked,lastActionAt:p.lastActionAt?new Date(p.lastActionAt):void 0,order:C};I.start=async l=>{let r=this.steps.get(f.id);if(this.getCurrentStep().id===r.id&&r.isStarted)return;r.isStarted=!0;let S=w(this.getGlobalState().userFlowStates[this.id]);S.stepStates[r.id].actionType=F,S.stepStates[r.id].lastActionAt=new Date().toISOString(),S.lastStepId=r.id,this.getGlobalState().userFlowStates[this.id]=S,r.isCompleted||(await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:r.id,data:l??{},createdAt:new Date().toISOString(),actionType:F})}),await this.refreshUserFlowState());let h=this.getUserFlowState();r.isCompleted=h.stepStates[r.id].actionType==y,r.isStarted=h.stepStates[r.id].actionType==F,r.lastActionAt=new Date},I.complete=async l=>{let r=this.steps.get(f.id);if(r.isCompleted)return;let h=this.getNumberOfCompletedSteps()+1==this.steps.size;r.isCompleted=!0,this.isStarted=!0;let d=w(this.getGlobalState().userFlowStates[this.id]);d.stepStates[r.id].actionType=y,d.stepStates[r.id].lastActionAt=new Date().toISOString(),d.flowState=h?D:A;let O=Array.from(this.steps.keys())[C+1];if(O){d.lastStepId=O,d.stepStates[O].actionType=F;let V=new Date;d.stepStates[O].lastActionAt=V.toISOString(),this.steps.get(O).isStarted=!0,this.steps.get(O).lastActionAt=V}h&&this.optimisticallyMarkFlowCompleted(),this.getGlobalState().userFlowStates[this.id]=d,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:r.id,data:l??{},createdAt:new Date().toISOString(),actionType:y})}),await this.refreshUserFlowState();let J=this.getUserFlowState();r.isCompleted=J.stepStates[r.id].actionType==y,r.isStarted=J.stepStates[r.id].actionType==F,r.lastActionAt=new Date},I.reset=async()=>{let l=this.steps.get(f.id);if(!l.isCompleted)return;l.isCompleted=!1,l.isStarted=!1,l.lastActionAt=void 0;let r=w(this.getGlobalState().userFlowStates[this.id]);r.stepStates[l.id].actionType=N,r.stepStates[l.id].lastActionAt=void 0,r.flowState=A,this.getGlobalState().userFlowStates[this.id]=r,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:l.id,data:{},createdAt:new Date().toISOString(),actionType:N})}),await this.refreshUserFlowState();let S=this.getUserFlowState();l.isCompleted=S.stepStates[l.id].actionType==y,l.isStarted=S.stepStates[l.id].actionType==F,l.lastActionAt=void 0},I.onStateChange=l=>{let r=S=>{if(S.id!==this.id)return;let h=S.steps.get(f.id),d=this.lastStepUpdate.get(l);(h.isCompleted!==(d==null?void 0:d.isCompleted)||h.isStarted!==(d==null?void 0:d.isStarted)||h.isHidden!==(d==null?void 0:d.isHidden)||h.isBlocked!==(d==null?void 0:d.isBlocked))&&(l(h,d??w(h)),this.lastStepUpdate.set(l,w(h)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(l,r),this.getGlobalState().onFlowStateChangeHandlers.push(r)},I.removeStateChangeHandler=l=>{let r=this.getGlobalState().onStepStateChangeHandlerWrappers.get(l);r&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(S=>S!==r))},T.set(f.id,I)}),this.steps=T,Object.keys(this.lastUsedVariables).length>0&&this.applyVariables(this.lastUsedVariables)}async start(t){if(this.isStarted||this.isCompleted)return;this.isStarted=!0;let e=w(this.getGlobalState().userFlowStates[this.id]);e.flowState=A,this.getGlobalState().userFlowStates[this.id]=e,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:A})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}async complete(t){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:D})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}optimisticallyMarkFlowCompleted(){this.isStarted=!0,this.isCompleted=!0;let t=w(this.getGlobalState().userFlowStates[this.id]);t.flowState=D,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}optimisticallyMarkFlowSkipped(){this.isSkipped=!0;let t=w(this.getGlobalState().userFlowStates[this.id]);t.flowState=U,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}async skip(t){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:U})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}async forward(t){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e&&await e.start(t)}async back(t){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&await e.start(t)}async restart(){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:Y})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let t=Array.from(this.steps.keys()).find(i=>this.steps.get(i).isCompleted===!1&&this.steps.get(i).isHidden===!1);Array.from(this.steps.keys()).forEach(i=>{var s;this.steps.get(i).isStarted&&this.steps.get(i).lastActionAt&&this.steps.get(i).lastActionAt>(((s=this.steps.get(t))==null?void 0:s.lastActionAt)??new Date(0))&&(t=i)});let e=t??Array.from(this.steps.keys())[0];return this.steps.get(e)}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>!t.isHidden).length}onStateChange(t){let e=(i,s)=>{(i.id===this.id&&(i.isCompleted!==s.isCompleted||i.isStarted!==s.isStarted||i.isSkipped!==s.isSkipped||i.isVisible!==s.isVisible)||JSON.stringify(i.steps)!==JSON.stringify(s.steps))&&t(i,s)};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){let e=i=>{let s=i.match(/\${(.*?)}/g);return s&&s.forEach(g=>{let a=g.replace("${","").replace("}","");t[a]&&(i=i.replace(g,t[a]))}),i};this.title=e(this.title??""),this.subtitle=e(this.subtitle??""),this.steps.forEach(i=>{Object.keys(i).forEach(s=>{typeof i[s]=="string"&&(i[s]=e(i[s]))})}),this.lastUsedVariables=t}getUserFlowState(){return this.getGlobalState().userFlowStates[this.id]}async refreshUserFlowState(){await this.getGlobalState().refreshUserFlowStates()}};var N="NOT_STARTED_STEP",D="COMPLETED_FLOW",U="SKIPPED_FLOW",A="STARTED_FLOW",Y="NOT_STARTED_FLOW",y="COMPLETED_STEP",F="STARTED_STEP",Z="frigade-last-call-at-",tt="frigade-last-call-data-",X="frigade-guest-key",x="guest_",et="get-cache-";var it=1e3,M="fr-js-";function _(o){let n=new b(o.config,o.rawData);return n.isCompleted=o.isCompleted,n.isStarted=o.isStarted,n.isSkipped=o.isSkipped,n.isVisible=o.isVisible,n.steps=o.steps,n}function w(o){return JSON.parse(JSON.stringify(o))}function B(o){return{headers:{Authorization:`Bearer ${o}`,"Content-Type":"application/json","X-Frigade-SDK-Version":K,"X-Frigade-SDK-Platform":"Javascript"}}}function L(o){return m()?window.localStorage.getItem(`${M}${o}`):null}function H(o,n){m()&&window.localStorage.setItem(`${M}${o}`,n)}function k(){Object.keys(c).forEach(o=>{o.startsWith(et)&&delete c[o]})}function z(){m()&&Object.keys(window.localStorage).forEach(o=>{o.startsWith(M)&&window.localStorage.removeItem(o)})}async function $(o,n){let t=Z+o,e=tt+o;if(m()&&n&&n.body&&n.method==="POST"){let s=L(t),g=L(e);if(s&&g&&g==n.body){let a=new Date(s);if(new Date().getTime()-a.getTime()<it)return u()}H(t,new Date().toISOString()),H(e,n.body),k()}let i;try{i=q(o,n),i=await i}catch(s){return u(s)}if(!i)return u("Received an empty response");if(i.status>=400)return u(i.statusText);try{if(i.status===204||i.status===201)return u();let s=await i.json();return s.error?u(s.error):s}catch(s){return u(s)}}function u(o){return o&&console.log("Call to Frigade failed",o),{json:()=>({})}}function j(){if(m()){let o=L(X);return o||(o=`${x}${Q()}`,H(X,o)),o}}function m(){return typeof window<"u"&&typeof window.document<"u"&&typeof window.localStorage<"u"}var W=class extends E{constructor(t,e){super({apiKey:t,...e});this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=async()=>{document.visibilityState==="visible"&&(await this.refreshFlows(),await this.refreshUserFlowStates(),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,_(t))})}))};this.init(this.config),m()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(m()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=R(this.config);c[t]&&(c[t].onFlowStateChangeHandlers=[])}}async init(t){return this.config={...this.config,...t},this.initPromise=(async()=>{var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(x))&&await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})}),await this.refreshUserFlowStates(),await this.refreshFlows()})(),this.initPromise}async identify(t,e){this.config={...this.config,userId:t},await this.initIfNeeded(),await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),await this.resync()}async group(t,e){await this.initIfNeeded(),this.config.groupId=t,await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,properties:e})}),await this.resync()}async track(t,e){if(await this.initIfNeeded(),!t){console.error("Event name is required to track an event");return}this.config.userId&&this.config.groupId?await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,events:[{event:t,properties:e}]})}):this.config.userId&&await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,events:[{event:t,properties:e}]})}),await this.resync()}isReady(){return!!(this.config.__instanceId&&this.config.apiKey&&this.initPromise)}async getFlow(t){return await this.initIfNeeded(),this.flows.find(e=>e.id==t)}async getFlows(){return await this.initIfNeeded(),this.flows}async reload(){z(),k(),await this.refreshUserFlowStates(),await this.refreshFlows(),this.initPromise=null,await 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,_(t))})})}async resync(){this.initPromise=null,await 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,_(t))})})}async onStateChange(t){await this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)}hasFailedToLoad(){return this.hasFailed}async removeStateChangeHandler(t){await this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)}async initIfNeeded(){return this.initPromise!==null?this.initPromise:this.init(this.config)}async refreshUserFlowStates(){let t=R(this.config);if(!c[t]){let e=this,i={set:function(s,g,a){return s[g]&&s[g].flowState&&(JSON.stringify(s[g].flowState)!==JSON.stringify(a==null?void 0:a.flowState)||JSON.stringify(s[g].stepStates)!==JSON.stringify(a==null?void 0:a.stepStates)||JSON.stringify(s[g].shouldTrigger)!==JSON.stringify(a==null?void 0:a.shouldTrigger))&&e.triggerEventHandlers(s[g]),s[g]=a,!0}};if(c[t]={refreshUserFlowStates:async()=>{},userFlowStates:new Proxy({},i),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockUserFlowStates(t);return}c[t].refreshUserFlowStates=async()=>{if(this.config.__readOnly)return;let s=await this.fetch(`/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`:""}`);s&&s.data?(s.data.forEach(a=>{let T=!1,f=c[t].userFlowStates[a.flowId];f&&f.shouldTrigger==!1&&a.shouldTrigger==!0&&(T=!0),c[t].userFlowStates[a.flowId]=a,T&&this.flows.forEach(C=>{C.id==a.flowId&&(C.reload(),this.triggerEventHandlers(c[t].userFlowStates[C.id]))})}),this.hasFailed=!1):this.hasFailed=!0}}await c[t].refreshUserFlowStates()}async refreshFlows(){if(this.flows=[],this.config.__flowConfigOverrides){this.mockFlowConfigs();return}let t=await this.fetch("/flows");t&&t.data?t.data.forEach(i=>{this.flows.push(new b(this.config,i)),this.getGlobalState().previousFlows.set(i.slug,_(this.flows[this.flows.length-1]))}):this.hasFailed=!0}mockFlowConfigs(){Object.keys(this.config.__flowConfigOverrides).forEach(t=>{this.flows.push(new b(this.config,{id:-1,name:"",description:"",data:this.config.__flowConfigOverrides[t],createdAt:new Date().toISOString(),modifiedAt:new Date().toISOString(),slug:t,targetingLogic:"",type:"CHECKLIST",triggerType:"MANUAL",status:"ACTIVE",version:1,active:!0}))})}mockUserFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var s;let i=JSON.parse(this.config.__flowConfigOverrides[e]);c[t].userFlowStates[e]={flowId:e,flowState:"NOT_STARTED_FLOW",lastStepId:null,userId:this.config.userId,foreignUserId:this.config.userId,stepStates:((s=i==null?void 0:i.steps)==null?void 0:s.reduce((g,a)=>(g[a.id]={stepId:a.id,flowSlug:e,actionType:"NOT_STARTED_STEP",createdAt:new Date().toISOString(),blocked:!1,hidden:!1},g),{}))??{},shouldTrigger:!1}})}async triggerEventHandlers(t){t&&this.flows.forEach(e=>{e.id==t.flowId&&this.getGlobalState().onFlowStateChangeHandlers.forEach(i=>{let s=this.getGlobalState().previousFlows.get(e.id);i(e,s),this.getGlobalState().previousFlows.set(e.id,_(e))})})}};export{b as Flow,P as FlowStatus,G as FlowType,W as Frigade,v as TriggerType};
2
+ var at=Object.defineProperty,ot=Object.defineProperties;var rt=Object.getOwnPropertyDescriptors;var X=Object.getOwnPropertySymbols;var nt=Object.prototype.hasOwnProperty,lt=Object.prototype.propertyIsEnumerable;var z=(s,o,t)=>o in s?at(s,o,{enumerable:!0,configurable:!0,writable:!0,value:t}):s[o]=t,F=(s,o)=>{for(var t in o||(o={}))nt.call(o,t)&&z(s,t,o[t]);if(X)for(var t of X(o))lt.call(o,t)&&z(s,t,o[t]);return s},G=(s,o)=>ot(s,rt(o));var l=(s,o,t)=>new Promise((e,i)=>{var a=h=>{try{r(t.next(h))}catch(f){i(f)}},d=h=>{try{r(t.throw(h))}catch(f){i(f)}},r=h=>h.done?e(h.value):Promise.resolve(h.value).then(a,d);r((t=t.apply(s,o)).next())});var L=(t=>(t.MANUAL="MANUAL",t.AUTOMATIC="AUTOMATIC",t))(L||{}),H=(e=>(e.DRAFT="DRAFT",e.ACTIVE="ACTIVE",e.ARCHIVED="ARCHIVED",e))(H||{}),x=(S=>(S.ANNOUNCEMENT="ANNOUNCEMENT",S.CHECKLIST="CHECKLIST",S.FORM="FORM",S.TOUR="TOUR",S.SUPPORT="SUPPORT",S.CUSTOM="CUSTOM",S.BANNER="BANNER",S.EMBEDDED_TIP="EMBEDDED_TIP",S.NPS_SURVEY="NPS_SURVEY",S.SURVEY="SURVEY",S.CARD="CARD",S))(x||{});var q="0.2.20";import dt from"cross-fetch";import{v4 as gt}from"uuid";var p={};function v(s){var o,t;return`${s.__instanceId}-${s.apiKey}:${(o=s.userId)!=null?o:""}:${(t=s.groupId)!=null?t:""}`}var R=class{constructor(o){this.config={apiKey:"",apiUrl:"https://api.frigade.com/v1/public",userId:tt(),__instanceId:Math.random().toString(36).substring(7)};let t=Object.fromEntries(Object.entries(o).filter(([e,i])=>i!=null));this.config=F(F({},this.config),t)}fetch(o,t){return l(this,null,function*(){return this.config.__readOnly?b():Z(`${this.config.apiUrl}${o}`,F(F({keepalive:!0},t!=null?t:{}),Q(this.config.apiKey)))})}getGlobalState(){let o=v(this.config);if(!p[o])throw new Error("Frigade not initialized");return p[o]}};var T=class extends R{constructor(t,e){super(t);this.isVisible=!1;this.isTargeted=!1;this.lastStepUpdate=new Map;this.lastUsedVariables={};this.flowDataRaw=e,this.initFromRawData(e)}reload(){this.initFromRawData(this.flowDataRaw)}initFromRawData(t){var f,O;let e=JSON.parse(t.data),i=(O=(f=e.steps)!=null?f:e.data)!=null?O:[];this.id=t.slug,this.rawData=t,this.configYmlAsJson=e,this.title=this.configYmlAsJson.title,this.subtitle=this.configYmlAsJson.subtitle;let a=this.getUserFlowState();if(!a)return;this.userFlowStateRaw=a,this.isCompleted=a.flowState==P,this.isStarted=a.flowState==D,this.isSkipped=a.flowState==N;let d=this.isCompleted||this.isSkipped,r=t.targetingLogic&&a.shouldTrigger===!1;this.isVisible=!d&&!r,this.flowDataRaw.active===!1&&(this.isVisible=!1),this.isTargeted=t.targetingLogic?r===!1:!0;let h=new Map;i.forEach((S,$)=>{let E=a.stepStates[S.id],A=G(F({},S),{isCompleted:E.actionType==I,isStarted:E.actionType==y,isHidden:E.hidden,isBlocked:E.blocked,lastActionAt:E.lastActionAt?new Date(E.lastActionAt):void 0,order:$});A.start=g=>l(this,null,function*(){let n=this.steps.get(S.id);if(this.getCurrentStep().id===n.id&&n.isStarted)return;n.isStarted=!0;let w=m(this.getGlobalState().userFlowStates[this.id]);w.stepStates[n.id].actionType=y,w.stepStates[n.id].lastActionAt=new Date().toISOString(),w.lastStepId=n.id,this.getGlobalState().userFlowStates[this.id]=w,n.isCompleted||(yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:n.id,data:g!=null?g:{},createdAt:new Date().toISOString(),actionType:y})}),yield this.refreshUserFlowState());let u=this.getUserFlowState();n.isCompleted=u.stepStates[n.id].actionType==I,n.isStarted=u.stepStates[n.id].actionType==y,n.lastActionAt=new Date}),A.complete=g=>l(this,null,function*(){let n=this.steps.get(S.id);if(n.isCompleted)return;let u=this.getNumberOfCompletedSteps()+1==this.steps.size;n.isCompleted=!0,this.isStarted=!0;let c=m(this.getGlobalState().userFlowStates[this.id]);c.stepStates[n.id].actionType=I,c.stepStates[n.id].lastActionAt=new Date().toISOString(),c.flowState=u?P:D;let _=Array.from(this.steps.keys())[$+1];if(_){c.lastStepId=_,c.stepStates[_].actionType=y;let Y=new Date;c.stepStates[_].lastActionAt=Y.toISOString(),this.steps.get(_).isStarted=!0,this.steps.get(_).lastActionAt=Y}u&&this.optimisticallyMarkFlowCompleted(),this.getGlobalState().userFlowStates[this.id]=c,yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:n.id,data:g!=null?g:{},createdAt:new Date().toISOString(),actionType:I})}),yield this.refreshUserFlowState();let j=this.getUserFlowState();n.isCompleted=j.stepStates[n.id].actionType==I,n.isStarted=j.stepStates[n.id].actionType==y,n.lastActionAt=new Date}),A.reset=()=>l(this,null,function*(){let g=this.steps.get(S.id);if(!g.isCompleted)return;g.isCompleted=!1,g.isStarted=!1,g.lastActionAt=void 0;let n=m(this.getGlobalState().userFlowStates[this.id]);n.stepStates[g.id].actionType=M,n.stepStates[g.id].lastActionAt=void 0,n.flowState=D,this.getGlobalState().userFlowStates[this.id]=n,yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:g.id,data:{},createdAt:new Date().toISOString(),actionType:M})}),yield this.refreshUserFlowState();let w=this.getUserFlowState();g.isCompleted=w.stepStates[g.id].actionType==I,g.isStarted=w.stepStates[g.id].actionType==y,g.lastActionAt=void 0}),A.onStateChange=g=>{let n=w=>{if(w.id!==this.id)return;let u=w.steps.get(S.id),c=this.lastStepUpdate.get(g);(u.isCompleted!==(c==null?void 0:c.isCompleted)||u.isStarted!==(c==null?void 0:c.isStarted)||u.isHidden!==(c==null?void 0:c.isHidden)||u.isBlocked!==(c==null?void 0:c.isBlocked))&&(g(u,c!=null?c:m(u)),this.lastStepUpdate.set(g,m(u)))};this.getGlobalState().onStepStateChangeHandlerWrappers.set(g,n),this.getGlobalState().onFlowStateChangeHandlers.push(n)},A.removeStateChangeHandler=g=>{let n=this.getGlobalState().onStepStateChangeHandlerWrappers.get(g);n&&(this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(w=>w!==n))},h.set(S.id,A)}),this.steps=h,Object.keys(this.lastUsedVariables).length>0&&this.applyVariables(this.lastUsedVariables)}start(t){return l(this,null,function*(){if(this.isStarted||this.isCompleted)return;this.isStarted=!0;let e=m(this.getGlobalState().userFlowStates[this.id]);e.flowState=D,this.getGlobalState().userFlowStates[this.id]=e,yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t!=null?t:{},createdAt:new Date().toISOString(),actionType:D})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)})}complete(t){return l(this,null,function*(){this.isCompleted||(this.optimisticallyMarkFlowCompleted(),yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t!=null?t:{},createdAt:new Date().toISOString(),actionType:P})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))})}optimisticallyMarkFlowCompleted(){this.isStarted=!0,this.isCompleted=!0;let t=m(this.getGlobalState().userFlowStates[this.id]);t.flowState=P,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}optimisticallyMarkFlowSkipped(){this.isSkipped=!0;let t=m(this.getGlobalState().userFlowStates[this.id]);t.flowState=N,this.getGlobalState().userFlowStates[this.id]=t,this.isVisible=!1}skip(t){return l(this,null,function*(){this.isSkipped||(this.optimisticallyMarkFlowSkipped(),yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t!=null?t:{},createdAt:new Date().toISOString(),actionType:N})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))})}forward(t){return l(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()+1);e&&(yield e.start(t))})}back(t){return l(this,null,function*(){let e=this.getStepByIndex(this.getCurrentStepIndex()-1);e&&(yield e.start(t))})}restart(){return l(this,null,function*(){yield this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.groupId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:et})}),yield this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let t=Array.from(this.steps.keys()).find(i=>this.steps.get(i).isCompleted===!1&&this.steps.get(i).isHidden===!1);Array.from(this.steps.keys()).forEach(i=>{var a,d;this.steps.get(i).isStarted&&this.steps.get(i).lastActionAt&&this.steps.get(i).lastActionAt>((d=(a=this.steps.get(t))==null?void 0:a.lastActionAt)!=null?d:new Date(0))&&(t=i)});let e=t!=null?t:Array.from(this.steps.keys())[0];return this.steps.get(e)}getCurrentStepIndex(){let t=this.getCurrentStep();return Array.from(this.steps.keys()).indexOf(t.id)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getNumberOfAvailableSteps(){return Array.from(this.steps.values()).filter(t=>!t.isHidden).length}onStateChange(t){let e=(i,a)=>{(i.id===this.id&&(i.isCompleted!==a.isCompleted||i.isStarted!==a.isStarted||i.isSkipped!==a.isSkipped||i.isVisible!==a.isVisible)||JSON.stringify(i.steps)!==JSON.stringify(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=d=>{let r=d.match(/\${(.*?)}/g);return r&&r.forEach(h=>{let f=h.replace("${","").replace("}","");t[f]&&(d=d.replace(h,t[f]))}),d};this.title=e((i=this.title)!=null?i:""),this.subtitle=e((a=this.subtitle)!=null?a:""),this.steps.forEach(d=>{Object.keys(d).forEach(r=>{typeof d[r]=="string"&&(d[r]=e(d[r]))})}),this.lastUsedVariables=t}getUserFlowState(){return this.getGlobalState().userFlowStates[this.id]}refreshUserFlowState(){return l(this,null,function*(){yield this.getGlobalState().refreshUserFlowStates()})}};var M="NOT_STARTED_STEP",P="COMPLETED_FLOW",N="SKIPPED_FLOW",D="STARTED_FLOW",et="NOT_STARTED_FLOW",I="COMPLETED_STEP",y="STARTED_STEP",ct="frigade-last-call-at-",ht="frigade-last-call-data-",it="frigade-guest-key",J="guest_",St="get-cache-";var pt=1e3,V="fr-js-";function U(s){let o=new T(s.config,s.rawData);return o.isCompleted=s.isCompleted,o.isStarted=s.isStarted,o.isSkipped=s.isSkipped,o.isVisible=s.isVisible,o.steps=s.steps,o}function m(s){return JSON.parse(JSON.stringify(s))}function Q(s){return{headers:{Authorization:`Bearer ${s}`,"Content-Type":"application/json","X-Frigade-SDK-Version":q,"X-Frigade-SDK-Platform":"Javascript"}}}function k(s){return C()?window.localStorage.getItem(`${V}${s}`):null}function W(s,o){C()&&window.localStorage.setItem(`${V}${s}`,o)}function K(){Object.keys(p).forEach(s=>{s.startsWith(St)&&delete p[s]})}function st(){C()&&Object.keys(window.localStorage).forEach(s=>{s.startsWith(V)&&window.localStorage.removeItem(s)})}function Z(s,o){return l(this,null,function*(){let t=ct+s,e=ht+s;if(C()&&o&&o.body&&o.method==="POST"){let a=k(t),d=k(e);if(a&&d&&d==o.body){let r=new Date(a);if(new Date().getTime()-r.getTime()<pt)return b()}W(t,new Date().toISOString()),W(e,o.body),K()}let i;try{i=dt(s,o),i=yield i}catch(a){return b(a)}if(!i)return b("Received an empty response");if(i.status>=400)return b(i.statusText);try{if(i.status===204||i.status===201)return b();let a=yield i.json();return a.error?b(a.error):a}catch(a){return b(a)}})}function b(s){return s&&console.log("Call to Frigade failed",s),{json:()=>({})}}function tt(){if(C()){let s=k(it);return s||(s=`${J}${gt()}`,W(it,s)),s}}function C(){return typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.localStorage!="undefined"}var B=class extends R{constructor(t,e){super(F({apiKey:t},e));this.flows=[];this.hasFailed=!1;this.visibilityChangeHandler=()=>l(this,null,function*(){document.visibilityState==="visible"&&(yield this.refreshFlows(),yield this.refreshUserFlowStates(),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,U(t))})}))});this.init(this.config),C()&&document.addEventListener("visibilitychange",this.visibilityChangeHandler)}destroy(){if(C()){document.removeEventListener("visibilitychange",this.visibilityChangeHandler);let t=v(this.config);p[t]&&(p[t].onFlowStateChangeHandlers=[])}}init(t){return l(this,null,function*(){return this.config=F(F({},this.config),t),this.initPromise=(()=>l(this,null,function*(){var e;this.config.userId&&!((e=this.config.userId)!=null&&e.startsWith(J))&&(yield this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId})})),yield this.refreshUserFlowStates(),yield this.refreshFlows()}))(),this.initPromise})}identify(t,e){return l(this,null,function*(){this.config=G(F({},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 l(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 l(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 l(this,null,function*(){return yield this.initIfNeeded(),this.flows.find(e=>e.id==t)})}getFlows(){return l(this,null,function*(){return yield this.initIfNeeded(),this.flows})}reload(){return l(this,null,function*(){st(),K(),yield this.refreshUserFlowStates(),yield this.refreshFlows(),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,U(t))})})})}resync(){return l(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,U(t))})})})}onStateChange(t){return l(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers.push(t)})}hasFailedToLoad(){return this.hasFailed}removeStateChangeHandler(t){return l(this,null,function*(){yield this.initIfNeeded(),this.getGlobalState().onFlowStateChangeHandlers=this.getGlobalState().onFlowStateChangeHandlers.filter(e=>e!==t)})}initIfNeeded(){return l(this,null,function*(){return this.initPromise!==null?this.initPromise:this.init(this.config)})}refreshUserFlowStates(){return l(this,null,function*(){let t=v(this.config);if(!p[t]){let e=this,i={set:function(a,d,r){return a[d]&&a[d].flowState&&(JSON.stringify(a[d].flowState)!==JSON.stringify(r==null?void 0:r.flowState)||JSON.stringify(a[d].stepStates)!==JSON.stringify(r==null?void 0:r.stepStates)||JSON.stringify(a[d].shouldTrigger)!==JSON.stringify(r==null?void 0:r.shouldTrigger))&&e.triggerEventHandlers(a[d]),a[d]=r,!0}};if(p[t]={refreshUserFlowStates:()=>l(this,null,function*(){}),userFlowStates:new Proxy({},i),onFlowStateChangeHandlerWrappers:new Map,onStepStateChangeHandlerWrappers:new Map,onFlowStateChangeHandlers:[],previousFlows:new Map},this.config.__readOnly&&this.config.__flowConfigOverrides){this.mockUserFlowStates(t);return}p[t].refreshUserFlowStates=()=>l(this,null,function*(){if(this.config.__readOnly)return;let a=yield this.fetch(`/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${this.config.groupId?`&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`:""}`);a&&a.data?(a.data.forEach(r=>{let h=!1,f=p[t].userFlowStates[r.flowId];f&&f.shouldTrigger==!1&&r.shouldTrigger==!0&&(h=!0),p[t].userFlowStates[r.flowId]=r,h&&this.flows.forEach(O=>{O.id==r.flowId&&(O.reload(),this.triggerEventHandlers(p[t].userFlowStates[O.id]))})}),this.hasFailed=!1):this.hasFailed=!0})}yield p[t].refreshUserFlowStates()})}refreshFlows(){return l(this,null,function*(){if(this.flows=[],this.config.__flowConfigOverrides){this.mockFlowConfigs();return}let t=yield this.fetch("/flows");t&&t.data?t.data.forEach(i=>{this.flows.push(new T(this.config,i)),this.getGlobalState().previousFlows.set(i.slug,U(this.flows[this.flows.length-1]))}):this.hasFailed=!0})}mockFlowConfigs(){Object.keys(this.config.__flowConfigOverrides).forEach(t=>{this.flows.push(new T(this.config,{id:-1,name:"",description:"",data:this.config.__flowConfigOverrides[t],createdAt:new Date().toISOString(),modifiedAt:new Date().toISOString(),slug:t,targetingLogic:"",type:"CHECKLIST",triggerType:"MANUAL",status:"ACTIVE",version:1,active:!0}))})}mockUserFlowStates(t){Object.keys(this.config.__flowConfigOverrides).forEach(e=>{var a,d;let i=JSON.parse(this.config.__flowConfigOverrides[e]);p[t].userFlowStates[e]={flowId:e,flowState:"NOT_STARTED_FLOW",lastStepId:null,userId:this.config.userId,foreignUserId:this.config.userId,stepStates:(d=(a=i==null?void 0:i.steps)==null?void 0:a.reduce((r,h)=>(r[h.id]={stepId:h.id,flowSlug:e,actionType:"NOT_STARTED_STEP",createdAt:new Date().toISOString(),blocked:!1,hidden:!1},r),{}))!=null?d:{},shouldTrigger:!1}})}triggerEventHandlers(t){return l(this,null,function*(){t&&this.flows.forEach(e=>{e.id==t.flowId&&this.getGlobalState().onFlowStateChangeHandlers.forEach(i=>{let a=this.getGlobalState().previousFlows.get(e.id);i(e,a),this.getGlobalState().previousFlows.set(e.id,U(e))})})})}};export{T as Flow,H as FlowStatus,x as FlowType,B as Frigade,L as TriggerType};
3
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core/types.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export interface FlowDataRaw {\n id: number\n name: string\n description: string\n data: string\n createdAt: string\n modifiedAt: string\n slug: string\n targetingLogic: string\n type: FlowType\n triggerType: TriggerType\n status: FlowStatus\n version: number\n active: boolean\n}\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 interface FlowStep {\n /**\n * Unique identifier for the step.\n */\n id: string\n\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 * Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * Primary button URI target (either _blank or _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * Secondary button URI target (either _blank or _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 * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isCompleted: boolean\n\n /**\n * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isStarted: boolean\n\n /**\n * Whether the step is blocked (can't be accessed yet) based on `startCriteria`.\n */\n isBlocked: boolean\n\n /**\n * Whether the step is hidden (not shown in the list view) based on `visibilityCriteria`.\n */\n isHidden: boolean\n\n /**\n * Last state update.\n */\n lastActionAt?: Date\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 for this given 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\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 UserFlowState {\n flowId: string\n flowState: 'COMPLETED_FLOW' | 'STARTED_FLOW' | 'SKIPPED_FLOW' | 'NOT_STARTED_FLOW'\n lastStepId: string\n userId: string\n foreignUserId: string\n stepStates: Record<string, UserFlowStepState>\n shouldTrigger: boolean\n}\n\nexport interface UserFlowStepState {\n stepId: string\n flowSlug: string\n actionType: 'NOT_STARTED_STEP' | 'STARTED_STEP' | 'COMPLETED_STEP'\n createdAt: string\n blocked: boolean\n hidden: boolean\n lastActionAt?: string\n}\n\nexport interface InternalConfig {\n apiKey: string\n userId?: string\n organizationId?: string\n __instanceId?: string\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","export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\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'\nexport type StepActionType = 'STARTED_STEP' | 'COMPLETED_STEP' | 'NOT_STARTED_STEP'\nexport type UserFlowStatus = 'NOT_STARTED_FLOW' | 'STARTED_FLOW' | 'COMPLETED_FLOW' | 'SKIPPED_FLOW'\nconst LAST_POST_CALL_AT = 'frigade-last-call-at-'\nconst LAST_POST_CALL_DATA = 'frigade-last-call-data-'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst GET_CACHE_TTL_MS = 1000\nconst POST_CACHE_TTL_MS = 1000\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow(flow.config, flow.rawData)\n newFlow.isCompleted = flow.isCompleted\n newFlow.isStarted = flow.isStarted\n newFlow.isSkipped = flow.isSkipped\n newFlow.isVisible = flow.isVisible\n newFlow.steps = flow.steps\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\nexport async function gracefulFetch(url: string, options: any) {\n const lastCallAtKey = LAST_POST_CALL_AT + url\n const lastCallDataKey = LAST_POST_CALL_DATA + url\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCall = getLocalStorage(lastCallAtKey)\n const lastCallData = getLocalStorage(lastCallDataKey)\n if (lastCall && lastCallData && lastCallData == options.body) {\n const lastCallDate = new Date(lastCall)\n const now = new Date()\n const diff = now.getTime() - lastCallDate.getTime()\n // Throttle consecutive POST calls to 1 second\n if (diff < POST_CACHE_TTL_MS) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n clearCache()\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 || response.status === 201) {\n return getEmptyResponse()\n }\n\n const body = await response.json()\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 { FlowStep, FrigadeConfig, UserFlowState } from '../core/types'\nimport { Flow } from '../core/flow'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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}\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(36).substring(7),\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.config.apiUrl}${path}`, {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\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 not initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import { FlowDataRaw, FlowStep, FrigadeConfig, UserFlowState } from './types'\nimport {\n clone,\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'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object.\n * @ignore\n */\n public configYmlAsJson: any\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: FlowDataRaw\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 * Whether the Flow is visible to the user based on the current user/group's state.\n */\n public isVisible: boolean = false\n /**\n * Whether the Flow targeting logic/audience matches the current user/group.\n */\n public isTargeted: boolean = false\n /**\n * @ignore\n */\n private readonly flowDataRaw: FlowDataRaw\n /**\n * @ignore\n */\n private userFlowStateRaw?: UserFlowState\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n /**\n * @ignore\n */\n private lastUsedVariables = {}\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private initFromRawData(flowDataRaw: FlowDataRaw) {\n const flowDataYml = JSON.parse(flowDataRaw.data)\n const steps = flowDataYml.steps ?? flowDataYml.data ?? []\n this.id = flowDataRaw.slug\n this.rawData = flowDataRaw\n this.configYmlAsJson = flowDataYml\n this.title = this.configYmlAsJson.title\n this.subtitle = this.configYmlAsJson.subtitle\n\n const userFlowState = this.getUserFlowState()\n if (!userFlowState) {\n return\n }\n this.userFlowStateRaw = userFlowState\n\n this.isCompleted = userFlowState.flowState == COMPLETED_FLOW\n this.isStarted = userFlowState.flowState == STARTED_FLOW\n this.isSkipped = userFlowState.flowState == SKIPPED_FLOW\n const hasCompleted = this.isCompleted || this.isSkipped\n const targetingShouldHideFlow =\n flowDataRaw.targetingLogic && userFlowState.shouldTrigger === false\n this.isVisible = !hasCompleted && !targetingShouldHideFlow\n if (this.flowDataRaw.active === false) {\n this.isVisible = false\n }\n this.isTargeted = !flowDataRaw.targetingLogic ? true : targetingShouldHideFlow === false\n\n const newSteps = new Map<string, FlowStep>()\n\n steps.forEach((step, index) => {\n const userFlowStateStep = userFlowState.stepStates[step.id]\n const stepObj = {\n ...step,\n isCompleted: userFlowStateStep.actionType == COMPLETED_STEP,\n isStarted: userFlowStateStep.actionType == STARTED_STEP,\n isHidden: userFlowStateStep.hidden,\n isBlocked: userFlowStateStep.blocked,\n lastActionAt: userFlowStateStep.lastActionAt\n ? new Date(userFlowStateStep.lastActionAt)\n : undefined,\n order: index,\n } as FlowStep\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.isStarted) {\n return\n }\n\n thisStep.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.lastStepId = thisStep.id\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n if (!thisStep.isCompleted) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n await this.refreshUserFlowState()\n }\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.isCompleted) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n\n thisStep.isCompleted = true\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n\n copy.stepStates[thisStep.id].actionType = COMPLETED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.flowState = isLastStep ? COMPLETED_FLOW : STARTED_FLOW\n\n const nextStepId = Array.from(this.steps.keys())[index + 1]\n if (nextStepId) {\n copy.lastStepId = nextStepId\n copy.stepStates[nextStepId].actionType = STARTED_STEP\n const lastAction = new Date()\n copy.stepStates[nextStepId].lastActionAt = lastAction.toISOString()\n this.steps.get(nextStepId).isStarted = true\n this.steps.get(nextStepId).lastActionAt = lastAction\n }\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n // if all steps are now completed, mark flow completed\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.isCompleted) {\n return\n }\n\n thisStep.isCompleted = false\n thisStep.isStarted = false\n thisStep.lastActionAt = undefined\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = NOT_STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = undefined\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = undefined\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.isCompleted !== previousStep?.isCompleted ||\n thisStep.isStarted !== previousStep?.isStarted ||\n thisStep.isHidden !== previousStep?.isHidden ||\n thisStep.isBlocked !== previousStep?.isBlocked\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)\n })\n this.steps = newSteps\n // Check if empty object\n if (Object.keys(this.lastUsedVariables).length > 0) {\n this.applyVariables(this.lastUsedVariables)\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\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n this.isStarted = true\n this.isCompleted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = COMPLETED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n this.isSkipped = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = SKIPPED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\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\n this.optimisticallyMarkFlowSkipped()\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: SKIPPED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 }\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 await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 let maybeCurrentStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false && this.steps.get(key).isHidden === false\n )\n Array.from(this.steps.keys()).forEach((key) => {\n if (\n this.steps.get(key).isStarted &&\n this.steps.get(key).lastActionAt &&\n this.steps.get(key).lastActionAt >\n (this.steps.get(maybeCurrentStepId)?.lastActionAt ?? new Date(0))\n ) {\n maybeCurrentStepId = key\n }\n })\n\n const currentStepId = maybeCurrentStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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.isCompleted).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.isHidden).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 (\n (flow.id === this.id &&\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 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.lastUsedVariables = variables\n }\n\n /**\n * @ignore\n */\n private getUserFlowState(): UserFlowState {\n const userFlowStates = this.getGlobalState().userFlowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private async refreshUserFlowState() {\n await this.getGlobalState().refreshUserFlowStates()\n }\n}\n","import {\n FlowDataRaw,\n FlowStatus,\n FlowType,\n FrigadeConfig,\n TriggerType,\n UserFlowState,\n} 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'\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.refreshFlows()\n await this.refreshUserFlowStates()\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\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.refreshUserFlowStates()\n await this.refreshFlows()\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.refreshUserFlowStates()\n await this.refreshFlows()\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 refreshUserFlowStates(): 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 (\n target[key] &&\n target[key].flowState &&\n (JSON.stringify(target[key].flowState) !== JSON.stringify(value?.flowState) ||\n JSON.stringify(target[key].stepStates) !== JSON.stringify(value?.stepStates) ||\n JSON.stringify(target[key].shouldTrigger) !== JSON.stringify(value?.shouldTrigger))\n ) {\n that.triggerEventHandlers(target[key])\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshUserFlowStates: async () => {},\n userFlowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockUserFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n if (this.config.__readOnly) {\n return\n }\n\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId\n ? `&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`\n : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n let shouldReload = false\n const before = frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId]\n\n // Special case: for flows that show up based on targeting logic/rules, we need to check if the flow should be triggered\n if (before && before.shouldTrigger == false && userFlowState.shouldTrigger == true) {\n shouldReload = true\n }\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n if (shouldReload) {\n this.flows.forEach((flow) => {\n if (flow.id == userFlowState.flowId) {\n flow.reload()\n this.triggerEventHandlers(\n frigadeGlobalState[globalStateKey].userFlowStates[flow.id]\n )\n }\n })\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n /**\n * @ignore\n */\n private async refreshFlows() {\n this.flows = []\n\n if (this.config.__flowConfigOverrides) {\n this.mockFlowConfigs()\n return\n }\n\n const flowDataRaw = await this.fetch('/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowDatas = flowDataRaw.data as FlowDataRaw[]\n flowDatas.forEach((flowData) => {\n this.flows.push(new Flow(this.config, flowData))\n this.getGlobalState().previousFlows.set(\n flowData.slug,\n cloneFlow(this.flows[this.flows.length - 1])\n )\n })\n } else {\n this.hasFailed = true\n }\n }\n\n /**\n * @ignore\n */\n private mockFlowConfigs() {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n this.flows.push(\n new Flow(this.config, {\n id: -1,\n name: '',\n description: '',\n data: this.config.__flowConfigOverrides[flowId],\n createdAt: new Date().toISOString(),\n modifiedAt: new Date().toISOString(),\n slug: flowId,\n targetingLogic: '',\n type: FlowType.CHECKLIST,\n triggerType: TriggerType.MANUAL,\n status: FlowStatus.ACTIVE,\n version: 1,\n active: true,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private mockUserFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].userFlowStates[flowId] = {\n flowId,\n flowState: 'NOT_STARTED_FLOW',\n lastStepId: null,\n userId: this.config.userId,\n foreignUserId: this.config.userId,\n stepStates:\n parsed?.steps?.reduce((acc, step) => {\n acc[step.id] = {\n stepId: step.id,\n flowSlug: flowId,\n actionType: 'NOT_STARTED_STEP',\n createdAt: new Date().toISOString(),\n blocked: false,\n hidden: false,\n }\n return acc\n }, {}) ?? {},\n shouldTrigger: false,\n }\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(previousUserFlowState: UserFlowState) {\n if (previousUserFlowState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousUserFlowState.flowId) {\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}\n"],"mappings":";AAgBO,IAAKA,OACVA,EAAA,OAAS,SACTA,EAAA,UAAY,YAFFA,OAAA,IAKAC,OACVA,EAAA,MAAQ,QACRA,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IAiPAC,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,ICtQL,IAAMC,EAAiB,SCC9B,OAAOC,MAAW,cAClB,OAAS,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,GAAA,YAAAA,EAAQ,QAAR,YAAAtB,EAAe,OAAO,CAACuB,EAAKC,KAC1BD,EAAIC,EAAK,EAAE,EAAI,CACb,OAAQA,EAAK,GACb,SAAUlB,EACV,WAAY,mBACZ,UAAW,IAAI,KAAK,EAAE,YAAY,EAClC,QAAS,GACT,OAAQ,EACV,EACOiB,GACN,CAAC,KAAM,CAAC,EACb,cAAe,EACjB,CACF,CAAC,CACH,CAKA,MAAc,qBAAqBE,EAAsC,CACnEA,GACF,KAAK,MAAM,QAASjC,GAAS,CACvBA,EAAK,IAAMiC,EAAsB,QACnC,KAAK,eAAe,EAAE,0BAA0B,QAAShC,GAAY,CACnE,IAAMC,EAAW,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,EAAE,EAChEC,EAAQD,EAAME,CAAQ,EACtB,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,GAAIG,EAAUH,CAAI,CAAC,CAClE,CAAC,CAEL,CAAC,CAEL,CACF","names":["TriggerType","FlowStatus","FlowType","VERSION_NUMBER","fetch","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","Fetchable","config","generateGuestId","filteredConfig","_","v","path","options","getEmptyResponse","gracefulFetch","getHeaders","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","flowDataRaw","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","hasCompleted","targetingShouldHideFlow","newSteps","step","index","userFlowStateStep","stepObj","COMPLETED_STEP","STARTED_STEP","properties","thisStep","copy","clone","updatedUserFlowState","isLastStep","nextStepId","lastAction","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","nextStep","NOT_STARTED_FLOW","maybeCurrentStepId","key","_a","currentStepId","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","POST_CACHE_TTL_MS","LOCAL_STORAGE_PREFIX","cloneFlow","flow","newFlow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","gracefulFetch","url","options","lastCallAtKey","LAST_POST_CALL_AT","lastCallDataKey","LAST_POST_CALL_DATA","lastCall","getLocalStorage","lastCallData","lastCallDate","POST_CACHE_TTL_MS","getEmptyResponse","setLocalStorage","response","fetch","error","body","e","generateGuestId","guestId","GUEST_KEY","GUEST_PREFIX","uuidv4","isWeb","Frigade","Fetchable","apiKey","config","flow","handler","lastFlow","cloneFlow","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","groupId","event","flowId","resetAllLocalStorage","clearCache","h","that","validator","target","key","value","userFlowStatesRaw","userFlowState","shouldReload","before","flowDataRaw","flowData","Flow","parsed","acc","step","previousUserFlowState"]}
1
+ {"version":3,"sources":["../src/core/types.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export interface FlowDataRaw {\n id: number\n name: string\n description: string\n data: string\n createdAt: string\n modifiedAt: string\n slug: string\n targetingLogic: string\n type: FlowType\n triggerType: TriggerType\n status: FlowStatus\n version: number\n active: boolean\n}\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 interface FlowStep {\n /**\n * Unique identifier for the step.\n */\n id: string\n\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 * Primary button title. If omitted, the primary button will not be shown.\n */\n primaryButtonTitle?: string\n\n /**\n * Primary button URI.\n */\n primaryButtonUri?: string\n\n /**\n * Primary button URI target (either _blank or _self).\n */\n primaryButtonUriTarget?: string\n\n /**\n * Secondary button title. If omitted, the secondary button will not be shown.\n */\n secondaryButtonTitle?: string\n\n /**\n * Secondary button URI.\n */\n secondaryButtonUri?: string\n\n /**\n * Secondary button URI target (either _blank or _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 * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isCompleted: boolean\n\n /**\n * Whether the step has been completed (equivalent to step status === COMPLETED_STEP).\n */\n isStarted: boolean\n\n /**\n * Whether the step is blocked (can't be accessed yet) based on `startCriteria`.\n */\n isBlocked: boolean\n\n /**\n * Whether the step is hidden (not shown in the list view) based on `visibilityCriteria`.\n */\n isHidden: boolean\n\n /**\n * Last state update.\n */\n lastActionAt?: Date\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 for this given 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\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 UserFlowState {\n flowId: string\n flowState: 'COMPLETED_FLOW' | 'STARTED_FLOW' | 'SKIPPED_FLOW' | 'NOT_STARTED_FLOW'\n lastStepId: string\n userId: string\n foreignUserId: string\n stepStates: Record<string, UserFlowStepState>\n shouldTrigger: boolean\n}\n\nexport interface UserFlowStepState {\n stepId: string\n flowSlug: string\n actionType: 'NOT_STARTED_STEP' | 'STARTED_STEP' | 'COMPLETED_STEP'\n createdAt: string\n blocked: boolean\n hidden: boolean\n lastActionAt?: string\n}\n\nexport interface InternalConfig {\n apiKey: string\n userId?: string\n organizationId?: string\n __instanceId?: string\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","export const VERSION_NUMBER = '0.2.20'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\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'\nexport type StepActionType = 'STARTED_STEP' | 'COMPLETED_STEP' | 'NOT_STARTED_STEP'\nexport type UserFlowStatus = 'NOT_STARTED_FLOW' | 'STARTED_FLOW' | 'COMPLETED_FLOW' | 'SKIPPED_FLOW'\nconst LAST_POST_CALL_AT = 'frigade-last-call-at-'\nconst LAST_POST_CALL_DATA = 'frigade-last-call-data-'\nconst GUEST_KEY = 'frigade-guest-key'\nexport const GUEST_PREFIX = 'guest_'\nconst GET_CACHE_PREFIX = 'get-cache-'\nconst GET_CACHE_TTL_MS = 1000\nconst POST_CACHE_TTL_MS = 1000\nconst LOCAL_STORAGE_PREFIX = 'fr-js-'\n\nexport function cloneFlow(flow: Flow): Flow {\n const newFlow = new Flow(flow.config, flow.rawData)\n newFlow.isCompleted = flow.isCompleted\n newFlow.isStarted = flow.isStarted\n newFlow.isSkipped = flow.isSkipped\n newFlow.isVisible = flow.isVisible\n newFlow.steps = flow.steps\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\nexport async function gracefulFetch(url: string, options: any) {\n const lastCallAtKey = LAST_POST_CALL_AT + url\n const lastCallDataKey = LAST_POST_CALL_DATA + url\n if (isWeb() && options && options.body && options.method === 'POST') {\n const lastCall = getLocalStorage(lastCallAtKey)\n const lastCallData = getLocalStorage(lastCallDataKey)\n if (lastCall && lastCallData && lastCallData == options.body) {\n const lastCallDate = new Date(lastCall)\n const now = new Date()\n const diff = now.getTime() - lastCallDate.getTime()\n // Throttle consecutive POST calls to 1 second\n if (diff < POST_CACHE_TTL_MS) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n clearCache()\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 || response.status === 201) {\n return getEmptyResponse()\n }\n\n const body = await response.json()\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 { FlowStep, FrigadeConfig, UserFlowState } from '../core/types'\nimport { Flow } from '../core/flow'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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}\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(36).substring(7),\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.config.apiUrl}${path}`, {\n keepalive: true,\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\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 not initialized')\n }\n return frigadeGlobalState[globalStateKey]\n }\n}\n","import { FlowDataRaw, FlowStep, FrigadeConfig, UserFlowState } from './types'\nimport {\n clone,\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'\n\nexport class Flow extends Fetchable {\n /**\n * The Flow's ID.\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object.\n * @ignore\n */\n public configYmlAsJson: any\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: FlowDataRaw\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 * Whether the Flow is visible to the user based on the current user/group's state.\n */\n public isVisible: boolean = false\n /**\n * Whether the Flow targeting logic/audience matches the current user/group.\n */\n public isTargeted: boolean = false\n /**\n * @ignore\n */\n private readonly flowDataRaw: FlowDataRaw\n /**\n * @ignore\n */\n private userFlowStateRaw?: UserFlowState\n /**\n * @ignore\n */\n private lastStepUpdate: Map<(step: FlowStep, previousStep: FlowStep) => void, FlowStep> =\n new Map()\n /**\n * @ignore\n */\n private lastUsedVariables = {}\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\n\n /**\n * Reload the Flow data from the server\n */\n reload() {\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private initFromRawData(flowDataRaw: FlowDataRaw) {\n const flowDataYml = JSON.parse(flowDataRaw.data)\n const steps = flowDataYml.steps ?? flowDataYml.data ?? []\n this.id = flowDataRaw.slug\n this.rawData = flowDataRaw\n this.configYmlAsJson = flowDataYml\n this.title = this.configYmlAsJson.title\n this.subtitle = this.configYmlAsJson.subtitle\n\n const userFlowState = this.getUserFlowState()\n if (!userFlowState) {\n return\n }\n this.userFlowStateRaw = userFlowState\n\n this.isCompleted = userFlowState.flowState == COMPLETED_FLOW\n this.isStarted = userFlowState.flowState == STARTED_FLOW\n this.isSkipped = userFlowState.flowState == SKIPPED_FLOW\n const hasCompleted = this.isCompleted || this.isSkipped\n const targetingShouldHideFlow =\n flowDataRaw.targetingLogic && userFlowState.shouldTrigger === false\n this.isVisible = !hasCompleted && !targetingShouldHideFlow\n if (this.flowDataRaw.active === false) {\n this.isVisible = false\n }\n this.isTargeted = !flowDataRaw.targetingLogic ? true : targetingShouldHideFlow === false\n\n const newSteps = new Map<string, FlowStep>()\n\n steps.forEach((step, index) => {\n const userFlowStateStep = userFlowState.stepStates[step.id]\n const stepObj = {\n ...step,\n isCompleted: userFlowStateStep.actionType == COMPLETED_STEP,\n isStarted: userFlowStateStep.actionType == STARTED_STEP,\n isHidden: userFlowStateStep.hidden,\n isBlocked: userFlowStateStep.blocked,\n lastActionAt: userFlowStateStep.lastActionAt\n ? new Date(userFlowStateStep.lastActionAt)\n : undefined,\n order: index,\n } as FlowStep\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.isStarted) {\n return\n }\n\n thisStep.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.lastStepId = thisStep.id\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n if (!thisStep.isCompleted) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n await this.refreshUserFlowState()\n }\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const thisStep = this.steps.get(step.id)\n\n if (thisStep.isCompleted) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n\n thisStep.isCompleted = true\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n\n copy.stepStates[thisStep.id].actionType = COMPLETED_STEP\n copy.stepStates[thisStep.id].lastActionAt = new Date().toISOString()\n copy.flowState = isLastStep ? COMPLETED_FLOW : STARTED_FLOW\n\n const nextStepId = Array.from(this.steps.keys())[index + 1]\n if (nextStepId) {\n copy.lastStepId = nextStepId\n copy.stepStates[nextStepId].actionType = STARTED_STEP\n const lastAction = new Date()\n copy.stepStates[nextStepId].lastActionAt = lastAction.toISOString()\n this.steps.get(nextStepId).isStarted = true\n this.steps.get(nextStepId).lastActionAt = lastAction\n }\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n this.getGlobalState().userFlowStates[this.id] = copy\n\n // if all steps are now completed, mark flow completed\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = new Date()\n }\n\n stepObj.reset = async () => {\n const thisStep = this.steps.get(step.id)\n\n if (!thisStep.isCompleted) {\n return\n }\n\n thisStep.isCompleted = false\n thisStep.isStarted = false\n thisStep.lastActionAt = undefined\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.stepStates[thisStep.id].actionType = NOT_STARTED_STEP\n copy.stepStates[thisStep.id].lastActionAt = undefined\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: thisStep.id,\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n thisStep.isCompleted =\n updatedUserFlowState.stepStates[thisStep.id].actionType == COMPLETED_STEP\n thisStep.isStarted = updatedUserFlowState.stepStates[thisStep.id].actionType == STARTED_STEP\n thisStep.lastActionAt = undefined\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.isCompleted !== previousStep?.isCompleted ||\n thisStep.isStarted !== previousStep?.isStarted ||\n thisStep.isHidden !== previousStep?.isHidden ||\n thisStep.isBlocked !== previousStep?.isBlocked\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)\n })\n this.steps = newSteps\n // Check if empty object\n if (Object.keys(this.lastUsedVariables).length > 0) {\n this.applyVariables(this.lastUsedVariables)\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\n this.isStarted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = STARTED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowCompleted() {\n this.isStarted = true\n this.isCompleted = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = COMPLETED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\n }\n\n /**\n * @ignore\n */\n private optimisticallyMarkFlowSkipped() {\n this.isSkipped = true\n const copy = clone(this.getGlobalState().userFlowStates[this.id])\n copy.flowState = SKIPPED_FLOW\n this.getGlobalState().userFlowStates[this.id] = copy\n this.isVisible = false\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\n this.optimisticallyMarkFlowSkipped()\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: SKIPPED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 }\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 await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.groupId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\n\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\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 let maybeCurrentStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false && this.steps.get(key).isHidden === false\n )\n Array.from(this.steps.keys()).forEach((key) => {\n if (\n this.steps.get(key).isStarted &&\n this.steps.get(key).lastActionAt &&\n this.steps.get(key).lastActionAt >\n (this.steps.get(maybeCurrentStepId)?.lastActionAt ?? new Date(0))\n ) {\n maybeCurrentStepId = key\n }\n })\n\n const currentStepId = maybeCurrentStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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.isCompleted).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.isHidden).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 (\n (flow.id === this.id &&\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 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.lastUsedVariables = variables\n }\n\n /**\n * @ignore\n */\n private getUserFlowState(): UserFlowState {\n const userFlowStates = this.getGlobalState().userFlowStates\n return userFlowStates[this.id]\n }\n\n /**\n * @ignore\n */\n private async refreshUserFlowState() {\n await this.getGlobalState().refreshUserFlowStates()\n }\n}\n","import {\n FlowDataRaw,\n FlowStatus,\n FlowType,\n FrigadeConfig,\n TriggerType,\n UserFlowState,\n} 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'\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.refreshFlows()\n await this.refreshUserFlowStates()\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\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.refreshUserFlowStates()\n await this.refreshFlows()\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.refreshUserFlowStates()\n await this.refreshFlows()\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 refreshUserFlowStates(): 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 (\n target[key] &&\n target[key].flowState &&\n (JSON.stringify(target[key].flowState) !== JSON.stringify(value?.flowState) ||\n JSON.stringify(target[key].stepStates) !== JSON.stringify(value?.stepStates) ||\n JSON.stringify(target[key].shouldTrigger) !== JSON.stringify(value?.shouldTrigger))\n ) {\n that.triggerEventHandlers(target[key])\n }\n\n target[key] = value\n return true\n },\n }\n\n frigadeGlobalState[globalStateKey] = {\n refreshUserFlowStates: async () => {},\n userFlowStates: new Proxy({}, validator),\n onFlowStateChangeHandlerWrappers: new Map(),\n onStepStateChangeHandlerWrappers: new Map(),\n onFlowStateChangeHandlers: [],\n previousFlows: new Map(),\n }\n\n if (this.config.__readOnly && this.config.__flowConfigOverrides) {\n this.mockUserFlowStates(globalStateKey)\n\n return\n }\n\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n if (this.config.__readOnly) {\n return\n }\n\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${encodeURIComponent(this.config.userId)}${\n this.config.groupId\n ? `&foreignUserGroupId=${encodeURIComponent(this.config.groupId)}`\n : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n let shouldReload = false\n const before = frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId]\n\n // Special case: for flows that show up based on targeting logic/rules, we need to check if the flow should be triggered\n if (before && before.shouldTrigger == false && userFlowState.shouldTrigger == true) {\n shouldReload = true\n }\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n if (shouldReload) {\n this.flows.forEach((flow) => {\n if (flow.id == userFlowState.flowId) {\n flow.reload()\n this.triggerEventHandlers(\n frigadeGlobalState[globalStateKey].userFlowStates[flow.id]\n )\n }\n })\n }\n })\n this.hasFailed = false\n } else {\n this.hasFailed = true\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n /**\n * @ignore\n */\n private async refreshFlows() {\n this.flows = []\n\n if (this.config.__flowConfigOverrides) {\n this.mockFlowConfigs()\n return\n }\n\n const flowDataRaw = await this.fetch('/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowDatas = flowDataRaw.data as FlowDataRaw[]\n flowDatas.forEach((flowData) => {\n this.flows.push(new Flow(this.config, flowData))\n this.getGlobalState().previousFlows.set(\n flowData.slug,\n cloneFlow(this.flows[this.flows.length - 1])\n )\n })\n } else {\n this.hasFailed = true\n }\n }\n\n /**\n * @ignore\n */\n private mockFlowConfigs() {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n this.flows.push(\n new Flow(this.config, {\n id: -1,\n name: '',\n description: '',\n data: this.config.__flowConfigOverrides[flowId],\n createdAt: new Date().toISOString(),\n modifiedAt: new Date().toISOString(),\n slug: flowId,\n targetingLogic: '',\n type: FlowType.CHECKLIST,\n triggerType: TriggerType.MANUAL,\n status: FlowStatus.ACTIVE,\n version: 1,\n active: true,\n })\n )\n })\n }\n\n /**\n * @ignore\n */\n private mockUserFlowStates(globalStateKey: string) {\n Object.keys(this.config.__flowConfigOverrides).forEach((flowId) => {\n const parsed = JSON.parse(this.config.__flowConfigOverrides[flowId])\n frigadeGlobalState[globalStateKey].userFlowStates[flowId] = {\n flowId,\n flowState: 'NOT_STARTED_FLOW',\n lastStepId: null,\n userId: this.config.userId,\n foreignUserId: this.config.userId,\n stepStates:\n parsed?.steps?.reduce((acc, step) => {\n acc[step.id] = {\n stepId: step.id,\n flowSlug: flowId,\n actionType: 'NOT_STARTED_STEP',\n createdAt: new Date().toISOString(),\n blocked: false,\n hidden: false,\n }\n return acc\n }, {}) ?? {},\n shouldTrigger: false,\n }\n })\n }\n\n /**\n * @ignore\n */\n private async triggerEventHandlers(previousUserFlowState: UserFlowState) {\n if (previousUserFlowState) {\n this.flows.forEach((flow) => {\n if (flow.id == previousUserFlowState.flowId) {\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}\n"],"mappings":";ooBAgBO,IAAKA,OACVA,EAAA,OAAS,SACTA,EAAA,UAAY,YAFFA,OAAA,IAKAC,OACVA,EAAA,MAAQ,QACRA,EAAA,OAAS,SACTA,EAAA,SAAW,WAHDA,OAAA,IAiPAC,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,ICtQL,IAAMC,EAAiB,SCC9B,OAAOC,OAAW,cAClB,OAAS,MAAMC,OAAc,OCgBtB,IAAIC,EAAyD,CAAC,EAE9D,SAASC,EAAkBC,EAAuC,CApBzE,IAAAC,EAAAC,EAqBE,MAAO,GAAGF,EAAe,gBAAgBA,EAAe,WAAUC,EAAAD,EAAe,SAAf,KAAAC,EAAyB,OACzFC,EAAAF,EAAe,UAAf,KAAAE,EAA0B,IAE9B,CCpBO,IAAMC,EAAN,KAAgB,CAQrB,YAAYC,EAAuB,CAPnC,KAAO,OAAwB,CAC7B,OAAQ,GACR,OAAQ,oCACR,OAAQC,GAAgB,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,EA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X,WAAY,KACZ,OAAQ,KAAK,OAAO,OACpB,cAAe,KAAK,OAAO,OAC3B,YACEgB,GAAAvB,EAAAwB,GAAA,YAAAA,EAAQ,QAAR,YAAAxB,EAAe,OAAO,CAACyB,EAAKC,KAC1BD,EAAIC,EAAK,EAAE,EAAI,CACb,OAAQA,EAAK,GACb,SAAUnB,EACV,WAAY,mBACZ,UAAW,IAAI,KAAK,EAAE,YAAY,EAClC,QAAS,GACT,OAAQ,EACV,EACOkB,GACN,CAAC,KAVJ,KAAAF,EAUU,CAAC,EACb,cAAe,EACjB,CACF,CAAC,CACH,CAKc,qBAAqBI,EAAsC,QAAApC,EAAA,sBACnEoC,GACF,KAAK,MAAM,QAASnC,GAAS,CACvBA,EAAK,IAAMmC,EAAsB,QACnC,KAAK,eAAe,EAAE,0BAA0B,QAASlC,GAAY,CACnE,IAAMC,EAAW,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,EAAE,EAChEC,EAAQD,EAAME,CAAQ,EACtB,KAAK,eAAe,EAAE,cAAc,IAAIF,EAAK,GAAIG,EAAUH,CAAI,CAAC,CAClE,CAAC,CAEL,CAAC,CAEL,GACF","names":["TriggerType","FlowStatus","FlowType","VERSION_NUMBER","fetch","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","_a","_b","Fetchable","config","generateGuestId","filteredConfig","_","v","__spreadValues","path","options","__async","getEmptyResponse","gracefulFetch","getHeaders","globalStateKey","getGlobalStateKey","frigadeGlobalState","Flow","Fetchable","config","flowDataRaw","_a","_b","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","hasCompleted","targetingShouldHideFlow","newSteps","step","index","userFlowStateStep","stepObj","__spreadProps","__spreadValues","COMPLETED_STEP","STARTED_STEP","properties","__async","thisStep","copy","clone","updatedUserFlowState","isLastStep","nextStepId","lastAction","NOT_STARTED_STEP","handler","wrapperHandler","flow","previousStep","h","nextStep","NOT_STARTED_FLOW","maybeCurrentStepId","key","currentStepId","currentStep","previousFlow","variables","replaceVariables","str","matches","match","variable","NOT_STARTED_STEP","COMPLETED_FLOW","SKIPPED_FLOW","STARTED_FLOW","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","GUEST_KEY","GUEST_PREFIX","GET_CACHE_PREFIX","POST_CACHE_TTL_MS","LOCAL_STORAGE_PREFIX","cloneFlow","flow","newFlow","Flow","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","isWeb","setLocalStorage","value","clearCache","frigadeGlobalState","key","GET_CACHE_PREFIX","resetAllLocalStorage","isWeb","LOCAL_STORAGE_PREFIX","gracefulFetch","url","options","__async","lastCallAtKey","LAST_POST_CALL_AT","lastCallDataKey","LAST_POST_CALL_DATA","lastCall","getLocalStorage","lastCallData","lastCallDate","POST_CACHE_TTL_MS","getEmptyResponse","setLocalStorage","response","fetch","error","body","e","generateGuestId","guestId","GUEST_KEY","GUEST_PREFIX","uuidv4","isWeb","Frigade","Fetchable","apiKey","config","__spreadValues","__async","flow","handler","lastFlow","cloneFlow","isWeb","globalStateKey","getGlobalStateKey","frigadeGlobalState","_a","GUEST_PREFIX","userId","properties","__spreadProps","groupId","event","flowId","resetAllLocalStorage","clearCache","h","that","validator","target","key","value","userFlowStatesRaw","userFlowState","shouldReload","before","flowDataRaw","flowData","Flow","_b","parsed","acc","step","previousUserFlowState"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frigade/js",
3
- "version": "0.2.35",
3
+ "version": "0.2.36",
4
4
  "description": "The official Javascript SDK for Frigade.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",