@frigade/js 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -1
- package/lib/index.d.ts +9 -7
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +1 -1
- package/lib/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -172,14 +172,16 @@ declare class Flow {
|
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
declare class Frigade {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
hasInitialized
|
|
175
|
+
private apiKey?;
|
|
176
|
+
private userId?;
|
|
177
|
+
private organizationId?;
|
|
178
|
+
private config?;
|
|
179
|
+
private hasInitialized;
|
|
180
180
|
private flows;
|
|
181
|
-
|
|
182
|
-
|
|
181
|
+
init(apiKey: string, config?: FrigadeConfig): Promise<void>;
|
|
182
|
+
identify(userId: string, properties?: Record<string, any>): Promise<void>;
|
|
183
|
+
group(organizationId: string, properties?: Record<string, any>): Promise<void>;
|
|
184
|
+
track(event: string, properties?: Record<string, any>): Promise<void>;
|
|
183
185
|
getFlow(flowId: string): Promise<Flow>;
|
|
184
186
|
private errorOnUninitialized;
|
|
185
187
|
private refreshFlows;
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
var
|
|
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});
|
|
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.32.18'\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 public readonly apiKey?: string\n public readonly userId?: string\n public readonly organizationId?: string\n public readonly config?: FrigadeConfig\n public hasInitialized = false\n\n private flows: Flow[] = []\n\n constructor(apiKey: string, userId?: string, organizationId?: string, config?: FrigadeConfig) {\n this.apiKey = apiKey\n this.userId = userId\n this.organizationId = organizationId\n this.config = config\n }\n\n public async init(): Promise<void> {\n await this.refreshFlows()\n this.hasInitialized = true\n return Promise.any(null)\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,UCW9B,IAAMC,EAAoB,wBACpBC,EAAsB,0BAE5B,SAASC,EAAUC,EAAgB,CACjC,MAAO,CACL,OAAQ,CACN,QAA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,CAAM,CACrB,CAAC,CACH,CAEA,SAASkB,EAAOC,EAAgBC,EAAiB,CAC/C,MAAO,GAAGD,cAAmBC,GAC/B,CC9FA,IAAqBC,EAArB,KAA0B,CA8BxB,YACEC,EACAC,EACAC,EACAC,EACAC,EACAC,EACA,CACA,KAAK,GAAKL,EACV,KAAK,OAASC,EACd,KAAK,SAAWC,EAChB,KAAK,MAAQC,EACb,KAAK,MAAQC,EACb,KAAK,SAAWC,CAClB,CACF,EC3CA,IAAqBC,EAArB,KAA6B,CAS3B,YAAYC,EAAgBC,EAAiBC,EAAyBC,EAAwB,CAJ9F,KAAO,eAAiB,GAExB,KAAQ,MAAgB,CAAC,EAGvB,KAAK,OAASH,EACd,KAAK,OAASC,EACd,KAAK,eAAiBC,EACtB,KAAK,OAASC,CAChB,CAEA,MAAa,MAAsB,CACjC,aAAM,KAAK,aAAa,EACxB,KAAK,eAAiB,GACf,QAAQ,IAAI,IAAI,CACzB,CAEA,MAAa,QAAQC,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,MAAMC,EAAQ,KAAK,OAAQ,QAAQ,EACnDD,GAAeA,EAAY,MACTA,EAAY,KAClB,QAASE,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,EJxDA,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","userId","organizationId","config","flowId","flow","flowDataRaw","fetcher","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 }\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,IAAI,KAAK,EACJ,QAAQ,EAAIE,EAAa,QAAQ,EAEvC,IACT,OAAOC,EAAiB,EAG5BV,EAAgBK,EAAe,IAAI,KAAK,EAAE,YAAY,CAAC,EACvDL,EAAgBM,EAAiBF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,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"]}
|
package/lib/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
var
|
|
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};
|
|
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.32.18'\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 public readonly apiKey?: string\n public readonly userId?: string\n public readonly organizationId?: string\n public readonly config?: FrigadeConfig\n public hasInitialized = false\n\n private flows: Flow[] = []\n\n constructor(apiKey: string, userId?: string, organizationId?: string, config?: FrigadeConfig) {\n this.apiKey = apiKey\n this.userId = userId\n this.organizationId = organizationId\n this.config = config\n }\n\n public async init(): Promise<void> {\n await this.refreshFlows()\n this.hasInitialized = true\n return Promise.any(null)\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,UCW9B,IAAMC,EAAo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AAOE,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","userId","organizationId","config","flowId","flow","flowDataRaw","fetcher","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.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,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,IAAI,KAAK,EACJ,QAAQ,EAAIE,EAAa,QAAQ,EAEvC,IACT,OAAOC,EAAiB,EAG5BV,EAAgBK,EAAe,IAAI,KAAK,EAAE,YAAY,CAAC,EACvDL,EAAgBM,EAAiBF,EAAQ,IAAI,EAG/C,IAAIO,EACJ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BE,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,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"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frigade/js",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "1.0.0",
|
|
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": "build",
|
|
12
|
+
"test": "yarn build",
|
|
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",
|