@frigade/js 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -102,6 +102,14 @@ interface FrigadeConfig {
102
102
  * API url to use for all requests. Defaults to https://api.frigade.com
103
103
  */
104
104
  apiUrl?: string;
105
+ /**
106
+ * User ID to use for all requests. Defaults to null.
107
+ */
108
+ userId?: string;
109
+ /**
110
+ * Organization ID to use for all requests. Defaults to null.
111
+ */
112
+ organizationId?: string;
105
113
  }
106
114
 
107
115
  interface FlowMetadata {
@@ -168,23 +176,32 @@ declare class Flow {
168
176
  * The metadata of the flow.
169
177
  */
170
178
  readonly metadata: FlowMetadata;
171
- constructor(id: string, status: FlowStatus, rawData: Record<any, any>, steps: FlowStepData[], title?: string, subtitle?: string);
179
+ constructor(id: string, status: FlowStatus, rawData: Record<any, any>, steps: FlowStepData[], title?: string, subtitle?: string, metadata?: FlowMetadata);
172
180
  }
173
181
 
174
182
  declare class Frigade {
175
183
  private apiKey?;
176
- private userId?;
184
+ private userId;
177
185
  private organizationId?;
178
186
  private config?;
179
187
  private hasInitialized;
188
+ /**
189
+ * Map flow IDs to their current state
190
+ * @private
191
+ */
192
+ private userFlowStatuses;
180
193
  private flows;
181
194
  init(apiKey: string, config?: FrigadeConfig): Promise<void>;
182
195
  identify(userId: string, properties?: Record<string, any>): Promise<void>;
183
196
  group(organizationId: string, properties?: Record<string, any>): Promise<void>;
184
197
  track(event: string, properties?: Record<string, any>): Promise<void>;
185
198
  getFlow(flowId: string): Promise<Flow>;
199
+ getFlows(): Promise<Flow[]>;
200
+ reset(): Promise<void>;
186
201
  private errorOnUninitialized;
202
+ private refreshUserFlowStates;
187
203
  private refreshFlows;
188
204
  }
205
+ declare const frigade: Frigade;
189
206
 
190
- export { Flow, Frigade as default };
207
+ export { Flow, frigade as default };
package/lib/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  "use client";
2
- var g=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var y=Object.prototype.hasOwnProperty;var w=(i,t)=>{for(var r in t)g(i,r,{get:t[r],enumerable:!0})},O=(i,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of h(t))!y.call(i,e)&&e!==r&&g(i,e,{get:()=>t[e],enumerable:!(o=S(t,e))||o.enumerable});return i};var E=i=>O(g({},"__esModule",{value:!0}),i);var L={};w(L,{Flow:()=>a,default:()=>R});module.exports=E(L);var f="1.0.0 ";var F="frigade-last-call-at-",m="frigade-last-call-data-";function _(i){return{config:{headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","X-Frigade-SDK-Version":f,"X-Frigade-SDK-Platform":"Javascript"}}}}function p(i){return window&&window.localStorage?window.localStorage.getItem(i):null}function u(i,t){window&&window.localStorage&&window.localStorage.setItem(i,t)}async function D(i,t){let r=F+i,o=m+i;if(window&&window.localStorage&&t&&t.body&&t.method==="POST"){let n=p(r),d=p(o);if(n&&d&&d==t.body){let T=new Date(n);if(new Date().getTime()-T.getTime()<1e3)return c()}u(r,new Date().toISOString()),u(o,t.body)}let e;try{e=await fetch(i,t)}catch(n){return c(n)}return e?e.ok?e:c(e.statusText):c()}function c(i){return i&&console.log("Call to Frigade failed",i),{json:()=>({})}}function s(i,t,r){return D(P(i,t),{...r??{},..._(i)})}function P(i,t){return`${i}/public/v1${t}`}var a=class{constructor(t,r,o,e,n,d){this.id=t,this.status=r,this.flowData=o,this.steps=e,this.title=n,this.subtitle=d}};var l=class{constructor(){this.hasInitialized=!1;this.flows=[]}async init(t,r){return this.apiKey=t,this.config=r,await this.refreshFlows(),this.hasInitialized=!0,Promise.any(null)}async identify(t,r){this.errorOnUninitialized(),this.userId=t,await s(this.apiKey,"/users",{method:"POST",body:JSON.stringify({foreignId:this.userId,properties:r})})}async group(t,r){this.errorOnUninitialized(),this.organizationId=t,await s(this.apiKey,"/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,properties:r})})}async track(t,r){this.errorOnUninitialized(),await s(this.apiKey,"/track",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,event:t,properties:r})})}async getFlow(t){return this.errorOnUninitialized(),this.flows.find(r=>r.id==t)}errorOnUninitialized(){if(!this.hasInitialized)throw new Error("Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.")}async refreshFlows(){this.flows=[];let t=await s(this.apiKey,"/flows");t&&t.data&&t.data.forEach(o=>{let e=JSON.parse(o.data);this.flows.push(new a(o.slug,o.status,e,(e==null?void 0:e.data)??[],e==null?void 0:e.title,e==null?void 0:e.subtitle))})}};var R=l;0&&(module.exports={Flow});
2
+ var m=Object.create;var d=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var D=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty;var P=(e,t)=>{for(var i in t)d(e,i,{get:t[i],enumerable:!0})},p=(e,t,i,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of _(t))!R.call(e,r)&&r!==i&&d(e,r,{get:()=>t[r],enumerable:!(a=I(t,r))||a.enumerable});return e};var L=(e,t,i)=>(i=e!=null?m(D(e)):{},p(t||!e||!e.__esModule?d(i,"default",{value:e,enumerable:!0}):i,e)),b=e=>p(d({},"__esModule",{value:!0}),e);var W={};P(W,{Flow:()=>o,default:()=>N});module.exports=b(W);var S="1.0.1 ";var y=L(require("cross-fetch")),F=require("uuid");var A="frigade-last-call-at-",U="frigade-last-call-data-",h="frigade-guest-key",z="guest_";function x(e){return{config:{headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json","X-Frigade-SDK-Version":S,"X-Frigade-SDK-Platform":"Javascript"}}}}function g(e){return window&&window.localStorage?window.localStorage.getItem(e):null}function T(e,t){window&&window.localStorage&&window.localStorage.setItem(e,t)}function E(){window&&window.localStorage&&Object.keys(window.localStorage).forEach(e=>{e.startsWith("frigade-")&&window.localStorage.removeItem(e)})}async function v(e,t){let i=A+e,a=U+e;if(window&&window.localStorage&&t&&t.body&&t.method==="POST"){let s=g(i),l=g(a);if(s&&l&&l==t.body){let u=new Date(s);if(new Date().getTime()-u.getTime()<1e3)return c()}T(i,new Date().toISOString()),T(a,t.body)}let r;try{r=await(0,y.default)(e,t)}catch(s){return c(s)}return r?r.staus>=400?c(r.statusText):r.json():c()}function c(e){return e&&console.log("Call to Frigade failed",e),{json:()=>({})}}function f(){if(window&&window.localStorage){let e=g(h);return e||(e=`${z}${(0,F.v4)()}`,window.localStorage.setItem(h,e)),e}}function n(e,t,i){return v(`//api.frigade.com/v1/public${t}`,{...i??{},...x(e).config})}var o=class{constructor(t,i,a,r,s,l,u){this.id=t,this.status=i,this.flowData=a,this.steps=r,this.title=s,this.subtitle=l,this.metadata=u}};var w=class{constructor(){this.userId=f();this.hasInitialized=!1;this.userFlowStatuses={};this.flows=[]}async init(t,i){this.apiKey=t,this.config=i,i!=null&&i.userId&&(this.userId=i.userId),i!=null&&i.organizationId&&(this.organizationId=i.organizationId),await Promise.all([this.refreshUserFlowStates(),this.refreshFlows()]),this.hasInitialized=!0}async identify(t,i){this.errorOnUninitialized(),this.userId=t,await n(this.apiKey,"/users",{method:"POST",body:JSON.stringify({foreignId:this.userId,properties:i})}),await this.refreshUserFlowStates()}async group(t,i){this.errorOnUninitialized(),this.organizationId=t,await n(this.apiKey,"/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,properties:i})}),await this.refreshUserFlowStates()}async track(t,i){this.errorOnUninitialized(),await n(this.apiKey,"/track",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,event:t,properties:i})})}async getFlow(t){return this.errorOnUninitialized(),this.flows.find(i=>i.id==t)}async getFlows(){return this.errorOnUninitialized(),this.flows}async reset(){E(),this.userId=f(),this.organizationId=void 0}errorOnUninitialized(){if(!this.hasInitialized)throw new Error("Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.")}async refreshUserFlowStates(){let t=await n(this.apiKey,`/userFlowStates?foreignUserId=${this.userId}${this.organizationId?`&foreignUserGroupId=${this.organizationId}`:""}`);t&&t.data&&t.data.forEach(a=>{this.userFlowStatuses[a.flowId]=a})}async refreshFlows(){this.flows=[];let t=await n(this.apiKey,"/flows");t&&t.data&&t.data.forEach(a=>{let r=JSON.parse(a.data);this.flows.push(new o(a.slug,a.status,r,(r==null?void 0:r.data)??[],r==null?void 0:r.title,r==null?void 0:r.subtitle,a))})}},C=new w,O=C;var N=O;0&&(module.exports={Flow});
3
3
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/core/version.ts","../src/shared/utils.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["import Frigade from './core/frigade'\nimport Flow from './core/flow'\n\nexport { Flow }\nexport default Frigade\n","export const VERSION_NUMBER = '1.0.0 '\n","import { VERSION_NUMBER } from '../core/version'\n\nexport const NOT_STARTED_STEP = 'NOT_STARTED_STEP'\nexport const COMPLETED_FLOW = 'COMPLETED_FLOW'\nexport const ABORTED_FLOW = 'ABORTED_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' | 'ABORTED_FLOW'\nconst LAST_POST_CALL_AT = 'frigade-last-call-at-'\nconst LAST_POST_CALL_DATA = 'frigade-last-call-data-'\n\nfunction getConfig(apiKey: string) {\n return {\n config: {\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}\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 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.ok) {\n return getEmptyResponse(response.statusText)\n }\n\n return response\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 fetcher(apiKey: string, url: string, options?: Record<any, any>) {\n return gracefulFetch(getUrl(apiKey, url), {\n ...(options ?? {}),\n ...getConfig(apiKey),\n })\n}\n\nfunction getUrl(apiUrl: string, postfix: string) {\n return `${apiUrl}/public/v1${postfix}`\n}\n","import { FlowStepData } from '../types'\nimport { FlowMetadata, FlowStatus } from './types'\n\nexport default class Flow {\n /**\n * THe Flow ID / slug of the flow\n */\n public readonly id: string\n /**\n * The status of the flow\n */\n public readonly status: FlowStatus\n /**\n * The raw data defined in `flow-data.yml` as a JSON decoded object\n */\n public readonly flowData: Record<any, any>\n /**\n * The steps contained in the `data` array in `flow-data.yml`\n */\n public readonly steps: FlowStepData[]\n /**\n * The user-facing title of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly subtitle?: string\n /**\n * The metadata of the flow.\n */\n public readonly metadata: FlowMetadata\n\n constructor(\n id: string,\n status: FlowStatus,\n rawData: Record<any, any>,\n steps: FlowStepData[],\n title?: string,\n subtitle?: string\n ) {\n this.id = id\n this.status = status\n this.flowData = rawData\n this.steps = steps\n this.title = title\n this.subtitle = subtitle\n }\n}\n","import { FrigadeConfig } from '../types'\nimport { fetcher } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowMetadata } from './types'\n\nexport default class Frigade {\n private apiKey?: string\n private userId?: string\n private organizationId?: string\n private config?: FrigadeConfig\n private hasInitialized = false\n\n private flows: Flow[] = []\n\n public async init(apiKey: string, config?: FrigadeConfig): Promise<void> {\n this.apiKey = apiKey\n this.config = config\n await this.refreshFlows()\n this.hasInitialized = true\n return Promise.any(null)\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.userId = userId\n await fetcher(this.apiKey, '/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.userId,\n properties,\n }),\n })\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.organizationId = organizationId\n await fetcher(this.apiKey, '/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n properties,\n }),\n })\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n await fetcher(this.apiKey, '/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n event,\n properties,\n }),\n })\n }\n\n public async getFlow(flowId: string) {\n this.errorOnUninitialized()\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n private errorOnUninitialized() {\n if (!this.hasInitialized) {\n throw new Error(\n 'Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.'\n )\n }\n }\n\n private async refreshFlows() {\n this.flows = []\n const flowDataRaw = await fetcher(this.apiKey, '/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowMetadatas = flowDataRaw.data as FlowMetadata[]\n flowMetadatas.forEach((flowMetadata) => {\n const rawData = JSON.parse(flowMetadata.data)\n this.flows.push(\n new Flow(\n flowMetadata.slug,\n flowMetadata.status,\n rawData,\n rawData?.data ?? [],\n rawData?.title,\n rawData?.subtitle\n )\n )\n })\n }\n }\n}\n"],"mappings":";4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,UAAAE,EAAA,YAAAC,IAAA,eAAAC,EAAAJ,GCAO,IAAMK,EAAiB,SCW9B,IAAMC,EAAoB,wBACpBC,EAAsB,0BAE5B,SAASC,EAAUC,EAAgB,CACjC,MAAO,CACL,OAAQ,CACN,QAAS,CACP,cAAe,UAAUA,IACzB,eAAgB,mBAChB,wBAAyBC,EACzB,yBAA0B,YAC5B,CACF,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,CAEA,eAAsBC,EAAcC,EAAaC,EAAc,CAC7D,IAAMC,EAAgBZ,EAAoBU,EACpCG,EAAkBZ,EAAsBS,EAC9C,GAAI,QAAU,OAAO,cAAgBC,GAAWA,EAAQ,MAAQA,EAAQ,SAAW,OAAQ,CACzF,IAAMG,EAAWT,EAAgBO,CAAa,EACxCG,EAAeV,EAAgBQ,CAAe,EACpD,GAAIC,GAAYC,GAAgBA,GAAgBJ,EAAQ,KAAM,CAC5D,IAAMK,EAAe,IAAI,KAAKF,CAAQ,EAItC,GAHY,IA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AAQ,SAAU,CACnC,OAAQ,OACR,KAAM,KAAK,UAAU,CACnB,UAAW,KAAK,OAChB,WAAAD,CACF,CAAC,CACH,CAAC,CACH,CAEA,MAAa,MAAME,EAAwBF,EAAiD,CAC1F,KAAK,qBAAqB,EAC1B,KAAK,eAAiBE,EACtB,MAAMD,EAAQ,KAAK,OAAQ,cAAe,CACxC,OAAQ,OACR,KAAM,KAAK,UAAU,CACnB,cAAe,KAAK,OACpB,mBAAoB,KAAK,eACzB,WAAAD,CACF,CAAC,CACH,CAAC,CACH,CAEA,MAAa,MAAMG,EAAeH,EAAiD,CACjF,KAAK,qBAAqB,EAC1B,MAAMC,EAAQ,KAAK,OAAQ,SAAU,CACnC,OAAQ,OACR,KAAM,KAAK,UAAU,CACnB,cAAe,KAAK,OACpB,mBAAoB,KAAK,eACzB,MAAAE,EACA,WAAAH,CACF,CAAC,CACH,CAAC,CACH,CAEA,MAAa,QAAQI,EAAgB,CACnC,YAAK,qBAAqB,EACnB,KAAK,MAAM,KAAMC,GAASA,EAAK,IAAMD,CAAM,CACpD,CAEQ,sBAAuB,CAC7B,GAAI,CAAC,KAAK,eACR,MAAM,IAAI,MACR,kGACF,CAEJ,CAEA,MAAc,cAAe,CAC3B,KAAK,MAAQ,CAAC,EACd,IAAME,EAAc,MAAML,EAAQ,KAAK,OAAQ,QAAQ,EACnDK,GAAeA,EAAY,MACTA,EAAY,KAClB,QAASC,GAAiB,CACtC,IAAMC,EAAU,KAAK,MAAMD,EAAa,IAAI,EAC5C,KAAK,MAAM,KACT,IAAIE,EACFF,EAAa,KACbA,EAAa,OACbC,GACAA,GAAA,YAAAA,EAAS,OAAQ,CAAC,EAClBA,GAAA,YAAAA,EAAS,MACTA,GAAA,YAAAA,EAAS,QACX,CACF,CACF,CAAC,CAEL,CACF,EJzFA,IAAOE,EAAQC","names":["src_exports","__export","Flow","src_default","__toCommonJS","VERSION_NUMBER","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","getConfig","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","error","fetcher","getUrl","apiUrl","postfix","Flow","id","status","rawData","steps","title","subtitle","Frigade","apiKey","config","userId","properties","fetcher","organizationId","event","flowId","flow","flowDataRaw","flowMetadata","rawData","Flow","src_default","Frigade"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/core/version.ts","../src/shared/utils.ts","../src/core/flow.ts","../src/core/frigade.ts"],"sourcesContent":["import frigade from './core/frigade'\nimport Flow from './core/flow'\n\nexport { Flow }\n\nexport default frigade\n","export const VERSION_NUMBER = '1.0.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 ABORTED_FLOW = 'ABORTED_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' | 'ABORTED_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\nfunction getConfig(apiKey: string) {\n return {\n config: {\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}\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 (window && window.localStorage) {\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 ...getConfig(apiKey).config,\n })\n}\n","import { FlowStepData } from '../types'\nimport { FlowMetadata, FlowStatus } from './types'\n\nexport default class Flow {\n /**\n * THe Flow ID / slug of the flow\n */\n public readonly id: string\n /**\n * The status of the flow\n */\n public readonly status: FlowStatus\n /**\n * The raw data defined in `flow-data.yml` as a JSON decoded object\n */\n public readonly flowData: Record<any, any>\n /**\n * The steps contained in the `data` array in `flow-data.yml`\n */\n public readonly steps: FlowStepData[]\n /**\n * The user-facing title of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly subtitle?: string\n /**\n * The metadata of the flow.\n */\n public readonly metadata: FlowMetadata\n\n constructor(\n id: string,\n status: FlowStatus,\n rawData: Record<any, any>,\n steps: FlowStepData[],\n title?: string,\n subtitle?: string,\n metadata?: FlowMetadata\n ) {\n this.id = id\n this.status = status\n this.flowData = rawData\n this.steps = steps\n this.title = title\n this.subtitle = subtitle\n this.metadata = metadata\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { fetcher, generateGuestId, resetAllLocalStorage } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowMetadata } from './types'\n\nexport class Frigade {\n private apiKey?: string\n private userId: string = generateGuestId()\n private organizationId?: string\n private config?: FrigadeConfig\n private hasInitialized = false\n /**\n * Map flow IDs to their current state\n * @private\n */\n private userFlowStatuses: Record<string, UserFlowState> = {}\n\n private flows: Flow[] = []\n\n public async init(apiKey: string, config?: FrigadeConfig): Promise<void> {\n this.apiKey = apiKey\n this.config = config\n if (config?.userId) {\n this.userId = config.userId\n }\n if (config?.organizationId) {\n this.organizationId = config.organizationId\n }\n await Promise.all([this.refreshUserFlowStates(), this.refreshFlows()])\n this.hasInitialized = true\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.userId = userId\n await fetcher(this.apiKey, '/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.userId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.organizationId = organizationId\n await fetcher(this.apiKey, '/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n await fetcher(this.apiKey, '/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n event,\n properties,\n }),\n })\n }\n\n public async getFlow(flowId: string) {\n this.errorOnUninitialized()\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n public async getFlows() {\n this.errorOnUninitialized()\n return this.flows\n }\n\n public async reset() {\n resetAllLocalStorage()\n this.userId = generateGuestId()\n this.organizationId = undefined\n }\n\n private errorOnUninitialized() {\n if (!this.hasInitialized) {\n throw new Error(\n 'Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.'\n )\n }\n }\n\n private async refreshUserFlowStates() {\n const userFlowStatesRaw = await fetcher(\n this.apiKey,\n `/userFlowStates?foreignUserId=${this.userId}${\n this.organizationId ? `&foreignUserGroupId=${this.organizationId}` : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n this.userFlowStatuses[userFlowState.flowId] = userFlowState\n })\n }\n }\n\n private async refreshFlows() {\n this.flows = []\n const flowDataRaw = await fetcher(this.apiKey, '/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowMetadatas = flowDataRaw.data as FlowMetadata[]\n flowMetadatas.forEach((flowMetadata) => {\n const rawData = JSON.parse(flowMetadata.data)\n this.flows.push(\n new Flow(\n flowMetadata.slug,\n flowMetadata.status,\n rawData,\n rawData?.data ?? [],\n rawData?.title,\n rawData?.subtitle,\n flowMetadata\n )\n )\n })\n }\n }\n}\n\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,KAAK,eAAiB,uBAAuB,KAAK,iBAAmB,IAEzE,EACIM,GAAqBA,EAAkB,MACpBA,EAAkB,KACxB,QAASC,GAAkB,CACxC,KAAK,iBAAiBA,EAAc,MAAM,EAAIA,CAChD,CAAC,CAEL,CAEA,MAAc,cAAe,CAC3B,KAAK,MAAQ,CAAC,EACd,IAAMC,EAAc,MAAMR,EAAQ,KAAK,OAAQ,QAAQ,EACnDQ,GAAeA,EAAY,MACTA,EAAY,KAClB,QAASC,GAAiB,CACtC,IAAMC,EAAU,KAAK,MAAMD,EAAa,IAAI,EAC5C,KAAK,MAAM,KACT,IAAIE,EACFF,EAAa,KACbA,EAAa,OACbC,GACAA,GAAA,YAAAA,EAAS,OAAQ,CAAC,EAClBA,GAAA,YAAAA,EAAS,MACTA,GAAA,YAAAA,EAAS,SACTD,CACF,CACF,CACF,CAAC,CAEL,CACF,EAEMG,EAAU,IAAIlB,EACbmB,EAAQD,EJlIf,IAAOE,EAAQC","names":["src_exports","__export","Flow","src_default","__toCommonJS","VERSION_NUMBER","import_cross_fetch","import_uuid","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","GUEST_KEY","GUEST_PREFIX","getConfig","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","resetAllLocalStorage","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","fetch","error","generateGuestId","guestId","uuidv4","fetcher","path","Flow","id","status","rawData","steps","title","subtitle","metadata","Frigade","generateGuestId","apiKey","config","userId","properties","fetcher","organizationId","event","flowId","flow","resetAllLocalStorage","userFlowStatesRaw","userFlowState","flowDataRaw","flowMetadata","rawData","Flow","frigade","frigade_default","src_default","frigade_default"]}
package/lib/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
1
  "use client";
2
- var g="1.0.0 ";var T="frigade-last-call-at-",S="frigade-last-call-data-";function h(i){return{config:{headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","X-Frigade-SDK-Version":g,"X-Frigade-SDK-Platform":"Javascript"}}}}function f(i){return window&&window.localStorage?window.localStorage.getItem(i):null}function p(i,t){window&&window.localStorage&&window.localStorage.setItem(i,t)}async function y(i,t){let r=T+i,o=S+i;if(window&&window.localStorage&&t&&t.body&&t.method==="POST"){let a=f(r),d=f(o);if(a&&d&&d==t.body){let u=new Date(a);if(new Date().getTime()-u.getTime()<1e3)return c()}p(r,new Date().toISOString()),p(o,t.body)}let e;try{e=await fetch(i,t)}catch(a){return c(a)}return e?e.ok?e:c(e.statusText):c()}function c(i){return i&&console.log("Call to Frigade failed",i),{json:()=>({})}}function s(i,t,r){return y(w(i,t),{...r??{},...h(i)})}function w(i,t){return`${i}/public/v1${t}`}var n=class{constructor(t,r,o,e,a,d){this.id=t,this.status=r,this.flowData=o,this.steps=e,this.title=a,this.subtitle=d}};var l=class{constructor(){this.hasInitialized=!1;this.flows=[]}async init(t,r){return this.apiKey=t,this.config=r,await this.refreshFlows(),this.hasInitialized=!0,Promise.any(null)}async identify(t,r){this.errorOnUninitialized(),this.userId=t,await s(this.apiKey,"/users",{method:"POST",body:JSON.stringify({foreignId:this.userId,properties:r})})}async group(t,r){this.errorOnUninitialized(),this.organizationId=t,await s(this.apiKey,"/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,properties:r})})}async track(t,r){this.errorOnUninitialized(),await s(this.apiKey,"/track",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,event:t,properties:r})})}async getFlow(t){return this.errorOnUninitialized(),this.flows.find(r=>r.id==t)}errorOnUninitialized(){if(!this.hasInitialized)throw new Error("Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.")}async refreshFlows(){this.flows=[];let t=await s(this.apiKey,"/flows");t&&t.data&&t.data.forEach(o=>{let e=JSON.parse(o.data);this.flows.push(new n(o.slug,o.status,e,(e==null?void 0:e.data)??[],e==null?void 0:e.title,e==null?void 0:e.subtitle))})}};var I=l;export{n as Flow,I as default};
2
+ var w="1.0.1 ";import y from"cross-fetch";import{v4 as F}from"uuid";var E="frigade-last-call-at-",O="frigade-last-call-data-",p="frigade-guest-key",m="guest_";function I(i){return{config:{headers:{Authorization:`Bearer ${i}`,"Content-Type":"application/json","X-Frigade-SDK-Version":w,"X-Frigade-SDK-Platform":"Javascript"}}}}function u(i){return window&&window.localStorage?window.localStorage.getItem(i):null}function S(i,t){window&&window.localStorage&&window.localStorage.setItem(i,t)}function h(){window&&window.localStorage&&Object.keys(window.localStorage).forEach(i=>{i.startsWith("frigade-")&&window.localStorage.removeItem(i)})}async function _(i,t){let e=E+i,a=O+i;if(window&&window.localStorage&&t&&t.body&&t.method==="POST"){let o=u(e),l=u(a);if(o&&l&&l==t.body){let c=new Date(o);if(new Date().getTime()-c.getTime()<1e3)return d()}S(e,new Date().toISOString()),S(a,t.body)}let r;try{r=await y(i,t)}catch(o){return d(o)}return r?r.staus>=400?d(r.statusText):r.json():d()}function d(i){return i&&console.log("Call to Frigade failed",i),{json:()=>({})}}function g(){if(window&&window.localStorage){let i=u(p);return i||(i=`${m}${F()}`,window.localStorage.setItem(p,i)),i}}function s(i,t,e){return _(`//api.frigade.com/v1/public${t}`,{...e??{},...I(i).config})}var n=class{constructor(t,e,a,r,o,l,c){this.id=t,this.status=e,this.flowData=a,this.steps=r,this.title=o,this.subtitle=l,this.metadata=c}};var f=class{constructor(){this.userId=g();this.hasInitialized=!1;this.userFlowStatuses={};this.flows=[]}async init(t,e){this.apiKey=t,this.config=e,e!=null&&e.userId&&(this.userId=e.userId),e!=null&&e.organizationId&&(this.organizationId=e.organizationId),await Promise.all([this.refreshUserFlowStates(),this.refreshFlows()]),this.hasInitialized=!0}async identify(t,e){this.errorOnUninitialized(),this.userId=t,await s(this.apiKey,"/users",{method:"POST",body:JSON.stringify({foreignId:this.userId,properties:e})}),await this.refreshUserFlowStates()}async group(t,e){this.errorOnUninitialized(),this.organizationId=t,await s(this.apiKey,"/userGroups",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,properties:e})}),await this.refreshUserFlowStates()}async track(t,e){this.errorOnUninitialized(),await s(this.apiKey,"/track",{method:"POST",body:JSON.stringify({foreignUserId:this.userId,foreignUserGroupId:this.organizationId,event:t,properties:e})})}async getFlow(t){return this.errorOnUninitialized(),this.flows.find(e=>e.id==t)}async getFlows(){return this.errorOnUninitialized(),this.flows}async reset(){h(),this.userId=g(),this.organizationId=void 0}errorOnUninitialized(){if(!this.hasInitialized)throw new Error("Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.")}async refreshUserFlowStates(){let t=await s(this.apiKey,`/userFlowStates?foreignUserId=${this.userId}${this.organizationId?`&foreignUserGroupId=${this.organizationId}`:""}`);t&&t.data&&t.data.forEach(a=>{this.userFlowStatuses[a.flowId]=a})}async refreshFlows(){this.flows=[];let t=await s(this.apiKey,"/flows");t&&t.data&&t.data.forEach(a=>{let r=JSON.parse(a.data);this.flows.push(new n(a.slug,a.status,r,(r==null?void 0:r.data)??[],r==null?void 0:r.title,r==null?void 0:r.subtitle,a))})}},D=new f,T=D;var M=T;export{n as Flow,M as default};
3
3
  //# sourceMappingURL=index.mjs.map
package/lib/index.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core/version.ts","../src/shared/utils.ts","../src/core/flow.ts","../src/core/frigade.ts","../src/index.ts"],"sourcesContent":["export const VERSION_NUMBER = '1.0.0 '\n","import { VERSION_NUMBER } from '../core/version'\n\nexport const NOT_STARTED_STEP = 'NOT_STARTED_STEP'\nexport const COMPLETED_FLOW = 'COMPLETED_FLOW'\nexport const ABORTED_FLOW = 'ABORTED_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' | 'ABORTED_FLOW'\nconst LAST_POST_CALL_AT = 'frigade-last-call-at-'\nconst LAST_POST_CALL_DATA = 'frigade-last-call-data-'\n\nfunction getConfig(apiKey: string) {\n return {\n config: {\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}\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 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.ok) {\n return getEmptyResponse(response.statusText)\n }\n\n return response\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 fetcher(apiKey: string, url: string, options?: Record<any, any>) {\n return gracefulFetch(getUrl(apiKey, url), {\n ...(options ?? {}),\n ...getConfig(apiKey),\n })\n}\n\nfunction getUrl(apiUrl: string, postfix: string) {\n return `${apiUrl}/public/v1${postfix}`\n}\n","import { FlowStepData } from '../types'\nimport { FlowMetadata, FlowStatus } from './types'\n\nexport default class Flow {\n /**\n * THe Flow ID / slug of the flow\n */\n public readonly id: string\n /**\n * The status of the flow\n */\n public readonly status: FlowStatus\n /**\n * The raw data defined in `flow-data.yml` as a JSON decoded object\n */\n public readonly flowData: Record<any, any>\n /**\n * The steps contained in the `data` array in `flow-data.yml`\n */\n public readonly steps: FlowStepData[]\n /**\n * The user-facing title of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly subtitle?: string\n /**\n * The metadata of the flow.\n */\n public readonly metadata: FlowMetadata\n\n constructor(\n id: string,\n status: FlowStatus,\n rawData: Record<any, any>,\n steps: FlowStepData[],\n title?: string,\n subtitle?: string\n ) {\n this.id = id\n this.status = status\n this.flowData = rawData\n this.steps = steps\n this.title = title\n this.subtitle = subtitle\n }\n}\n","import { FrigadeConfig } from '../types'\nimport { fetcher } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowMetadata } from './types'\n\nexport default class Frigade {\n private apiKey?: string\n private userId?: string\n private organizationId?: string\n private config?: FrigadeConfig\n private hasInitialized = false\n\n private flows: Flow[] = []\n\n public async init(apiKey: string, config?: FrigadeConfig): Promise<void> {\n this.apiKey = apiKey\n this.config = config\n await this.refreshFlows()\n this.hasInitialized = true\n return Promise.any(null)\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.userId = userId\n await fetcher(this.apiKey, '/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.userId,\n properties,\n }),\n })\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.organizationId = organizationId\n await fetcher(this.apiKey, '/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n properties,\n }),\n })\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n await fetcher(this.apiKey, '/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n event,\n properties,\n }),\n })\n }\n\n public async getFlow(flowId: string) {\n this.errorOnUninitialized()\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n private errorOnUninitialized() {\n if (!this.hasInitialized) {\n throw new Error(\n 'Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.'\n )\n }\n }\n\n private async refreshFlows() {\n this.flows = []\n const flowDataRaw = await fetcher(this.apiKey, '/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowMetadatas = flowDataRaw.data as FlowMetadata[]\n flowMetadatas.forEach((flowMetadata) => {\n const rawData = JSON.parse(flowMetadata.data)\n this.flows.push(\n new Flow(\n flowMetadata.slug,\n flowMetadata.status,\n rawData,\n rawData?.data ?? [],\n rawData?.title,\n rawData?.subtitle\n )\n )\n })\n }\n }\n}\n","import Frigade from './core/frigade'\nimport Flow from './core/flow'\n\nexport { Flow }\nexport default Frigade\n"],"mappings":";AAAO,IAAMA,EAAiB,SCW9B,IAAMC,EAAoB,wBACpBC,EAAsB,0BAE5B,SAASC,EAAUC,EAAgB,CACjC,MAAO,CACL,OAAQ,CACN,QAAS,CACP,cAAe,UAAUA,IACzB,eAAgB,mBAChB,wBAAyBC,EACzB,yBAA0B,YAC5B,CACF,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,CAEA,eAAsBC,EAAcC,EAAaC,EAAc,CAC7D,IAAMC,EAAgBZ,EAAoBU,EACpCG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,EAAa,KACbA,EAAa,OACbC,GACAA,GAAA,YAAAA,EAAS,OAAQ,CAAC,EAClBA,GAAA,YAAAA,EAAS,MACTA,GAAA,YAAAA,EAAS,QACX,CACF,CACF,CAAC,CAEL,CACF,ECzFA,IAAOE,EAAQC","names":["VERSION_NUMBER","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","getConfig","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","error","fetcher","getUrl","apiUrl","postfix","Flow","id","status","rawData","steps","title","subtitle","Frigade","apiKey","config","userId","properties","fetcher","organizationId","event","flowId","flow","flowDataRaw","flowMetadata","rawData","Flow","src_default","Frigade"]}
1
+ {"version":3,"sources":["../src/core/version.ts","../src/shared/utils.ts","../src/core/flow.ts","../src/core/frigade.ts","../src/index.ts"],"sourcesContent":["export const VERSION_NUMBER = '1.0.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 ABORTED_FLOW = 'ABORTED_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' | 'ABORTED_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\nfunction getConfig(apiKey: string) {\n return {\n config: {\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}\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 (window && window.localStorage) {\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 ...getConfig(apiKey).config,\n })\n}\n","import { FlowStepData } from '../types'\nimport { FlowMetadata, FlowStatus } from './types'\n\nexport default class Flow {\n /**\n * THe Flow ID / slug of the flow\n */\n public readonly id: string\n /**\n * The status of the flow\n */\n public readonly status: FlowStatus\n /**\n * The raw data defined in `flow-data.yml` as a JSON decoded object\n */\n public readonly flowData: Record<any, any>\n /**\n * The steps contained in the `data` array in `flow-data.yml`\n */\n public readonly steps: FlowStepData[]\n /**\n * The user-facing title of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly title?: string\n /**\n * The user-facing description of the flow, if defined at the top level of `flow-data.yml`\n */\n public readonly subtitle?: string\n /**\n * The metadata of the flow.\n */\n public readonly metadata: FlowMetadata\n\n constructor(\n id: string,\n status: FlowStatus,\n rawData: Record<any, any>,\n steps: FlowStepData[],\n title?: string,\n subtitle?: string,\n metadata?: FlowMetadata\n ) {\n this.id = id\n this.status = status\n this.flowData = rawData\n this.steps = steps\n this.title = title\n this.subtitle = subtitle\n this.metadata = metadata\n }\n}\n","import { FrigadeConfig, UserFlowState } from '../types'\nimport { fetcher, generateGuestId, resetAllLocalStorage } from '../shared/utils'\nimport Flow from './flow'\nimport { FlowMetadata } from './types'\n\nexport class Frigade {\n private apiKey?: string\n private userId: string = generateGuestId()\n private organizationId?: string\n private config?: FrigadeConfig\n private hasInitialized = false\n /**\n * Map flow IDs to their current state\n * @private\n */\n private userFlowStatuses: Record<string, UserFlowState> = {}\n\n private flows: Flow[] = []\n\n public async init(apiKey: string, config?: FrigadeConfig): Promise<void> {\n this.apiKey = apiKey\n this.config = config\n if (config?.userId) {\n this.userId = config.userId\n }\n if (config?.organizationId) {\n this.organizationId = config.organizationId\n }\n await Promise.all([this.refreshUserFlowStates(), this.refreshFlows()])\n this.hasInitialized = true\n }\n\n public async identify(userId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.userId = userId\n await fetcher(this.apiKey, '/users', {\n method: 'POST',\n body: JSON.stringify({\n foreignId: this.userId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async group(organizationId: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n this.organizationId = organizationId\n await fetcher(this.apiKey, '/userGroups', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n properties,\n }),\n })\n await this.refreshUserFlowStates()\n }\n\n public async track(event: string, properties?: Record<string, any>): Promise<void> {\n this.errorOnUninitialized()\n await fetcher(this.apiKey, '/track', {\n method: 'POST',\n body: JSON.stringify({\n foreignUserId: this.userId,\n foreignUserGroupId: this.organizationId,\n event,\n properties,\n }),\n })\n }\n\n public async getFlow(flowId: string) {\n this.errorOnUninitialized()\n return this.flows.find((flow) => flow.id == flowId)\n }\n\n public async getFlows() {\n this.errorOnUninitialized()\n return this.flows\n }\n\n public async reset() {\n resetAllLocalStorage()\n this.userId = generateGuestId()\n this.organizationId = undefined\n }\n\n private errorOnUninitialized() {\n if (!this.hasInitialized) {\n throw new Error(\n 'Frigade has not been initialized yet. Please call Frigade.init() before using any other methods.'\n )\n }\n }\n\n private async refreshUserFlowStates() {\n const userFlowStatesRaw = await fetcher(\n this.apiKey,\n `/userFlowStates?foreignUserId=${this.userId}${\n this.organizationId ? `&foreignUserGroupId=${this.organizationId}` : ''\n }`\n )\n if (userFlowStatesRaw && userFlowStatesRaw.data) {\n let userFlowStates = userFlowStatesRaw.data as UserFlowState[]\n userFlowStates.forEach((userFlowState) => {\n this.userFlowStatuses[userFlowState.flowId] = userFlowState\n })\n }\n }\n\n private async refreshFlows() {\n this.flows = []\n const flowDataRaw = await fetcher(this.apiKey, '/flows')\n if (flowDataRaw && flowDataRaw.data) {\n let flowMetadatas = flowDataRaw.data as FlowMetadata[]\n flowMetadatas.forEach((flowMetadata) => {\n const rawData = JSON.parse(flowMetadata.data)\n this.flows.push(\n new Flow(\n flowMetadata.slug,\n flowMetadata.status,\n rawData,\n rawData?.data ?? [],\n rawData?.title,\n rawData?.subtitle,\n flowMetadata\n )\n )\n })\n }\n }\n}\n\nconst frigade = new Frigade()\nexport default frigade\n","import frigade from './core/frigade'\nimport Flow from './core/flow'\n\nexport { Flow }\n\nexport default frigade\n"],"mappings":";AAAO,IAAMA,EAAiB,SCC9B,OAAOC,MAAW,cAClB,OAAS,MAAMC,MAAc,OAW7B,IAAMC,EAAoB,wBACpBC,EAAsB,0BACtBC,EAAY,oBACZC,EAAe,SAErB,SAASC,EAAUC,EAAgB,CACjC,MAAO,CACL,OAAQ,CACN,QAAS,CACP,cAAe,UAAUA,IACzB,eAAgB,mBAChB,wBAAyBC,EACzB,yBAA0B,YAC5B,CACF,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,EACxCG,EAAeX,EAAgBS,CAAe,EACpD,GAAIC,GAAYC,GAAgBA,GAAgBJ,EAAQ,KAAM,CAC5D,IAAMK,EAAe,IAAI,KAAKF,CAAQ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uB,CAC7B,GAAI,CAAC,KAAK,eACR,MAAM,IAAI,MACR,kGACF,CAEJ,CAEA,MAAc,uBAAwB,CACpC,IAAMW,EAAoB,MAAMN,EAC9B,KAAK,OACL,iCAAiC,KAAK,SACpC,KAAK,eAAiB,uBAAuB,KAAK,iBAAmB,IAEzE,EACIM,GAAqBA,EAAkB,MACpBA,EAAkB,KACxB,QAASC,GAAkB,CACxC,KAAK,iBAAiBA,EAAc,MAAM,EAAIA,CAChD,CAAC,CAEL,CAEA,MAAc,cAAe,CAC3B,KAAK,MAAQ,CAAC,EACd,IAAMC,EAAc,MAAMR,EAAQ,KAAK,OAAQ,QAAQ,EACnDQ,GAAeA,EAAY,MACTA,EAAY,KAClB,QAASC,GAAiB,CACtC,IAAMC,EAAU,KAAK,MAAMD,EAAa,IAAI,EAC5C,KAAK,MAAM,KACT,IAAIE,EACFF,EAAa,KACbA,EAAa,OACbC,GACAA,GAAA,YAAAA,EAAS,OAAQ,CAAC,EAClBA,GAAA,YAAAA,EAAS,MACTA,GAAA,YAAAA,EAAS,SACTD,CACF,CACF,CACF,CAAC,CAEL,CACF,EAEMG,EAAU,IAAIlB,EACbmB,EAAQD,EClIf,IAAOE,EAAQC","names":["VERSION_NUMBER","fetch","uuidv4","LAST_POST_CALL_AT","LAST_POST_CALL_DATA","GUEST_KEY","GUEST_PREFIX","getConfig","apiKey","VERSION_NUMBER","getLocalStorage","key","setLocalStorage","value","resetAllLocalStorage","gracefulFetch","url","options","lastCallAtKey","lastCallDataKey","lastCall","lastCallData","lastCallDate","getEmptyResponse","response","fetch","error","generateGuestId","guestId","uuidv4","fetcher","path","Flow","id","status","rawData","steps","title","subtitle","metadata","Frigade","generateGuestId","apiKey","config","userId","properties","fetcher","organizationId","event","flowId","flow","resetAllLocalStorage","userFlowStatesRaw","userFlowState","flowDataRaw","flowMetadata","rawData","Flow","frigade","frigade_default","src_default","frigade_default"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@frigade/js",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "The official Javascript SDK for Frigade.",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
@@ -9,7 +9,7 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "clean": "rimraf ./lib",
12
- "test": "yarn build",
12
+ "test": "jest",
13
13
  "semantic-release": "semantic-release",
14
14
  "lint": "eslint --fix --ext .ts,.tsx .",
15
15
  "copy-files": "copyfiles -u 1 src/**/*.html src/**/*.css ./lib",
@@ -46,6 +46,7 @@
46
46
  "@types/jest": "^23.3.1",
47
47
  "babel-jest": "^29.4.1",
48
48
  "copyfiles": "^2.4.1",
49
+ "husky": "^8.0.3",
49
50
  "jest": "^29.4.1",
50
51
  "jest-config": "^29.3.1",
51
52
  "jest-environment-jsdom": "^29.4.1",
@@ -94,6 +95,7 @@
94
95
  },
95
96
  "homepage": "https://github.com/FrigadeHQ/frigade-js#readme",
96
97
  "dependencies": {
98
+ "cross-fetch": "^4.0.0",
97
99
  "uuid": "^9.0.0"
98
100
  }
99
101
  }