@inploi/sdk 0.4.2 → 0.5.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 CHANGED
@@ -36,8 +36,6 @@ const sdk = initialiseInploiSdk({
36
36
  plugins: [journeysPlugin],
37
37
  env: 'sandbox',
38
38
  });
39
-
40
- // in your component or function
41
-
39
+ / in your component or function
42
40
  sdk.journey.log({ event: 'PAGE_VIEW' });
43
41
  ```
@@ -24,7 +24,9 @@
24
24
  };
25
25
 
26
26
  // src/common/common.plugins.ts
27
- var createPlugin = (pluginName, { initialise }) => ({
27
+ var createPlugin = (pluginName, {
28
+ initialise
29
+ }) => ({
28
30
  pluginName,
29
31
  initialise
30
32
  });
@@ -68,7 +70,12 @@
68
70
  }
69
71
  });
70
72
  }
71
- return { log };
73
+ return {
74
+ /** Logs an event including some client-side metadata.
75
+ * Will fail if called on the server, as it needs access to window and document.
76
+ */
77
+ log
78
+ };
72
79
  }
73
80
 
74
81
  // src/journeys/journeys.ts
package/cdn/sdk.js CHANGED
@@ -42,10 +42,14 @@
42
42
  });
43
43
  };
44
44
 
45
- // src/journeys/journeys.constants.ts
46
- var ENV_TO_API_URL = {
47
- sandbox: "https://preview.api.inploi.com",
48
- production: "https://api.inploi.com"
45
+ // src/common/common.logger.ts
46
+ var CONSOLE_PREFIX = "%c[inploi SDK]";
47
+ var CONSOLE_STYLE = "color: #65BC67; font-weight: bold;";
48
+ var inploiBrandedLogger = {
49
+ warn: (message) => console.warn(CONSOLE_PREFIX, CONSOLE_STYLE, message),
50
+ error: (message) => console.error(CONSOLE_PREFIX, CONSOLE_STYLE, message),
51
+ info: (message) => console.info(CONSOLE_PREFIX, CONSOLE_STYLE, message),
52
+ log: (message) => console.log(CONSOLE_PREFIX, CONSOLE_STYLE, message)
49
53
  };
50
54
 
51
55
  // src/journeys/journeys.api.ts
@@ -80,24 +84,12 @@
80
84
  };
81
85
  };
82
86
 
83
- // src/common/common.logger.ts
84
- var CONSOLE_PREFIX = "%c[inploi SDK]";
85
- var CONSOLE_STYLE = "color: #65BC67; font-weight: bold;";
86
- var inploiBrandedLogger = {
87
- warn: (message) => console.warn(CONSOLE_PREFIX, CONSOLE_STYLE, message),
88
- error: (message) => console.error(CONSOLE_PREFIX, CONSOLE_STYLE, message),
89
- info: (message) => console.info(CONSOLE_PREFIX, CONSOLE_STYLE, message),
90
- log: (message) => console.log(CONSOLE_PREFIX, CONSOLE_STYLE, message)
87
+ // src/journeys/journeys.constants.ts
88
+ var ENV_TO_API_URL = {
89
+ sandbox: "https://preview.api.inploi.com",
90
+ production: "https://api.inploi.com"
91
91
  };
92
92
 
93
- // ../core/dist/chunk-H7ZOE7ZC.mjs
94
- function invariant(condition, message) {
95
- if (condition) {
96
- return;
97
- }
98
- throw new Error(message);
99
- }
100
-
101
93
  // src/sdk.ts
102
94
  function initialiseSdk({
103
95
  publishableKey,
@@ -105,12 +97,14 @@
105
97
  plugins,
106
98
  logger = inploiBrandedLogger
107
99
  }) {
108
- invariant(typeof window !== "undefined", "Can't initialise the inploi SDK outside of a browser environment.");
109
100
  const apiClient = createApiClient({ baseUrl: ENV_TO_API_URL[env], publishableKey });
110
- const pluginsObj = plugins.reduce((acc, plugin) => {
111
- acc[plugin.pluginName] = plugin.initialise({ apiClient, logger });
112
- return acc;
113
- }, {});
101
+ const pluginsObj = plugins.reduce(
102
+ (acc, plugin) => {
103
+ acc[plugin.pluginName] = plugin.initialise({ apiClient, logger });
104
+ return acc;
105
+ },
106
+ {}
107
+ );
114
108
  return pluginsObj;
115
109
  }
116
110
 
@@ -1,2 +1,2 @@
1
1
  var P=Object.defineProperty,u=Object.defineProperties;var c=Object.getOwnPropertyDescriptors;var a=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var s=(t,i,n)=>i in t?P(t,i,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[i]=n,T=(t,i)=>{for(var n in i||(i={}))x.call(i,n)&&s(t,n,i[n]);if(a)for(var n of a(i))A.call(i,n)&&s(t,n,i[n]);return t},d=(t,i)=>u(t,c(i));var E=(t,i,n)=>new Promise((g,p)=>{var l=o=>{try{r(n.next(o))}catch(e){p(e)}},m=o=>{try{r(n.throw(o))}catch(e){p(e)}},r=o=>o.done?g(o.value):Promise.resolve(o.value).then(l,m);r((n=n.apply(t,i)).next())});var _={sandbox:"https://preview.api.inploi.com",production:"https://api.inploi.com"},f="/journey/log";var y=(t,{initialise:i})=>({pluginName:t,initialise:i});export{T as a,d as b,E as c,_ as d,f as e,y as f};
2
- //# sourceMappingURL=chunk-KHVPBEFD.mjs.map
2
+ //# sourceMappingURL=chunk-DK3TFXRH.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/journeys/journeys.constants.ts","../src/common/common.plugins.ts"],"sourcesContent":["import { AppEnvironment } from '../common/common.constants';\n\nexport const ENV_TO_API_URL: Record<AppEnvironment, string> = {\n\tsandbox: 'https://preview.api.inploi.com',\n\tproduction: 'https://api.inploi.com',\n};\n\nexport const JOURNEY_PATHNAME = '/journey/log';\n","import { ApiClient } from '../journeys/journeys.api';\nimport { Logger } from './common.logger';\n\nexport type PluginParams = {\n\tapiClient: ApiClient;\n\tlogger?: Logger;\n};\n\nexport type Plugin<T extends string, P> = {\n\tpluginName: T;\n\tinitialise: (params: PluginParams) => P;\n};\n\nexport const createPlugin = <T extends string, P>(\n\t/** The plugin name to access its properties.\n\t * @example const myPluginVariable = createPlugin('myPluginName', { initialise: () => 'works' });\n\t * const sdk = initialiseSdk({plugins: [myPluginVariable]});\n\t * sdk.myPluginName // 'works'\n\t * sdk.myPluginVariable // undefined\n\t */\n\tpluginName: T,\n\t{\n\t\tinitialise,\n\t}: {\n\t\t/** Called when the SDK is initialised.\n\t\t * Here’s where you can do any one-off initialisation.\n\t\t * Returning a value or function will make it available to the SDK client.\n\t\t * @example const plugin = createPlugin('foo', { initialise: () => ({ bar: 'baz' }) });\n\t\t * const sdk = initialiseSdk({plugins: [plugin]});\n\t\t * sdk.foo.bar // 'baz'\n\t\t */\n\t\tinitialise: (params: PluginParams) => P;\n\t},\n): Plugin<T, P> => ({\n\tpluginName,\n\tinitialise,\n});\n"],"mappings":"0nBAEO,IAAMA,EAAiD,CAC7D,QAAS,iCACT,WAAY,wBACb,EAEaC,EAAmB,eCMzB,IAAMC,EAAe,CAO3BC,EACA,CACC,WAAAC,CACD,KAUmB,CACnB,WAAAD,EACA,WAAAC,CACD","names":["ENV_TO_API_URL","JOURNEY_PATHNAME","createPlugin","pluginName","initialise"]}
@@ -22,7 +22,14 @@ type Plugin<T extends string, P> = {
22
22
  pluginName: T;
23
23
  initialise: (params: PluginParams) => P;
24
24
  };
25
- declare const createPlugin: <T extends string, P>(pluginName: T, { initialise }: {
25
+ declare const createPlugin: <T extends string, P>(pluginName: T, { initialise, }: {
26
+ /** Called when the SDK is initialised.
27
+ * Here’s where you can do any one-off initialisation.
28
+ * Returning a value or function will make it available to the SDK client.
29
+ * @example const plugin = createPlugin('foo', { initialise: () => ({ bar: 'baz' }) });
30
+ * const sdk = initialiseSdk({plugins: [plugin]});
31
+ * sdk.foo.bar // 'baz'
32
+ */
26
33
  initialise: (params: PluginParams) => P;
27
34
  }) => Plugin<T, P>;
28
35
 
@@ -1,4 +1,4 @@
1
- import { P as Plugin } from '../common.plugins-030c372f.js';
1
+ import { P as Plugin } from '../common.plugins-1ba83e05.js';
2
2
  import * as _inploi_core_common from '@inploi/core/common';
3
3
 
4
4
  declare const journeysPlugin: Plugin<"journey", {
@@ -1,4 +1,4 @@
1
- import { P as Plugin } from '../common.plugins-030c372f.js';
1
+ import { P as Plugin } from '../common.plugins-1ba83e05.js';
2
2
  import * as _inploi_core_common from '@inploi/core/common';
3
3
 
4
4
  declare const journeysPlugin: Plugin<"journey", {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/journeys/journeys.ts","../../src/common/common.plugins.ts","../../src/journeys/journeys.constants.ts","../../src/journeys/journeys.log.ts"],"sourcesContent":["import { createPlugin } from '../common/common.plugins';\nimport { initialiseJourneysPlugin } from './journeys.log';\n\nexport const journeysPlugin = createPlugin('journey', { initialise: initialiseJourneysPlugin });\n","import { Logger } from './common.logger';\nimport { ApiClient } from '../journeys/journeys.api';\n\nexport type PluginParams = {\n\tapiClient: ApiClient;\n\tlogger?: Logger;\n};\n\nexport type Plugin<T extends string, P> = {\n\tpluginName: T;\n\tinitialise: (params: PluginParams) => P;\n};\n\nexport const createPlugin = <T extends string, P>(\n\tpluginName: T,\n\t{ initialise }: { initialise: (params: PluginParams) => P }\n): Plugin<T, P> => ({\n\tpluginName,\n\tinitialise,\n});\n","import { AppEnvironment } from '../common/common.constants';\n\nexport const ENV_TO_API_URL: Record<AppEnvironment, string> = {\n\tsandbox: 'https://preview.api.inploi.com',\n\tproduction: 'https://api.inploi.com',\n};\n\nexport const JOURNEY_PATHNAME = '/journey/log';\n","import { Flatten, ResponseObj } from '@inploi/core/common';\nimport { SDK_VERSION } from '../common/common.constants';\nimport { JOURNEY_PATHNAME } from './journeys.constants';\nimport { PluginParams } from '../common/common.plugins';\n\ntype JourneyLogParams =\n\t| {\n\t\t\tevent: 'VIEW_JOB' | 'APPLY_START' | 'APPLY_COMPLETE';\n\t\t\tproperties: {\n\t\t\t\tjob_id?: string | number | null; // * inploi job id or external job id?\n\t\t\t};\n\t }\n\t| {\n\t\t\tevent: 'IDENTIFY' | 'SUBMIT_FORM' | 'VIEW_PAGE';\n\t\t\tproperties?: undefined;\n\t };\n\nexport type JourneyLogEvent = JourneyLogParams['event'];\n\ntype EventPropertyMap = {\n\t[Param in JourneyLogParams as Param['event']]: Flatten<Omit<Param, 'event'>>;\n};\n\ntype TrackPayload<P> = {\n\tevent: JourneyLogEvent;\n\tsent_at: string;\n\tcontext: {\n\t\tlibrary: {\n\t\t\tname: 'inploi-sdk';\n\t\t\tversion: number;\n\t\t};\n\t\tpage: {\n\t\t\thref: string;\n\t\t\treferrer: string;\n\t\t\ttitle: string;\n\t\t};\n\t};\n\tproperties: P;\n\tcustom_properties?: Record<string, unknown>;\n};\n\nexport function initialiseJourneysPlugin({ apiClient, logger }: PluginParams) {\n\tasync function log<T extends keyof EventPropertyMap>(\n\t\tparams: { event: T; customProperties?: Record<string, unknown> } & EventPropertyMap[T]\n\t): Promise<ResponseObj<TrackPayload<EventPropertyMap[T]['properties']>>> {\n\t\ttry {\n\t\t\tconst data: TrackPayload<EventPropertyMap[T]['properties']> = {\n\t\t\t\tevent: params.event,\n\t\t\t\tsent_at: new Date().toISOString(),\n\t\t\t\tcontext: {\n\t\t\t\t\tlibrary: {\n\t\t\t\t\t\tname: 'inploi-sdk' as const,\n\t\t\t\t\t\tversion: SDK_VERSION,\n\t\t\t\t\t},\n\t\t\t\t\tpage: {\n\t\t\t\t\t\thref: location.href,\n\t\t\t\t\t\treferrer: document.referrer,\n\t\t\t\t\t\ttitle: document.title,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tproperties: params.properties as EventPropertyMap[T]['properties'],\n\t\t\t\tcustom_properties: params.customProperties,\n\t\t\t};\n\n\t\t\tawait apiClient.fetch(JOURNEY_PATHNAME, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify(data),\n\t\t\t});\n\n\t\t\treturn { success: true, data };\n\t\t} catch (e) {\n\t\t\t/** We dont’t log any PII on the console */\n\t\t\tlogger?.error('Failed to send journey log to API. Inspect error response of `log` for more information.');\n\t\t\treturn { success: false, error: e };\n\t\t}\n\t}\n\n\treturn { log };\n}\n"],"mappings":"snBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,IAAA,eAAAC,EAAAH,GCaO,IAAMI,EAAe,CAC3BC,EACA,CAAE,WAAAC,CAAW,KACM,CACnB,WAAAD,EACA,WAAAC,CACD,GCZO,IAAMC,EAAmB,eCkCzB,SAASC,EAAyB,CAAE,UAAAC,EAAW,OAAAC,CAAO,EAAiB,CAC7E,SAAeC,EACdC,EACwE,QAAAC,EAAA,sBACxE,GAAI,CACH,IAAMC,EAAwD,CAC7D,MAAOF,EAAO,MACd,QAAS,IAAI,KAAK,EAAE,YAAY,EAChC,QAAS,CACR,QAAS,CACR,KAAM,aACN,QAAS,CACV,EACA,KAAM,CACL,KAAM,SAAS,KACf,SAAU,SAAS,SACnB,MAAO,SAAS,KACjB,CACD,EACA,WAAYA,EAAO,WACnB,kBAAmBA,EAAO,gBAC3B,EAEA,aAAMH,EAAU,MAAMM,EAAkB,CACvC,OAAQ,OACR,KAAM,KAAK,UAAUD,CAAI,CAC1B,CAAC,EAEM,CAAE,QAAS,GAAM,KAAAA,CAAK,CAC9B,OAASE,EAAG,CAEX,OAAAN,GAAA,MAAAA,EAAQ,MAAM,4FACP,CAAE,QAAS,GAAO,MAAOM,CAAE,CACnC,CACD,GAEA,MAAO,CAAE,IAAAL,CAAI,CACd,CH3EO,IAAMM,EAAiBC,EAAa,UAAW,CAAE,WAAYC,CAAyB,CAAC","names":["journeys_exports","__export","journeysPlugin","__toCommonJS","createPlugin","pluginName","initialise","JOURNEY_PATHNAME","initialiseJourneysPlugin","apiClient","logger","log","params","__async","data","JOURNEY_PATHNAME","e","journeysPlugin","createPlugin","initialiseJourneysPlugin"]}
1
+ {"version":3,"sources":["../../src/journeys/journeys.ts","../../src/common/common.plugins.ts","../../src/journeys/journeys.constants.ts","../../src/journeys/journeys.log.ts"],"sourcesContent":["import { createPlugin } from '../common/common.plugins';\nimport { initialiseJourneysPlugin } from './journeys.log';\n\nexport const journeysPlugin = createPlugin('journey', { initialise: initialiseJourneysPlugin });\n","import { ApiClient } from '../journeys/journeys.api';\nimport { Logger } from './common.logger';\n\nexport type PluginParams = {\n\tapiClient: ApiClient;\n\tlogger?: Logger;\n};\n\nexport type Plugin<T extends string, P> = {\n\tpluginName: T;\n\tinitialise: (params: PluginParams) => P;\n};\n\nexport const createPlugin = <T extends string, P>(\n\t/** The plugin name to access its properties.\n\t * @example const myPluginVariable = createPlugin('myPluginName', { initialise: () => 'works' });\n\t * const sdk = initialiseSdk({plugins: [myPluginVariable]});\n\t * sdk.myPluginName // 'works'\n\t * sdk.myPluginVariable // undefined\n\t */\n\tpluginName: T,\n\t{\n\t\tinitialise,\n\t}: {\n\t\t/** Called when the SDK is initialised.\n\t\t * Here’s where you can do any one-off initialisation.\n\t\t * Returning a value or function will make it available to the SDK client.\n\t\t * @example const plugin = createPlugin('foo', { initialise: () => ({ bar: 'baz' }) });\n\t\t * const sdk = initialiseSdk({plugins: [plugin]});\n\t\t * sdk.foo.bar // 'baz'\n\t\t */\n\t\tinitialise: (params: PluginParams) => P;\n\t},\n): Plugin<T, P> => ({\n\tpluginName,\n\tinitialise,\n});\n","import { AppEnvironment } from '../common/common.constants';\n\nexport const ENV_TO_API_URL: Record<AppEnvironment, string> = {\n\tsandbox: 'https://preview.api.inploi.com',\n\tproduction: 'https://api.inploi.com',\n};\n\nexport const JOURNEY_PATHNAME = '/journey/log';\n","import { Flatten, ResponseObj } from '@inploi/core/common';\n\nimport { SDK_VERSION } from '../common/common.constants';\nimport { PluginParams } from '../common/common.plugins';\nimport { JOURNEY_PATHNAME } from './journeys.constants';\n\ntype JourneyLogParams =\n\t| {\n\t\t\tevent: 'VIEW_JOB' | 'APPLY_START' | 'APPLY_COMPLETE';\n\t\t\tproperties: {\n\t\t\t\tjob_id?: string | number | null; // * inploi job id or external job id?\n\t\t\t};\n\t }\n\t| {\n\t\t\tevent: 'IDENTIFY' | 'SUBMIT_FORM' | 'VIEW_PAGE';\n\t\t\tproperties?: undefined;\n\t };\n\nexport type JourneyLogEvent = JourneyLogParams['event'];\n\ntype EventPropertyMap = {\n\t[Param in JourneyLogParams as Param['event']]: Flatten<Omit<Param, 'event'>>;\n};\n\ntype TrackPayload<P> = {\n\tevent: JourneyLogEvent;\n\tsent_at: string;\n\tcontext: {\n\t\tlibrary: {\n\t\t\tname: 'inploi-sdk';\n\t\t\tversion: number;\n\t\t};\n\t\tpage: {\n\t\t\thref: string;\n\t\t\treferrer: string;\n\t\t\ttitle: string;\n\t\t};\n\t};\n\tproperties: P;\n\tcustom_properties?: Record<string, unknown>;\n};\n\nexport function initialiseJourneysPlugin({ apiClient, logger }: PluginParams) {\n\tasync function log<T extends keyof EventPropertyMap>(\n\t\tparams: { event: T; customProperties?: Record<string, unknown> } & EventPropertyMap[T],\n\t): Promise<ResponseObj<TrackPayload<EventPropertyMap[T]['properties']>>> {\n\t\ttry {\n\t\t\tconst data: TrackPayload<EventPropertyMap[T]['properties']> = {\n\t\t\t\tevent: params.event,\n\t\t\t\tsent_at: new Date().toISOString(),\n\t\t\t\tcontext: {\n\t\t\t\t\tlibrary: {\n\t\t\t\t\t\tname: 'inploi-sdk' as const,\n\t\t\t\t\t\tversion: SDK_VERSION,\n\t\t\t\t\t},\n\t\t\t\t\tpage: {\n\t\t\t\t\t\thref: location.href,\n\t\t\t\t\t\treferrer: document.referrer,\n\t\t\t\t\t\ttitle: document.title,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tproperties: params.properties as EventPropertyMap[T]['properties'],\n\t\t\t\tcustom_properties: params.customProperties,\n\t\t\t};\n\n\t\t\tawait apiClient.fetch(JOURNEY_PATHNAME, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify(data),\n\t\t\t});\n\n\t\t\treturn { success: true, data };\n\t\t} catch (e) {\n\t\t\t/** We dont’t log any PII on the console */\n\t\t\tlogger?.error('Failed to send journey log to API. Inspect error response of `log` for more information.');\n\t\t\treturn { success: false, error: e };\n\t\t}\n\t}\n\n\treturn {\n\t\t/** Logs an event including some client-side metadata.\n\t\t * Will fail if called on the server, as it needs access to window and document.\n\t\t */\n\t\tlog,\n\t};\n}\n"],"mappings":"snBAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,IAAA,eAAAC,EAAAH,GCaO,IAAMI,EAAe,CAO3BC,EACA,CACC,WAAAC,CACD,KAUmB,CACnB,WAAAD,EACA,WAAAC,CACD,GC7BO,IAAMC,EAAmB,eCmCzB,SAASC,EAAyB,CAAE,UAAAC,EAAW,OAAAC,CAAO,EAAiB,CAC7E,SAAeC,EACdC,EACwE,QAAAC,EAAA,sBACxE,GAAI,CACH,IAAMC,EAAwD,CAC7D,MAAOF,EAAO,MACd,QAAS,IAAI,KAAK,EAAE,YAAY,EAChC,QAAS,CACR,QAAS,CACR,KAAM,aACN,QAAS,CACV,EACA,KAAM,CACL,KAAM,SAAS,KACf,SAAU,SAAS,SACnB,MAAO,SAAS,KACjB,CACD,EACA,WAAYA,EAAO,WACnB,kBAAmBA,EAAO,gBAC3B,EAEA,aAAMH,EAAU,MAAMM,EAAkB,CACvC,OAAQ,OACR,KAAM,KAAK,UAAUD,CAAI,CAC1B,CAAC,EAEM,CAAE,QAAS,GAAM,KAAAA,CAAK,CAC9B,OAASE,EAAG,CAEX,OAAAN,GAAA,MAAAA,EAAQ,MAAM,4FACP,CAAE,QAAS,GAAO,MAAOM,CAAE,CACnC,CACD,GAEA,MAAO,CAIN,IAAAL,CACD,CACD,CHjFO,IAAMM,EAAiBC,EAAa,UAAW,CAAE,WAAYC,CAAyB,CAAC","names":["journeys_exports","__export","journeysPlugin","__toCommonJS","createPlugin","pluginName","initialise","JOURNEY_PATHNAME","initialiseJourneysPlugin","apiClient","logger","log","params","__async","data","JOURNEY_PATHNAME","e","journeysPlugin","createPlugin","initialiseJourneysPlugin"]}
@@ -1,2 +1,2 @@
1
- import{c as o,e as n,f as i}from"../chunk-KHVPBEFD.mjs";function s({apiClient:p,logger:r}){function a(t){return o(this,null,function*(){try{let e={event:t.event,sent_at:new Date().toISOString(),context:{library:{name:"inploi-sdk",version:1},page:{href:location.href,referrer:document.referrer,title:document.title}},properties:t.properties,custom_properties:t.customProperties};return yield p.fetch(n,{method:"POST",body:JSON.stringify(e)}),{success:!0,data:e}}catch(e){return r==null||r.error("Failed to send journey log to API. Inspect error response of `log` for more information."),{success:!1,error:e}}})}return{log:a}}var v=i("journey",{initialise:s});export{v as journeysPlugin};
1
+ import{c as o,e as n,f as i}from"../chunk-DK3TFXRH.mjs";function s({apiClient:p,logger:r}){function a(t){return o(this,null,function*(){try{let e={event:t.event,sent_at:new Date().toISOString(),context:{library:{name:"inploi-sdk",version:1},page:{href:location.href,referrer:document.referrer,title:document.title}},properties:t.properties,custom_properties:t.customProperties};return yield p.fetch(n,{method:"POST",body:JSON.stringify(e)}),{success:!0,data:e}}catch(e){return r==null||r.error("Failed to send journey log to API. Inspect error response of `log` for more information."),{success:!1,error:e}}})}return{log:a}}var v=i("journey",{initialise:s});export{v as journeysPlugin};
2
2
  //# sourceMappingURL=journeys.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/journeys/journeys.log.ts","../../src/journeys/journeys.ts"],"sourcesContent":["import { Flatten, ResponseObj } from '@inploi/core/common';\nimport { SDK_VERSION } from '../common/common.constants';\nimport { JOURNEY_PATHNAME } from './journeys.constants';\nimport { PluginParams } from '../common/common.plugins';\n\ntype JourneyLogParams =\n\t| {\n\t\t\tevent: 'VIEW_JOB' | 'APPLY_START' | 'APPLY_COMPLETE';\n\t\t\tproperties: {\n\t\t\t\tjob_id?: string | number | null; // * inploi job id or external job id?\n\t\t\t};\n\t }\n\t| {\n\t\t\tevent: 'IDENTIFY' | 'SUBMIT_FORM' | 'VIEW_PAGE';\n\t\t\tproperties?: undefined;\n\t };\n\nexport type JourneyLogEvent = JourneyLogParams['event'];\n\ntype EventPropertyMap = {\n\t[Param in JourneyLogParams as Param['event']]: Flatten<Omit<Param, 'event'>>;\n};\n\ntype TrackPayload<P> = {\n\tevent: JourneyLogEvent;\n\tsent_at: string;\n\tcontext: {\n\t\tlibrary: {\n\t\t\tname: 'inploi-sdk';\n\t\t\tversion: number;\n\t\t};\n\t\tpage: {\n\t\t\thref: string;\n\t\t\treferrer: string;\n\t\t\ttitle: string;\n\t\t};\n\t};\n\tproperties: P;\n\tcustom_properties?: Record<string, unknown>;\n};\n\nexport function initialiseJourneysPlugin({ apiClient, logger }: PluginParams) {\n\tasync function log<T extends keyof EventPropertyMap>(\n\t\tparams: { event: T; customProperties?: Record<string, unknown> } & EventPropertyMap[T]\n\t): Promise<ResponseObj<TrackPayload<EventPropertyMap[T]['properties']>>> {\n\t\ttry {\n\t\t\tconst data: TrackPayload<EventPropertyMap[T]['properties']> = {\n\t\t\t\tevent: params.event,\n\t\t\t\tsent_at: new Date().toISOString(),\n\t\t\t\tcontext: {\n\t\t\t\t\tlibrary: {\n\t\t\t\t\t\tname: 'inploi-sdk' as const,\n\t\t\t\t\t\tversion: SDK_VERSION,\n\t\t\t\t\t},\n\t\t\t\t\tpage: {\n\t\t\t\t\t\thref: location.href,\n\t\t\t\t\t\treferrer: document.referrer,\n\t\t\t\t\t\ttitle: document.title,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tproperties: params.properties as EventPropertyMap[T]['properties'],\n\t\t\t\tcustom_properties: params.customProperties,\n\t\t\t};\n\n\t\t\tawait apiClient.fetch(JOURNEY_PATHNAME, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify(data),\n\t\t\t});\n\n\t\t\treturn { success: true, data };\n\t\t} catch (e) {\n\t\t\t/** We dont’t log any PII on the console */\n\t\t\tlogger?.error('Failed to send journey log to API. Inspect error response of `log` for more information.');\n\t\t\treturn { success: false, error: e };\n\t\t}\n\t}\n\n\treturn { log };\n}\n","import { createPlugin } from '../common/common.plugins';\nimport { initialiseJourneysPlugin } from './journeys.log';\n\nexport const journeysPlugin = createPlugin('journey', { initialise: initialiseJourneysPlugin });\n"],"mappings":"wDAyCO,SAASA,EAAyB,CAAE,UAAAC,EAAW,OAAAC,CAAO,EAAiB,CAC7E,SAAeC,EACdC,EACwE,QAAAC,EAAA,sBACxE,GAAI,CACH,IAAMC,EAAwD,CAC7D,MAAOF,EAAO,MACd,QAAS,IAAI,KAAK,EAAE,YAAY,EAChC,QAAS,CACR,QAAS,CACR,KAAM,aACN,QAAS,CACV,EACA,KAAM,CACL,KAAM,SAAS,KACf,SAAU,SAAS,SACnB,MAAO,SAAS,KACjB,CACD,EACA,WAAYA,EAAO,WACnB,kBAAmBA,EAAO,gBAC3B,EAEA,aAAMH,EAAU,MAAMM,EAAkB,CACvC,OAAQ,OACR,KAAM,KAAK,UAAUD,CAAI,CAC1B,CAAC,EAEM,CAAE,QAAS,GAAM,KAAAA,CAAK,CAC9B,OAAS,EAAG,CAEX,OAAAJ,GAAA,MAAAA,EAAQ,MAAM,4FACP,CAAE,QAAS,GAAO,MAAO,CAAE,CACnC,CACD,GAEA,MAAO,CAAE,IAAAC,CAAI,CACd,CC3EO,IAAMK,EAAiBC,EAAa,UAAW,CAAE,WAAYC,CAAyB,CAAC","names":["initialiseJourneysPlugin","apiClient","logger","log","params","__async","data","JOURNEY_PATHNAME","journeysPlugin","createPlugin","initialiseJourneysPlugin"]}
1
+ {"version":3,"sources":["../../src/journeys/journeys.log.ts","../../src/journeys/journeys.ts"],"sourcesContent":["import { Flatten, ResponseObj } from '@inploi/core/common';\n\nimport { SDK_VERSION } from '../common/common.constants';\nimport { PluginParams } from '../common/common.plugins';\nimport { JOURNEY_PATHNAME } from './journeys.constants';\n\ntype JourneyLogParams =\n\t| {\n\t\t\tevent: 'VIEW_JOB' | 'APPLY_START' | 'APPLY_COMPLETE';\n\t\t\tproperties: {\n\t\t\t\tjob_id?: string | number | null; // * inploi job id or external job id?\n\t\t\t};\n\t }\n\t| {\n\t\t\tevent: 'IDENTIFY' | 'SUBMIT_FORM' | 'VIEW_PAGE';\n\t\t\tproperties?: undefined;\n\t };\n\nexport type JourneyLogEvent = JourneyLogParams['event'];\n\ntype EventPropertyMap = {\n\t[Param in JourneyLogParams as Param['event']]: Flatten<Omit<Param, 'event'>>;\n};\n\ntype TrackPayload<P> = {\n\tevent: JourneyLogEvent;\n\tsent_at: string;\n\tcontext: {\n\t\tlibrary: {\n\t\t\tname: 'inploi-sdk';\n\t\t\tversion: number;\n\t\t};\n\t\tpage: {\n\t\t\thref: string;\n\t\t\treferrer: string;\n\t\t\ttitle: string;\n\t\t};\n\t};\n\tproperties: P;\n\tcustom_properties?: Record<string, unknown>;\n};\n\nexport function initialiseJourneysPlugin({ apiClient, logger }: PluginParams) {\n\tasync function log<T extends keyof EventPropertyMap>(\n\t\tparams: { event: T; customProperties?: Record<string, unknown> } & EventPropertyMap[T],\n\t): Promise<ResponseObj<TrackPayload<EventPropertyMap[T]['properties']>>> {\n\t\ttry {\n\t\t\tconst data: TrackPayload<EventPropertyMap[T]['properties']> = {\n\t\t\t\tevent: params.event,\n\t\t\t\tsent_at: new Date().toISOString(),\n\t\t\t\tcontext: {\n\t\t\t\t\tlibrary: {\n\t\t\t\t\t\tname: 'inploi-sdk' as const,\n\t\t\t\t\t\tversion: SDK_VERSION,\n\t\t\t\t\t},\n\t\t\t\t\tpage: {\n\t\t\t\t\t\thref: location.href,\n\t\t\t\t\t\treferrer: document.referrer,\n\t\t\t\t\t\ttitle: document.title,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tproperties: params.properties as EventPropertyMap[T]['properties'],\n\t\t\t\tcustom_properties: params.customProperties,\n\t\t\t};\n\n\t\t\tawait apiClient.fetch(JOURNEY_PATHNAME, {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tbody: JSON.stringify(data),\n\t\t\t});\n\n\t\t\treturn { success: true, data };\n\t\t} catch (e) {\n\t\t\t/** We dont’t log any PII on the console */\n\t\t\tlogger?.error('Failed to send journey log to API. Inspect error response of `log` for more information.');\n\t\t\treturn { success: false, error: e };\n\t\t}\n\t}\n\n\treturn {\n\t\t/** Logs an event including some client-side metadata.\n\t\t * Will fail if called on the server, as it needs access to window and document.\n\t\t */\n\t\tlog,\n\t};\n}\n","import { createPlugin } from '../common/common.plugins';\nimport { initialiseJourneysPlugin } from './journeys.log';\n\nexport const journeysPlugin = createPlugin('journey', { initialise: initialiseJourneysPlugin });\n"],"mappings":"wDA0CO,SAASA,EAAyB,CAAE,UAAAC,EAAW,OAAAC,CAAO,EAAiB,CAC7E,SAAeC,EACdC,EACwE,QAAAC,EAAA,sBACxE,GAAI,CACH,IAAMC,EAAwD,CAC7D,MAAOF,EAAO,MACd,QAAS,IAAI,KAAK,EAAE,YAAY,EAChC,QAAS,CACR,QAAS,CACR,KAAM,aACN,QAAS,CACV,EACA,KAAM,CACL,KAAM,SAAS,KACf,SAAU,SAAS,SACnB,MAAO,SAAS,KACjB,CACD,EACA,WAAYA,EAAO,WACnB,kBAAmBA,EAAO,gBAC3B,EAEA,aAAMH,EAAU,MAAMM,EAAkB,CACvC,OAAQ,OACR,KAAM,KAAK,UAAUD,CAAI,CAC1B,CAAC,EAEM,CAAE,QAAS,GAAM,KAAAA,CAAK,CAC9B,OAAS,EAAG,CAEX,OAAAJ,GAAA,MAAAA,EAAQ,MAAM,4FACP,CAAE,QAAS,GAAO,MAAO,CAAE,CACnC,CACD,GAEA,MAAO,CAIN,IAAAC,CACD,CACD,CCjFO,IAAMK,EAAiBC,EAAa,UAAW,CAAE,WAAYC,CAAyB,CAAC","names":["initialiseJourneysPlugin","apiClient","logger","log","params","__async","data","JOURNEY_PATHNAME","journeysPlugin","createPlugin","initialiseJourneysPlugin"]}
package/dist/sdk.d.mts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as Plugin, L as Logger } from './common.plugins-030c372f.js';
2
- export { C as CONSOLE_PREFIX, a as CONSOLE_STYLE, c as createPlugin, i as inploiBrandedLogger, n as noLogging } from './common.plugins-030c372f.js';
1
+ import { P as Plugin, L as Logger } from './common.plugins-1ba83e05.js';
2
+ export { C as CONSOLE_PREFIX, a as CONSOLE_STYLE, c as createPlugin, i as inploiBrandedLogger, n as noLogging } from './common.plugins-1ba83e05.js';
3
3
 
4
4
  /**
5
5
  * The environment the SDK should run in.
package/dist/sdk.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as Plugin, L as Logger } from './common.plugins-030c372f.js';
2
- export { C as CONSOLE_PREFIX, a as CONSOLE_STYLE, c as createPlugin, i as inploiBrandedLogger, n as noLogging } from './common.plugins-030c372f.js';
1
+ import { P as Plugin, L as Logger } from './common.plugins-1ba83e05.js';
2
+ export { C as CONSOLE_PREFIX, a as CONSOLE_STYLE, c as createPlugin, i as inploiBrandedLogger, n as noLogging } from './common.plugins-1ba83e05.js';
3
3
 
4
4
  /**
5
5
  * The environment the SDK should run in.
package/dist/sdk.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var g=Object.defineProperty,L=Object.defineProperties,b=Object.getOwnPropertyDescriptor,A=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertyNames,h=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,C=Object.prototype.propertyIsEnumerable;var P=(e,n,r)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[n]=r,u=(e,n)=>{for(var r in n||(n={}))w.call(n,r)&&P(e,r,n[r]);if(h)for(var r of h(n))C.call(n,r)&&P(e,r,n[r]);return e},x=(e,n)=>L(e,A(n));var N=(e,n)=>{for(var r in n)g(e,r,{get:n[r],enumerable:!0})},S=(e,n,r,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let i of O(n))!w.call(e,i)&&i!==r&&g(e,i,{get:()=>n[i],enumerable:!(t=b(n,i))||t.enumerable});return e};var I=e=>S(g({},"__esModule",{value:!0}),e);var y=(e,n,r)=>new Promise((t,i)=>{var f=a=>{try{o(r.next(a))}catch(m){i(m)}},s=a=>{try{o(r.throw(a))}catch(m){i(m)}},o=a=>a.done?t(a.value):Promise.resolve(a.value).then(f,s);o((r=r.apply(e,n)).next())});var R={};N(R,{CONSOLE_PREFIX:()=>p,CONSOLE_STYLE:()=>l,createPlugin:()=>E,initialiseSdk:()=>q,inploiBrandedLogger:()=>c,noLogging:()=>U});module.exports=I(R);var v={sandbox:"https://preview.api.inploi.com",production:"https://api.inploi.com"};var _="Unauthenticated",M="This action is unauthorized.",T=e=>{let n={Accept:"application/json","Content-Type":"application/json","x-publishable-key":e.publishableKey};return{fetch:(i,...f)=>y(void 0,[i,...f],function*(r,t={}){let s=x(u({},t),{headers:u(u({},t.headers),n)}),o=yield fetch(`${e.baseUrl}${r}`,s).then(a=>a.json());if("message"in o){if(o.message===_)throw new Error("You are not authenticated.");if(o.message===M)throw new Error("You are not authenticated.");if("exception"in o)throw new Error(`API error: \u201C${o.message}\u201D`);if("errors"in o)throw new Error(`API error: \u201C${o.message}\u201D`)}return o})}};var p="%c[inploi SDK]",l="color: #65BC67; font-weight: bold;",c={warn:e=>console.warn(p,l,e),error:e=>console.error(p,l,e),info:e=>console.info(p,l,e),log:e=>console.log(p,l,e)},U={info:()=>{},error:()=>{},log:()=>{},warn:()=>{}};function d(e,n){if(!e)throw new Error(n)}var E=(e,{initialise:n})=>({pluginName:e,initialise:n});function q({publishableKey:e,env:n,plugins:r,logger:t=c}){d(typeof window!="undefined","Can't initialise the inploi SDK outside of a browser environment.");let i=T({baseUrl:v[n],publishableKey:e});return r.reduce((s,o)=>(s[o.pluginName]=o.initialise({apiClient:i,logger:t}),s),{})}0&&(module.exports={CONSOLE_PREFIX,CONSOLE_STYLE,createPlugin,initialiseSdk,inploiBrandedLogger,noLogging});
1
+ "use strict";var l=Object.defineProperty,w=Object.defineProperties,E=Object.getOwnPropertyDescriptor,b=Object.getOwnPropertyDescriptors,C=Object.getOwnPropertyNames,d=Object.getOwnPropertySymbols;var x=Object.prototype.hasOwnProperty,v=Object.prototype.propertyIsEnumerable;var f=(e,n,o)=>n in e?l(e,n,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[n]=o,u=(e,n)=>{for(var o in n||(n={}))x.call(n,o)&&f(e,o,n[o]);if(d)for(var o of d(n))v.call(n,o)&&f(e,o,n[o]);return e},h=(e,n)=>w(e,b(n));var I=(e,n)=>{for(var o in n)l(e,o,{get:n[o],enumerable:!0})},N=(e,n,o,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of C(n))!x.call(e,r)&&r!==o&&l(e,r,{get:()=>n[r],enumerable:!(t=E(n,r))||t.enumerable});return e};var O=e=>N(l({},"__esModule",{value:!0}),e);var y=(e,n,o)=>new Promise((t,r)=>{var P=s=>{try{i(o.next(s))}catch(m){r(m)}},a=s=>{try{i(o.throw(s))}catch(m){r(m)}},i=s=>s.done?t(s.value):Promise.resolve(s.value).then(P,a);i((o=o.apply(e,n)).next())});var U={};I(U,{CONSOLE_PREFIX:()=>p,CONSOLE_STYLE:()=>g,createPlugin:()=>T,initialiseSdk:()=>S,inploiBrandedLogger:()=>c,noLogging:()=>_});module.exports=O(U);var p="%c[inploi SDK]",g="color: #65BC67; font-weight: bold;",c={warn:e=>console.warn(p,g,e),error:e=>console.error(p,g,e),info:e=>console.info(p,g,e),log:e=>console.log(p,g,e)},_={info:()=>{},error:()=>{},log:()=>{},warn:()=>{}};var M="Unauthenticated",R="This action is unauthorized.",L=e=>{let n={Accept:"application/json","Content-Type":"application/json","x-publishable-key":e.publishableKey};return{fetch:(r,...P)=>y(void 0,[r,...P],function*(o,t={}){let a=h(u({},t),{headers:u(u({},t.headers),n)}),i=yield fetch(`${e.baseUrl}${o}`,a).then(s=>s.json());if("message"in i){if(i.message===M)throw new Error("You are not authenticated.");if(i.message===R)throw new Error("You are not authenticated.");if("exception"in i)throw new Error(`API error: \u201C${i.message}\u201D`);if("errors"in i)throw new Error(`API error: \u201C${i.message}\u201D`)}return i})}};var A={sandbox:"https://preview.api.inploi.com",production:"https://api.inploi.com"};var T=(e,{initialise:n})=>({pluginName:e,initialise:n});function S({publishableKey:e,env:n,plugins:o,logger:t=c}){let r=L({baseUrl:A[n],publishableKey:e});return o.reduce((a,i)=>(a[i.pluginName]=i.initialise({apiClient:r,logger:t}),a),{})}0&&(module.exports={CONSOLE_PREFIX,CONSOLE_STYLE,createPlugin,initialiseSdk,inploiBrandedLogger,noLogging});
2
2
  //# sourceMappingURL=sdk.js.map
package/dist/sdk.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/sdk.ts","../src/journeys/journeys.constants.ts","../src/journeys/journeys.api.ts","../src/common/common.logger.ts","../../core/src/common/common.utils.ts","../src/common/common.plugins.ts"],"sourcesContent":["import { ENV_TO_API_URL } from './journeys/journeys.constants';\nimport { createApiClient } from './journeys/journeys.api';\nimport { Logger, inploiBrandedLogger } from './common/common.logger';\n\nimport { Flatten, invariant } from '@inploi/core/common';\nimport { AppEnvironment } from './common/common.constants';\nimport { Plugin } from './common/common.plugins';\n\n/** Different loggers so people can use `noLogging` */\nexport * from './common/common.logger';\nexport type { Plugin } from './common/common.plugins';\nexport { createPlugin } from './common/common.plugins';\n\nexport type InitialiseInploiSdkParams<P extends Plugin<any, any>> = {\n\t/** Your public API key for the inploi SDK. */\n\tpublishableKey: string;\n\t/** Add your plugins here. Try importing and passing `journeysPlugin` ;) */\n\tplugins: P[];\n\t/** Which app environment to run. This ultimately affects which inploi endpoints to gather data are going to be used.\n\t * Anything other than `production` should be considered a development environment and the data periodicaly purged. */\n\tenv: AppEnvironment;\n\t/** Logger object that handles logging of different levels.\n\t * You can override this to use your own logger, or to disable logging altogether by importing and passing `noLogging`.\n\t * @default inploiBrandedLogger\n\t * */\n\tlogger?: Logger;\n};\n\nexport function initialiseSdk<TPlugin extends Plugin<any, any>>({\n\tpublishableKey,\n\tenv,\n\tplugins,\n\tlogger = inploiBrandedLogger,\n}: InitialiseInploiSdkParams<TPlugin>) {\n\tinvariant(typeof window !== 'undefined', \"Can't initialise the inploi SDK outside of a browser environment.\");\n\n\tconst apiClient = createApiClient({ baseUrl: ENV_TO_API_URL[env], publishableKey });\n\n\tconst pluginsObj = plugins.reduce((acc, plugin) => {\n\t\tacc[plugin.pluginName as TPlugin['pluginName']] = plugin.initialise({ apiClient, logger });\n\t\treturn acc;\n\t}, {} as { [K in TPlugin['pluginName']]: ReturnType<Flatten<TPlugin & { pluginName: K }>['initialise']> });\n\n\treturn pluginsObj;\n}\n","import { AppEnvironment } from '../common/common.constants';\n\nexport const ENV_TO_API_URL: Record<AppEnvironment, string> = {\n\tsandbox: 'https://preview.api.inploi.com',\n\tproduction: 'https://api.inploi.com',\n};\n\nexport const JOURNEY_PATHNAME = '/journey/log';\n","export const unauthenticatedMessage = 'Unauthenticated';\nexport const unauthorisedMessage = 'This action is unauthorized.';\n\nexport type ApiClient = {\n\tfetch: (pathname: string, options?: RequestInit) => Promise<unknown>;\n};\n\nexport const createApiClient = (params: { baseUrl: string; publishableKey: string }): ApiClient => {\n\tconst defaultHeaders: HeadersInit = {\n\t\tAccept: 'application/json',\n\t\t'Content-Type': 'application/json',\n\t\t'x-publishable-key': params.publishableKey,\n\t};\n\n\treturn {\n\t\tfetch: async (pathname, options = {}) => {\n\t\t\tconst init = { ...options, headers: { ...options.headers, ...defaultHeaders } };\n\t\t\tconst response = await fetch(`${params.baseUrl}${pathname}`, init).then(res => res.json());\n\n\t\t\tif ('message' in response) {\n\t\t\t\tif (response.message === unauthenticatedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif (response.message === unauthorisedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif ('exception' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t\tif ('errors' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn response;\n\t\t},\n\t};\n};\n","export const CONSOLE_PREFIX = '%c[inploi SDK]';\nexport const CONSOLE_STYLE = 'color: #65BC67; font-weight: bold;';\n\ntype LogMessage = (...data: any[]) => void;\nexport type Logger = {\n\twarn: LogMessage;\n\terror: LogMessage;\n\tinfo: LogMessage;\n\tlog: LogMessage;\n};\n\nexport const inploiBrandedLogger: Logger = {\n\twarn: message => console.warn(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\terror: message => console.error(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tinfo: message => console.info(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tlog: message => console.log(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n};\n\nexport const noLogging: Logger = { info: () => void 0, error: () => void 0, log: () => void 0, warn: () => void 0 };\n","export const exhaustive = (value: never) => {\n\tthrow new Error(`Unhandled discriminated union member: ${JSON.stringify(value)}`);\n};\n\nexport function invariant(condition: any, message?: string): asserts condition {\n\tif (condition) {\n\t\treturn;\n\t}\n\n\tthrow new Error(message);\n}\n\nexport const uniqueOrThrow = <T>(array: T[]) => {\n\tconst uniqueValues = new Set(array);\n\n\tif (array.length !== uniqueValues.size) {\n\t\tthrow new Error('Duplicate values found');\n\t}\n\treturn true;\n};\n\nexport const uniqueByPropertyOrThrow =\n\t<T extends PropertyKey>(propertyKey: T) =>\n\t(array: { [K in T]: any }[]) => {\n\t\tconst values = array.map(item => item[propertyKey]);\n\t\tconst uniqueValues = new Set(values);\n\n\t\tif (values.length !== uniqueValues.size) {\n\t\t\tthrow new Error(`Duplicate values found for property: ${propertyKey.toString()}`);\n\t\t}\n\t\treturn true;\n\t};\n","import { Logger } from './common.logger';\nimport { ApiClient } from '../journeys/journeys.api';\n\nexport type PluginParams = {\n\tapiClient: ApiClient;\n\tlogger?: Logger;\n};\n\nexport type Plugin<T extends string, P> = {\n\tpluginName: T;\n\tinitialise: (params: PluginParams) => P;\n};\n\nexport const createPlugin = <T extends string, P>(\n\tpluginName: T,\n\t{ initialise }: { initialise: (params: PluginParams) => P }\n): Plugin<T, P> => ({\n\tpluginName,\n\tinitialise,\n});\n"],"mappings":"i9BAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,kBAAAC,EAAA,iBAAAC,EAAA,kBAAAC,EAAA,wBAAAC,EAAA,cAAAC,IAAA,eAAAC,EAAAR,GCEO,IAAMS,EAAiD,CAC7D,QAAS,iCACT,WAAY,wBACb,ECLO,IAAMC,EAAyB,kBACzBC,EAAsB,+BAMtBC,EAAmBC,GAAmE,CAClG,IAAMC,EAA8B,CACnC,OAAQ,mBACR,eAAgB,mBAChB,oBAAqBD,EAAO,cAC7B,EAEA,MAAO,CACN,MAAO,CAAOE,KAA2BC,IAAAC,EAAA,QAA3BF,EAA2B,GAAAC,GAAA,UAA3BE,EAAUC,EAAU,CAAC,EAAM,CACxC,IAAMC,EAAOC,EAAAC,EAAA,GAAKH,GAAL,CAAc,QAASG,IAAA,GAAKH,EAAQ,SAAYL,EAAiB,GACxES,EAAW,MAAM,MAAM,GAAGV,EAAO,OAAO,GAAGK,CAAQ,GAAIE,CAAI,EAAE,KAAKI,GAAOA,EAAI,KAAK,CAAC,EAEzF,GAAI,YAAaD,EAAU,CAC1B,GAAIA,EAAS,UAAYb,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAIa,EAAS,UAAYZ,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAI,cAAeY,EAClB,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,EAEnD,GAAI,WAAYA,EACf,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,CAEpD,CACA,OAAOA,CACR,EACD,CACD,ECpCO,IAAME,EAAiB,iBACjBC,EAAgB,qCAUhBC,EAA8B,CAC1C,KAAMC,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,MAAOA,GAAW,QAAQ,MAAMH,EAAgBC,EAAeE,CAAO,EACtE,KAAMA,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,IAAKA,GAAW,QAAQ,IAAIH,EAAgBC,EAAeE,CAAO,CACnE,EAEaC,EAAoB,CAAE,KAAM,IAAG,GAAW,MAAO,IAAG,GAAW,IAAK,IAAG,GAAW,KAAM,IAAG,EAAU,ECd3G,SAASC,EAAUC,EAAgBC,EAAqC,CAC9E,GAAI,CAAAD,EAIJ,MAAM,IAAI,MAAMC,CAAO,CACxB,CCGO,IAAMC,EAAe,CAC3BC,EACA,CAAE,WAAAC,CAAW,KACM,CACnB,WAAAD,EACA,WAAAC,CACD,GLSO,SAASC,EAAgD,CAC/D,eAAAC,EACA,IAAAC,EACA,QAAAC,EACA,OAAAC,EAASC,CACV,EAAuC,CACtCC,EAAU,OAAO,QAAW,YAAa,mEAAmE,EAE5G,IAAMC,EAAYC,EAAgB,CAAE,QAASC,EAAeP,CAAG,EAAG,eAAAD,CAAe,CAAC,EAOlF,OALmBE,EAAQ,OAAO,CAACO,EAAKC,KACvCD,EAAIC,EAAO,UAAmC,EAAIA,EAAO,WAAW,CAAE,UAAAJ,EAAW,OAAAH,CAAO,CAAC,EAClFM,GACL,CAAC,CAAqG,CAG1G","names":["sdk_exports","__export","CONSOLE_PREFIX","CONSOLE_STYLE","createPlugin","initialiseSdk","inploiBrandedLogger","noLogging","__toCommonJS","ENV_TO_API_URL","unauthenticatedMessage","unauthorisedMessage","createApiClient","params","defaultHeaders","_0","_1","__async","pathname","options","init","__spreadProps","__spreadValues","response","res","CONSOLE_PREFIX","CONSOLE_STYLE","inploiBrandedLogger","message","noLogging","invariant","condition","message","createPlugin","pluginName","initialise","initialiseSdk","publishableKey","env","plugins","logger","inploiBrandedLogger","invariant","apiClient","createApiClient","ENV_TO_API_URL","acc","plugin"]}
1
+ {"version":3,"sources":["../src/sdk.ts","../src/common/common.logger.ts","../src/journeys/journeys.api.ts","../src/journeys/journeys.constants.ts","../src/common/common.plugins.ts"],"sourcesContent":["import { Flatten } from '@inploi/core/common';\n\nimport { AppEnvironment } from './common/common.constants';\nimport { Logger, inploiBrandedLogger } from './common/common.logger';\nimport { Plugin } from './common/common.plugins';\nimport { createApiClient } from './journeys/journeys.api';\nimport { ENV_TO_API_URL } from './journeys/journeys.constants';\n\n/** Different loggers so people can use `noLogging` */\nexport * from './common/common.logger';\nexport type { Plugin } from './common/common.plugins';\nexport { createPlugin } from './common/common.plugins';\n\nexport type InitialiseInploiSdkParams<P extends Plugin<any, any>> = {\n\t/** Your public API key for the inploi SDK. */\n\tpublishableKey: string;\n\t/** Add your plugins here. Try importing and passing `journeysPlugin` ;) */\n\tplugins: P[];\n\t/** Which app environment to run. This ultimately affects which inploi endpoints to gather data are going to be used.\n\t * Anything other than `production` should be considered a development environment and the data periodicaly purged. */\n\tenv: AppEnvironment;\n\t/** Logger object that handles logging of different levels.\n\t * You can override this to use your own logger, or to disable logging altogether by importing and passing `noLogging`.\n\t * @default inploiBrandedLogger\n\t * */\n\tlogger?: Logger;\n};\n\nexport function initialiseSdk<TPlugin extends Plugin<any, any>>({\n\tpublishableKey,\n\tenv,\n\tplugins,\n\tlogger = inploiBrandedLogger,\n}: InitialiseInploiSdkParams<TPlugin>) {\n\tconst apiClient = createApiClient({ baseUrl: ENV_TO_API_URL[env], publishableKey });\n\n\tconst pluginsObj = plugins.reduce(\n\t\t(acc, plugin) => {\n\t\t\tacc[plugin.pluginName as TPlugin['pluginName']] = plugin.initialise({ apiClient, logger });\n\t\t\treturn acc;\n\t\t},\n\t\t{} as { [K in TPlugin['pluginName']]: ReturnType<Flatten<TPlugin & { pluginName: K }>['initialise']> },\n\t);\n\n\treturn pluginsObj;\n}\n","export const CONSOLE_PREFIX = '%c[inploi SDK]';\nexport const CONSOLE_STYLE = 'color: #65BC67; font-weight: bold;';\n\ntype LogMessage = (...data: any[]) => void;\nexport type Logger = {\n\twarn: LogMessage;\n\terror: LogMessage;\n\tinfo: LogMessage;\n\tlog: LogMessage;\n};\n\nexport const inploiBrandedLogger: Logger = {\n\twarn: message => console.warn(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\terror: message => console.error(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tinfo: message => console.info(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tlog: message => console.log(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n};\n\nexport const noLogging: Logger = { info: () => void 0, error: () => void 0, log: () => void 0, warn: () => void 0 };\n","export const unauthenticatedMessage = 'Unauthenticated';\nexport const unauthorisedMessage = 'This action is unauthorized.';\n\nexport type ApiClient = {\n\tfetch: (pathname: string, options?: RequestInit) => Promise<unknown>;\n};\n\nexport const createApiClient = (params: { baseUrl: string; publishableKey: string }): ApiClient => {\n\tconst defaultHeaders: HeadersInit = {\n\t\tAccept: 'application/json',\n\t\t'Content-Type': 'application/json',\n\t\t'x-publishable-key': params.publishableKey,\n\t};\n\n\treturn {\n\t\tfetch: async (pathname, options = {}) => {\n\t\t\tconst init = { ...options, headers: { ...options.headers, ...defaultHeaders } };\n\t\t\tconst response = await fetch(`${params.baseUrl}${pathname}`, init).then(res => res.json());\n\n\t\t\tif ('message' in response) {\n\t\t\t\tif (response.message === unauthenticatedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif (response.message === unauthorisedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif ('exception' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t\tif ('errors' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn response;\n\t\t},\n\t};\n};\n","import { AppEnvironment } from '../common/common.constants';\n\nexport const ENV_TO_API_URL: Record<AppEnvironment, string> = {\n\tsandbox: 'https://preview.api.inploi.com',\n\tproduction: 'https://api.inploi.com',\n};\n\nexport const JOURNEY_PATHNAME = '/journey/log';\n","import { ApiClient } from '../journeys/journeys.api';\nimport { Logger } from './common.logger';\n\nexport type PluginParams = {\n\tapiClient: ApiClient;\n\tlogger?: Logger;\n};\n\nexport type Plugin<T extends string, P> = {\n\tpluginName: T;\n\tinitialise: (params: PluginParams) => P;\n};\n\nexport const createPlugin = <T extends string, P>(\n\t/** The plugin name to access its properties.\n\t * @example const myPluginVariable = createPlugin('myPluginName', { initialise: () => 'works' });\n\t * const sdk = initialiseSdk({plugins: [myPluginVariable]});\n\t * sdk.myPluginName // 'works'\n\t * sdk.myPluginVariable // undefined\n\t */\n\tpluginName: T,\n\t{\n\t\tinitialise,\n\t}: {\n\t\t/** Called when the SDK is initialised.\n\t\t * Here’s where you can do any one-off initialisation.\n\t\t * Returning a value or function will make it available to the SDK client.\n\t\t * @example const plugin = createPlugin('foo', { initialise: () => ({ bar: 'baz' }) });\n\t\t * const sdk = initialiseSdk({plugins: [plugin]});\n\t\t * sdk.foo.bar // 'baz'\n\t\t */\n\t\tinitialise: (params: PluginParams) => P;\n\t},\n): Plugin<T, P> => ({\n\tpluginName,\n\tinitialise,\n});\n"],"mappings":"i9BAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,kBAAAC,EAAA,iBAAAC,EAAA,kBAAAC,EAAA,wBAAAC,EAAA,cAAAC,IAAA,eAAAC,EAAAR,GCAO,IAAMS,EAAiB,iBACjBC,EAAgB,qCAUhBC,EAA8B,CAC1C,KAAMC,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,MAAOA,GAAW,QAAQ,MAAMH,EAAgBC,EAAeE,CAAO,EACtE,KAAMA,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,IAAKA,GAAW,QAAQ,IAAIH,EAAgBC,EAAeE,CAAO,CACnE,EAEaC,EAAoB,CAAE,KAAM,IAAG,GAAW,MAAO,IAAG,GAAW,IAAK,IAAG,GAAW,KAAM,IAAG,EAAU,EClB3G,IAAMC,EAAyB,kBACzBC,EAAsB,+BAMtBC,EAAmBC,GAAmE,CAClG,IAAMC,EAA8B,CACnC,OAAQ,mBACR,eAAgB,mBAChB,oBAAqBD,EAAO,cAC7B,EAEA,MAAO,CACN,MAAO,CAAOE,KAA2BC,IAAAC,EAAA,QAA3BF,EAA2B,GAAAC,GAAA,UAA3BE,EAAUC,EAAU,CAAC,EAAM,CACxC,IAAMC,EAAOC,EAAAC,EAAA,GAAKH,GAAL,CAAc,QAASG,IAAA,GAAKH,EAAQ,SAAYL,EAAiB,GACxES,EAAW,MAAM,MAAM,GAAGV,EAAO,OAAO,GAAGK,CAAQ,GAAIE,CAAI,EAAE,KAAKI,GAAOA,EAAI,KAAK,CAAC,EAEzF,GAAI,YAAaD,EAAU,CAC1B,GAAIA,EAAS,UAAYb,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAIa,EAAS,UAAYZ,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAI,cAAeY,EAClB,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,EAEnD,GAAI,WAAYA,EACf,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,CAEpD,CACA,OAAOA,CACR,EACD,CACD,EClCO,IAAME,EAAiD,CAC7D,QAAS,iCACT,WAAY,wBACb,ECQO,IAAMC,EAAe,CAO3BC,EACA,CACC,WAAAC,CACD,KAUmB,CACnB,WAAAD,EACA,WAAAC,CACD,GJRO,SAASC,EAAgD,CAC/D,eAAAC,EACA,IAAAC,EACA,QAAAC,EACA,OAAAC,EAASC,CACV,EAAuC,CACtC,IAAMC,EAAYC,EAAgB,CAAE,QAASC,EAAeN,CAAG,EAAG,eAAAD,CAAe,CAAC,EAUlF,OARmBE,EAAQ,OAC1B,CAACM,EAAKC,KACLD,EAAIC,EAAO,UAAmC,EAAIA,EAAO,WAAW,CAAE,UAAAJ,EAAW,OAAAF,CAAO,CAAC,EAClFK,GAER,CAAC,CACF,CAGD","names":["sdk_exports","__export","CONSOLE_PREFIX","CONSOLE_STYLE","createPlugin","initialiseSdk","inploiBrandedLogger","noLogging","__toCommonJS","CONSOLE_PREFIX","CONSOLE_STYLE","inploiBrandedLogger","message","noLogging","unauthenticatedMessage","unauthorisedMessage","createApiClient","params","defaultHeaders","_0","_1","__async","pathname","options","init","__spreadProps","__spreadValues","response","res","ENV_TO_API_URL","createPlugin","pluginName","initialise","initialiseSdk","publishableKey","env","plugins","logger","inploiBrandedLogger","apiClient","createApiClient","ENV_TO_API_URL","acc","plugin"]}
package/dist/sdk.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{a as t,b as p,c as g,d as c,f as w}from"./chunk-KHVPBEFD.mjs";var x="Unauthenticated",y="This action is unauthorized.",m=e=>{let r={Accept:"application/json","Content-Type":"application/json","x-publishable-key":e.publishableKey};return{fetch:(h,...P)=>g(void 0,[h,...P],function*(u,o={}){let i=p(t({},o),{headers:t(t({},o.headers),r)}),n=yield fetch(`${e.baseUrl}${u}`,i).then(f=>f.json());if("message"in n){if(n.message===x)throw new Error("You are not authenticated.");if(n.message===y)throw new Error("You are not authenticated.");if("exception"in n)throw new Error(`API error: \u201C${n.message}\u201D`);if("errors"in n)throw new Error(`API error: \u201C${n.message}\u201D`)}return n})}};var a="%c[inploi SDK]",s="color: #65BC67; font-weight: bold;",d={warn:e=>console.warn(a,s,e),error:e=>console.error(a,s,e),info:e=>console.info(a,s,e),log:e=>console.log(a,s,e)},b={info:()=>{},error:()=>{},log:()=>{},warn:()=>{}};function l(e,r){if(!e)throw new Error(r)}function j({publishableKey:e,env:r,plugins:u,logger:o=d}){l(typeof window!="undefined","Can't initialise the inploi SDK outside of a browser environment.");let h=m({baseUrl:c[r],publishableKey:e});return u.reduce((i,n)=>(i[n.pluginName]=n.initialise({apiClient:h,logger:o}),i),{})}export{a as CONSOLE_PREFIX,s as CONSOLE_STYLE,w as createPlugin,j as initialiseSdk,d as inploiBrandedLogger,b as noLogging};
1
+ import{a as i,b as l,c as p,d as u,f as h}from"./chunk-DK3TFXRH.mjs";var t="%c[inploi SDK]",s="color: #65BC67; font-weight: bold;",c={warn:e=>console.warn(t,s,e),error:e=>console.error(t,s,e),info:e=>console.info(t,s,e),log:e=>console.log(t,s,e)},L={info:()=>{},error:()=>{},log:()=>{},warn:()=>{}};var P="Unauthenticated",x="This action is unauthorized.",m=e=>{let a={Accept:"application/json","Content-Type":"application/json","x-publishable-key":e.publishableKey};return{fetch:(d,...y)=>p(void 0,[d,...y],function*(g,o={}){let r=l(i({},o),{headers:i(i({},o.headers),a)}),n=yield fetch(`${e.baseUrl}${g}`,r).then(f=>f.json());if("message"in n){if(n.message===P)throw new Error("You are not authenticated.");if(n.message===x)throw new Error("You are not authenticated.");if("exception"in n)throw new Error(`API error: \u201C${n.message}\u201D`);if("errors"in n)throw new Error(`API error: \u201C${n.message}\u201D`)}return n})}};function v({publishableKey:e,env:a,plugins:g,logger:o=c}){let d=m({baseUrl:u[a],publishableKey:e});return g.reduce((r,n)=>(r[n.pluginName]=n.initialise({apiClient:d,logger:o}),r),{})}export{t as CONSOLE_PREFIX,s as CONSOLE_STYLE,h as createPlugin,v as initialiseSdk,c as inploiBrandedLogger,L as noLogging};
2
2
  //# sourceMappingURL=sdk.mjs.map
package/dist/sdk.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/journeys/journeys.api.ts","../src/common/common.logger.ts","../../core/src/common/common.utils.ts","../src/sdk.ts"],"sourcesContent":["export const unauthenticatedMessage = 'Unauthenticated';\nexport const unauthorisedMessage = 'This action is unauthorized.';\n\nexport type ApiClient = {\n\tfetch: (pathname: string, options?: RequestInit) => Promise<unknown>;\n};\n\nexport const createApiClient = (params: { baseUrl: string; publishableKey: string }): ApiClient => {\n\tconst defaultHeaders: HeadersInit = {\n\t\tAccept: 'application/json',\n\t\t'Content-Type': 'application/json',\n\t\t'x-publishable-key': params.publishableKey,\n\t};\n\n\treturn {\n\t\tfetch: async (pathname, options = {}) => {\n\t\t\tconst init = { ...options, headers: { ...options.headers, ...defaultHeaders } };\n\t\t\tconst response = await fetch(`${params.baseUrl}${pathname}`, init).then(res => res.json());\n\n\t\t\tif ('message' in response) {\n\t\t\t\tif (response.message === unauthenticatedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif (response.message === unauthorisedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif ('exception' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t\tif ('errors' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn response;\n\t\t},\n\t};\n};\n","export const CONSOLE_PREFIX = '%c[inploi SDK]';\nexport const CONSOLE_STYLE = 'color: #65BC67; font-weight: bold;';\n\ntype LogMessage = (...data: any[]) => void;\nexport type Logger = {\n\twarn: LogMessage;\n\terror: LogMessage;\n\tinfo: LogMessage;\n\tlog: LogMessage;\n};\n\nexport const inploiBrandedLogger: Logger = {\n\twarn: message => console.warn(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\terror: message => console.error(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tinfo: message => console.info(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tlog: message => console.log(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n};\n\nexport const noLogging: Logger = { info: () => void 0, error: () => void 0, log: () => void 0, warn: () => void 0 };\n","export const exhaustive = (value: never) => {\n\tthrow new Error(`Unhandled discriminated union member: ${JSON.stringify(value)}`);\n};\n\nexport function invariant(condition: any, message?: string): asserts condition {\n\tif (condition) {\n\t\treturn;\n\t}\n\n\tthrow new Error(message);\n}\n\nexport const uniqueOrThrow = <T>(array: T[]) => {\n\tconst uniqueValues = new Set(array);\n\n\tif (array.length !== uniqueValues.size) {\n\t\tthrow new Error('Duplicate values found');\n\t}\n\treturn true;\n};\n\nexport const uniqueByPropertyOrThrow =\n\t<T extends PropertyKey>(propertyKey: T) =>\n\t(array: { [K in T]: any }[]) => {\n\t\tconst values = array.map(item => item[propertyKey]);\n\t\tconst uniqueValues = new Set(values);\n\n\t\tif (values.length !== uniqueValues.size) {\n\t\t\tthrow new Error(`Duplicate values found for property: ${propertyKey.toString()}`);\n\t\t}\n\t\treturn true;\n\t};\n","import { ENV_TO_API_URL } from './journeys/journeys.constants';\nimport { createApiClient } from './journeys/journeys.api';\nimport { Logger, inploiBrandedLogger } from './common/common.logger';\n\nimport { Flatten, invariant } from '@inploi/core/common';\nimport { AppEnvironment } from './common/common.constants';\nimport { Plugin } from './common/common.plugins';\n\n/** Different loggers so people can use `noLogging` */\nexport * from './common/common.logger';\nexport type { Plugin } from './common/common.plugins';\nexport { createPlugin } from './common/common.plugins';\n\nexport type InitialiseInploiSdkParams<P extends Plugin<any, any>> = {\n\t/** Your public API key for the inploi SDK. */\n\tpublishableKey: string;\n\t/** Add your plugins here. Try importing and passing `journeysPlugin` ;) */\n\tplugins: P[];\n\t/** Which app environment to run. This ultimately affects which inploi endpoints to gather data are going to be used.\n\t * Anything other than `production` should be considered a development environment and the data periodicaly purged. */\n\tenv: AppEnvironment;\n\t/** Logger object that handles logging of different levels.\n\t * You can override this to use your own logger, or to disable logging altogether by importing and passing `noLogging`.\n\t * @default inploiBrandedLogger\n\t * */\n\tlogger?: Logger;\n};\n\nexport function initialiseSdk<TPlugin extends Plugin<any, any>>({\n\tpublishableKey,\n\tenv,\n\tplugins,\n\tlogger = inploiBrandedLogger,\n}: InitialiseInploiSdkParams<TPlugin>) {\n\tinvariant(typeof window !== 'undefined', \"Can't initialise the inploi SDK outside of a browser environment.\");\n\n\tconst apiClient = createApiClient({ baseUrl: ENV_TO_API_URL[env], publishableKey });\n\n\tconst pluginsObj = plugins.reduce((acc, plugin) => {\n\t\tacc[plugin.pluginName as TPlugin['pluginName']] = plugin.initialise({ apiClient, logger });\n\t\treturn acc;\n\t}, {} as { [K in TPlugin['pluginName']]: ReturnType<Flatten<TPlugin & { pluginName: K }>['initialise']> });\n\n\treturn pluginsObj;\n}\n"],"mappings":"qEAAO,IAAMA,EAAyB,kBACzBC,EAAsB,+BAMtBC,EAAmBC,GAAmE,CAClG,IAAMC,EAA8B,CACnC,OAAQ,mBACR,eAAgB,mBAChB,oBAAqBD,EAAO,cAC7B,EAEA,MAAO,CACN,MAAO,CAAOE,KAA2BC,IAAAC,EAAA,QAA3BF,EAA2B,GAAAC,GAAA,UAA3BE,EAAUC,EAAU,CAAC,EAAM,CACxC,IAAMC,EAAOC,EAAAC,EAAA,GAAKH,GAAL,CAAc,QAASG,IAAA,GAAKH,EAAQ,SAAYL,EAAiB,GACxES,EAAW,MAAM,MAAM,GAAGV,EAAO,OAAO,GAAGK,CAAQ,GAAIE,CAAI,EAAE,KAAKI,GAAOA,EAAI,KAAK,CAAC,EAEzF,GAAI,YAAaD,EAAU,CAC1B,GAAIA,EAAS,UAAYb,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAIa,EAAS,UAAYZ,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAI,cAAeY,EAClB,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,EAEnD,GAAI,WAAYA,EACf,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,CAEpD,CACA,OAAOA,CACR,EACD,CACD,ECpCO,IAAME,EAAiB,iBACjBC,EAAgB,qCAUhBC,EAA8B,CAC1C,KAAMC,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,MAAOA,GAAW,QAAQ,MAAMH,EAAgBC,EAAeE,CAAO,EACtE,KAAMA,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,IAAKA,GAAW,QAAQ,IAAIH,EAAgBC,EAAeE,CAAO,CACnE,EAEaC,EAAoB,CAAE,KAAM,IAAG,GAAW,MAAO,IAAG,GAAW,IAAK,IAAG,GAAW,KAAM,IAAG,EAAU,ECd3G,SAASC,EAAUC,EAAgBC,EAAqC,CAC9E,GAAI,CAAAD,EAIJ,MAAM,IAAI,MAAMC,CAAO,CACxB,CCkBO,SAASC,EAAgD,CAC/D,eAAAC,EACA,IAAAC,EACA,QAAAC,EACA,OAAAC,EAASC,CACV,EAAuC,CACtCC,EAAU,OAAO,QAAW,YAAa,mEAAmE,EAE5G,IAAMC,EAAYC,EAAgB,CAAE,QAASC,EAAeP,CAAG,EAAG,eAAAD,CAAe,CAAC,EAOlF,OALmBE,EAAQ,OAAO,CAACO,EAAKC,KACvCD,EAAIC,EAAO,UAAmC,EAAIA,EAAO,WAAW,CAAE,UAAAJ,EAAW,OAAAH,CAAO,CAAC,EAClFM,GACL,CAAC,CAAqG,CAG1G","names":["unauthenticatedMessage","unauthorisedMessage","createApiClient","params","defaultHeaders","_0","_1","__async","pathname","options","init","__spreadProps","__spreadValues","response","res","CONSOLE_PREFIX","CONSOLE_STYLE","inploiBrandedLogger","message","noLogging","invariant","condition","message","initialiseSdk","publishableKey","env","plugins","logger","inploiBrandedLogger","invariant","apiClient","createApiClient","ENV_TO_API_URL","acc","plugin"]}
1
+ {"version":3,"sources":["../src/common/common.logger.ts","../src/journeys/journeys.api.ts","../src/sdk.ts"],"sourcesContent":["export const CONSOLE_PREFIX = '%c[inploi SDK]';\nexport const CONSOLE_STYLE = 'color: #65BC67; font-weight: bold;';\n\ntype LogMessage = (...data: any[]) => void;\nexport type Logger = {\n\twarn: LogMessage;\n\terror: LogMessage;\n\tinfo: LogMessage;\n\tlog: LogMessage;\n};\n\nexport const inploiBrandedLogger: Logger = {\n\twarn: message => console.warn(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\terror: message => console.error(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tinfo: message => console.info(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n\tlog: message => console.log(CONSOLE_PREFIX, CONSOLE_STYLE, message),\n};\n\nexport const noLogging: Logger = { info: () => void 0, error: () => void 0, log: () => void 0, warn: () => void 0 };\n","export const unauthenticatedMessage = 'Unauthenticated';\nexport const unauthorisedMessage = 'This action is unauthorized.';\n\nexport type ApiClient = {\n\tfetch: (pathname: string, options?: RequestInit) => Promise<unknown>;\n};\n\nexport const createApiClient = (params: { baseUrl: string; publishableKey: string }): ApiClient => {\n\tconst defaultHeaders: HeadersInit = {\n\t\tAccept: 'application/json',\n\t\t'Content-Type': 'application/json',\n\t\t'x-publishable-key': params.publishableKey,\n\t};\n\n\treturn {\n\t\tfetch: async (pathname, options = {}) => {\n\t\t\tconst init = { ...options, headers: { ...options.headers, ...defaultHeaders } };\n\t\t\tconst response = await fetch(`${params.baseUrl}${pathname}`, init).then(res => res.json());\n\n\t\t\tif ('message' in response) {\n\t\t\t\tif (response.message === unauthenticatedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif (response.message === unauthorisedMessage) {\n\t\t\t\t\tthrow new Error('You are not authenticated.');\n\t\t\t\t}\n\t\t\t\tif ('exception' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t\tif ('errors' in response) {\n\t\t\t\t\tthrow new Error(`API error: “${response.message}”`);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn response;\n\t\t},\n\t};\n};\n","import { Flatten } from '@inploi/core/common';\n\nimport { AppEnvironment } from './common/common.constants';\nimport { Logger, inploiBrandedLogger } from './common/common.logger';\nimport { Plugin } from './common/common.plugins';\nimport { createApiClient } from './journeys/journeys.api';\nimport { ENV_TO_API_URL } from './journeys/journeys.constants';\n\n/** Different loggers so people can use `noLogging` */\nexport * from './common/common.logger';\nexport type { Plugin } from './common/common.plugins';\nexport { createPlugin } from './common/common.plugins';\n\nexport type InitialiseInploiSdkParams<P extends Plugin<any, any>> = {\n\t/** Your public API key for the inploi SDK. */\n\tpublishableKey: string;\n\t/** Add your plugins here. Try importing and passing `journeysPlugin` ;) */\n\tplugins: P[];\n\t/** Which app environment to run. This ultimately affects which inploi endpoints to gather data are going to be used.\n\t * Anything other than `production` should be considered a development environment and the data periodicaly purged. */\n\tenv: AppEnvironment;\n\t/** Logger object that handles logging of different levels.\n\t * You can override this to use your own logger, or to disable logging altogether by importing and passing `noLogging`.\n\t * @default inploiBrandedLogger\n\t * */\n\tlogger?: Logger;\n};\n\nexport function initialiseSdk<TPlugin extends Plugin<any, any>>({\n\tpublishableKey,\n\tenv,\n\tplugins,\n\tlogger = inploiBrandedLogger,\n}: InitialiseInploiSdkParams<TPlugin>) {\n\tconst apiClient = createApiClient({ baseUrl: ENV_TO_API_URL[env], publishableKey });\n\n\tconst pluginsObj = plugins.reduce(\n\t\t(acc, plugin) => {\n\t\t\tacc[plugin.pluginName as TPlugin['pluginName']] = plugin.initialise({ apiClient, logger });\n\t\t\treturn acc;\n\t\t},\n\t\t{} as { [K in TPlugin['pluginName']]: ReturnType<Flatten<TPlugin & { pluginName: K }>['initialise']> },\n\t);\n\n\treturn pluginsObj;\n}\n"],"mappings":"qEAAO,IAAMA,EAAiB,iBACjBC,EAAgB,qCAUhBC,EAA8B,CAC1C,KAAMC,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,MAAOA,GAAW,QAAQ,MAAMH,EAAgBC,EAAeE,CAAO,EACtE,KAAMA,GAAW,QAAQ,KAAKH,EAAgBC,EAAeE,CAAO,EACpE,IAAKA,GAAW,QAAQ,IAAIH,EAAgBC,EAAeE,CAAO,CACnE,EAEaC,EAAoB,CAAE,KAAM,IAAG,GAAW,MAAO,IAAG,GAAW,IAAK,IAAG,GAAW,KAAM,IAAG,EAAU,EClB3G,IAAMC,EAAyB,kBACzBC,EAAsB,+BAMtBC,EAAmBC,GAAmE,CAClG,IAAMC,EAA8B,CACnC,OAAQ,mBACR,eAAgB,mBAChB,oBAAqBD,EAAO,cAC7B,EAEA,MAAO,CACN,MAAO,CAAOE,KAA2BC,IAAAC,EAAA,QAA3BF,EAA2B,GAAAC,GAAA,UAA3BE,EAAUC,EAAU,CAAC,EAAM,CACxC,IAAMC,EAAOC,EAAAC,EAAA,GAAKH,GAAL,CAAc,QAASG,IAAA,GAAKH,EAAQ,SAAYL,EAAiB,GACxES,EAAW,MAAM,MAAM,GAAGV,EAAO,OAAO,GAAGK,CAAQ,GAAIE,CAAI,EAAE,KAAKI,GAAOA,EAAI,KAAK,CAAC,EAEzF,GAAI,YAAaD,EAAU,CAC1B,GAAIA,EAAS,UAAYb,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAIa,EAAS,UAAYZ,EACxB,MAAM,IAAI,MAAM,4BAA4B,EAE7C,GAAI,cAAeY,EAClB,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,EAEnD,GAAI,WAAYA,EACf,MAAM,IAAI,MAAM,oBAAeA,EAAS,OAAO,QAAG,CAEpD,CACA,OAAOA,CACR,EACD,CACD,ECRO,SAASE,EAAgD,CAC/D,eAAAC,EACA,IAAAC,EACA,QAAAC,EACA,OAAAC,EAASC,CACV,EAAuC,CACtC,IAAMC,EAAYC,EAAgB,CAAE,QAASC,EAAeN,CAAG,EAAG,eAAAD,CAAe,CAAC,EAUlF,OARmBE,EAAQ,OAC1B,CAACM,EAAKC,KACLD,EAAIC,EAAO,UAAmC,EAAIA,EAAO,WAAW,CAAE,UAAAJ,EAAW,OAAAF,CAAO,CAAC,EAClFK,GAER,CAAC,CACF,CAGD","names":["CONSOLE_PREFIX","CONSOLE_STYLE","inploiBrandedLogger","message","noLogging","unauthenticatedMessage","unauthorisedMessage","createApiClient","params","defaultHeaders","_0","_1","__async","pathname","options","init","__spreadProps","__spreadValues","response","res","initialiseSdk","publishableKey","env","plugins","logger","inploiBrandedLogger","apiClient","createApiClient","ENV_TO_API_URL","acc","plugin"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inploi/sdk",
3
- "version": "0.4.2",
3
+ "version": "0.5.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "MIT",
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/journeys/journeys.constants.ts","../src/common/common.plugins.ts"],"sourcesContent":["import { AppEnvironment } from '../common/common.constants';\n\nexport const ENV_TO_API_URL: Record<AppEnvironment, string> = {\n\tsandbox: 'https://preview.api.inploi.com',\n\tproduction: 'https://api.inploi.com',\n};\n\nexport const JOURNEY_PATHNAME = '/journey/log';\n","import { Logger } from './common.logger';\nimport { ApiClient } from '../journeys/journeys.api';\n\nexport type PluginParams = {\n\tapiClient: ApiClient;\n\tlogger?: Logger;\n};\n\nexport type Plugin<T extends string, P> = {\n\tpluginName: T;\n\tinitialise: (params: PluginParams) => P;\n};\n\nexport const createPlugin = <T extends string, P>(\n\tpluginName: T,\n\t{ initialise }: { initialise: (params: PluginParams) => P }\n): Plugin<T, P> => ({\n\tpluginName,\n\tinitialise,\n});\n"],"mappings":"0nBAEO,IAAMA,EAAiD,CAC7D,QAAS,iCACT,WAAY,wBACb,EAEaC,EAAmB,eCMzB,IAAMC,EAAe,CAC3BC,EACA,CAAE,WAAAC,CAAW,KACM,CACnB,WAAAD,EACA,WAAAC,CACD","names":["ENV_TO_API_URL","JOURNEY_PATHNAME","createPlugin","pluginName","initialise"]}