@inploi/sdk 0.1.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 +43 -0
- package/dist/chunk-A5DU6CID.mjs +2 -0
- package/dist/chunk-A5DU6CID.mjs.map +1 -0
- package/dist/index-d9ce7ffb.d.ts +53 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/journeys/index.d.mts +78 -0
- package/dist/journeys/index.d.ts +78 -0
- package/dist/journeys/index.js +2 -0
- package/dist/journeys/index.js.map +1 -0
- package/dist/journeys/index.mjs +2 -0
- package/dist/journeys/index.mjs.map +1 -0
- package/package.json +43 -0
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @inploi/sdk
|
|
2
|
+
|
|
3
|
+
This package contains the core functionality for the inploi SDK.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @inploi/sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
You can initialise the SDK by calling `initialiseInploiSdk` with the following options:
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { initialiseInploiSdk } from '@inploi/sdk';
|
|
17
|
+
import { journeysPlugin } from '@inploi/sdk/journeys';
|
|
18
|
+
|
|
19
|
+
const sdk = initialiseInploiSdk({
|
|
20
|
+
publishableKey: 'your-publishable-key',
|
|
21
|
+
plugins: [],
|
|
22
|
+
env: 'sandbox',
|
|
23
|
+
});
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Plugins
|
|
27
|
+
|
|
28
|
+
The plugins you install will highly depend on
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
import { initialiseInploiSdk } from '@inploi/sdk';
|
|
32
|
+
import { journeysPlugin } from '@inploi/sdk/journeys';
|
|
33
|
+
|
|
34
|
+
const sdk = initialiseInploiSdk({
|
|
35
|
+
publishableKey: 'your-publishable-key',
|
|
36
|
+
plugins: [journeysPlugin],
|
|
37
|
+
env: 'sandbox',
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// in your component or function
|
|
41
|
+
|
|
42
|
+
sdk.journey.log({ event: 'PAGE_VIEW' });
|
|
43
|
+
```
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
var v=Object.defineProperty,E=Object.defineProperties;var L=Object.getOwnPropertyDescriptors;var m=Object.getOwnPropertySymbols;var b=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var h=(e,n,o)=>n in e?v(e,n,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[n]=o,p=(e,n)=>{for(var o in n||(n={}))b.call(n,o)&&h(e,o,n[o]);if(m)for(var o of m(n))A.call(n,o)&&h(e,o,n[o]);return e},f=(e,n)=>E(e,L(n));var w=(e,n,o)=>new Promise((t,s)=>{var d=r=>{try{i(o.next(r))}catch(u){s(u)}},a=r=>{try{i(o.throw(r))}catch(u){s(u)}},i=r=>r.done?t(r.value):Promise.resolve(r.value).then(d,a);i((o=o.apply(e,n)).next())});var l="%c[inploi SDK]",g="color: #65BC67; font-weight: bold;",x={warn:e=>console.warn(l,g,e),error:e=>console.error(l,g,e),info:e=>console.info(l,g,e),log:e=>console.log(l,g,e)},C={info:()=>{},error:()=>{},log:()=>{},warn:()=>{}};var y={sandbox:"https://preview.api.inploi.com",production:"https://api.inploi.com"},S="/journey/log";var I="Unauthenticated",T="This action is unauthorized.",P=e=>{let n={Accept:"application/json","Content-Type":"application/json","x-publishable-key":e.publishableKey};return{fetch:(s,...d)=>w(void 0,[s,...d],function*(o,t={}){let a=f(p({},t),{headers:p(p({},t.headers),n)}),i=yield fetch(`${e.baseUrl}${o}`,a).then(r=>r.json());if("message"in i){if(i.message===I)throw new Error("You are not authenticated.");if(i.message===T)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})}};function c(e,n){if(!e)throw new Error(n)}function J({publishableKey:e,env:n,plugins:o,logger:t=x}){c(typeof window!="undefined","Can't initialise the inploi SDK outside of a browser environment.");let s=P({baseUrl:y[n],publishableKey:e});return o.reduce((a,i)=>(a[i.pluginName]=i.initialise({apiClient:s,logger:t}),a),{})}export{w as a,S as b,l as c,g as d,x as e,C as f,J as g};
|
|
2
|
+
//# sourceMappingURL=chunk-A5DU6CID.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/common/common.logger.ts","../src/journeys/constants.ts","../src/journeys/api.ts","../../core/src/common/common.utils.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","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 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","import { ENV_TO_API_URL } from './journeys/constants';\nimport { createApiClient } from './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\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 initialiseInploiSdk<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":"0nBAAO,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,EChB3G,IAAMC,EAAiD,CAC7D,QAAS,iCACT,WAAY,wBACb,EAEaC,EAAmB,eCPzB,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,EChCO,SAASE,EAAUC,EAAgBC,EAAqC,CAC9E,GAAI,CAAAD,EAIJ,MAAM,IAAI,MAAMC,CAAO,CACxB,CCaO,SAASC,EAAsD,CACrE,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":["CONSOLE_PREFIX","CONSOLE_STYLE","inploiBrandedLogger","message","noLogging","ENV_TO_API_URL","JOURNEY_PATHNAME","unauthenticatedMessage","unauthorisedMessage","createApiClient","params","defaultHeaders","_0","_1","__async","pathname","options","init","__spreadProps","__spreadValues","response","res","invariant","condition","message","initialiseInploiSdk","publishableKey","env","plugins","logger","inploiBrandedLogger","invariant","apiClient","createApiClient","ENV_TO_API_URL","acc","plugin"]}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
declare const CONSOLE_PREFIX = "%c[inploi SDK]";
|
|
2
|
+
declare const CONSOLE_STYLE = "color: #65BC67; font-weight: bold;";
|
|
3
|
+
type LogMessage = (...data: any[]) => void;
|
|
4
|
+
type Logger = {
|
|
5
|
+
warn: LogMessage;
|
|
6
|
+
error: LogMessage;
|
|
7
|
+
info: LogMessage;
|
|
8
|
+
log: LogMessage;
|
|
9
|
+
};
|
|
10
|
+
declare const inploiBrandedLogger: Logger;
|
|
11
|
+
declare const noLogging: Logger;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The environment the SDK should run in.
|
|
15
|
+
* - `production`: data will be stored and displayed on dashboards
|
|
16
|
+
* - `sandbox`: data will be stored temporarily and purged periodically
|
|
17
|
+
*/
|
|
18
|
+
type AppEnvironment = 'production' | 'sandbox';
|
|
19
|
+
|
|
20
|
+
type ApiClient = {
|
|
21
|
+
fetch: (pathname: string, options?: RequestInit) => Promise<unknown>;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type PluginParams = {
|
|
25
|
+
apiClient: ApiClient;
|
|
26
|
+
logger?: Logger;
|
|
27
|
+
};
|
|
28
|
+
type Plugin<T extends string, P> = {
|
|
29
|
+
pluginName: T;
|
|
30
|
+
initialise: (params: PluginParams) => P;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
type InitialiseInploiSdkParams<P extends Plugin<any, any>> = {
|
|
34
|
+
/** Your public API key for the inploi SDK. */
|
|
35
|
+
publishableKey: string;
|
|
36
|
+
/** Add your plugins here. Try importing and passing `journeysPlugin` ;) */
|
|
37
|
+
plugins: P[];
|
|
38
|
+
/** Which app environment to run. This ultimately affects which inploi endpoints to gather data are going to be used.
|
|
39
|
+
* Anything other than `production` should be considered a development environment and the data periodicaly purged. */
|
|
40
|
+
env: AppEnvironment;
|
|
41
|
+
/** Logger object that handles logging of different levels.
|
|
42
|
+
* You can override this to use your own logger, or to disable logging altogether by importing and passing `noLogging`.
|
|
43
|
+
* @default inploiBrandedLogger
|
|
44
|
+
* */
|
|
45
|
+
logger?: Logger;
|
|
46
|
+
};
|
|
47
|
+
declare function initialiseInploiSdk<TPlugin extends Plugin<any, any>>({ publishableKey, env, plugins, logger, }: InitialiseInploiSdkParams<TPlugin>): { [K in TPlugin["pluginName"]]: ReturnType<(TPlugin & {
|
|
48
|
+
pluginName: K;
|
|
49
|
+
} extends infer T ? { [k in keyof T]: (TPlugin & {
|
|
50
|
+
pluginName: K;
|
|
51
|
+
})[k]; } : never)["initialise"]>; };
|
|
52
|
+
|
|
53
|
+
export { CONSOLE_PREFIX as C, InitialiseInploiSdkParams as I, Logger as L, Plugin as P, CONSOLE_STYLE as a, inploiBrandedLogger as b, initialiseInploiSdk as i, noLogging as n };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { C as CONSOLE_PREFIX, a as CONSOLE_STYLE, I as InitialiseInploiSdkParams, L as Logger, i as initialiseInploiSdk, b as inploiBrandedLogger, n as noLogging } from './index-d9ce7ffb.js';
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { C as CONSOLE_PREFIX, a as CONSOLE_STYLE, I as InitialiseInploiSdkParams, L as Logger, i as initialiseInploiSdk, b as inploiBrandedLogger, n as noLogging } from './index-d9ce7ffb.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var g=Object.defineProperty,L=Object.defineProperties,b=Object.getOwnPropertyDescriptor,A=Object.getOwnPropertyDescriptors,I=Object.getOwnPropertyNames,h=Object.getOwnPropertySymbols;var w=Object.prototype.hasOwnProperty,T=Object.prototype.propertyIsEnumerable;var x=(e,n,o)=>n in e?g(e,n,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[n]=o,u=(e,n)=>{for(var o in n||(n={}))w.call(n,o)&&x(e,o,n[o]);if(h)for(var o of h(n))T.call(n,o)&&x(e,o,n[o]);return e},y=(e,n)=>L(e,A(n));var N=(e,n)=>{for(var o in n)g(e,o,{get:n[o],enumerable:!0})},C=(e,n,o,t)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of I(n))!w.call(e,r)&&r!==o&&g(e,r,{get:()=>n[r],enumerable:!(t=b(n,r))||t.enumerable});return e};var O=e=>C(g({},"__esModule",{value:!0}),e);var P=(e,n,o)=>new Promise((t,r)=>{var f=a=>{try{i(o.next(a))}catch(c){r(c)}},s=a=>{try{i(o.throw(a))}catch(c){r(c)}},i=a=>a.done?t(a.value):Promise.resolve(a.value).then(f,s);i((o=o.apply(e,n)).next())});var K={};N(K,{CONSOLE_PREFIX:()=>p,CONSOLE_STYLE:()=>l,initialiseInploiSdk:()=>U,inploiBrandedLogger:()=>m,noLogging:()=>M});module.exports=O(K);var v={sandbox:"https://preview.api.inploi.com",production:"https://api.inploi.com"};var S="Unauthenticated",_="This action is unauthorized.",E=e=>{let n={Accept:"application/json","Content-Type":"application/json","x-publishable-key":e.publishableKey};return{fetch:(r,...f)=>P(void 0,[r,...f],function*(o,t={}){let s=y(u({},t),{headers:u(u({},t.headers),n)}),i=yield fetch(`${e.baseUrl}${o}`,s).then(a=>a.json());if("message"in i){if(i.message===S)throw new Error("You are not authenticated.");if(i.message===_)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 p="%c[inploi SDK]",l="color: #65BC67; font-weight: bold;",m={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)},M={info:()=>{},error:()=>{},log:()=>{},warn:()=>{}};function d(e,n){if(!e)throw new Error(n)}function U({publishableKey:e,env:n,plugins:o,logger:t=m}){d(typeof window!="undefined","Can't initialise the inploi SDK outside of a browser environment.");let r=E({baseUrl:v[n],publishableKey:e});return o.reduce((s,i)=>(s[i.pluginName]=i.initialise({apiClient:r,logger:t}),s),{})}0&&(module.exports={CONSOLE_PREFIX,CONSOLE_STYLE,initialiseInploiSdk,inploiBrandedLogger,noLogging});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/journeys/constants.ts","../src/journeys/api.ts","../src/common/common.logger.ts","../../core/src/common/common.utils.ts","../src/sdk.ts"],"sourcesContent":["export * from './sdk';\n\n/** Different loggers so people can use `noLogging` */\nexport * from './common/common.logger';\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","import { ENV_TO_API_URL } from './journeys/constants';\nimport { createApiClient } from './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\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 initialiseInploiSdk<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":"i9BAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,oBAAAE,EAAA,kBAAAC,EAAA,wBAAAC,EAAA,wBAAAC,EAAA,cAAAC,IAAA,eAAAC,EAAAP,GCEO,IAAMQ,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,CCaO,SAASC,EAAsD,CACrE,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":["src_exports","__export","CONSOLE_PREFIX","CONSOLE_STYLE","initialiseInploiSdk","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","initialiseInploiSdk","publishableKey","env","plugins","logger","inploiBrandedLogger","invariant","apiClient","createApiClient","ENV_TO_API_URL","acc","plugin"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { P as Plugin } from '../index-d9ce7ffb.js';
|
|
2
|
+
export { I as InitialiseInploiSdkParams, i as initialiseInploiSdk } from '../index-d9ce7ffb.js';
|
|
3
|
+
import * as _inploi_core_common from '@inploi/core/common';
|
|
4
|
+
|
|
5
|
+
declare const journeysPlugin: Plugin<"journey", {
|
|
6
|
+
log: <T extends "VIEW_JOB" | "APPLY_START" | "APPLY_COMPLETE" | "IDENTIFY" | "SUBMIT_FORM" | "VIEW_PAGE">(params: {
|
|
7
|
+
event: T;
|
|
8
|
+
customProperties?: Record<string, unknown> | undefined;
|
|
9
|
+
} & {
|
|
10
|
+
VIEW_JOB: {
|
|
11
|
+
properties: {
|
|
12
|
+
job_id?: string | number | null | undefined;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
APPLY_START: {
|
|
16
|
+
properties: {
|
|
17
|
+
job_id?: string | number | null | undefined;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
APPLY_COMPLETE: {
|
|
21
|
+
properties: {
|
|
22
|
+
job_id?: string | number | null | undefined;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
IDENTIFY: {
|
|
26
|
+
properties?: undefined;
|
|
27
|
+
};
|
|
28
|
+
SUBMIT_FORM: {
|
|
29
|
+
properties?: undefined;
|
|
30
|
+
};
|
|
31
|
+
VIEW_PAGE: {
|
|
32
|
+
properties?: undefined;
|
|
33
|
+
};
|
|
34
|
+
}[T]) => Promise<_inploi_core_common.ResponseObj<{
|
|
35
|
+
event: "VIEW_JOB" | "APPLY_START" | "APPLY_COMPLETE" | "IDENTIFY" | "SUBMIT_FORM" | "VIEW_PAGE";
|
|
36
|
+
sent_at: string;
|
|
37
|
+
context: {
|
|
38
|
+
library: {
|
|
39
|
+
name: "inploi-sdk";
|
|
40
|
+
version: number;
|
|
41
|
+
};
|
|
42
|
+
page: {
|
|
43
|
+
href: string;
|
|
44
|
+
referrer: string;
|
|
45
|
+
title: string;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
properties: {
|
|
49
|
+
VIEW_JOB: {
|
|
50
|
+
properties: {
|
|
51
|
+
job_id?: string | number | null | undefined;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
APPLY_START: {
|
|
55
|
+
properties: {
|
|
56
|
+
job_id?: string | number | null | undefined;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
APPLY_COMPLETE: {
|
|
60
|
+
properties: {
|
|
61
|
+
job_id?: string | number | null | undefined;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
IDENTIFY: {
|
|
65
|
+
properties?: undefined;
|
|
66
|
+
};
|
|
67
|
+
SUBMIT_FORM: {
|
|
68
|
+
properties?: undefined;
|
|
69
|
+
};
|
|
70
|
+
VIEW_PAGE: {
|
|
71
|
+
properties?: undefined;
|
|
72
|
+
};
|
|
73
|
+
}[T]["properties"];
|
|
74
|
+
custom_properties?: Record<string, unknown> | undefined;
|
|
75
|
+
}>>;
|
|
76
|
+
}>;
|
|
77
|
+
|
|
78
|
+
export { journeysPlugin };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { P as Plugin } from '../index-d9ce7ffb.js';
|
|
2
|
+
export { I as InitialiseInploiSdkParams, i as initialiseInploiSdk } from '../index-d9ce7ffb.js';
|
|
3
|
+
import * as _inploi_core_common from '@inploi/core/common';
|
|
4
|
+
|
|
5
|
+
declare const journeysPlugin: Plugin<"journey", {
|
|
6
|
+
log: <T extends "VIEW_JOB" | "APPLY_START" | "APPLY_COMPLETE" | "IDENTIFY" | "SUBMIT_FORM" | "VIEW_PAGE">(params: {
|
|
7
|
+
event: T;
|
|
8
|
+
customProperties?: Record<string, unknown> | undefined;
|
|
9
|
+
} & {
|
|
10
|
+
VIEW_JOB: {
|
|
11
|
+
properties: {
|
|
12
|
+
job_id?: string | number | null | undefined;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
APPLY_START: {
|
|
16
|
+
properties: {
|
|
17
|
+
job_id?: string | number | null | undefined;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
APPLY_COMPLETE: {
|
|
21
|
+
properties: {
|
|
22
|
+
job_id?: string | number | null | undefined;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
IDENTIFY: {
|
|
26
|
+
properties?: undefined;
|
|
27
|
+
};
|
|
28
|
+
SUBMIT_FORM: {
|
|
29
|
+
properties?: undefined;
|
|
30
|
+
};
|
|
31
|
+
VIEW_PAGE: {
|
|
32
|
+
properties?: undefined;
|
|
33
|
+
};
|
|
34
|
+
}[T]) => Promise<_inploi_core_common.ResponseObj<{
|
|
35
|
+
event: "VIEW_JOB" | "APPLY_START" | "APPLY_COMPLETE" | "IDENTIFY" | "SUBMIT_FORM" | "VIEW_PAGE";
|
|
36
|
+
sent_at: string;
|
|
37
|
+
context: {
|
|
38
|
+
library: {
|
|
39
|
+
name: "inploi-sdk";
|
|
40
|
+
version: number;
|
|
41
|
+
};
|
|
42
|
+
page: {
|
|
43
|
+
href: string;
|
|
44
|
+
referrer: string;
|
|
45
|
+
title: string;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
properties: {
|
|
49
|
+
VIEW_JOB: {
|
|
50
|
+
properties: {
|
|
51
|
+
job_id?: string | number | null | undefined;
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
APPLY_START: {
|
|
55
|
+
properties: {
|
|
56
|
+
job_id?: string | number | null | undefined;
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
APPLY_COMPLETE: {
|
|
60
|
+
properties: {
|
|
61
|
+
job_id?: string | number | null | undefined;
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
IDENTIFY: {
|
|
65
|
+
properties?: undefined;
|
|
66
|
+
};
|
|
67
|
+
SUBMIT_FORM: {
|
|
68
|
+
properties?: undefined;
|
|
69
|
+
};
|
|
70
|
+
VIEW_PAGE: {
|
|
71
|
+
properties?: undefined;
|
|
72
|
+
};
|
|
73
|
+
}[T]["properties"];
|
|
74
|
+
custom_properties?: Record<string, unknown> | undefined;
|
|
75
|
+
}>>;
|
|
76
|
+
}>;
|
|
77
|
+
|
|
78
|
+
export { journeysPlugin };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var p=Object.defineProperty,A=Object.defineProperties,I=Object.getOwnPropertyDescriptor,O=Object.getOwnPropertyDescriptors,_=Object.getOwnPropertyNames,f=Object.getOwnPropertySymbols;var v=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable;var y=(e,r,n)=>r in e?p(e,r,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[r]=n,l=(e,r)=>{for(var n in r||(r={}))v.call(r,n)&&y(e,n,r[n]);if(f)for(var n of f(r))S.call(r,n)&&y(e,n,r[n]);return e},h=(e,r)=>A(e,O(r));var M=(e,r)=>{for(var n in r)p(e,n,{get:r[n],enumerable:!0})},N=(e,r,n,i)=>{if(r&&typeof r=="object"||typeof r=="function")for(let t of _(r))!v.call(e,t)&&t!==n&&p(e,t,{get:()=>r[t],enumerable:!(i=I(r,t))||i.enumerable});return e};var R=e=>N(p({},"__esModule",{value:!0}),e);var g=(e,r,n)=>new Promise((i,t)=>{var d=s=>{try{o(n.next(s))}catch(c){t(c)}},a=s=>{try{o(n.throw(s))}catch(c){t(c)}},o=s=>s.done?i(s.value):Promise.resolve(s.value).then(d,a);o((n=n.apply(e,r)).next())});var K={};M(K,{initialiseInploiSdk:()=>J,journeysPlugin:()=>U});module.exports=R(K);var x=(e,{initialise:r})=>({pluginName:e,initialise:r});var E={sandbox:"https://preview.api.inploi.com",production:"https://api.inploi.com"},T="/journey/log";function L({apiClient:e,logger:r}){function n(i){return g(this,null,function*(){try{let t={event:i.event,sent_at:new Date().toISOString(),context:{library:{name:"inploi-sdk",version:1},page:{href:location.href,referrer:document.referrer,title:document.title}},properties:i.properties,custom_properties:i.customProperties};return yield e.fetch(T,{method:"POST",body:JSON.stringify(t)}),{success:!0,data:t}}catch(t){return r==null||r.error("Failed to send journey log to API. Inspect error response of `log` for more information."),{success:!1,error:t}}})}return{log:n}}var k="Unauthenticated",j="This action is unauthorized.",w=e=>{let r={Accept:"application/json","Content-Type":"application/json","x-publishable-key":e.publishableKey};return{fetch:(t,...d)=>g(void 0,[t,...d],function*(n,i={}){let a=h(l({},i),{headers:l(l({},i.headers),r)}),o=yield fetch(`${e.baseUrl}${n}`,a).then(s=>s.json());if("message"in o){if(o.message===k)throw new Error("You are not authenticated.");if(o.message===j)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 u="%c[inploi SDK]",m="color: #65BC67; font-weight: bold;",b={warn:e=>console.warn(u,m,e),error:e=>console.error(u,m,e),info:e=>console.info(u,m,e),log:e=>console.log(u,m,e)};function P(e,r){if(!e)throw new Error(r)}function J({publishableKey:e,env:r,plugins:n,logger:i=b}){P(typeof window!="undefined","Can't initialise the inploi SDK outside of a browser environment.");let t=w({baseUrl:E[r],publishableKey:e});return n.reduce((a,o)=>(a[o.pluginName]=o.initialise({apiClient:t,logger:i}),a),{})}var U=x("journey",{initialise:L});0&&(module.exports={initialiseInploiSdk,journeysPlugin});
|
|
2
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/journeys/index.ts","../../src/common/common.plugins.ts","../../src/journeys/constants.ts","../../src/journeys/log.ts","../../src/journeys/api.ts","../../src/common/common.logger.ts","../../../core/src/common/common.utils.ts","../../src/sdk.ts"],"sourcesContent":["import { createPlugin } from '../common/common.plugins';\nimport { initialiseJourneysPlugin } from './log';\n\nexport * from '../sdk';\n\nexport const journeysPlugin = createPlugin('journey', { initialise: initialiseJourneysPlugin });\n","import { Logger } from '..';\nimport { ApiClient } from '../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 './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","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","import { ENV_TO_API_URL } from './journeys/constants';\nimport { createApiClient } from './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\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 initialiseInploiSdk<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":"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","names":["journeys_exports","__export","initialiseInploiSdk","journeysPlugin","__toCommonJS","createPlugin","pluginName","initialise","ENV_TO_API_URL","JOURNEY_PATHNAME","initialiseJourneysPlugin","apiClient","logger","log","params","__async","data","JOURNEY_PATHNAME","e","unauthenticatedMessage","unauthorisedMessage","createApiClient","params","defaultHeaders","_0","_1","__async","pathname","options","init","__spreadProps","__spreadValues","response","res","CONSOLE_PREFIX","CONSOLE_STYLE","inploiBrandedLogger","message","invariant","condition","message","initialiseInploiSdk","publishableKey","env","plugins","logger","inploiBrandedLogger","invariant","apiClient","createApiClient","ENV_TO_API_URL","acc","plugin","journeysPlugin","createPlugin","initialiseJourneysPlugin"]}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{a as o,b as i,g as m}from"../chunk-A5DU6CID.mjs";var s=(t,{initialise:e})=>({pluginName:t,initialise:e});function p({apiClient:t,logger:e}){function a(n){return o(this,null,function*(){try{let r={event:n.event,sent_at:new Date().toISOString(),context:{library:{name:"inploi-sdk",version:1},page:{href:location.href,referrer:document.referrer,title:document.title}},properties:n.properties,custom_properties:n.customProperties};return yield t.fetch(i,{method:"POST",body:JSON.stringify(r)}),{success:!0,data:r}}catch(r){return e==null||e.error("Failed to send journey log to API. Inspect error response of `log` for more information."),{success:!1,error:r}}})}return{log:a}}var v=s("journey",{initialise:p});export{m as initialiseInploiSdk,v as journeysPlugin};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/common/common.plugins.ts","../../src/journeys/log.ts","../../src/journeys/index.ts"],"sourcesContent":["import { Logger } from '..';\nimport { ApiClient } from '../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 { Flatten, ResponseObj } from '@inploi/core/common';\nimport { SDK_VERSION } from '../common/common.constants';\nimport { JOURNEY_PATHNAME } from './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 './log';\n\nexport * from '../sdk';\n\nexport const journeysPlugin = createPlugin('journey', { initialise: initialiseJourneysPlugin });\n"],"mappings":"wDAaO,IAAMA,EAAe,CAC3BC,EACA,CAAE,WAAAC,CAAW,KACM,CACnB,WAAAD,EACA,WAAAC,CACD,GCsBO,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,CCzEO,IAAMM,EAAiBC,EAAa,UAAW,CAAE,WAAYC,CAAyB,CAAC","names":["createPlugin","pluginName","initialise","initialiseJourneysPlugin","apiClient","logger","log","params","__async","data","JOURNEY_PATHNAME","e","journeysPlugin","createPlugin","initialiseJourneysPlugin"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@inploi/sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"files": [
|
|
8
|
+
"/dist"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"require": "./dist/index.js",
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./journeys": {
|
|
17
|
+
"require": "./dist/journeys/index.js",
|
|
18
|
+
"import": "./dist/journeys/index.mjs",
|
|
19
|
+
"types": "./dist/journeys/index.d.ts"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup --dts-resolve",
|
|
24
|
+
"dev": "tsup --watch --dts-resolve",
|
|
25
|
+
"lint": "eslint \"**/*.ts*\"",
|
|
26
|
+
"release": "bun run build && cd ../.. && changeset publish"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"uuid": "^9.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@happy-dom/global-registrator": "^12.6.0",
|
|
33
|
+
"@inploi/core": "workspace:*",
|
|
34
|
+
"@types/uuid": "^9.0.2",
|
|
35
|
+
"eslint": "^7.32.0",
|
|
36
|
+
"eslint-config-custom": "workspace:*",
|
|
37
|
+
"happy-dom": "^12.6.0",
|
|
38
|
+
"ts-toolbelt": "^9.6.0",
|
|
39
|
+
"tsconfig": "workspace:*",
|
|
40
|
+
"tsup": "^7.2.0",
|
|
41
|
+
"typescript": "^5.1.6"
|
|
42
|
+
}
|
|
43
|
+
}
|