@account-kit/logging 4.0.0-beta.10
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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/esm/_writeKey.d.ts +1 -0
- package/dist/esm/_writeKey.js +3 -0
- package/dist/esm/_writeKey.js.map +1 -0
- package/dist/esm/client.d.ts +2 -0
- package/dist/esm/client.js +65 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/fetchRemoteWriteKey.d.ts +1 -0
- package/dist/esm/fetchRemoteWriteKey.js +16 -0
- package/dist/esm/fetchRemoteWriteKey.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +38 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/noop.d.ts +2 -0
- package/dist/esm/noop.js +7 -0
- package/dist/esm/noop.js.map +1 -0
- package/dist/esm/plugins/contextAllowlist.d.ts +7 -0
- package/dist/esm/plugins/contextAllowlist.js +26 -0
- package/dist/esm/plugins/contextAllowlist.js.map +1 -0
- package/dist/esm/plugins/devDestination.d.ts +2 -0
- package/dist/esm/plugins/devDestination.js +17 -0
- package/dist/esm/plugins/devDestination.js.map +1 -0
- package/dist/esm/server.d.ts +2 -0
- package/dist/esm/server.js +8 -0
- package/dist/esm/server.js.map +1 -0
- package/dist/esm/types.d.ts +37 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/version.d.ts +1 -0
- package/dist/esm/version.js +4 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/types/_writeKey.d.ts +2 -0
- package/dist/types/_writeKey.d.ts.map +1 -0
- package/dist/types/client.d.ts +3 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/fetchRemoteWriteKey.d.ts +2 -0
- package/dist/types/fetchRemoteWriteKey.d.ts.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/noop.d.ts +3 -0
- package/dist/types/noop.d.ts.map +1 -0
- package/dist/types/plugins/contextAllowlist.d.ts +8 -0
- package/dist/types/plugins/contextAllowlist.d.ts.map +1 -0
- package/dist/types/plugins/devDestination.d.ts +3 -0
- package/dist/types/plugins/devDestination.d.ts.map +1 -0
- package/dist/types/server.d.ts +3 -0
- package/dist/types/server.d.ts.map +1 -0
- package/dist/types/types.d.ts +38 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/version.d.ts +2 -0
- package/dist/types/version.d.ts.map +1 -0
- package/package.json +65 -0
- package/src/_writeKey.ts +2 -0
- package/src/client.ts +79 -0
- package/src/fetchRemoteWriteKey.ts +18 -0
- package/src/index.ts +53 -0
- package/src/noop.ts +8 -0
- package/src/plugins/contextAllowlist.ts +33 -0
- package/src/plugins/devDestination.ts +21 -0
- package/src/server.ts +11 -0
- package/src/types.ts +48 -0
- package/src/version.ts +3 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Alchemy Insights, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const WRITE_IN_DEV: boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_writeKey.js","sourceRoot":"","sources":["../../src/_writeKey.ts"],"names":[],"mappings":"AAAA,kFAAkF;AAClF,MAAM,CAAC,MAAM,YAAY,GAAY,KAAK,CAAC","sourcesContent":["// this gets replaced after the build completes if it's present in the Environment\nexport const WRITE_IN_DEV: boolean = false;\n"]}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { AnalyticsBrowser } from "@segment/analytics-next";
|
|
2
|
+
import { uuid } from "uuidv4";
|
|
3
|
+
import { WRITE_IN_DEV } from "./_writeKey.js";
|
|
4
|
+
import { fetchRemoteWriteKey } from "./fetchRemoteWriteKey.js";
|
|
5
|
+
import { noopLogger } from "./noop.js";
|
|
6
|
+
import { ContextAllowlistPlugin } from "./plugins/contextAllowlist.js";
|
|
7
|
+
import { DevDestinationPlugin } from "./plugins/devDestination.js";
|
|
8
|
+
export function createClientLogger(context) {
|
|
9
|
+
const isDev = window.location.hostname.includes("localhost");
|
|
10
|
+
if (isDev && !WRITE_IN_DEV) {
|
|
11
|
+
// If we don't have a write key, we don't want to log anything
|
|
12
|
+
// This is useful for dev so we don't log dev metrics
|
|
13
|
+
//
|
|
14
|
+
// We also don't allow logging on localhost unless WRITE_IN_DEV is set to true
|
|
15
|
+
// WRITE_IN_DEV is only ever true if you're building from source with env vars set to true
|
|
16
|
+
return noopLogger;
|
|
17
|
+
}
|
|
18
|
+
const analytics = new AnalyticsBrowser();
|
|
19
|
+
const writeKey = fetchRemoteWriteKey();
|
|
20
|
+
if (!sessionStorage.getItem("anonId")) {
|
|
21
|
+
sessionStorage.setItem("anonId", uuid());
|
|
22
|
+
}
|
|
23
|
+
const anonId = sessionStorage.getItem("anonId");
|
|
24
|
+
analytics.setAnonymousId(anonId);
|
|
25
|
+
analytics.register(ContextAllowlistPlugin);
|
|
26
|
+
analytics.debug(isDev);
|
|
27
|
+
// This lets us log events in the console
|
|
28
|
+
if (isDev) {
|
|
29
|
+
analytics.register(DevDestinationPlugin);
|
|
30
|
+
}
|
|
31
|
+
const ready = writeKey.then((writeKey) => {
|
|
32
|
+
if (writeKey == null) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
analytics.load({
|
|
36
|
+
writeKey,
|
|
37
|
+
// we disable these settings in dev so we don't fetch anything from segment
|
|
38
|
+
cdnSettings: isDev
|
|
39
|
+
? {
|
|
40
|
+
integrations: {},
|
|
41
|
+
}
|
|
42
|
+
: undefined,
|
|
43
|
+
},
|
|
44
|
+
// further we disable the segment integration dev
|
|
45
|
+
{
|
|
46
|
+
disableClientPersistence: true,
|
|
47
|
+
integrations: {
|
|
48
|
+
"Segment.io": !isDev,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
return analytics.ready();
|
|
52
|
+
});
|
|
53
|
+
return {
|
|
54
|
+
_internal: {
|
|
55
|
+
ready,
|
|
56
|
+
},
|
|
57
|
+
trackEvent: async ({ name, data }) => {
|
|
58
|
+
if (!(await writeKey)) {
|
|
59
|
+
return noopLogger.trackEvent({ name, data });
|
|
60
|
+
}
|
|
61
|
+
await analytics.track(name, { ...data, ...context });
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAGnE,MAAM,UAAU,kBAAkB,CAChC,OAAsB;IAEtB,MAAM,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC7D,IAAI,KAAK,IAAI,CAAC,YAAY,EAAE,CAAC;QAC3B,8DAA8D;QAC9D,qDAAqD;QACrD,EAAE;QACF,8EAA8E;QAC9E,0FAA0F;QAC1F,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,MAAM,SAAS,GAAG,IAAI,gBAAgB,EAAE,CAAC;IACzC,MAAM,QAAQ,GAAG,mBAAmB,EAAE,CAAC;IAEvC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;QACtC,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,MAAM,GAAG,cAAc,CAAC,OAAO,CAAC,QAAQ,CAAE,CAAC;IACjD,SAAS,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IACjC,SAAS,CAAC,QAAQ,CAAC,sBAAsB,CAAC,CAAC;IAC3C,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAEvB,yCAAyC;IACzC,IAAI,KAAK,EAAE,CAAC;QACV,SAAS,CAAC,QAAQ,CAAC,oBAAoB,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,KAAK,GAAqB,QAAQ,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;QACzD,IAAI,QAAQ,IAAI,IAAI,EAAE,CAAC;YACrB,OAAO;QACT,CAAC;QAED,SAAS,CAAC,IAAI,CACZ;YACE,QAAQ;YACR,2EAA2E;YAC3E,WAAW,EAAE,KAAK;gBAChB,CAAC,CAAC;oBACE,YAAY,EAAE,EAAE;iBACjB;gBACH,CAAC,CAAC,SAAS;SACd;QACD,iDAAiD;QACjD;YACE,wBAAwB,EAAE,IAAI;YAC9B,YAAY,EAAE;gBACZ,YAAY,EAAE,CAAC,KAAK;aACrB;SACF,CACF,CAAC;QAEF,OAAO,SAAS,CAAC,KAAK,EAAE,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,SAAS,EAAE;YACT,KAAK;SACN;QACD,UAAU,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,EAAE;YACnC,IAAI,CAAC,CAAC,MAAM,QAAQ,CAAC,EAAE,CAAC;gBACtB,OAAO,UAAU,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;YAC/C,CAAC;YAED,MAAM,SAAS,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC;QACvD,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { AnalyticsBrowser } from \"@segment/analytics-next\";\nimport { uuid } from \"uuidv4\";\nimport { WRITE_IN_DEV } from \"./_writeKey.js\";\nimport { fetchRemoteWriteKey } from \"./fetchRemoteWriteKey.js\";\nimport { noopLogger } from \"./noop.js\";\nimport { ContextAllowlistPlugin } from \"./plugins/contextAllowlist.js\";\nimport { DevDestinationPlugin } from \"./plugins/devDestination.js\";\nimport type { EventsSchema, InnerLogger, LoggerContext } from \"./types\";\n\nexport function createClientLogger<Schema extends EventsSchema = []>(\n context: LoggerContext\n): InnerLogger<Schema> {\n const isDev = window.location.hostname.includes(\"localhost\");\n if (isDev && !WRITE_IN_DEV) {\n // If we don't have a write key, we don't want to log anything\n // This is useful for dev so we don't log dev metrics\n //\n // We also don't allow logging on localhost unless WRITE_IN_DEV is set to true\n // WRITE_IN_DEV is only ever true if you're building from source with env vars set to true\n return noopLogger;\n }\n\n const analytics = new AnalyticsBrowser();\n const writeKey = fetchRemoteWriteKey();\n\n if (!sessionStorage.getItem(\"anonId\")) {\n sessionStorage.setItem(\"anonId\", uuid());\n }\n\n const anonId = sessionStorage.getItem(\"anonId\")!;\n analytics.setAnonymousId(anonId);\n analytics.register(ContextAllowlistPlugin);\n analytics.debug(isDev);\n\n // This lets us log events in the console\n if (isDev) {\n analytics.register(DevDestinationPlugin);\n }\n\n const ready: Promise<unknown> = writeKey.then((writeKey) => {\n if (writeKey == null) {\n return;\n }\n\n analytics.load(\n {\n writeKey,\n // we disable these settings in dev so we don't fetch anything from segment\n cdnSettings: isDev\n ? {\n integrations: {},\n }\n : undefined,\n },\n // further we disable the segment integration dev\n {\n disableClientPersistence: true,\n integrations: {\n \"Segment.io\": !isDev,\n },\n }\n );\n\n return analytics.ready();\n });\n\n return {\n _internal: {\n ready,\n },\n trackEvent: async ({ name, data }) => {\n if (!(await writeKey)) {\n return noopLogger.trackEvent({ name, data });\n }\n\n await analytics.track(name, { ...data, ...context });\n },\n };\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function fetchRemoteWriteKey(): Promise<string | undefined>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export async function fetchRemoteWriteKey() {
|
|
2
|
+
try {
|
|
3
|
+
const json = await fetch("https://static.alchemyapi.io/assets/accountkit/logger_config.json", {
|
|
4
|
+
headers: {
|
|
5
|
+
ContentType: "application/json",
|
|
6
|
+
},
|
|
7
|
+
mode: "no-cors",
|
|
8
|
+
}).then((res) => res.json());
|
|
9
|
+
return json.writeKey;
|
|
10
|
+
}
|
|
11
|
+
catch (e) {
|
|
12
|
+
console.warn("failed to fetch write key");
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=fetchRemoteWriteKey.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetchRemoteWriteKey.js","sourceRoot":"","sources":["../../src/fetchRemoteWriteKey.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,KAAK,UAAU,mBAAmB;IACvC,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,MAAM,KAAK,CACtB,mEAAmE,EACnE;YACE,OAAO,EAAE;gBACP,WAAW,EAAE,kBAAkB;aAChC;YACD,IAAI,EAAE,SAAS;SAChB,CACF,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;QAE5B,OAAO,IAAI,CAAC,QAA8B,CAAC;IAC7C,CAAC;IAAC,OAAO,CAAC,EAAE,CAAC;QACX,OAAO,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;QAC1C,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC","sourcesContent":["export async function fetchRemoteWriteKey(): Promise<string | undefined> {\n try {\n const json = await fetch(\n \"https://static.alchemyapi.io/assets/accountkit/logger_config.json\",\n {\n headers: {\n ContentType: \"application/json\",\n },\n mode: \"no-cors\",\n }\n ).then((res) => res.json());\n\n return json.writeKey as string | undefined;\n } catch (e) {\n console.warn(\"failed to fetch write key\");\n return undefined;\n }\n}\n"]}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { createClientLogger } from "./client.js";
|
|
2
|
+
import { createServerLogger } from "./server.js";
|
|
3
|
+
export function createLogger(context) {
|
|
4
|
+
const innerLogger = typeof window === "undefined"
|
|
5
|
+
? createServerLogger(context)
|
|
6
|
+
: createClientLogger(context);
|
|
7
|
+
const logger = {
|
|
8
|
+
...innerLogger,
|
|
9
|
+
profiled(name, func) {
|
|
10
|
+
return function (...args) {
|
|
11
|
+
const start = Date.now();
|
|
12
|
+
const result = func.apply(this, args);
|
|
13
|
+
if (result instanceof Promise) {
|
|
14
|
+
return result.then((res) => {
|
|
15
|
+
innerLogger.trackEvent({
|
|
16
|
+
name: "performance",
|
|
17
|
+
data: {
|
|
18
|
+
executionTimeMs: Date.now() - start,
|
|
19
|
+
functionName: name,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
return res;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
innerLogger.trackEvent({
|
|
26
|
+
name: "performance",
|
|
27
|
+
data: {
|
|
28
|
+
executionTimeMs: Date.now() - start,
|
|
29
|
+
functionName: name,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
return logger;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AASjD,MAAM,UAAU,YAAY,CAAC,OAAsB;IACjD,MAAM,WAAW,GACf,OAAO,MAAM,KAAK,WAAW;QAC3B,CAAC,CAAC,kBAAkB,CAAC,OAAO,CAAC;QAC7B,CAAC,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC;IAElC,MAAM,MAAM,GAAgB;QAC1B,GAAG,WAAW;QACd,QAAQ,CACN,IAAY,EACZ,IAA8B;YAE9B,OAAO,UAAqB,GAAG,IAAW;gBACxC,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;gBACzB,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;gBACtC,IAAI,MAAM,YAAY,OAAO,EAAE,CAAC;oBAC9B,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,EAAE;wBACzB,WAAW,CAAC,UAAU,CAAC;4BACrB,IAAI,EAAE,aAAa;4BACnB,IAAI,EAAE;gCACJ,eAAe,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK;gCACnC,YAAY,EAAE,IAAI;6BACnB;yBACF,CAAC,CAAC;wBAEH,OAAO,GAAG,CAAC;oBACb,CAAC,CAAS,CAAC;gBACb,CAAC;gBAED,WAAW,CAAC,UAAU,CAAC;oBACrB,IAAI,EAAE,aAAa;oBACnB,IAAI,EAAE;wBACJ,eAAe,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK;wBACnC,YAAY,EAAE,IAAI;qBACnB;iBACF,CAAC,CAAC;gBACH,OAAO,MAAM,CAAC;YAChB,CAAC,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { createClientLogger } from \"./client.js\";\nimport { createServerLogger } from \"./server.js\";\nimport type { EventLogger, EventsSchema, LoggerContext } from \"./types\";\n\nexport type * from \"./types.js\";\n\nexport function createLogger<Schema extends EventsSchema = []>(\n context: LoggerContext\n): EventLogger<Schema>;\n\nexport function createLogger(context: LoggerContext): EventLogger {\n const innerLogger =\n typeof window === \"undefined\"\n ? createServerLogger(context)\n : createClientLogger(context);\n\n const logger: EventLogger = {\n ...innerLogger,\n profiled<TArgs extends any[], TRet>(\n name: string,\n func: (...args: TArgs) => TRet\n ): (...args: TArgs) => TRet {\n return function (this: any, ...args: TArgs): TRet {\n const start = Date.now();\n const result = func.apply(this, args);\n if (result instanceof Promise) {\n return result.then((res) => {\n innerLogger.trackEvent({\n name: \"performance\",\n data: {\n executionTimeMs: Date.now() - start,\n functionName: name,\n },\n });\n\n return res;\n }) as TRet;\n }\n\n innerLogger.trackEvent({\n name: \"performance\",\n data: {\n executionTimeMs: Date.now() - start,\n functionName: name,\n },\n });\n return result;\n };\n },\n };\n\n return logger;\n}\n"]}
|
package/dist/esm/noop.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.js","sourceRoot":"","sources":["../../src/noop.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,UAAU,GAAqB;IAC1C,UAAU,EAAE,KAAK,IAAI,EAAE,GAAE,CAAC;IAC1B,SAAS,EAAE;QACT,KAAK,EAAE,OAAO,CAAC,OAAO,EAAE;KACzB;CACF,CAAC","sourcesContent":["import type { InnerLogger } from \"./types\";\n\nexport const noopLogger: InnerLogger<any> = {\n trackEvent: async () => {},\n _internal: {\n ready: Promise.resolve(),\n },\n};\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Plugin } from "@segment/analytics-next";
|
|
2
|
+
/**
|
|
3
|
+
* This plugin enforces a context allowlist for all events
|
|
4
|
+
*
|
|
5
|
+
* This is done to make sure that anything Analytics.js sends to Segment is only the data we want to send
|
|
6
|
+
*/
|
|
7
|
+
export declare const ContextAllowlistPlugin: Plugin;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const allowlist = ["page"];
|
|
2
|
+
function stripContext(ctx) {
|
|
3
|
+
ctx.event.context = allowlist.reduce((acc, key) => {
|
|
4
|
+
acc[key] = ctx.event?.context?.[key];
|
|
5
|
+
return acc;
|
|
6
|
+
}, {});
|
|
7
|
+
return ctx;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* This plugin enforces a context allowlist for all events
|
|
11
|
+
*
|
|
12
|
+
* This is done to make sure that anything Analytics.js sends to Segment is only the data we want to send
|
|
13
|
+
*/
|
|
14
|
+
export const ContextAllowlistPlugin = {
|
|
15
|
+
name: "Enforce Context Allowlist",
|
|
16
|
+
type: "enrichment",
|
|
17
|
+
isLoaded: () => true,
|
|
18
|
+
load: () => Promise.resolve(),
|
|
19
|
+
track: stripContext,
|
|
20
|
+
identify: stripContext,
|
|
21
|
+
page: stripContext,
|
|
22
|
+
alias: stripContext,
|
|
23
|
+
group: stripContext,
|
|
24
|
+
screen: stripContext,
|
|
25
|
+
};
|
|
26
|
+
//# sourceMappingURL=contextAllowlist.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextAllowlist.js","sourceRoot":"","sources":["../../../src/plugins/contextAllowlist.ts"],"names":[],"mappings":"AAEA,MAAM,SAAS,GAAsB,CAAC,MAAM,CAAU,CAAC;AAEvD,SAAS,YAAY,CAAC,GAAY;IAChC,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAChD,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,CAAC,GAAG,CAAC,CAAC;QAErC,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAiD,CAAC,CAAC;IAEtD,OAAO,GAAG,CAAC;AACb,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAW;IAC5C,IAAI,EAAE,2BAA2B;IACjC,IAAI,EAAE,YAAY;IAElB,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI;IACpB,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;IAE7B,KAAK,EAAE,YAAY;IACnB,QAAQ,EAAE,YAAY;IACtB,IAAI,EAAE,YAAY;IAClB,KAAK,EAAE,YAAY;IACnB,KAAK,EAAE,YAAY;IACnB,MAAM,EAAE,YAAY;CACrB,CAAC","sourcesContent":["import type { Context, Plugin } from \"@segment/analytics-next\";\n\nconst allowlist: readonly string[] = [\"page\"] as const;\n\nfunction stripContext(ctx: Context): Context {\n ctx.event.context = allowlist.reduce((acc, key) => {\n acc[key] = ctx.event?.context?.[key];\n\n return acc;\n }, {} as Record<(typeof allowlist)[number], unknown>);\n\n return ctx;\n}\n\n/**\n * This plugin enforces a context allowlist for all events\n *\n * This is done to make sure that anything Analytics.js sends to Segment is only the data we want to send\n */\nexport const ContextAllowlistPlugin: Plugin = {\n name: \"Enforce Context Allowlist\",\n type: \"enrichment\",\n\n isLoaded: () => true,\n load: () => Promise.resolve(),\n\n track: stripContext,\n identify: stripContext,\n page: stripContext,\n alias: stripContext,\n group: stripContext,\n screen: stripContext,\n};\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function consoleLogEvent(ctx) {
|
|
2
|
+
console.log(JSON.stringify(ctx.event, null, 2));
|
|
3
|
+
return ctx;
|
|
4
|
+
}
|
|
5
|
+
export const DevDestinationPlugin = {
|
|
6
|
+
name: "Dev Destination Plugin",
|
|
7
|
+
type: "destination",
|
|
8
|
+
isLoaded: () => true,
|
|
9
|
+
load: () => Promise.resolve(),
|
|
10
|
+
track: consoleLogEvent,
|
|
11
|
+
identify: consoleLogEvent,
|
|
12
|
+
page: consoleLogEvent,
|
|
13
|
+
alias: consoleLogEvent,
|
|
14
|
+
group: consoleLogEvent,
|
|
15
|
+
screen: consoleLogEvent,
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=devDestination.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devDestination.js","sourceRoot":"","sources":["../../../src/plugins/devDestination.ts"],"names":[],"mappings":"AAEA,SAAS,eAAe,CAAC,GAAY;IACnC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;IAEhD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,CAAC,MAAM,oBAAoB,GAAW;IAC1C,IAAI,EAAE,wBAAwB;IAC9B,IAAI,EAAE,aAAa;IACnB,QAAQ,EAAE,GAAG,EAAE,CAAC,IAAI;IACpB,IAAI,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE;IAE7B,KAAK,EAAE,eAAe;IACtB,QAAQ,EAAE,eAAe;IACzB,IAAI,EAAE,eAAe;IACrB,KAAK,EAAE,eAAe;IACtB,KAAK,EAAE,eAAe;IACtB,MAAM,EAAE,eAAe;CACxB,CAAC","sourcesContent":["import type { Context, Plugin } from \"@segment/analytics-next\";\n\nfunction consoleLogEvent(ctx: Context): Context {\n console.log(JSON.stringify(ctx.event, null, 2));\n\n return ctx;\n}\n\nexport const DevDestinationPlugin: Plugin = {\n name: \"Dev Destination Plugin\",\n type: \"destination\",\n isLoaded: () => true,\n load: () => Promise.resolve(),\n\n track: consoleLogEvent,\n identify: consoleLogEvent,\n page: consoleLogEvent,\n alias: consoleLogEvent,\n group: consoleLogEvent,\n screen: consoleLogEvent,\n};\n"]}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { noopLogger } from "./noop.js";
|
|
2
|
+
export function createServerLogger(_context) {
|
|
3
|
+
// TODO: there is an analytics js node package that we can use, but we'll come back to that later
|
|
4
|
+
// this is harder than client, because on the client we can filter out events that are originating from localhost,
|
|
5
|
+
// whereas here we can't do that as easily
|
|
6
|
+
return noopLogger;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAGvC,MAAM,UAAU,kBAAkB,CAChC,QAAuB;IAEvB,iGAAiG;IACjG,kHAAkH;IAClH,0CAA0C;IAC1C,OAAO,UAAU,CAAC;AACpB,CAAC","sourcesContent":["import { noopLogger } from \"./noop.js\";\nimport type { EventsSchema, InnerLogger, LoggerContext } from \"./types\";\n\nexport function createServerLogger<Schema extends EventsSchema = []>(\n _context: LoggerContext\n): InnerLogger<Schema> {\n // TODO: there is an analytics js node package that we can use, but we'll come back to that later\n // this is harder than client, because on the client we can filter out events that are originating from localhost,\n // whereas here we can't do that as easily\n return noopLogger;\n}\n"]}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type EventsSchema = readonly {
|
|
2
|
+
EventName: string;
|
|
3
|
+
EventData?: Record<string, any>;
|
|
4
|
+
}[];
|
|
5
|
+
type Prettify<T> = {
|
|
6
|
+
[K in keyof T]: T[K];
|
|
7
|
+
} & {};
|
|
8
|
+
export type TrackEventParameters<Schema extends EventsSchema> = {
|
|
9
|
+
[K in keyof Schema]: Prettify<{
|
|
10
|
+
name: Schema[K]["EventName"];
|
|
11
|
+
} & (Schema[K]["EventData"] extends undefined ? {
|
|
12
|
+
data?: undefined;
|
|
13
|
+
} : {
|
|
14
|
+
data: Schema[K]["EventData"];
|
|
15
|
+
})>;
|
|
16
|
+
}[number];
|
|
17
|
+
export interface EventLogger<Schema extends EventsSchema = []> {
|
|
18
|
+
trackEvent(params: TrackEventParameters<[...Schema, PerformanceEvent]>): Promise<void>;
|
|
19
|
+
profiled<TArgs extends any[], TRet>(name: string, func: (...args: TArgs) => TRet): (...args: TArgs) => TRet;
|
|
20
|
+
_internal: {
|
|
21
|
+
ready: Promise<unknown>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export type InnerLogger<Schema extends EventsSchema> = Omit<EventLogger<Schema>, "profiled">;
|
|
25
|
+
export type LoggerContext = {
|
|
26
|
+
package: string;
|
|
27
|
+
version: string;
|
|
28
|
+
[key: string]: string;
|
|
29
|
+
};
|
|
30
|
+
export type PerformanceEvent = {
|
|
31
|
+
EventName: "performance";
|
|
32
|
+
EventData: {
|
|
33
|
+
executionTimeMs: number;
|
|
34
|
+
functionName: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export type EventsSchema = readonly {\n EventName: string;\n EventData?: Record<string, any>;\n}[];\n\ntype Prettify<T> = {\n [K in keyof T]: T[K];\n} & {};\n\nexport type TrackEventParameters<Schema extends EventsSchema> = {\n [K in keyof Schema]: Prettify<\n { name: Schema[K][\"EventName\"] } & (Schema[K][\"EventData\"] extends undefined\n ? { data?: undefined }\n : { data: Schema[K][\"EventData\"] })\n >;\n}[number];\n\nexport interface EventLogger<Schema extends EventsSchema = []> {\n trackEvent(\n params: TrackEventParameters<[...Schema, PerformanceEvent]>\n ): Promise<void>;\n profiled<TArgs extends any[], TRet>(\n name: string,\n func: (...args: TArgs) => TRet\n ): (...args: TArgs) => TRet;\n _internal: {\n ready: Promise<unknown>;\n };\n}\n\nexport type InnerLogger<Schema extends EventsSchema> = Omit<\n EventLogger<Schema>,\n \"profiled\"\n>;\n\nexport type LoggerContext = {\n package: string;\n version: string;\n [key: string]: string;\n};\n\nexport type PerformanceEvent = {\n EventName: \"performance\";\n EventData: {\n executionTimeMs: number;\n functionName: string;\n };\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const VERSION = "4.0.0-beta.10";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,uEAAuE;AACvE,yBAAyB;AACzB,MAAM,CAAC,MAAM,OAAO,GAAG,eAAe,CAAC","sourcesContent":["// This file is autogenerated by inject-version.ts. Any changes will be\n// overwritten on commit!\nexport const VERSION = \"4.0.0-beta.10\";\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"_writeKey.d.ts","sourceRoot":"","sources":["../../src/_writeKey.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,YAAY,EAAE,OAAe,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExE,wBAAgB,kBAAkB,CAAC,MAAM,SAAS,YAAY,GAAG,EAAE,EACjE,OAAO,EAAE,aAAa,GACrB,WAAW,CAAC,MAAM,CAAC,CAmErB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetchRemoteWriteKey.d.ts","sourceRoot":"","sources":["../../src/fetchRemoteWriteKey.ts"],"names":[],"mappings":"AAAA,wBAAsB,mBAAmB,IAAI,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAiBvE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExE,mBAAmB,YAAY,CAAC;AAEhC,wBAAgB,YAAY,CAAC,MAAM,SAAS,YAAY,GAAG,EAAE,EAC3D,OAAO,EAAE,aAAa,GACrB,WAAW,CAAC,MAAM,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../../src/noop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,eAAO,MAAM,UAAU,EAAE,WAAW,CAAC,GAAG,CAKvC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Plugin } from "@segment/analytics-next";
|
|
2
|
+
/**
|
|
3
|
+
* This plugin enforces a context allowlist for all events
|
|
4
|
+
*
|
|
5
|
+
* This is done to make sure that anything Analytics.js sends to Segment is only the data we want to send
|
|
6
|
+
*/
|
|
7
|
+
export declare const ContextAllowlistPlugin: Plugin;
|
|
8
|
+
//# sourceMappingURL=contextAllowlist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contextAllowlist.d.ts","sourceRoot":"","sources":["../../../src/plugins/contextAllowlist.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAc/D;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,MAapC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"devDestination.d.ts","sourceRoot":"","sources":["../../../src/plugins/devDestination.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAW,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAQ/D,eAAO,MAAM,oBAAoB,EAAE,MAYlC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExE,wBAAgB,kBAAkB,CAAC,MAAM,SAAS,YAAY,GAAG,EAAE,EACjE,QAAQ,EAAE,aAAa,GACtB,WAAW,CAAC,MAAM,CAAC,CAKrB"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type EventsSchema = readonly {
|
|
2
|
+
EventName: string;
|
|
3
|
+
EventData?: Record<string, any>;
|
|
4
|
+
}[];
|
|
5
|
+
type Prettify<T> = {
|
|
6
|
+
[K in keyof T]: T[K];
|
|
7
|
+
} & {};
|
|
8
|
+
export type TrackEventParameters<Schema extends EventsSchema> = {
|
|
9
|
+
[K in keyof Schema]: Prettify<{
|
|
10
|
+
name: Schema[K]["EventName"];
|
|
11
|
+
} & (Schema[K]["EventData"] extends undefined ? {
|
|
12
|
+
data?: undefined;
|
|
13
|
+
} : {
|
|
14
|
+
data: Schema[K]["EventData"];
|
|
15
|
+
})>;
|
|
16
|
+
}[number];
|
|
17
|
+
export interface EventLogger<Schema extends EventsSchema = []> {
|
|
18
|
+
trackEvent(params: TrackEventParameters<[...Schema, PerformanceEvent]>): Promise<void>;
|
|
19
|
+
profiled<TArgs extends any[], TRet>(name: string, func: (...args: TArgs) => TRet): (...args: TArgs) => TRet;
|
|
20
|
+
_internal: {
|
|
21
|
+
ready: Promise<unknown>;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
export type InnerLogger<Schema extends EventsSchema> = Omit<EventLogger<Schema>, "profiled">;
|
|
25
|
+
export type LoggerContext = {
|
|
26
|
+
package: string;
|
|
27
|
+
version: string;
|
|
28
|
+
[key: string]: string;
|
|
29
|
+
};
|
|
30
|
+
export type PerformanceEvent = {
|
|
31
|
+
EventName: "performance";
|
|
32
|
+
EventData: {
|
|
33
|
+
executionTimeMs: number;
|
|
34
|
+
functionName: string;
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,YAAY,GAAG,SAAS;IAClC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACjC,EAAE,CAAC;AAEJ,KAAK,QAAQ,CAAC,CAAC,IAAI;KAChB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACrB,GAAG,EAAE,CAAC;AAEP,MAAM,MAAM,oBAAoB,CAAC,MAAM,SAAS,YAAY,IAAI;KAC7D,CAAC,IAAI,MAAM,MAAM,GAAG,QAAQ,CAC3B;QAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAA;KAAE,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,SAAS,SAAS,GACxE;QAAE,IAAI,CAAC,EAAE,SAAS,CAAA;KAAE,GACpB;QAAE,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAA;KAAE,CAAC,CACtC;CACF,CAAC,MAAM,CAAC,CAAC;AAEV,MAAM,WAAW,WAAW,CAAC,MAAM,SAAS,YAAY,GAAG,EAAE;IAC3D,UAAU,CACR,MAAM,EAAE,oBAAoB,CAAC,CAAC,GAAG,MAAM,EAAE,gBAAgB,CAAC,CAAC,GAC1D,OAAO,CAAC,IAAI,CAAC,CAAC;IACjB,QAAQ,CAAC,KAAK,SAAS,GAAG,EAAE,EAAE,IAAI,EAChC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,IAAI,GAC7B,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,IAAI,CAAC;IAC5B,SAAS,EAAE;QACT,KAAK,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;KACzB,CAAC;CACH;AAED,MAAM,MAAM,WAAW,CAAC,MAAM,SAAS,YAAY,IAAI,IAAI,CACzD,WAAW,CAAC,MAAM,CAAC,EACnB,UAAU,CACX,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,SAAS,EAAE,aAAa,CAAC;IACzB,SAAS,EAAE;QACT,eAAe,EAAE,MAAM,CAAC;QACxB,YAAY,EAAE,MAAM,CAAC;KACtB,CAAC;CACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,kBAAkB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@account-kit/logging",
|
|
3
|
+
"version": "4.0.0-beta.10",
|
|
4
|
+
"description": "Core logging library for Account Kit packages",
|
|
5
|
+
"author": "Alchemy",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"private": false,
|
|
8
|
+
"type": "module",
|
|
9
|
+
"main": "./dist/esm/index.js",
|
|
10
|
+
"module": "./dist/esm/index.js",
|
|
11
|
+
"types": "./dist/types/index.d.ts",
|
|
12
|
+
"typings": "./dist/types/index.d.ts",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src/**/*.ts",
|
|
17
|
+
"!dist/**/*.tsbuildinfo",
|
|
18
|
+
"!vitest.config.ts",
|
|
19
|
+
"!.env",
|
|
20
|
+
"!src/**/*.test.ts",
|
|
21
|
+
"!src/__tests__/**/*"
|
|
22
|
+
],
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/types/index.d.ts",
|
|
26
|
+
"import": "./dist/esm/index.js",
|
|
27
|
+
"default": "./dist/esm/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"prebuild": "npx tsx ./prebuild.ts",
|
|
33
|
+
"build": "yarn clean && yarn build:esm && yarn build:types",
|
|
34
|
+
"build:esm": "tsc --project tsconfig.build.json --outDir ./dist/esm",
|
|
35
|
+
"build:types": "tsc --project tsconfig.build.json --declarationDir ./dist/types --emitDeclarationOnly --declaration --declarationMap",
|
|
36
|
+
"postbuild": "npx tsx ./postbuild.ts",
|
|
37
|
+
"clean": "rm -rf ./dist",
|
|
38
|
+
"test": "vitest --passWithNoTests",
|
|
39
|
+
"test:run": "vitest run --passWithNoTests"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@types/jsdom": "^21.1.7",
|
|
43
|
+
"jsdom": "^25.0.1",
|
|
44
|
+
"typescript": "^5.0.4",
|
|
45
|
+
"typescript-template": "*",
|
|
46
|
+
"vitest": "^2.0.4"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public",
|
|
50
|
+
"registry": "https://registry.npmjs.org/"
|
|
51
|
+
},
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/alchemyplatform/aa-sdk.git"
|
|
55
|
+
},
|
|
56
|
+
"bugs": {
|
|
57
|
+
"url": "https://github.com/alchemyplatform/aa-sdk/issues"
|
|
58
|
+
},
|
|
59
|
+
"homepage": "https://github.com/alchemyplatform/aa-sdk#readme",
|
|
60
|
+
"dependencies": {
|
|
61
|
+
"@segment/analytics-next": "^1.74.0",
|
|
62
|
+
"uuidv4": "^6.2.13"
|
|
63
|
+
},
|
|
64
|
+
"gitHead": "3429eb6a07e6a929a1c2069839a525135d081d3e"
|
|
65
|
+
}
|
package/src/_writeKey.ts
ADDED
package/src/client.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { AnalyticsBrowser } from "@segment/analytics-next";
|
|
2
|
+
import { uuid } from "uuidv4";
|
|
3
|
+
import { WRITE_IN_DEV } from "./_writeKey.js";
|
|
4
|
+
import { fetchRemoteWriteKey } from "./fetchRemoteWriteKey.js";
|
|
5
|
+
import { noopLogger } from "./noop.js";
|
|
6
|
+
import { ContextAllowlistPlugin } from "./plugins/contextAllowlist.js";
|
|
7
|
+
import { DevDestinationPlugin } from "./plugins/devDestination.js";
|
|
8
|
+
import type { EventsSchema, InnerLogger, LoggerContext } from "./types";
|
|
9
|
+
|
|
10
|
+
export function createClientLogger<Schema extends EventsSchema = []>(
|
|
11
|
+
context: LoggerContext
|
|
12
|
+
): InnerLogger<Schema> {
|
|
13
|
+
const isDev = window.location.hostname.includes("localhost");
|
|
14
|
+
if (isDev && !WRITE_IN_DEV) {
|
|
15
|
+
// If we don't have a write key, we don't want to log anything
|
|
16
|
+
// This is useful for dev so we don't log dev metrics
|
|
17
|
+
//
|
|
18
|
+
// We also don't allow logging on localhost unless WRITE_IN_DEV is set to true
|
|
19
|
+
// WRITE_IN_DEV is only ever true if you're building from source with env vars set to true
|
|
20
|
+
return noopLogger;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const analytics = new AnalyticsBrowser();
|
|
24
|
+
const writeKey = fetchRemoteWriteKey();
|
|
25
|
+
|
|
26
|
+
if (!sessionStorage.getItem("anonId")) {
|
|
27
|
+
sessionStorage.setItem("anonId", uuid());
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const anonId = sessionStorage.getItem("anonId")!;
|
|
31
|
+
analytics.setAnonymousId(anonId);
|
|
32
|
+
analytics.register(ContextAllowlistPlugin);
|
|
33
|
+
analytics.debug(isDev);
|
|
34
|
+
|
|
35
|
+
// This lets us log events in the console
|
|
36
|
+
if (isDev) {
|
|
37
|
+
analytics.register(DevDestinationPlugin);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const ready: Promise<unknown> = writeKey.then((writeKey) => {
|
|
41
|
+
if (writeKey == null) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
analytics.load(
|
|
46
|
+
{
|
|
47
|
+
writeKey,
|
|
48
|
+
// we disable these settings in dev so we don't fetch anything from segment
|
|
49
|
+
cdnSettings: isDev
|
|
50
|
+
? {
|
|
51
|
+
integrations: {},
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
},
|
|
55
|
+
// further we disable the segment integration dev
|
|
56
|
+
{
|
|
57
|
+
disableClientPersistence: true,
|
|
58
|
+
integrations: {
|
|
59
|
+
"Segment.io": !isDev,
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return analytics.ready();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return {
|
|
68
|
+
_internal: {
|
|
69
|
+
ready,
|
|
70
|
+
},
|
|
71
|
+
trackEvent: async ({ name, data }) => {
|
|
72
|
+
if (!(await writeKey)) {
|
|
73
|
+
return noopLogger.trackEvent({ name, data });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
await analytics.track(name, { ...data, ...context });
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export async function fetchRemoteWriteKey(): Promise<string | undefined> {
|
|
2
|
+
try {
|
|
3
|
+
const json = await fetch(
|
|
4
|
+
"https://static.alchemyapi.io/assets/accountkit/logger_config.json",
|
|
5
|
+
{
|
|
6
|
+
headers: {
|
|
7
|
+
ContentType: "application/json",
|
|
8
|
+
},
|
|
9
|
+
mode: "no-cors",
|
|
10
|
+
}
|
|
11
|
+
).then((res) => res.json());
|
|
12
|
+
|
|
13
|
+
return json.writeKey as string | undefined;
|
|
14
|
+
} catch (e) {
|
|
15
|
+
console.warn("failed to fetch write key");
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createClientLogger } from "./client.js";
|
|
2
|
+
import { createServerLogger } from "./server.js";
|
|
3
|
+
import type { EventLogger, EventsSchema, LoggerContext } from "./types";
|
|
4
|
+
|
|
5
|
+
export type * from "./types.js";
|
|
6
|
+
|
|
7
|
+
export function createLogger<Schema extends EventsSchema = []>(
|
|
8
|
+
context: LoggerContext
|
|
9
|
+
): EventLogger<Schema>;
|
|
10
|
+
|
|
11
|
+
export function createLogger(context: LoggerContext): EventLogger {
|
|
12
|
+
const innerLogger =
|
|
13
|
+
typeof window === "undefined"
|
|
14
|
+
? createServerLogger(context)
|
|
15
|
+
: createClientLogger(context);
|
|
16
|
+
|
|
17
|
+
const logger: EventLogger = {
|
|
18
|
+
...innerLogger,
|
|
19
|
+
profiled<TArgs extends any[], TRet>(
|
|
20
|
+
name: string,
|
|
21
|
+
func: (...args: TArgs) => TRet
|
|
22
|
+
): (...args: TArgs) => TRet {
|
|
23
|
+
return function (this: any, ...args: TArgs): TRet {
|
|
24
|
+
const start = Date.now();
|
|
25
|
+
const result = func.apply(this, args);
|
|
26
|
+
if (result instanceof Promise) {
|
|
27
|
+
return result.then((res) => {
|
|
28
|
+
innerLogger.trackEvent({
|
|
29
|
+
name: "performance",
|
|
30
|
+
data: {
|
|
31
|
+
executionTimeMs: Date.now() - start,
|
|
32
|
+
functionName: name,
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return res;
|
|
37
|
+
}) as TRet;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
innerLogger.trackEvent({
|
|
41
|
+
name: "performance",
|
|
42
|
+
data: {
|
|
43
|
+
executionTimeMs: Date.now() - start,
|
|
44
|
+
functionName: name,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
return logger;
|
|
53
|
+
}
|
package/src/noop.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Context, Plugin } from "@segment/analytics-next";
|
|
2
|
+
|
|
3
|
+
const allowlist: readonly string[] = ["page"] as const;
|
|
4
|
+
|
|
5
|
+
function stripContext(ctx: Context): Context {
|
|
6
|
+
ctx.event.context = allowlist.reduce((acc, key) => {
|
|
7
|
+
acc[key] = ctx.event?.context?.[key];
|
|
8
|
+
|
|
9
|
+
return acc;
|
|
10
|
+
}, {} as Record<(typeof allowlist)[number], unknown>);
|
|
11
|
+
|
|
12
|
+
return ctx;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* This plugin enforces a context allowlist for all events
|
|
17
|
+
*
|
|
18
|
+
* This is done to make sure that anything Analytics.js sends to Segment is only the data we want to send
|
|
19
|
+
*/
|
|
20
|
+
export const ContextAllowlistPlugin: Plugin = {
|
|
21
|
+
name: "Enforce Context Allowlist",
|
|
22
|
+
type: "enrichment",
|
|
23
|
+
|
|
24
|
+
isLoaded: () => true,
|
|
25
|
+
load: () => Promise.resolve(),
|
|
26
|
+
|
|
27
|
+
track: stripContext,
|
|
28
|
+
identify: stripContext,
|
|
29
|
+
page: stripContext,
|
|
30
|
+
alias: stripContext,
|
|
31
|
+
group: stripContext,
|
|
32
|
+
screen: stripContext,
|
|
33
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Context, Plugin } from "@segment/analytics-next";
|
|
2
|
+
|
|
3
|
+
function consoleLogEvent(ctx: Context): Context {
|
|
4
|
+
console.log(JSON.stringify(ctx.event, null, 2));
|
|
5
|
+
|
|
6
|
+
return ctx;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const DevDestinationPlugin: Plugin = {
|
|
10
|
+
name: "Dev Destination Plugin",
|
|
11
|
+
type: "destination",
|
|
12
|
+
isLoaded: () => true,
|
|
13
|
+
load: () => Promise.resolve(),
|
|
14
|
+
|
|
15
|
+
track: consoleLogEvent,
|
|
16
|
+
identify: consoleLogEvent,
|
|
17
|
+
page: consoleLogEvent,
|
|
18
|
+
alias: consoleLogEvent,
|
|
19
|
+
group: consoleLogEvent,
|
|
20
|
+
screen: consoleLogEvent,
|
|
21
|
+
};
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { noopLogger } from "./noop.js";
|
|
2
|
+
import type { EventsSchema, InnerLogger, LoggerContext } from "./types";
|
|
3
|
+
|
|
4
|
+
export function createServerLogger<Schema extends EventsSchema = []>(
|
|
5
|
+
_context: LoggerContext
|
|
6
|
+
): InnerLogger<Schema> {
|
|
7
|
+
// TODO: there is an analytics js node package that we can use, but we'll come back to that later
|
|
8
|
+
// this is harder than client, because on the client we can filter out events that are originating from localhost,
|
|
9
|
+
// whereas here we can't do that as easily
|
|
10
|
+
return noopLogger;
|
|
11
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export type EventsSchema = readonly {
|
|
2
|
+
EventName: string;
|
|
3
|
+
EventData?: Record<string, any>;
|
|
4
|
+
}[];
|
|
5
|
+
|
|
6
|
+
type Prettify<T> = {
|
|
7
|
+
[K in keyof T]: T[K];
|
|
8
|
+
} & {};
|
|
9
|
+
|
|
10
|
+
export type TrackEventParameters<Schema extends EventsSchema> = {
|
|
11
|
+
[K in keyof Schema]: Prettify<
|
|
12
|
+
{ name: Schema[K]["EventName"] } & (Schema[K]["EventData"] extends undefined
|
|
13
|
+
? { data?: undefined }
|
|
14
|
+
: { data: Schema[K]["EventData"] })
|
|
15
|
+
>;
|
|
16
|
+
}[number];
|
|
17
|
+
|
|
18
|
+
export interface EventLogger<Schema extends EventsSchema = []> {
|
|
19
|
+
trackEvent(
|
|
20
|
+
params: TrackEventParameters<[...Schema, PerformanceEvent]>
|
|
21
|
+
): Promise<void>;
|
|
22
|
+
profiled<TArgs extends any[], TRet>(
|
|
23
|
+
name: string,
|
|
24
|
+
func: (...args: TArgs) => TRet
|
|
25
|
+
): (...args: TArgs) => TRet;
|
|
26
|
+
_internal: {
|
|
27
|
+
ready: Promise<unknown>;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type InnerLogger<Schema extends EventsSchema> = Omit<
|
|
32
|
+
EventLogger<Schema>,
|
|
33
|
+
"profiled"
|
|
34
|
+
>;
|
|
35
|
+
|
|
36
|
+
export type LoggerContext = {
|
|
37
|
+
package: string;
|
|
38
|
+
version: string;
|
|
39
|
+
[key: string]: string;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export type PerformanceEvent = {
|
|
43
|
+
EventName: "performance";
|
|
44
|
+
EventData: {
|
|
45
|
+
executionTimeMs: number;
|
|
46
|
+
functionName: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
package/src/version.ts
ADDED