Skip to content

Commit 77720a5

Browse files
authored
Merge pull request #17 from livesession/feat/esm
feat(esm): support esm build
2 parents 8306638 + cd78086 commit 77720a5

12 files changed

Lines changed: 184 additions & 4 deletions

dist/api.types.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ var ConsoleLogLevel;
77
ConsoleLogLevel["info"] = "info";
88
ConsoleLogLevel["warn"] = "warn";
99
ConsoleLogLevel["error"] = "error";
10-
})(ConsoleLogLevel || (exports.ConsoleLogLevel = ConsoleLogLevel = {}));
10+
})(ConsoleLogLevel = exports.ConsoleLogLevel || (exports.ConsoleLogLevel = {}));

dist/esm/api.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ConsoleLogLevel } from "./api.types";
2+
export interface apiConfig {
3+
init: (trackID: string, options?: object | null) => void;
4+
getSessionURL: (callback?: void) => void;
5+
identify: (data?: object) => void;
6+
invalidateSession: () => void;
7+
newPageView: (options?: object) => void;
8+
setOptions: (options?: object) => void;
9+
setCustomParams: (data?: object) => void;
10+
off: () => void;
11+
optOut: () => void;
12+
debug: () => void;
13+
track: (eventName: string, properties?: object) => void;
14+
log: (logLevel: ConsoleLogLevel, ...rest: any) => void;
15+
}
16+
declare const api: apiConfig;
17+
export declare const SDK_VERSION = "1.1.0";
18+
export default api;

dist/esm/api.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const apiCall = (name, ...args) => {
2+
return window.__ls(name, ...args);
3+
};
4+
const api = {
5+
init: (trackID, options) => apiCall("init", trackID, options),
6+
getSessionURL: (callback) => apiCall("getSessionURL", callback),
7+
identify: (data) => apiCall("identify", data),
8+
invalidateSession: () => apiCall("invalidateSession"),
9+
newPageView: (options) => apiCall("newPageView", options),
10+
setOptions: (options) => apiCall("setOptions", options),
11+
setCustomParams: (data) => apiCall("setCustomParams", data),
12+
off: () => apiCall("off"),
13+
optOut: () => apiCall("optOut", true),
14+
debug: () => apiCall("debug", true),
15+
track: (eventName, properties) => apiCall("track", eventName, properties),
16+
log: (logLevel, ...rest) => apiCall("log", logLevel, rest),
17+
};
18+
export const SDK_VERSION = "1.1.0";
19+
export default api;

dist/esm/api.types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export declare enum ConsoleLogLevel {
2+
log = "log",
3+
info = "info",
4+
warn = "warn",
5+
error = "error"
6+
}

dist/esm/api.types.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export var ConsoleLogLevel;
2+
(function (ConsoleLogLevel) {
3+
ConsoleLogLevel["log"] = "log";
4+
ConsoleLogLevel["info"] = "info";
5+
ConsoleLogLevel["warn"] = "warn";
6+
ConsoleLogLevel["error"] = "error";
7+
})(ConsoleLogLevel || (ConsoleLogLevel = {}));

dist/esm/index.d.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
declare global {
2+
interface Window {
3+
__ls: any;
4+
}
5+
}
6+
declare const _default: {
7+
init: (trackID: string, options?: object | null | undefined, sdkOptions?: {
8+
devMode: boolean;
9+
scriptURL: string;
10+
}) => void;
11+
getSessionURL: (args?: void | undefined) => any;
12+
identify: (args?: object | undefined) => any;
13+
invalidateSession: (args?: null | undefined) => any;
14+
newPageView: (args?: object | undefined) => any;
15+
setOptions: (args?: object | undefined) => any;
16+
setCustomParams: (args?: object | undefined) => any;
17+
off: (args?: null | undefined) => any;
18+
optOut: (args?: null | undefined) => any;
19+
debug: (args?: null | undefined) => any;
20+
track: (eventName: string, properties?: object) => void;
21+
log: (logLevel: string, ...args: any) => void;
22+
};
23+
export default _default;

dist/esm/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import snippet, { defaultScriptURL } from "./snippet";
2+
import api from "./api";
3+
const sdkOptionsDefaults = {
4+
devMode: false,
5+
scriptURL: defaultScriptURL,
6+
};
7+
let opts = Object.assign({}, sdkOptionsDefaults);
8+
const isLoaded = () => window.__ls;
9+
const getApiMethod = (name) => {
10+
if (!isLoaded()) {
11+
throw new Error("LiveSession is not loaded. Call init() before calling other API functions");
12+
}
13+
const objectAPI = api;
14+
if (!objectAPI.hasOwnProperty(name)) {
15+
throw new Error(`method "${name}" doesn't exist`);
16+
}
17+
if (opts.devMode) {
18+
const msg = `Skipping method: ${name}, devMode enabled`;
19+
console.warn(msg);
20+
return () => msg;
21+
}
22+
return objectAPI[name];
23+
};
24+
const safeCall = (name) => {
25+
return (args) => {
26+
const apiMethod = getApiMethod(name);
27+
if (apiMethod) {
28+
return apiMethod(args);
29+
}
30+
};
31+
};
32+
const safeCallManyArgs = (name) => {
33+
return (...args) => {
34+
const apiMethod = getApiMethod(name);
35+
if (apiMethod) {
36+
return apiMethod(...args);
37+
}
38+
};
39+
};
40+
const _init = (trackID, options, sdkOptions = sdkOptionsDefaults) => {
41+
opts = Object.assign(Object.assign({}, sdkOptionsDefaults), sdkOptions);
42+
if (isLoaded()) {
43+
console.warn("LiveSession already inited (skipping init() call)");
44+
return;
45+
}
46+
if (!trackID) {
47+
throw new Error(`trackID is required`);
48+
}
49+
snippet(window, document, "script", sdkOptions.scriptURL);
50+
return api.init(trackID, options);
51+
};
52+
export default {
53+
init: _init,
54+
getSessionURL: safeCall("getSessionURL"),
55+
identify: safeCall("identify"),
56+
invalidateSession: safeCall("invalidateSession"),
57+
newPageView: safeCall("newPageView"),
58+
setOptions: safeCall("setOptions"),
59+
setCustomParams: safeCall("setCustomParams"),
60+
off: safeCall("off"),
61+
optOut: safeCall("optOut"),
62+
debug: safeCall("debug"),
63+
track: function (eventName, properties) {
64+
safeCallManyArgs("track")(eventName, properties);
65+
},
66+
log: function (logLevel, ...args) {
67+
safeCallManyArgs("log")(logLevel, ...args);
68+
},
69+
};

dist/esm/snippet.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export declare const defaultScriptURL = "https://cdn.livesession.io/track.js";
2+
declare const snippet: (wnd?: Window & typeof globalThis, doc?: Document, type?: string, scriptURL?: string) => void;
3+
export default snippet;

dist/esm/snippet.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { SDK_VERSION } from "./api";
2+
export const defaultScriptURL = "https://cdn.livesession.io/track.js";
3+
const snippet = (wnd = window, doc = document, type = "script", scriptURL = defaultScriptURL) => {
4+
return ((w, d, t, u) => {
5+
if (w.__ls) {
6+
throw new Error("LiveSession script already added");
7+
}
8+
const f = (w.__ls = function () {
9+
f.push ? f.push.apply(f, arguments) : f.store.push(arguments);
10+
});
11+
if (!w.__ls)
12+
w.__ls = f;
13+
f.store = [];
14+
f.v = SDK_VERSION;
15+
const ls = d.createElement(t);
16+
ls.async = true;
17+
ls.src = u;
18+
ls.charset = "utf-8";
19+
ls.crossOrigin = "anonymous";
20+
const h = d.getElementsByTagName("head")[0];
21+
h.appendChild(ls);
22+
})(wnd, doc, type, scriptURL);
23+
};
24+
export default snippet;

dist/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ declare global {
44
}
55
}
66
declare const _default: {
7-
init: (trackID: string, options?: object | null, sdkOptions?: {
7+
init: (trackID: string, options?: object | null | undefined, sdkOptions?: {
88
devMode: boolean;
99
scriptURL: string;
1010
}) => void;

0 commit comments

Comments
 (0)