@frigade/js 0.1.1 → 0.1.3

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 G=Object.create;var u=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,$=Object.prototype.hasOwnProperty;var k=(e,s)=>{for(var t in s)u(e,t,{get:s[t],enumerable:!0})},R=(e,s,t,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let a of K(s))!$.call(e,a)&&a!==t&&u(e,a,{get:()=>s[a],enumerable:!(i=W(s,a))||i.enumerable});return e};var J=(e,s,t)=>(t=e!=null?G(M(e)):{},R(s||!e||!e.__esModule?u(t,"default",{value:e,enumerable:!0}):t,e)),z=e=>R(u({},"__esModule",{value:!0}),e);var V={};k(V,{Flow:()=>d,Frigade:()=>y});module.exports=z(V);var P="0.1.1";var C=J(require("cross-fetch"),1),A=require("uuid");var O="COMPLETED_FLOW",D="SKIPPED_FLOW",E="STARTED_FLOW",L="NOT_STARTED_FLOW",w="COMPLETED_STEP",S="STARTED_STEP",H="frigade-last-call-at-",j="frigade-last-call-data-",_="frigade-guest-key",B="guest_";function v(e){return{headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json","X-Frigade-SDK-Version":P,"X-Frigade-SDK-Platform":"Javascript"}}}function b(e){return window&&window.localStorage?window.localStorage.getItem(e):null}function U(e,s){window&&window.localStorage&&window.localStorage.setItem(e,s)}function N(){window&&window.localStorage&&Object.keys(window.localStorage).forEach(e=>{e.startsWith("frigade-")&&window.localStorage.removeItem(e)})}async function x(e,s){let t=H+e,i=j+e;if(window&&window.localStorage&&s&&s.body&&s.method==="POST"){let o=b(t),l=b(i);if(o&&l&&l==s.body){let T=new Date(o);if(new Date().getTime()-T.getTime()<1e3)return m()}U(t,new Date().toISOString()),U(i,s.body)}let a;try{a=await(0,C.default)(e,s)}catch(o){return m(o)}return a?a.staus>=400?m(a.statusText):a.json():m()}function m(e){return e&&console.log("Call to Frigade failed",e),{json:()=>({})}}function F(){if(typeof window<"u"&&typeof window.localStorage<"u"){let e=b(_);return e||(e=`${B}${(0,A.v4)()}`,window.localStorage.setItem(_,e)),e}}var n={};function p(e){return`${e.__instanceId}-${e.apiKey}:${e.userId??""}:${e.organizationId??""}`}var c=class{constructor(s){this.config={apiKey:"",apiUrl:"//api.frigade.com/v1/public",userId:F(),__instanceId:Math.random().toString(36).substring(7)};this.config={...this.config,...s}}async fetch(s,t){return x(`${this.config.apiUrl}${s}`,{...t??{},...v(this.config.apiKey)})}};var d=class extends c{constructor(t,i){super(t);this.flowDataRaw=i,this.initFromRawData(i)}initFromRawData(t){let i=JSON.parse(t.data),a=i.steps??i.data??[];this.id=t.slug,this.metadata=t,this.rawData=i,this.title=this.rawData.title,this.subtitle=this.rawData.subtitle;let o=this.getUserFlowState();this.isCompleted=o.flowState==O,this.isStarted=o.flowState==E,this.isSkipped=o.flowState==D,this.steps=new Map,a.forEach((l,T)=>{let f=o.stepStates[l.id],h={...l,isCompleted:f.actionType==w,isStarted:f.actionType==S,isHidden:f.hidden,isBlocked:f.blocked,order:T};h.start=async I=>{let r=this.steps.get(l.id);if(r.isStarted||r.isCompleted)return;r.isStarted=!0,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:r.id,data:I??{},createdAt:new Date().toISOString(),actionType:S})}),await this.refreshUserFlowState();let g=this.getUserFlowState();r.isCompleted=g.stepStates[r.id].actionType==w,r.isStarted=g.stepStates[r.id].actionType==S},h.complete=async I=>{let r=this.steps.get(l.id);if(r.isCompleted)return;r.isCompleted=!0,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:r.id,data:I??{},createdAt:new Date().toISOString(),actionType:w})}),await this.refreshUserFlowState();let g=this.getUserFlowState();r.isCompleted=g.stepStates[r.id].actionType==w,r.isStarted=g.stepStates[r.id].actionType==S},this.steps.set(l.id,h)})}async start(t){this.isStarted||this.isCompleted||(await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:E})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}async complete(t){this.isCompleted||(await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:O})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}async skip(t){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:D})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}async restart(){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:L})})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let t=this.getUserFlowState(),a=Array.from(this.steps.keys()).find(o=>this.steps.get(o).isCompleted===!1)??Array.from(this.steps.keys())[0];return this.steps.get(a)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getUserFlowState(){return n[p(this.config)].userFlowStates[this.id]}async refreshUserFlowState(){await n[p(this.config)].refreshUserFlowStates()}};var y=class extends c{constructor(t,i){super({apiKey:t,...i});this.flows=[];this.onFlowStateChangeHandlers=[];this.init(this.config)}async init(t){return this.config={...this.config,...t},this.initPromise=(async()=>{await this.refreshUserFlowStates(),await this.refreshFlows()})(),this.initPromise}async identify(t,i){await this.initIfNeeded(),this.config.userId=t,await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:i})}),await this.refreshUserFlowStates()}async group(t,i){await this.initIfNeeded(),this.config.organizationId=t,await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,properties:i})}),await this.refreshUserFlowStates()}async track(t,i){await this.initIfNeeded(),await this.fetch("/track",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,event:t,properties:i})})}async getFlow(t){return await this.initIfNeeded(),this.flows.find(i=>i.id==t)}async getFlows(){return await this.initIfNeeded(),this.flows}async reset(){N(),this.config.userId=F(),this.config.organizationId=void 0}onFlowStateChange(t){this.onFlowStateChangeHandlers.push(t)}async initIfNeeded(){return this.initPromise!==null?this.initPromise:this.init(this.config)}async refreshUserFlowStates(){let t=p(this.config);n[t]={refreshUserFlowStates:async()=>{},userFlowStates:{}},n[t].refreshUserFlowStates=async()=>{let i=await this.fetch(`/userFlowStates?foreignUserId=${this.config.userId}${this.config.organizationId?`&foreignUserGroupId=${this.config.organizationId}`:""}`);i&&i.data&&i.data.forEach(o=>{n[t].userFlowStates["previous_"+o.flowId]=n[t].userFlowStates[o.flowId],n[t].userFlowStates[o.flowId]=o,this.triggerEventHandlers(n[t].userFlowStates["previous_"+o.flowId],o)})},await n[t].refreshUserFlowStates()}async refreshFlows(){this.flows=[];let t=await this.fetch("/flows");t&&t.data&&t.data.forEach(a=>{this.flows.push(new d(this.config,a))})}triggerEventHandlers(t,i){t&&t.flowState!==i.flowState&&this.flows.forEach(a=>{a.id==i.flowId&&this.onFlowStateChangeHandlers.forEach(o=>{o(a,i.flowState,t==null?void 0:t.flowState)})})}};0&&(module.exports={Flow,Frigade});
2
+ var k=Object.create;var I=Object.defineProperty;var z=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,B=Object.prototype.hasOwnProperty;var X=(e,s)=>{for(var t in s)I(e,t,{get:s[t],enumerable:!0})},N=(e,s,t,i)=>{if(s&&typeof s=="object"||typeof s=="function")for(let r of V(s))!B.call(e,r)&&r!==t&&I(e,r,{get:()=>s[r],enumerable:!(i=z(s,r))||i.enumerable});return e};var Y=(e,s,t)=>(t=e!=null?k(j(e)):{},N(s||!e||!e.__esModule?I(t,"default",{value:e,enumerable:!0}):t,e)),q=e=>N(I({},"__esModule",{value:!0}),e);var et={};X(et,{Flow:()=>f,Frigade:()=>E});module.exports=q(et);var L="0.1.3";var J=Y(require("cross-fetch"),1),G=require("uuid");var u="COMPLETED_FLOW",U="SKIPPED_FLOW",F="STARTED_FLOW",W="NOT_STARTED_FLOW",S="COMPLETED_STEP",h="STARTED_STEP",Q="frigade-last-call-at-",Z="frigade-last-call-data-",x="frigade-guest-key",tt="guest_";function m(e){return JSON.parse(JSON.stringify(e))}function K(e){return{headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json","X-Frigade-SDK-Version":L,"X-Frigade-SDK-Platform":"Javascript"}}}function _(e){return window&&window.localStorage?window.localStorage.getItem(e):null}function M(e,s){window&&window.localStorage&&window.localStorage.setItem(e,s)}function $(){window&&window.localStorage&&Object.keys(window.localStorage).forEach(e=>{e.startsWith("frigade-")&&window.localStorage.removeItem(e)})}async function H(e,s){let t=Q+e,i=Z+e;if(window&&window.localStorage&&s&&s.body&&s.method==="POST"){let o=_(t),l=_(i);if(o&&l&&l==s.body){let c=new Date(o);if(new Date().getTime()-c.getTime()<1e3)return O()}M(t,new Date().toISOString()),M(i,s.body)}let r;try{r=await(0,J.default)(e,s)}catch(o){return O(o)}return r?r.staus>=400?O(r.statusText):r.json():O()}function O(e){return e&&console.log("Call to Frigade failed",e),{json:()=>({})}}function D(){if(typeof window<"u"&&typeof window.localStorage<"u"){let e=_(x);return e||(e=`${tt}${(0,G.v4)()}`,window.localStorage.setItem(x,e)),e}}var n={};function d(e){return`${e.__instanceId}-${e.apiKey}:${e.userId??""}:${e.organizationId??""}`}var w=class{constructor(s){this.config={apiKey:"",apiUrl:"//api.frigade.com/v1/public",userId:D(),__instanceId:Math.random().toString(36).substring(7)};this.config={...this.config,...s}}async fetch(s,t){return H(`${this.config.apiUrl}${s}`,{...t??{},...K(this.config.apiKey)})}};var f=class extends w{constructor(t,i){super(t);this.isVisible=!1;this.nonce=Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15);this.flowDataRaw=i,this.initFromRawData(i)}initFromRawData(t){let i=JSON.parse(t.data),r=i.steps??i.data??[];this.id=t.slug,this.metadata=t,this.rawData=i,this.title=this.rawData.title,this.subtitle=this.rawData.subtitle;let o=this.getUserFlowState();this.isCompleted=o.flowState==u,this.isStarted=o.flowState==F,this.isSkipped=o.flowState==U;let l=this.isCompleted||this.isSkipped,c=t.targetingLogic&&o.shouldTrigger===!1;this.isVisible=!l&&!c,this.steps=new Map,r.forEach((p,C)=>{let y=o.stepStates[p.id],R={...p,isCompleted:y.actionType==S,isStarted:y.actionType==h,isHidden:y.hidden,isBlocked:y.blocked,order:C};R.start=async P=>{let a=this.steps.get(p.id);if(a.isStarted||a.isCompleted)return;a.isStarted=!0;let T=m(n[d(this.config)].userFlowStates[this.id]);T.stepStates[a.id].actionType=h,T.lastStepId=a.id,n[d(this.config)].userFlowStates[this.id]=T,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:a.id,data:P??{},createdAt:new Date().toISOString(),actionType:h})}),await this.refreshUserFlowState();let g=this.getUserFlowState();a.isCompleted=g.stepStates[a.id].actionType==S,a.isStarted=g.stepStates[a.id].actionType==h},R.complete=async P=>{let a=this.steps.get(p.id);if(a.isCompleted)return;let g=this.getNumberOfCompletedSteps()+1==this.steps.size;a.isCompleted=!0,this.isStarted=!0;let b=m(n[d(this.config)].userFlowStates[this.id]);b.stepStates[a.id].actionType=S,b.flowState=g?u:F;let v=Array.from(this.steps.keys())[C+1];v&&(b.lastStepId=v),g&&this.optimisticallyMarkFlowCompleted(),n[d(this.config)].userFlowStates[this.id]=b,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:a.id,data:P??{},createdAt:new Date().toISOString(),actionType:S})}),g?await this.complete():await this.refreshUserFlowState();let A=this.getUserFlowState();a.isCompleted=A.stepStates[a.id].actionType==S,a.isStarted=A.stepStates[a.id].actionType==h},this.steps.set(p.id,R)})}async start(t){if(this.isStarted||this.isCompleted)return;this.isStarted=!0;let i=m(n[d(this.config)].userFlowStates[this.id]);i.flowState=F,n[d(this.config)].userFlowStates[this.id]=i,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:F})}),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,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:u})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}optimisticallyMarkFlowCompleted(){this.isStarted=!0,this.isCompleted=!0;let t=m(n[d(this.config)].userFlowStates[this.id]);t.flowState=u,n[d(this.config)].userFlowStates[this.id]=t,this.isVisible=!1}async skip(t){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:U})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}async restart(){this.isCompleted=!1,this.isCompleted=!0,n[d(this.config)].userFlowStates[this.id]=null,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:W})}),await this.refreshUserFlowState()}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let i=Array.from(this.steps.keys()).find(r=>this.steps.get(r).isCompleted===!1)??Array.from(this.steps.keys())[0];return this.steps.get(i)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getUserFlowState(){return n[d(this.config)].userFlowStates[this.id]}async refreshUserFlowState(){await n[d(this.config)].refreshUserFlowStates()}};var E=class extends w{constructor(t,i){super({apiKey:t,...i});this.flows=[];this.onFlowStateChangeHandlers=[];this.previousFlows=new Map;this.init(this.config)}async init(t){return this.config={...this.config,...t},this.initPromise=(async()=>{await this.refreshUserFlowStates(),await this.refreshFlows()})(),this.initPromise}async identify(t,i){await this.initIfNeeded(),this.config.userId=t,await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:i})}),await this.refreshUserFlowStates()}async group(t,i){await this.initIfNeeded(),this.config.organizationId=t,await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,properties:i})}),await this.refreshUserFlowStates()}async track(t,i){await this.initIfNeeded(),await this.fetch("/track",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,event:t,properties:i})})}async getFlow(t){return await this.initIfNeeded(),this.flows.find(i=>i.id==t)}async getFlows(){return await this.initIfNeeded(),this.flows}async reset(){$(),this.config.userId=D(),this.config.organizationId=void 0}onFlowStateChange(t){this.onFlowStateChangeHandlers.push(t)}removeOnFlowStateChangeHandler(t){this.onFlowStateChangeHandlers=this.onFlowStateChangeHandlers.filter(i=>i!==t)}async initIfNeeded(){return this.initPromise!==null?this.initPromise:this.init(this.config)}async refreshUserFlowStates(){let t=d(this.config);if(!n[t]){let i=this,r={set:function(o,l,c){return o[l]&&o[l].flowState&&(JSON.stringify(o[l].flowState)!==JSON.stringify(c.flowState)||JSON.stringify(o[l].stepStates)!==JSON.stringify(c.stepStates)||JSON.stringify(o[l].shouldTrigger)!==JSON.stringify(c.shouldTrigger))&&i.triggerEventHandlers(o[l]),o[l]=c,!0}};n[t]={refreshUserFlowStates:async()=>{},userFlowStates:new Proxy({},r)},n[t].refreshUserFlowStates=async()=>{let o=await this.fetch(`/userFlowStates?foreignUserId=${this.config.userId}${this.config.organizationId?`&foreignUserGroupId=${this.config.organizationId}`:""}`);o&&o.data&&o.data.forEach(c=>{n[t].userFlowStates[c.flowId]=c})}}await n[t].refreshUserFlowStates()}async refreshFlows(){this.flows=[];let t=await this.fetch("/flows");t&&t.data&&t.data.forEach(r=>{this.flows.push(new f(this.config,r))})}async triggerEventHandlers(t){t&&this.flows.forEach(i=>{i.nonce=Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15),i.id==t.flowId&&this.onFlowStateChangeHandlers.forEach(r=>{let o=this.previousFlows.get(r),l=o?o.get(i.id):void 0;r(i,l),o||this.previousFlows.set(r,new Map),this.previousFlows.get(r).set(i.id,i)})})}};0&&(module.exports={Flow,Frigade});
3
3
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/Fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export { Frigade } from './core/frigade'\nimport Flow from './core/flow'\n\nexport { Flow }\n","export const VERSION_NUMBER = '0.1.1'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\nimport { v4 as uuidv4 } from 'uuid'\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'\nconst GUEST_PREFIX = 'guest_'\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 (window && window.localStorage) {\n return window.localStorage.getItem(key)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (window && window.localStorage) {\n window.localStorage.setItem(key, value)\n }\n}\n\nexport function resetAllLocalStorage() {\n if (window && window.localStorage) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith('frigade-')) {\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 (window && window.localStorage && 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 < 1000) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n }\n\n let response\n try {\n response = await fetch(url, options)\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse()\n }\n\n if (response.staus >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n return response.json()\n}\n\nfunction 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 (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n window.localStorage.setItem(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","import { FrigadeConfig, UserFlowState } from '../types'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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.organizationId ?? ''\n }`\n}\n","import { generateGuestId, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../types'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: '//api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(36).substring(7),\n }\n\n constructor(config: FrigadeConfig) {\n this.config = {\n ...this.config,\n ...config,\n }\n }\n\n public async fetch(path: string, options?: Record<any, any>) {\n return gracefulFetch(`${this.config.apiUrl}${path}`, {\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { FlowDataRaw } from './types'\nimport {\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { FlowStep } from './flow-step'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport default class Flow extends Fetchable {\n /**\n * THe Flow ID / slug of the flow\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object\n */\n public rawData: Record<any, any>\n /**\n * Ordered map from Step ID to step data. The `steps` array in `config.yml`\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the flow, if defined at the top level of `config.yml`\n */\n public title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `config.yml`\n */\n public subtitle?: string\n /**\n * The metadata of the flow.\n */\n public metadata: 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 private flowDataRaw: FlowDataRaw\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\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.metadata = flowDataRaw\n this.rawData = flowDataYml\n this.title = this.rawData.title\n this.subtitle = this.rawData.subtitle\n\n const userFlowState = this.getUserFlowState()\n\n this.isCompleted = userFlowState.flowState == COMPLETED_FLOW\n this.isStarted = userFlowState.flowState == STARTED_FLOW\n this.isSkipped = userFlowState.flowState == SKIPPED_FLOW\n this.steps = new Map()\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 order: index,\n } as FlowStep\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isStarted || currentStep.isCompleted) {\n return\n }\n\n currentStep.isStarted = true\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: currentStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isCompleted) {\n return\n }\n\n currentStep.isCompleted = true\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: currentStep.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 currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n this.steps.set(step.id, stepObj)\n })\n }\n\n /**\n * Function that 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 await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that marks the flow completed\n */\n public async complete(properties?: Record<string | number, any>) {\n if (this.isCompleted) {\n return\n }\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * Function that marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that restarts the flow/marks it not started\n */\n public async restart() {\n // TODO: Reset internal flow responses / steps / isStarted / isCompleted\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\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 * Function that gets current step\n */\n public getCurrentStep(): FlowStep {\n // Find the userFlowState with most recent timestamp\n const userFlowState = this.getUserFlowState()\n\n // TEMP: lastStepId appears to be the last step that a flowState event was recorded for?\n // const lastStepId =\n // userFlowState?.lastStepId?.length > 0 && userFlowState?.lastStepId !== 'unknown'\n // ? userFlowState?.lastStepId\n // : undefined\n\n // TEMP: Return the lowest-ordered incomplete step in the flow\n const lastStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false\n )\n\n const currentStepId = lastStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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 private getUserFlowState(): UserFlowState {\n const userFlowStates = frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates\n return userFlowStates[this.id]\n }\n\n private async refreshUserFlowState() {\n await frigadeGlobalState[getGlobalStateKey(this.config)].refreshUserFlowStates()\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { generateGuestId, resetAllLocalStorage, UserFlowStatus } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowDataRaw } from './types'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport class Frigade extends Fetchable {\n private flows: Flow[] = []\n private initPromise: Promise<void>\n private onFlowStateChangeHandlers: ((\n flow: Flow,\n newState: UserFlowStatus,\n previousState: UserFlowStatus\n ) => void)[] = []\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n\n this.init(this.config)\n }\n\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n await this.refreshUserFlowStates()\n await this.refreshFlows()\n })()\n\n return this.initPromise\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.userId = userId\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.organizationId = organizationId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n await this.fetch('/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n event,\n properties,\n }),\n })\n }\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 public async reset() {\n resetAllLocalStorage()\n this.config.userId = generateGuestId()\n this.config.organizationId = undefined\n }\n\n public onFlowStateChange(\n handler: (flow: Flow, newState: UserFlowStatus, previousState: UserFlowStatus) => void\n ) {\n this.onFlowStateChangeHandlers.push(handler)\n }\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 private async refreshUserFlowStates(): Promise<void> {\n const globalStateKey = getGlobalStateKey(this.config)\n frigadeGlobalState[globalStateKey] = {\n refreshUserFlowStates: async () => {},\n userFlowStates: {},\n }\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${this.config.userId}${\n this.config.organizationId ? `&foreignUserGroupId=${this.config.organizationId}` : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n frigadeGlobalState[globalStateKey].userFlowStates['previous_' + userFlowState.flowId] =\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId]\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n this.triggerEventHandlers(\n frigadeGlobalState[globalStateKey].userFlowStates['previous_' + userFlowState.flowId],\n userFlowState\n )\n })\n }\n }\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n private async refreshFlows() {\n this.flows = []\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 })\n }\n }\n\n private triggerEventHandlers(\n previousUserFlowState: UserFlowState,\n newUserFlowState: UserFlowState\n ) {\n if (previousUserFlowState && previousUserFlowState.flowState !== newUserFlowState.flowState) {\n this.flows.forEach((flow) => {\n if (flow.id == newUserFlowState.flowId) {\n this.onFlowStateChangeHandlers.forEach((handler) => {\n handler(flow, newUserFlowState.flowState, previousUserFlowState?.flowState)\n })\n }\n })\n }\n }\n}\n"],"mappings":";6iBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,EAAA,YAAAC,IAAA,eAAAC,EAAAJ,GCAO,IAAMK,EAAiB,QCC9B,IAAAC,EAAkB,4BAClBC,EAA6B,gBAGtB,IAAMC,EAAiB,iBACjBC,EAAe,eACfC,EAAe,eACfC,EAAmB,mBACnBC,EAAiB,iBACjBC,EAAe,eAGtBC,EAAoB,wBACpBC,EAAsB,0BACtBC,EAAY,oBACZC,EAAe,SAEd,SAASC,EAAWC,EAAgB,CACzC,MAAO,CACL,QAAS,CACP,cAAe,UAAUA,IACzB,eAAgB,mBAChB,wBAAyBC,EACzB,yBAA0B,YAC5B,CACF,CACF,CAEA,SAASC,EAAgBC,EAAa,CACpC,OAAI,QAAU,OAAO,aACZ,OAAO,aAAa,QAAQA,CAAG,EAEjC,IACT,CAEA,SAASC,EAAgBD,EAAaE,EAAe,CAC/C,QAAU,OAAO,cACnB,OAAO,aAAa,QAAQF,EAAKE,CAAK,CAE1C,CAEO,SAASC,GAAuB,CACjC,QAAU,OAAO,cAEnB,OAAO,KAAK,OAAO,YAAY,EAAE,QAASH,GAAQ,CAC5CA,EAAI,WAAW,UAAU,GAC3B,OAAO,aAAa,WAAWA,CAAG,CAEtC,CAAC,CAEL,CAEA,eAAsBI,EAAcC,EAAaC,EAAc,CAC7D,IAAMC,EAAgBf,EAAoBa,EACpCG,EAAkBf,EAAsBY,EAC9C,GAAI,QAAU,OAAO,cAAgBC,GAAWA,EAAQ,MAAQA,EAAQ,SAAW,OAAQ,CACzF,IAAMG,EAAWV,EAAgBQ,CAAa,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,MAAM,EACpFA,CACF,CACF,CAAC,CAEL,EACA,MAAMF,EAAmBF,CAAc,EAAE,sBAAsB,CACjE,CAEA,MAAc,cAAe,CAC3B,KAAK,MAAQ,CAAC,EACd,IAAMK,EAAc,MAAM,KAAK,MAAM,QAAQ,EACzCA,GAAeA,EAAY,MACbA,EAAY,KAClB,QAASC,GAAa,CAC9B,KAAK,MAAM,KAAK,IAAIC,EAAK,KAAK,OAAQD,CAAQ,CAAC,CACjD,CAAC,CAEL,CAEQ,qBACNE,EACAC,EACA,CACID,GAAyBA,EAAsB,YAAcC,EAAiB,WAChF,KAAK,MAAM,QAASb,GAAS,CACvBA,EAAK,IAAMa,EAAiB,QAC9B,KAAK,0BAA0B,QAASV,GAAY,CAClDA,EAAQH,EAAMa,EAAiB,UAAWD,GAAA,YAAAA,EAAuB,SAAS,CAC5E,CAAC,CAEL,CAAC,CAEL,CACF","names":["src_exports","__export","Flow","Frigade","__toCommonJS","VERSION_NUMBER","import_cross_fetch","import_uuid","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","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","resetAllLocalStorage","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","fetch","error","generateGuestId","guestId","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","Fetchable","config","generateGuestId","path","options","gracefulFetch","getHeaders","Flow","Fetchable","config","flowDataRaw","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","step","index","userFlowStateStep","stepObj","COMPLETED_STEP","STARTED_STEP","properties","currentStep","updatedUserFlowState","NOT_STARTED_FLOW","currentStepId","key","frigadeGlobalState","getGlobalStateKey","Frigade","Fetchable","apiKey","config","userId","properties","organizationId","event","flowId","flow","resetAllLocalStorage","generateGuestId","handler","globalStateKey","getGlobalStateKey","frigadeGlobalState","userFlowStatesRaw","userFlowState","flowDataRaw","flowData","Flow","previousUserFlowState","newUserFlowState"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/Fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export { Frigade } from './core/frigade'\nimport Flow from './core/flow'\n\nexport { Flow }\n\nexport * from './types'\n","export const VERSION_NUMBER = '0.1.3'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\nimport { v4 as uuidv4 } from 'uuid'\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'\nconst GUEST_PREFIX = 'guest_'\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 (window && window.localStorage) {\n return window.localStorage.getItem(key)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (window && window.localStorage) {\n window.localStorage.setItem(key, value)\n }\n}\n\nexport function resetAllLocalStorage() {\n if (window && window.localStorage) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith('frigade-')) {\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 (window && window.localStorage && 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 < 1000) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n }\n\n let response\n try {\n response = await fetch(url, options)\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse()\n }\n\n if (response.staus >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n return response.json()\n}\n\nfunction 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 (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n window.localStorage.setItem(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","import { FrigadeConfig, UserFlowState } from '../types'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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.organizationId ?? ''\n }`\n}\n","import { generateGuestId, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../types'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: '//api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(36).substring(7),\n }\n\n constructor(config: FrigadeConfig) {\n this.config = {\n ...this.config,\n ...config,\n }\n }\n\n public async fetch(path: string, options?: Record<any, any>) {\n return gracefulFetch(`${this.config.apiUrl}${path}`, {\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { FlowDataRaw } from './types'\nimport {\n clone,\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { FlowStep } from './flow-step'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport default class Flow extends Fetchable {\n /**\n * THe Flow ID / slug of the flow\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object\n */\n public rawData: Record<any, any>\n /**\n * Ordered map from Step ID to step data. The `steps` array in `config.yml`\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the flow, if defined at the top level of `config.yml`\n */\n public title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `config.yml`\n */\n public subtitle?: string\n /**\n * The metadata of the flow.\n */\n public metadata: 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 /**\n * Nonce\n */\n public nonce =\n Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)\n\n private flowDataRaw: FlowDataRaw\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\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.metadata = flowDataRaw\n this.rawData = flowDataYml\n this.title = this.rawData.title\n this.subtitle = this.rawData.subtitle\n\n const userFlowState = this.getUserFlowState()\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 this.steps = new Map()\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 order: index,\n } as FlowStep\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isStarted || currentStep.isCompleted) {\n return\n }\n\n currentStep.isStarted = true\n const copy = clone(\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id]\n )\n copy.stepStates[currentStep.id].actionType = STARTED_STEP\n copy.lastStepId = currentStep.id\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: currentStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isCompleted) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n\n currentStep.isCompleted = true\n this.isStarted = true\n const copy = clone(\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id]\n )\n\n copy.stepStates[currentStep.id].actionType = COMPLETED_STEP\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 }\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n frigadeGlobalState[getGlobalStateKey(this.config)].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 flowSlug: this.id,\n stepId: currentStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_STEP,\n }),\n })\n\n if (isLastStep) {\n await this.complete()\n } else {\n await this.refreshUserFlowState()\n }\n\n const updatedUserFlowState = this.getUserFlowState()\n currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n this.steps.set(step.id, stepObj)\n })\n }\n\n /**\n * Function that 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(frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id])\n copy.flowState = STARTED_FLOW\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that 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 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 private optimisticallyMarkFlowCompleted() {\n this.isStarted = true\n this.isCompleted = true\n const copy = clone(frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id])\n copy.flowState = COMPLETED_FLOW\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = copy\n this.isVisible = false\n }\n\n /**\n * Function that marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that restarts the flow/marks it not started\n */\n public async restart() {\n this.isCompleted = false\n this.isCompleted = true\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = null\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\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 * Function that gets current step\n */\n public getCurrentStep(): FlowStep {\n const lastStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false\n )\n\n const currentStepId = lastStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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 private getUserFlowState(): UserFlowState {\n const userFlowStates = frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates\n return userFlowStates[this.id]\n }\n\n private async refreshUserFlowState() {\n await frigadeGlobalState[getGlobalStateKey(this.config)].refreshUserFlowStates()\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { generateGuestId, resetAllLocalStorage } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowDataRaw } from './types'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport class Frigade extends Fetchable {\n private flows: Flow[] = []\n private initPromise: Promise<void>\n private onFlowStateChangeHandlers: ((flow: Flow, previousFlow?: Flow) => void)[] = []\n private previousFlows: Map<(flow: Flow, previousFlow: Flow) => void, Map<string, Flow>> =\n new Map()\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n\n this.init(this.config)\n }\n\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n await this.refreshUserFlowStates()\n await this.refreshFlows()\n })()\n\n return this.initPromise\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.userId = userId\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.organizationId = organizationId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n await this.fetch('/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n event,\n properties,\n }),\n })\n }\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 public async reset() {\n resetAllLocalStorage()\n this.config.userId = generateGuestId()\n this.config.organizationId = undefined\n }\n\n public onFlowStateChange(handler: (flow: Flow, previousFlow?: Flow) => void) {\n this.onFlowStateChangeHandlers.push(handler)\n }\n\n public removeOnFlowStateChangeHandler(handler: (flow: Flow, previousFlow?: Flow) => void) {\n this.onFlowStateChangeHandlers = this.onFlowStateChangeHandlers.filter((h) => h !== handler)\n }\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 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 }\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${this.config.userId}${\n this.config.organizationId ? `&foreignUserGroupId=${this.config.organizationId}` : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n })\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n private async refreshFlows() {\n this.flows = []\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 })\n }\n }\n\n private async triggerEventHandlers(previousUserFlowState: UserFlowState) {\n if (previousUserFlowState) {\n this.flows.forEach((flow) => {\n flow.nonce =\n Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)\n if (flow.id == previousUserFlowState.flowId) {\n this.onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlows = this.previousFlows.get(handler)\n const lastFlow = lastFlows ? lastFlows.get(flow.id) : undefined\n handler(flow, lastFlow)\n if (!lastFlows) {\n this.previousFlows.set(hand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EAAE,EACtFA,EAAK,IAAMkB,EAAsB,QACnC,KAAK,0BAA0B,QAASf,GAAY,CAClD,IAAMgB,EAAY,KAAK,cAAc,IAAIhB,CAAO,EAC1CiB,EAAWD,EAAYA,EAAU,IAAInB,EAAK,EAAE,EAAI,OACtDG,EAAQH,EAAMoB,CAAQ,EACjBD,GACH,KAAK,cAAc,IAAIhB,EAAS,IAAI,GAAK,EAE3C,KAAK,cAAc,IAAIA,CAAO,EAAE,IAAIH,EAAK,GAAIA,CAAI,CACnD,CAAC,CAEL,CAAC,CAEL,CACF","names":["src_exports","__export","Flow","Frigade","__toCommonJS","VERSION_NUMBER","import_cross_fetch","import_uuid","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","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","resetAllLocalStorage","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","fetch","error","generateGuestId","guestId","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","Fetchable","config","generateGuestId","path","options","gracefulFetch","getHeaders","Flow","Fetchable","config","flowDataRaw","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","hasCompleted","targetingShouldHideFlow","step","index","userFlowStateStep","stepObj","COMPLETED_STEP","STARTED_STEP","properties","currentStep","copy","clone","frigadeGlobalState","getGlobalStateKey","updatedUserFlowState","isLastStep","nextStepId","NOT_STARTED_FLOW","currentStepId","key","Frigade","Fetchable","apiKey","config","userId","properties","organizationId","event","flowId","flow","resetAllLocalStorage","generateGuestId","handler","h","globalStateKey","getGlobalStateKey","frigadeGlobalState","that","validator","target","key","value","userFlowStatesRaw","userFlowState","flowDataRaw","flowData","Flow","previousUserFlowState","lastFlows","lastFlow"]}
package/dist/index.d.ts CHANGED
@@ -14,8 +14,29 @@ interface FrigadeConfig {
14
14
  organizationId?: string;
15
15
  __instanceId?: string;
16
16
  }
17
-
18
- type UserFlowStatus = 'NOT_STARTED_FLOW' | 'STARTED_FLOW' | 'COMPLETED_FLOW' | 'SKIPPED_FLOW';
17
+ interface UserFlowState {
18
+ flowId: string;
19
+ flowState: 'COMPLETED_FLOW' | 'STARTED_FLOW' | 'SKIPPED_FLOW' | 'NOT_STARTED_FLOW';
20
+ lastStepId: string;
21
+ userId: string;
22
+ foreignUserId: string;
23
+ stepStates: Record<string, UserFlowStepState>;
24
+ shouldTrigger: boolean;
25
+ }
26
+ interface UserFlowStepState {
27
+ stepId: string;
28
+ flowSlug: string;
29
+ actionType: 'NOT_STARTED_STEP' | 'STARTED_STEP' | 'COMPLETED_STEP';
30
+ createdAt: string;
31
+ blocked: boolean;
32
+ hidden: boolean;
33
+ }
34
+ interface InternalConfig {
35
+ apiKey: string;
36
+ userId?: string;
37
+ organizationId?: string;
38
+ __instanceId?: string;
39
+ }
19
40
 
20
41
  interface FlowDataRaw {
21
42
  id: number;
@@ -207,6 +228,14 @@ declare class Flow extends Fetchable {
207
228
  * Whether the flow has been skipped or not
208
229
  */
209
230
  isSkipped: boolean;
231
+ /**
232
+ * Whether the flow is visible to the user based on the current user/group's state
233
+ */
234
+ isVisible: boolean;
235
+ /**
236
+ * Nonce
237
+ */
238
+ nonce: string;
210
239
  private flowDataRaw;
211
240
  constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw);
212
241
  private initFromRawData;
@@ -218,6 +247,7 @@ declare class Flow extends Fetchable {
218
247
  * Function that marks the flow completed
219
248
  */
220
249
  complete(properties?: Record<string | number, any>): Promise<void>;
250
+ private optimisticallyMarkFlowCompleted;
221
251
  /**
222
252
  * Function that marks the flow skipped
223
253
  */
@@ -247,6 +277,7 @@ declare class Frigade extends Fetchable {
247
277
  private flows;
248
278
  private initPromise;
249
279
  private onFlowStateChangeHandlers;
280
+ private previousFlows;
250
281
  constructor(apiKey: string, config?: FrigadeConfig);
251
282
  private init;
252
283
  identify(userId: string, properties?: Record<string, any>): Promise<void>;
@@ -255,11 +286,12 @@ declare class Frigade extends Fetchable {
255
286
  getFlow(flowId: string): Promise<Flow>;
256
287
  getFlows(): Promise<Flow[]>;
257
288
  reset(): Promise<void>;
258
- onFlowStateChange(handler: (flow: Flow, newState: UserFlowStatus, previousState: UserFlowStatus) => void): void;
289
+ onFlowStateChange(handler: (flow: Flow, previousFlow?: Flow) => void): void;
290
+ removeOnFlowStateChangeHandler(handler: (flow: Flow, previousFlow?: Flow) => void): void;
259
291
  private initIfNeeded;
260
292
  private refreshUserFlowStates;
261
293
  private refreshFlows;
262
294
  private triggerEventHandlers;
263
295
  }
264
296
 
265
- export { Flow, Frigade };
297
+ export { Flow, Frigade, FrigadeConfig, InternalConfig, UserFlowState, UserFlowStepState };
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  "use client";
2
- var E="0.1.1";import L from"cross-fetch";import{v4 as v}from"uuid";var I="COMPLETED_FLOW",b="SKIPPED_FLOW",O="STARTED_FLOW",_="NOT_STARTED_FLOW",w="COMPLETED_STEP",S="STARTED_STEP",N="frigade-last-call-at-",x="frigade-last-call-data-",R="frigade-guest-key",G="guest_";function U(i){return{headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","X-Frigade-SDK-Version":E,"X-Frigade-SDK-Platform":"Javascript"}}}function T(i){return window&&window.localStorage?window.localStorage.getItem(i):null}function P(i,r){window&&window.localStorage&&window.localStorage.setItem(i,r)}function C(){window&&window.localStorage&&Object.keys(window.localStorage).forEach(i=>{i.startsWith("frigade-")&&window.localStorage.removeItem(i)})}async function A(i,r){let t=N+i,e=x+i;if(window&&window.localStorage&&r&&r.body&&r.method==="POST"){let s=T(t),l=T(e);if(s&&l&&l==r.body){let F=new Date(s);if(new Date().getTime()-F.getTime()<1e3)return u()}P(t,new Date().toISOString()),P(e,r.body)}let o;try{o=await L(i,r)}catch(s){return u(s)}return o?o.staus>=400?u(o.statusText):o.json():u()}function u(i){return i&&console.log("Call to Frigade failed",i),{json:()=>({})}}function m(){if(typeof window<"u"&&typeof window.localStorage<"u"){let i=T(R);return i||(i=`${G}${v()}`,window.localStorage.setItem(R,i)),i}}var n={};function p(i){return`${i.__instanceId}-${i.apiKey}:${i.userId??""}:${i.organizationId??""}`}var d=class{constructor(r){this.config={apiKey:"",apiUrl:"//api.frigade.com/v1/public",userId:m(),__instanceId:Math.random().toString(36).substring(7)};this.config={...this.config,...r}}async fetch(r,t){return A(`${this.config.apiUrl}${r}`,{...t??{},...U(this.config.apiKey)})}};var c=class extends d{constructor(t,e){super(t);this.flowDataRaw=e,this.initFromRawData(e)}initFromRawData(t){let e=JSON.parse(t.data),o=e.steps??e.data??[];this.id=t.slug,this.metadata=t,this.rawData=e,this.title=this.rawData.title,this.subtitle=this.rawData.subtitle;let s=this.getUserFlowState();this.isCompleted=s.flowState==I,this.isStarted=s.flowState==O,this.isSkipped=s.flowState==b,this.steps=new Map,o.forEach((l,F)=>{let f=s.stepStates[l.id],h={...l,isCompleted:f.actionType==w,isStarted:f.actionType==S,isHidden:f.hidden,isBlocked:f.blocked,order:F};h.start=async y=>{let a=this.steps.get(l.id);if(a.isStarted||a.isCompleted)return;a.isStarted=!0,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:a.id,data:y??{},createdAt:new Date().toISOString(),actionType:S})}),await this.refreshUserFlowState();let g=this.getUserFlowState();a.isCompleted=g.stepStates[a.id].actionType==w,a.isStarted=g.stepStates[a.id].actionType==S},h.complete=async y=>{let a=this.steps.get(l.id);if(a.isCompleted)return;a.isCompleted=!0,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:a.id,data:y??{},createdAt:new Date().toISOString(),actionType:w})}),await this.refreshUserFlowState();let g=this.getUserFlowState();a.isCompleted=g.stepStates[a.id].actionType==w,a.isStarted=g.stepStates[a.id].actionType==S},this.steps.set(l.id,h)})}async start(t){this.isStarted||this.isCompleted||(await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:O})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}async complete(t){this.isCompleted||(await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:I})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}async skip(t){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:b})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}async restart(){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:_})})}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let t=this.getUserFlowState(),o=Array.from(this.steps.keys()).find(s=>this.steps.get(s).isCompleted===!1)??Array.from(this.steps.keys())[0];return this.steps.get(o)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getUserFlowState(){return n[p(this.config)].userFlowStates[this.id]}async refreshUserFlowState(){await n[p(this.config)].refreshUserFlowStates()}};var D=class extends d{constructor(t,e){super({apiKey:t,...e});this.flows=[];this.onFlowStateChangeHandlers=[];this.init(this.config)}async init(t){return this.config={...this.config,...t},this.initPromise=(async()=>{await this.refreshUserFlowStates(),await this.refreshFlows()})(),this.initPromise}async identify(t,e){await this.initIfNeeded(),this.config.userId=t,await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),await this.refreshUserFlowStates()}async group(t,e){await this.initIfNeeded(),this.config.organizationId=t,await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,properties:e})}),await this.refreshUserFlowStates()}async track(t,e){await this.initIfNeeded(),await this.fetch("/track",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,event:t,properties:e})})}async getFlow(t){return await this.initIfNeeded(),this.flows.find(e=>e.id==t)}async getFlows(){return await this.initIfNeeded(),this.flows}async reset(){C(),this.config.userId=m(),this.config.organizationId=void 0}onFlowStateChange(t){this.onFlowStateChangeHandlers.push(t)}async initIfNeeded(){return this.initPromise!==null?this.initPromise:this.init(this.config)}async refreshUserFlowStates(){let t=p(this.config);n[t]={refreshUserFlowStates:async()=>{},userFlowStates:{}},n[t].refreshUserFlowStates=async()=>{let e=await this.fetch(`/userFlowStates?foreignUserId=${this.config.userId}${this.config.organizationId?`&foreignUserGroupId=${this.config.organizationId}`:""}`);e&&e.data&&e.data.forEach(s=>{n[t].userFlowStates["previous_"+s.flowId]=n[t].userFlowStates[s.flowId],n[t].userFlowStates[s.flowId]=s,this.triggerEventHandlers(n[t].userFlowStates["previous_"+s.flowId],s)})},await n[t].refreshUserFlowStates()}async refreshFlows(){this.flows=[];let t=await this.fetch("/flows");t&&t.data&&t.data.forEach(o=>{this.flows.push(new c(this.config,o))})}triggerEventHandlers(t,e){t&&t.flowState!==e.flowState&&this.flows.forEach(o=>{o.id==e.flowId&&this.onFlowStateChangeHandlers.forEach(s=>{s(o,e.flowState,t==null?void 0:t.flowState)})})}};export{c as Flow,D as Frigade};
2
+ var A="0.1.3";import W from"cross-fetch";import{v4 as K}from"uuid";var u="COMPLETED_FLOW",P="SKIPPED_FLOW",F="STARTED_FLOW",x="NOT_STARTED_FLOW",g="COMPLETED_STEP",S="STARTED_STEP",$="frigade-last-call-at-",H="frigade-last-call-data-",N="frigade-guest-key",k="guest_";function m(i){return JSON.parse(JSON.stringify(i))}function M(i){return{headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","X-Frigade-SDK-Version":A,"X-Frigade-SDK-Platform":"Javascript"}}}function R(i){return window&&window.localStorage?window.localStorage.getItem(i):null}function L(i,l){window&&window.localStorage&&window.localStorage.setItem(i,l)}function J(){window&&window.localStorage&&Object.keys(window.localStorage).forEach(i=>{i.startsWith("frigade-")&&window.localStorage.removeItem(i)})}async function G(i,l){let t=$+i,e=H+i;if(window&&window.localStorage&&l&&l.body&&l.method==="POST"){let s=R(t),n=R(e);if(s&&n&&n==l.body){let c=new Date(s);if(new Date().getTime()-c.getTime()<1e3)return I()}L(t,new Date().toISOString()),L(e,l.body)}let r;try{r=await W(i,l)}catch(s){return I(s)}return r?r.staus>=400?I(r.statusText):r.json():I()}function I(i){return i&&console.log("Call to Frigade failed",i),{json:()=>({})}}function O(){if(typeof window<"u"&&typeof window.localStorage<"u"){let i=R(N);return i||(i=`${k}${K()}`,window.localStorage.setItem(N,i)),i}}var a={};function d(i){return`${i.__instanceId}-${i.apiKey}:${i.userId??""}:${i.organizationId??""}`}var h=class{constructor(l){this.config={apiKey:"",apiUrl:"//api.frigade.com/v1/public",userId:O(),__instanceId:Math.random().toString(36).substring(7)};this.config={...this.config,...l}}async fetch(l,t){return G(`${this.config.apiUrl}${l}`,{...t??{},...M(this.config.apiKey)})}};var w=class extends h{constructor(t,e){super(t);this.isVisible=!1;this.nonce=Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15);this.flowDataRaw=e,this.initFromRawData(e)}initFromRawData(t){let e=JSON.parse(t.data),r=e.steps??e.data??[];this.id=t.slug,this.metadata=t,this.rawData=e,this.title=this.rawData.title,this.subtitle=this.rawData.subtitle;let s=this.getUserFlowState();this.isCompleted=s.flowState==u,this.isStarted=s.flowState==F,this.isSkipped=s.flowState==P;let n=this.isCompleted||this.isSkipped,c=t.targetingLogic&&s.shouldTrigger===!1;this.isVisible=!n&&!c,this.steps=new Map,r.forEach((f,D)=>{let y=s.stepStates[f.id],E={...f,isCompleted:y.actionType==g,isStarted:y.actionType==S,isHidden:y.hidden,isBlocked:y.blocked,order:D};E.start=async C=>{let o=this.steps.get(f.id);if(o.isStarted||o.isCompleted)return;o.isStarted=!0;let T=m(a[d(this.config)].userFlowStates[this.id]);T.stepStates[o.id].actionType=S,T.lastStepId=o.id,a[d(this.config)].userFlowStates[this.id]=T,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:o.id,data:C??{},createdAt:new Date().toISOString(),actionType:S})}),await this.refreshUserFlowState();let p=this.getUserFlowState();o.isCompleted=p.stepStates[o.id].actionType==g,o.isStarted=p.stepStates[o.id].actionType==S},E.complete=async C=>{let o=this.steps.get(f.id);if(o.isCompleted)return;let p=this.getNumberOfCompletedSteps()+1==this.steps.size;o.isCompleted=!0,this.isStarted=!0;let b=m(a[d(this.config)].userFlowStates[this.id]);b.stepStates[o.id].actionType=g,b.flowState=p?u:F;let U=Array.from(this.steps.keys())[D+1];U&&(b.lastStepId=U),p&&this.optimisticallyMarkFlowCompleted(),a[d(this.config)].userFlowStates[this.id]=b,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:o.id,data:C??{},createdAt:new Date().toISOString(),actionType:g})}),p?await this.complete():await this.refreshUserFlowState();let v=this.getUserFlowState();o.isCompleted=v.stepStates[o.id].actionType==g,o.isStarted=v.stepStates[o.id].actionType==S},this.steps.set(f.id,E)})}async start(t){if(this.isStarted||this.isCompleted)return;this.isStarted=!0;let e=m(a[d(this.config)].userFlowStates[this.id]);e.flowState=F,a[d(this.config)].userFlowStates[this.id]=e,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:F})}),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,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:u})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw))}optimisticallyMarkFlowCompleted(){this.isStarted=!0,this.isCompleted=!0;let t=m(a[d(this.config)].userFlowStates[this.id]);t.flowState=u,a[d(this.config)].userFlowStates[this.id]=t,this.isVisible=!1}async skip(t){await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:this.getCurrentStep().id,data:t??{},createdAt:new Date().toISOString(),actionType:P})}),await this.refreshUserFlowState(),this.initFromRawData(this.flowDataRaw)}async restart(){this.isCompleted=!1,this.isCompleted=!0,a[d(this.config)].userFlowStates[this.id]=null,await this.fetch("/flowResponses",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,flowSlug:this.id,stepId:"unknown",data:{},createdAt:new Date().toISOString(),actionType:x})}),await this.refreshUserFlowState()}getStepByIndex(t){return this.steps.get(Array.from(this.steps.keys())[t])}getCurrentStep(){let e=Array.from(this.steps.keys()).find(r=>this.steps.get(r).isCompleted===!1)??Array.from(this.steps.keys())[0];return this.steps.get(e)}getNumberOfCompletedSteps(){return Array.from(this.steps.values()).filter(t=>t.isCompleted).length}getUserFlowState(){return a[d(this.config)].userFlowStates[this.id]}async refreshUserFlowState(){await a[d(this.config)].refreshUserFlowStates()}};var _=class extends h{constructor(t,e){super({apiKey:t,...e});this.flows=[];this.onFlowStateChangeHandlers=[];this.previousFlows=new Map;this.init(this.config)}async init(t){return this.config={...this.config,...t},this.initPromise=(async()=>{await this.refreshUserFlowStates(),await this.refreshFlows()})(),this.initPromise}async identify(t,e){await this.initIfNeeded(),this.config.userId=t,await this.fetch("/users",{method:"POST",body:JSON.stringify({foreignId:this.config.userId,properties:e})}),await this.refreshUserFlowStates()}async group(t,e){await this.initIfNeeded(),this.config.organizationId=t,await this.fetch("/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,properties:e})}),await this.refreshUserFlowStates()}async track(t,e){await this.initIfNeeded(),await this.fetch("/track",{method:"POST",body:JSON.stringify({foreignUserId:this.config.userId,foreignUserGroupId:this.config.organizationId,event:t,properties:e})})}async getFlow(t){return await this.initIfNeeded(),this.flows.find(e=>e.id==t)}async getFlows(){return await this.initIfNeeded(),this.flows}async reset(){J(),this.config.userId=O(),this.config.organizationId=void 0}onFlowStateChange(t){this.onFlowStateChangeHandlers.push(t)}removeOnFlowStateChangeHandler(t){this.onFlowStateChangeHandlers=this.onFlowStateChangeHandlers.filter(e=>e!==t)}async initIfNeeded(){return this.initPromise!==null?this.initPromise:this.init(this.config)}async refreshUserFlowStates(){let t=d(this.config);if(!a[t]){let e=this,r={set:function(s,n,c){return s[n]&&s[n].flowState&&(JSON.stringify(s[n].flowState)!==JSON.stringify(c.flowState)||JSON.stringify(s[n].stepStates)!==JSON.stringify(c.stepStates)||JSON.stringify(s[n].shouldTrigger)!==JSON.stringify(c.shouldTrigger))&&e.triggerEventHandlers(s[n]),s[n]=c,!0}};a[t]={refreshUserFlowStates:async()=>{},userFlowStates:new Proxy({},r)},a[t].refreshUserFlowStates=async()=>{let s=await this.fetch(`/userFlowStates?foreignUserId=${this.config.userId}${this.config.organizationId?`&foreignUserGroupId=${this.config.organizationId}`:""}`);s&&s.data&&s.data.forEach(c=>{a[t].userFlowStates[c.flowId]=c})}}await a[t].refreshUserFlowStates()}async refreshFlows(){this.flows=[];let t=await this.fetch("/flows");t&&t.data&&t.data.forEach(r=>{this.flows.push(new w(this.config,r))})}async triggerEventHandlers(t){t&&this.flows.forEach(e=>{e.nonce=Math.random().toString(36).substring(2,15)+Math.random().toString(36).substring(2,15),e.id==t.flowId&&this.onFlowStateChangeHandlers.forEach(r=>{let s=this.previousFlows.get(r),n=s?s.get(e.id):void 0;r(e,n),s||this.previousFlows.set(r,new Map),this.previousFlows.get(r).set(e.id,e)})})}};export{w as Flow,_ as Frigade};
3
3
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/Fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export const VERSION_NUMBER = '0.1.1'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\nimport { v4 as uuidv4 } from 'uuid'\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'\nconst GUEST_PREFIX = 'guest_'\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 (window && window.localStorage) {\n return window.localStorage.getItem(key)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (window && window.localStorage) {\n window.localStorage.setItem(key, value)\n }\n}\n\nexport function resetAllLocalStorage() {\n if (window && window.localStorage) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith('frigade-')) {\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 (window && window.localStorage && 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 < 1000) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n }\n\n let response\n try {\n response = await fetch(url, options)\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse()\n }\n\n if (response.staus >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n return response.json()\n}\n\nfunction 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 (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n window.localStorage.setItem(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","import { FrigadeConfig, UserFlowState } from '../types'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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.organizationId ?? ''\n }`\n}\n","import { generateGuestId, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../types'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: '//api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(36).substring(7),\n }\n\n constructor(config: FrigadeConfig) {\n this.config = {\n ...this.config,\n ...config,\n }\n }\n\n public async fetch(path: string, options?: Record<any, any>) {\n return gracefulFetch(`${this.config.apiUrl}${path}`, {\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { FlowDataRaw } from './types'\nimport {\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { FlowStep } from './flow-step'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport default class Flow extends Fetchable {\n /**\n * THe Flow ID / slug of the flow\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object\n */\n public rawData: Record<any, any>\n /**\n * Ordered map from Step ID to step data. The `steps` array in `config.yml`\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the flow, if defined at the top level of `config.yml`\n */\n public title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `config.yml`\n */\n public subtitle?: string\n /**\n * The metadata of the flow.\n */\n public metadata: 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 private flowDataRaw: FlowDataRaw\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\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.metadata = flowDataRaw\n this.rawData = flowDataYml\n this.title = this.rawData.title\n this.subtitle = this.rawData.subtitle\n\n const userFlowState = this.getUserFlowState()\n\n this.isCompleted = userFlowState.flowState == COMPLETED_FLOW\n this.isStarted = userFlowState.flowState == STARTED_FLOW\n this.isSkipped = userFlowState.flowState == SKIPPED_FLOW\n this.steps = new Map()\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 order: index,\n } as FlowStep\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isStarted || currentStep.isCompleted) {\n return\n }\n\n currentStep.isStarted = true\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: currentStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isCompleted) {\n return\n }\n\n currentStep.isCompleted = true\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: currentStep.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 currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n this.steps.set(step.id, stepObj)\n })\n }\n\n /**\n * Function that 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 await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that marks the flow completed\n */\n public async complete(properties?: Record<string | number, any>) {\n if (this.isCompleted) {\n return\n }\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: this.getCurrentStep().id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\n this.initFromRawData(this.flowDataRaw)\n }\n\n /**\n * Function that marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that restarts the flow/marks it not started\n */\n public async restart() {\n // TODO: Reset internal flow responses / steps / isStarted / isCompleted\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\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 * Function that gets current step\n */\n public getCurrentStep(): FlowStep {\n // Find the userFlowState with most recent timestamp\n const userFlowState = this.getUserFlowState()\n\n // TEMP: lastStepId appears to be the last step that a flowState event was recorded for?\n // const lastStepId =\n // userFlowState?.lastStepId?.length > 0 && userFlowState?.lastStepId !== 'unknown'\n // ? userFlowState?.lastStepId\n // : undefined\n\n // TEMP: Return the lowest-ordered incomplete step in the flow\n const lastStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false\n )\n\n const currentStepId = lastStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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 private getUserFlowState(): UserFlowState {\n const userFlowStates = frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates\n return userFlowStates[this.id]\n }\n\n private async refreshUserFlowState() {\n await frigadeGlobalState[getGlobalStateKey(this.config)].refreshUserFlowStates()\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { generateGuestId, resetAllLocalStorage, UserFlowStatus } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowDataRaw } from './types'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport class Frigade extends Fetchable {\n private flows: Flow[] = []\n private initPromise: Promise<void>\n private onFlowStateChangeHandlers: ((\n flow: Flow,\n newState: UserFlowStatus,\n previousState: UserFlowStatus\n ) => void)[] = []\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n\n this.init(this.config)\n }\n\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n await this.refreshUserFlowStates()\n await this.refreshFlows()\n })()\n\n return this.initPromise\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.userId = userId\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.organizationId = organizationId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n await this.fetch('/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n event,\n properties,\n }),\n })\n }\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 public async reset() {\n resetAllLocalStorage()\n this.config.userId = generateGuestId()\n this.config.organizationId = undefined\n }\n\n public onFlowStateChange(\n handler: (flow: Flow, newState: UserFlowStatus, previousState: UserFlowStatus) => void\n ) {\n this.onFlowStateChangeHandlers.push(handler)\n }\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 private async refreshUserFlowStates(): Promise<void> {\n const globalStateKey = getGlobalStateKey(this.config)\n frigadeGlobalState[globalStateKey] = {\n refreshUserFlowStates: async () => {},\n userFlowStates: {},\n }\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${this.config.userId}${\n this.config.organizationId ? `&foreignUserGroupId=${this.config.organizationId}` : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n frigadeGlobalState[globalStateKey].userFlowStates['previous_' + userFlowState.flowId] =\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId]\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n this.triggerEventHandlers(\n frigadeGlobalState[globalStateKey].userFlowStates['previous_' + userFlowState.flowId],\n userFlowState\n )\n })\n }\n }\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n private async refreshFlows() {\n this.flows = []\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 })\n }\n }\n\n private triggerEventHandlers(\n previousUserFlowState: UserFlowState,\n newUserFlowState: UserFlowState\n ) {\n if (previousUserFlowState && previousUserFlowState.flowState !== newUserFlowState.flowState) {\n this.flows.forEach((flow) => {\n if (flow.id == newUserFlowState.flowId) {\n this.onFlowStateChangeHandlers.forEach((handler) => {\n handler(flow, newUserFlowState.flowState, previousUserFlowState?.flowState)\n })\n }\n })\n }\n }\n}\n"],"mappings":";AAAO,IAAMA,EAAiB,QCC9B,OAAOC,MAAW,cAClB,OAAS,MAAMC,MAAc,OAGtB,IAAMC,EAAiB,iBACjBC,EAAe,eACfC,EAAe,eACfC,EAAmB,mBACnBC,EAAiB,iBACjBC,EAAe,eAGtBC,EAAoB,wBACpBC,EAAsB,0BACtBC,EAAY,oBACZC,EAAe,SAEd,SAASC,EAAWC,EAAgB,CACzC,MAAO,CACL,QAAS,CACP,cAAe,UAAUA,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W","NOT_STARTED_FLOW","COMPLETED_STEP","STARTED_STEP","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","GUEST_KEY","GUEST_PREFIX","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","resetAllLocalStorage","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","fetch","error","generateGuestId","guestId","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","Fetchable","config","generateGuestId","path","options","gracefulFetch","getHeaders","Flow","Fetchable","config","flowDataRaw","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","step","index","userFlowStateStep","stepObj","COMPLETED_STEP","STARTED_STEP","properties","currentStep","updatedUserFlowState","NOT_STARTED_FLOW","currentStepId","key","frigadeGlobalState","getGlobalStateKey","Frigade","Fetchable","apiKey","config","userId","properties","organizationId","event","flowId","flow","resetAllLocalStorage","generateGuestId","handler","globalStateKey","getGlobalStateKey","frigadeGlobalState","userFlowStatesRaw","userFlowState","flowDataRaw","flowData","Flow","previousUserFlowState","newUserFlowState"]}
1
+ {"version":3,"sources":["../src/core/version.ts","../src/shared/utils.ts","../src/shared/state.ts","../src/shared/Fetchable.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["export const VERSION_NUMBER = '0.1.3'\n","import { VERSION_NUMBER } from '../core/version'\nimport fetch from 'cross-fetch'\nimport { v4 as uuidv4 } from 'uuid'\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'\nconst GUEST_PREFIX = 'guest_'\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 (window && window.localStorage) {\n return window.localStorage.getItem(key)\n }\n return null\n}\n\nfunction setLocalStorage(key: string, value: string) {\n if (window && window.localStorage) {\n window.localStorage.setItem(key, value)\n }\n}\n\nexport function resetAllLocalStorage() {\n if (window && window.localStorage) {\n // Clear all local storage items that begin with `frigade-`\n Object.keys(window.localStorage).forEach((key) => {\n if (key.startsWith('frigade-')) {\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 (window && window.localStorage && 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 < 1000) {\n return getEmptyResponse()\n }\n }\n setLocalStorage(lastCallAtKey, new Date().toISOString())\n setLocalStorage(lastCallDataKey, options.body)\n }\n\n let response\n try {\n response = await fetch(url, options)\n } catch (error) {\n return getEmptyResponse(error)\n }\n\n if (!response) {\n return getEmptyResponse()\n }\n\n if (response.staus >= 400) {\n return getEmptyResponse(response.statusText)\n }\n\n return response.json()\n}\n\nfunction 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 (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n let guestId = getLocalStorage(GUEST_KEY)\n if (!guestId) {\n guestId = `${GUEST_PREFIX}${uuidv4()}`\n window.localStorage.setItem(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","import { FrigadeConfig, UserFlowState } from '../types'\n\nexport interface FrigadeGlobalState {\n refreshUserFlowStates: () => Promise<void>\n userFlowStates: Record<string, UserFlowState>\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.organizationId ?? ''\n }`\n}\n","import { generateGuestId, getHeaders, gracefulFetch } from './utils'\nimport { FrigadeConfig } from '../types'\n\nexport class Fetchable {\n public config: FrigadeConfig = {\n apiKey: '',\n apiUrl: '//api.frigade.com/v1/public',\n userId: generateGuestId(),\n __instanceId: Math.random().toString(36).substring(7),\n }\n\n constructor(config: FrigadeConfig) {\n this.config = {\n ...this.config,\n ...config,\n }\n }\n\n public async fetch(path: string, options?: Record<any, any>) {\n return gracefulFetch(`${this.config.apiUrl}${path}`, {\n ...(options ?? {}),\n ...getHeaders(this.config.apiKey),\n })\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { FlowDataRaw } from './types'\nimport {\n clone,\n COMPLETED_FLOW,\n COMPLETED_STEP,\n NOT_STARTED_FLOW,\n SKIPPED_FLOW,\n STARTED_FLOW,\n STARTED_STEP,\n} from '../shared/utils'\nimport { FlowStep } from './flow-step'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport default class Flow extends Fetchable {\n /**\n * THe Flow ID / slug of the flow\n */\n public id: string\n /**\n * The raw data defined in `config.yml` as a JSON decoded object\n */\n public rawData: Record<any, any>\n /**\n * Ordered map from Step ID to step data. The `steps` array in `config.yml`\n */\n public steps: Map<string, FlowStep>\n /**\n * The user-facing title of the flow, if defined at the top level of `config.yml`\n */\n public title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `config.yml`\n */\n public subtitle?: string\n /**\n * The metadata of the flow.\n */\n public metadata: 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 /**\n * Nonce\n */\n public nonce =\n Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)\n\n private flowDataRaw: FlowDataRaw\n\n constructor(config: FrigadeConfig, flowDataRaw: FlowDataRaw) {\n super(config)\n this.flowDataRaw = flowDataRaw\n this.initFromRawData(flowDataRaw)\n }\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.metadata = flowDataRaw\n this.rawData = flowDataYml\n this.title = this.rawData.title\n this.subtitle = this.rawData.subtitle\n\n const userFlowState = this.getUserFlowState()\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 this.steps = new Map()\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 order: index,\n } as FlowStep\n\n stepObj.start = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isStarted || currentStep.isCompleted) {\n return\n }\n\n currentStep.isStarted = true\n const copy = clone(\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id]\n )\n copy.stepStates[currentStep.id].actionType = STARTED_STEP\n copy.lastStepId = currentStep.id\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: currentStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: STARTED_STEP,\n }),\n })\n\n await this.refreshUserFlowState()\n\n const updatedUserFlowState = this.getUserFlowState()\n currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n stepObj.complete = async (properties?: Record<string | number, any>) => {\n const currentStep = this.steps.get(step.id)\n\n if (currentStep.isCompleted) {\n return\n }\n\n const numberOfCompletedSteps = this.getNumberOfCompletedSteps()\n const isLastStep = numberOfCompletedSteps + 1 == this.steps.size\n\n currentStep.isCompleted = true\n this.isStarted = true\n const copy = clone(\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id]\n )\n\n copy.stepStates[currentStep.id].actionType = COMPLETED_STEP\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 }\n\n if (isLastStep) {\n this.optimisticallyMarkFlowCompleted()\n }\n\n frigadeGlobalState[getGlobalStateKey(this.config)].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 flowSlug: this.id,\n stepId: currentStep.id,\n data: properties ?? {},\n createdAt: new Date().toISOString(),\n actionType: COMPLETED_STEP,\n }),\n })\n\n if (isLastStep) {\n await this.complete()\n } else {\n await this.refreshUserFlowState()\n }\n\n const updatedUserFlowState = this.getUserFlowState()\n currentStep.isCompleted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == COMPLETED_STEP\n currentStep.isStarted =\n updatedUserFlowState.stepStates[currentStep.id].actionType == STARTED_STEP\n }\n\n this.steps.set(step.id, stepObj)\n })\n }\n\n /**\n * Function that 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(frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id])\n copy.flowState = STARTED_FLOW\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = copy\n\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that 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 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 private optimisticallyMarkFlowCompleted() {\n this.isStarted = true\n this.isCompleted = true\n const copy = clone(frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id])\n copy.flowState = COMPLETED_FLOW\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = copy\n this.isVisible = false\n }\n\n /**\n * Function that marks the flow skipped\n */\n public async skip(properties?: Record<string | number, any>) {\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\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 * Function that restarts the flow/marks it not started\n */\n public async restart() {\n this.isCompleted = false\n this.isCompleted = true\n frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates[this.id] = null\n await this.fetch('/flowResponses', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n flowSlug: this.id,\n stepId: 'unknown',\n data: {},\n createdAt: new Date().toISOString(),\n actionType: NOT_STARTED_FLOW,\n }),\n })\n await this.refreshUserFlowState()\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 * Function that gets current step\n */\n public getCurrentStep(): FlowStep {\n const lastStepId = Array.from(this.steps.keys()).find(\n (key) => this.steps.get(key).isCompleted === false\n )\n\n const currentStepId = lastStepId ?? Array.from(this.steps.keys())[0]\n return this.steps.get(currentStepId)\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 private getUserFlowState(): UserFlowState {\n const userFlowStates = frigadeGlobalState[getGlobalStateKey(this.config)].userFlowStates\n return userFlowStates[this.id]\n }\n\n private async refreshUserFlowState() {\n await frigadeGlobalState[getGlobalStateKey(this.config)].refreshUserFlowStates()\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { generateGuestId, resetAllLocalStorage } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowDataRaw } from './types'\nimport { frigadeGlobalState, getGlobalStateKey } from '../shared/state'\nimport { Fetchable } from '../shared/Fetchable'\n\nexport class Frigade extends Fetchable {\n private flows: Flow[] = []\n private initPromise: Promise<void>\n private onFlowStateChangeHandlers: ((flow: Flow, previousFlow?: Flow) => void)[] = []\n private previousFlows: Map<(flow: Flow, previousFlow: Flow) => void, Map<string, Flow>> =\n new Map()\n\n constructor(apiKey: string, config?: FrigadeConfig) {\n super({\n apiKey,\n ...config,\n })\n\n this.init(this.config)\n }\n\n private async init(config: FrigadeConfig): Promise<void> {\n this.config = {\n ...this.config,\n ...config,\n }\n\n this.initPromise = (async () => {\n await this.refreshUserFlowStates()\n await this.refreshFlows()\n })()\n\n return this.initPromise\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.userId = userId\n await this.fetch('/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.config.userId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n this.config.organizationId = organizationId\n await this.fetch('/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n await this.initIfNeeded()\n await this.fetch('/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.config.userId,\n foreignUserGroupId: this.config.organizationId,\n event,\n properties,\n }),\n })\n }\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 public async reset() {\n resetAllLocalStorage()\n this.config.userId = generateGuestId()\n this.config.organizationId = undefined\n }\n\n public onFlowStateChange(handler: (flow: Flow, previousFlow?: Flow) => void) {\n this.onFlowStateChangeHandlers.push(handler)\n }\n\n public removeOnFlowStateChangeHandler(handler: (flow: Flow, previousFlow?: Flow) => void) {\n this.onFlowStateChangeHandlers = this.onFlowStateChangeHandlers.filter((h) => h !== handler)\n }\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 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 }\n frigadeGlobalState[globalStateKey].refreshUserFlowStates = async () => {\n const userFlowStatesRaw = await this.fetch(\n `/userFlowStates?foreignUserId=${this.config.userId}${\n this.config.organizationId ? `&foreignUserGroupId=${this.config.organizationId}` : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n frigadeGlobalState[globalStateKey].userFlowStates[userFlowState.flowId] = userFlowState\n })\n }\n }\n }\n\n await frigadeGlobalState[globalStateKey].refreshUserFlowStates()\n }\n\n private async refreshFlows() {\n this.flows = []\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 })\n }\n }\n\n private async triggerEventHandlers(previousUserFlowState: UserFlowState) {\n if (previousUserFlowState) {\n this.flows.forEach((flow) => {\n flow.nonce =\n Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15)\n if (flow.id == previousUserFlowState.flowId) {\n this.onFlowStateChangeHandlers.forEach((handler) => {\n const lastFlows = this.previousFlows.get(handler)\n const lastFlow = lastFlows ? lastFlows.get(flow.id) : undefined\n handler(flow, lastFlow)\n if (!lastFlows) {\n this.previousFlows.set(handler, new Map())\n }\n this.previousFlows.get(handler).set(flow.id, flow)\n })\n }\n })\n }\n }\n}\n"],"mappings":";AAAO,IAAMA,EAAiB,QCC9B,OAAOC,MAAW,cAClB,OAAS,MAAMC,MAAc,OAGtB,IAAMC,EAAiB,iBACjBC,EAAe,eACfC,EAAe,eACfC,EAAmB,mBACnBC,EAAiB,iBACjBC,EAAe,eAGtBC,EAAoB,wBACpBC,EAAsB,0BACtBC,EAAY,oBACZC,EAAe,SAEd,SAASC,EAASC,EAAW,CAClC,OAAO,KAAK,MAAM,KAAK,UAAUA,CAAG,CAAC,CACvC,CAEO,SAASC,EAAWC,EAAgB,CACzC,MAAO,CACL,QAAS,CACP,cAAe,UAAUA,IACzB,eAAgB,mBAChB,wBAAyBC,EACzB,yBAA0B,YAC5B,CACF,CACF,CAEA,SAASC,EAAgBC,EAAa,CACpC,OAAI,QAAU,OAAO,aACZ,OAAO,aAAa,QAAQA,CAAG,EAEjC,IACT,CAEA,SAASC,EAAgBD,EAAaE,EAAe,CAC/C,QAAU,OAAO,cACnB,OAAO,aAAa,QAAQF,EAAKE,CAAK,CAE1C,CAEO,SAASC,GAAuB,CACjC,QAAU,OAAO,cAE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,MAAc,cAAe,CAC3B,KAAK,MAAQ,CAAC,EACd,IAAMU,EAAc,MAAM,KAAK,MAAM,QAAQ,EACzCA,GAAeA,EAAY,MACbA,EAAY,KAClB,QAASC,GAAa,CAC9B,KAAK,MAAM,KAAK,IAAIC,EAAK,KAAK,OAAQD,CAAQ,CAAC,CACjD,CAAC,CAEL,CAEA,MAAc,qBAAqBE,EAAsC,CACnEA,GACF,KAAK,MAAM,QAASlB,GAAS,CAC3BA,EAAK,MACH,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,EAAG,EAAE,EAAI,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,UAAU,EAAG,EAAE,EACtFA,EAAK,IAAMkB,EAAsB,QACnC,KAAK,0BAA0B,QAASf,GAAY,CAClD,IAAMgB,EAAY,KAAK,cAAc,IAAIhB,CAAO,EAC1CiB,EAAWD,EAAYA,EAAU,IAAInB,EAAK,EAAE,EAAI,OACtDG,EAAQH,EAAMoB,CAAQ,EACjBD,GACH,KAAK,cAAc,IAAIhB,EAAS,IAAI,GAAK,EAE3C,KAAK,cAAc,IAAIA,CAAO,EAAE,IAAIH,EAAK,GAAIA,CAAI,CACnD,CAAC,CAEL,CAAC,CAEL,CACF","names":["VERSION_NUMBER","fetch","uuidv4","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","clone","obj","getHeaders","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","resetAllLocalStorage","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","fetch","error","generateGuestId","guestId","uuidv4","frigadeGlobalState","getGlobalStateKey","internalConfig","Fetchable","config","generateGuestId","path","options","gracefulFetch","getHeaders","Flow","Fetchable","config","flowDataRaw","flowDataYml","steps","userFlowState","COMPLETED_FLOW","STARTED_FLOW","SKIPPED_FLOW","hasCompleted","targetingShouldHideFlow","step","index","userFlowStateStep","stepObj","COMPLETED_STEP","STARTED_STEP","properties","currentStep","copy","clone","frigadeGlobalState","getGlobalStateKey","updatedUserFlowState","isLastStep","nextStepId","NOT_STARTED_FLOW","currentStepId","key","Frigade","Fetchable","apiKey","config","userId","properties","organizationId","event","flowId","flow","resetAllLocalStorage","generateGuestId","handler","h","globalStateKey","getGlobalStateKey","frigadeGlobalState","that","validator","target","key","value","userFlowStatesRaw","userFlowState","flowDataRaw","flowData","Flow","previousUserFlowState","lastFlows","lastFlow"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frigade/js",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "The official Javascript SDK for Frigade.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",