@vercel/analytics 0.1.7 → 0.1.8-beta.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/dist/index.cjs +67 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +65 -10
- package/dist/index.js.map +1 -1
- package/dist/react/index.cjs +67 -11
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.d.ts +5 -1
- package/dist/react/index.js +65 -10
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/generic.ts
|
|
21
21
|
var generic_exports = {};
|
|
22
22
|
__export(generic_exports, {
|
|
23
|
-
inject: () => inject
|
|
23
|
+
inject: () => inject,
|
|
24
|
+
track: () => track
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(generic_exports);
|
|
26
27
|
|
|
@@ -37,16 +38,50 @@ var initQueue = () => {
|
|
|
37
38
|
function isBrowser() {
|
|
38
39
|
return typeof window !== "undefined";
|
|
39
40
|
}
|
|
40
|
-
function
|
|
41
|
+
function detectEnvironment() {
|
|
41
42
|
if (typeof process === "undefined")
|
|
42
|
-
return
|
|
43
|
-
|
|
43
|
+
return "production";
|
|
44
|
+
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
|
|
45
|
+
return "development";
|
|
46
|
+
return "production";
|
|
44
47
|
}
|
|
45
|
-
function
|
|
48
|
+
function setMode(mode = "auto") {
|
|
46
49
|
if (mode === "auto") {
|
|
47
|
-
|
|
50
|
+
window.vam = detectEnvironment();
|
|
51
|
+
return;
|
|
48
52
|
}
|
|
49
|
-
|
|
53
|
+
window.vam = mode;
|
|
54
|
+
}
|
|
55
|
+
function getMode() {
|
|
56
|
+
return window.vam || "production";
|
|
57
|
+
}
|
|
58
|
+
function isProduction() {
|
|
59
|
+
return getMode() === "production";
|
|
60
|
+
}
|
|
61
|
+
function isDevelopment() {
|
|
62
|
+
return getMode() === "development";
|
|
63
|
+
}
|
|
64
|
+
var removeKey = (key, { [key]: _, ...rest }) => rest;
|
|
65
|
+
function parseProperties(properties, options) {
|
|
66
|
+
let props = properties;
|
|
67
|
+
const errorProperties = [];
|
|
68
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
69
|
+
if (typeof value === "object" && value !== null) {
|
|
70
|
+
if (options.strip) {
|
|
71
|
+
props = removeKey(key, props);
|
|
72
|
+
} else {
|
|
73
|
+
errorProperties.push(key);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
78
|
+
throw Error(
|
|
79
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
80
|
+
", "
|
|
81
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return props;
|
|
50
85
|
}
|
|
51
86
|
|
|
52
87
|
// src/generic.ts
|
|
@@ -56,24 +91,45 @@ var inject = (props = {
|
|
|
56
91
|
var _a;
|
|
57
92
|
if (!isBrowser())
|
|
58
93
|
return;
|
|
59
|
-
|
|
94
|
+
setMode(props.mode);
|
|
60
95
|
initQueue();
|
|
61
96
|
if (props.beforeSend) {
|
|
62
97
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
63
98
|
}
|
|
64
|
-
const src =
|
|
99
|
+
const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
65
100
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
66
101
|
return;
|
|
67
102
|
const script = document.createElement("script");
|
|
68
103
|
script.src = src;
|
|
69
104
|
script.defer = true;
|
|
70
|
-
if (
|
|
105
|
+
if (isDevelopment() && props.debug === false) {
|
|
71
106
|
script.setAttribute("data-debug", "false");
|
|
72
107
|
}
|
|
73
108
|
document.head.appendChild(script);
|
|
74
109
|
};
|
|
110
|
+
var track = (name, properties) => {
|
|
111
|
+
var _a, _b;
|
|
112
|
+
if (!properties) {
|
|
113
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
try {
|
|
117
|
+
const props = parseProperties(properties, {
|
|
118
|
+
strip: isProduction()
|
|
119
|
+
});
|
|
120
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "track", {
|
|
121
|
+
name,
|
|
122
|
+
data: props
|
|
123
|
+
});
|
|
124
|
+
} catch (err) {
|
|
125
|
+
if (err instanceof Error && isDevelopment()) {
|
|
126
|
+
console.error(err);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
};
|
|
75
130
|
// Annotate the CommonJS export names for ESM import in node:
|
|
76
131
|
0 && (module.exports = {
|
|
77
|
-
inject
|
|
132
|
+
inject,
|
|
133
|
+
track
|
|
78
134
|
});
|
|
79
135
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/generic.ts","../src/queue.ts","../src/utils.ts"],"sourcesContent":["import { initQueue } from './queue';\nimport type { AnalyticsProps } from './types';\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/generic.ts","../src/queue.ts","../src/utils.ts"],"sourcesContent":["import { initQueue } from './queue';\nimport type { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\n name,\n data: props,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console\n console.error(err);\n }\n }\n};\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n return window.vam || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\n\nexport function parseProperties(\n properties: Record<string, unknown>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | undefined {\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', ',\n )}. Only str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C,gBAAgB;AAAA,QACzD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACA,SAAO;AACT;;;AFvDO,IAAM,SAAS,CACpB,QAAwB;AAAA,EACtB,OAAO;AACT,MACS;AAdX;AAeE,MAAI,CAAC,UAAU;AAAG;AAElB,UAAQ,MAAM,IAAI;AAElB,YAAU;AAEV,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,MAAM,cAAc,IACtB,uDACA;AAEJ,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,MAAM,UAAU,OAAO;AAC5C,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;AAEO,IAAM,QAAQ,CACnB,MACA,eACS;AA7CX;AA8CE,MAAI,CAAC,YAAY;AACf,iBAAO,OAAP,gCAAY,SAAS,EAAE,KAAK;AAC5B;AAAA,EACF;AAEA,MAAI;AACF,UAAM,QAAQ,gBAAgB,YAAY;AAAA,MACxC,OAAO,aAAa;AAAA,IACtB,CAAC;AAED,iBAAO,OAAP,gCAAY,SAAS;AAAA,MACnB;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ interface PageViewEvent {
|
|
|
4
4
|
}
|
|
5
5
|
declare type Event = PageViewEvent;
|
|
6
6
|
declare type Mode = 'auto' | 'development' | 'production';
|
|
7
|
+
declare type AllowedPropertyValues = string | number | boolean | null;
|
|
7
8
|
declare type BeforeSend = (event: Event) => Event | null;
|
|
8
9
|
interface AnalyticsProps {
|
|
9
10
|
beforeSend?: BeforeSend;
|
|
@@ -15,9 +16,11 @@ declare global {
|
|
|
15
16
|
va?: (event: string, properties?: unknown) => void;
|
|
16
17
|
vaq?: [string, unknown?][];
|
|
17
18
|
vai?: boolean;
|
|
19
|
+
vam?: Mode;
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
declare const inject: (props?: AnalyticsProps) => void;
|
|
24
|
+
declare const track: (name: string, properties?: Record<string, AllowedPropertyValues>) => void;
|
|
22
25
|
|
|
23
|
-
export { inject };
|
|
26
|
+
export { inject, track };
|
package/dist/index.js
CHANGED
|
@@ -11,16 +11,50 @@ var initQueue = () => {
|
|
|
11
11
|
function isBrowser() {
|
|
12
12
|
return typeof window !== "undefined";
|
|
13
13
|
}
|
|
14
|
-
function
|
|
14
|
+
function detectEnvironment() {
|
|
15
15
|
if (typeof process === "undefined")
|
|
16
|
-
return
|
|
17
|
-
|
|
16
|
+
return "production";
|
|
17
|
+
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
|
|
18
|
+
return "development";
|
|
19
|
+
return "production";
|
|
18
20
|
}
|
|
19
|
-
function
|
|
21
|
+
function setMode(mode = "auto") {
|
|
20
22
|
if (mode === "auto") {
|
|
21
|
-
|
|
23
|
+
window.vam = detectEnvironment();
|
|
24
|
+
return;
|
|
22
25
|
}
|
|
23
|
-
|
|
26
|
+
window.vam = mode;
|
|
27
|
+
}
|
|
28
|
+
function getMode() {
|
|
29
|
+
return window.vam || "production";
|
|
30
|
+
}
|
|
31
|
+
function isProduction() {
|
|
32
|
+
return getMode() === "production";
|
|
33
|
+
}
|
|
34
|
+
function isDevelopment() {
|
|
35
|
+
return getMode() === "development";
|
|
36
|
+
}
|
|
37
|
+
var removeKey = (key, { [key]: _, ...rest }) => rest;
|
|
38
|
+
function parseProperties(properties, options) {
|
|
39
|
+
let props = properties;
|
|
40
|
+
const errorProperties = [];
|
|
41
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
42
|
+
if (typeof value === "object" && value !== null) {
|
|
43
|
+
if (options.strip) {
|
|
44
|
+
props = removeKey(key, props);
|
|
45
|
+
} else {
|
|
46
|
+
errorProperties.push(key);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
51
|
+
throw Error(
|
|
52
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
53
|
+
", "
|
|
54
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return props;
|
|
24
58
|
}
|
|
25
59
|
|
|
26
60
|
// src/generic.ts
|
|
@@ -30,23 +64,44 @@ var inject = (props = {
|
|
|
30
64
|
var _a;
|
|
31
65
|
if (!isBrowser())
|
|
32
66
|
return;
|
|
33
|
-
|
|
67
|
+
setMode(props.mode);
|
|
34
68
|
initQueue();
|
|
35
69
|
if (props.beforeSend) {
|
|
36
70
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
37
71
|
}
|
|
38
|
-
const src =
|
|
72
|
+
const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
39
73
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
40
74
|
return;
|
|
41
75
|
const script = document.createElement("script");
|
|
42
76
|
script.src = src;
|
|
43
77
|
script.defer = true;
|
|
44
|
-
if (
|
|
78
|
+
if (isDevelopment() && props.debug === false) {
|
|
45
79
|
script.setAttribute("data-debug", "false");
|
|
46
80
|
}
|
|
47
81
|
document.head.appendChild(script);
|
|
48
82
|
};
|
|
83
|
+
var track = (name, properties) => {
|
|
84
|
+
var _a, _b;
|
|
85
|
+
if (!properties) {
|
|
86
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
const props = parseProperties(properties, {
|
|
91
|
+
strip: isProduction()
|
|
92
|
+
});
|
|
93
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "track", {
|
|
94
|
+
name,
|
|
95
|
+
data: props
|
|
96
|
+
});
|
|
97
|
+
} catch (err) {
|
|
98
|
+
if (err instanceof Error && isDevelopment()) {
|
|
99
|
+
console.error(err);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
49
103
|
export {
|
|
50
|
-
inject
|
|
104
|
+
inject,
|
|
105
|
+
track
|
|
51
106
|
};
|
|
52
107
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\
|
|
1
|
+
{"version":3,"sources":["../src/queue.ts","../src/utils.ts","../src/generic.ts"],"sourcesContent":["export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n return window.vam || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\n\nexport function parseProperties(\n properties: Record<string, unknown>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | undefined {\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', ',\n )}. Only strings, numbers, booleans, and null are allowed.`,\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n","import { initQueue } from './queue';\nimport type { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\n name,\n data: props,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console\n console.error(err);\n }\n }\n};\n"],"mappings":";AAAO,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GA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MAAM;AAAA,EAClC;AAEA,QAAM,MAAM,cAAc,IACtB,uDACA;AAEJ,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,MAAM,UAAU,OAAO;AAC5C,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;AAEO,IAAM,QAAQ,CACnB,MACA,eACS;AA7CX;AA8CE,MAAI,CAAC,YAAY;AACf,iBAAO,OAAP,gCAAY,SAAS,EAAE,KAAK;AAC5B;AAAA,EACF;AAEA,MAAI;AACF,UAAM,QAAQ,gBAAgB,YAAY;AAAA,MACxC,OAAO,aAAa;AAAA,IACtB,CAAC;AAED,iBAAO,OAAP,gCAAY,SAAS;AAAA,MACnB;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;","names":[]}
|
package/dist/react/index.cjs
CHANGED
|
@@ -20,7 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/react.tsx
|
|
21
21
|
var react_exports = {};
|
|
22
22
|
__export(react_exports, {
|
|
23
|
-
Analytics: () => Analytics
|
|
23
|
+
Analytics: () => Analytics,
|
|
24
|
+
track: () => track
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(react_exports);
|
|
26
27
|
var import_react = require("react");
|
|
@@ -38,16 +39,50 @@ var initQueue = () => {
|
|
|
38
39
|
function isBrowser() {
|
|
39
40
|
return typeof window !== "undefined";
|
|
40
41
|
}
|
|
41
|
-
function
|
|
42
|
+
function detectEnvironment() {
|
|
42
43
|
if (typeof process === "undefined")
|
|
43
|
-
return
|
|
44
|
-
|
|
44
|
+
return "production";
|
|
45
|
+
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
|
|
46
|
+
return "development";
|
|
47
|
+
return "production";
|
|
45
48
|
}
|
|
46
|
-
function
|
|
49
|
+
function setMode(mode = "auto") {
|
|
47
50
|
if (mode === "auto") {
|
|
48
|
-
|
|
51
|
+
window.vam = detectEnvironment();
|
|
52
|
+
return;
|
|
49
53
|
}
|
|
50
|
-
|
|
54
|
+
window.vam = mode;
|
|
55
|
+
}
|
|
56
|
+
function getMode() {
|
|
57
|
+
return window.vam || "production";
|
|
58
|
+
}
|
|
59
|
+
function isProduction() {
|
|
60
|
+
return getMode() === "production";
|
|
61
|
+
}
|
|
62
|
+
function isDevelopment() {
|
|
63
|
+
return getMode() === "development";
|
|
64
|
+
}
|
|
65
|
+
var removeKey = (key, { [key]: _, ...rest }) => rest;
|
|
66
|
+
function parseProperties(properties, options) {
|
|
67
|
+
let props = properties;
|
|
68
|
+
const errorProperties = [];
|
|
69
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
70
|
+
if (typeof value === "object" && value !== null) {
|
|
71
|
+
if (options.strip) {
|
|
72
|
+
props = removeKey(key, props);
|
|
73
|
+
} else {
|
|
74
|
+
errorProperties.push(key);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
79
|
+
throw Error(
|
|
80
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
81
|
+
", "
|
|
82
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
return props;
|
|
51
86
|
}
|
|
52
87
|
|
|
53
88
|
// src/generic.ts
|
|
@@ -57,22 +92,42 @@ var inject = (props = {
|
|
|
57
92
|
var _a;
|
|
58
93
|
if (!isBrowser())
|
|
59
94
|
return;
|
|
60
|
-
|
|
95
|
+
setMode(props.mode);
|
|
61
96
|
initQueue();
|
|
62
97
|
if (props.beforeSend) {
|
|
63
98
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
64
99
|
}
|
|
65
|
-
const src =
|
|
100
|
+
const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
66
101
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
67
102
|
return;
|
|
68
103
|
const script = document.createElement("script");
|
|
69
104
|
script.src = src;
|
|
70
105
|
script.defer = true;
|
|
71
|
-
if (
|
|
106
|
+
if (isDevelopment() && props.debug === false) {
|
|
72
107
|
script.setAttribute("data-debug", "false");
|
|
73
108
|
}
|
|
74
109
|
document.head.appendChild(script);
|
|
75
110
|
};
|
|
111
|
+
var track = (name, properties) => {
|
|
112
|
+
var _a, _b;
|
|
113
|
+
if (!properties) {
|
|
114
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const props = parseProperties(properties, {
|
|
119
|
+
strip: isProduction()
|
|
120
|
+
});
|
|
121
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "track", {
|
|
122
|
+
name,
|
|
123
|
+
data: props
|
|
124
|
+
});
|
|
125
|
+
} catch (err) {
|
|
126
|
+
if (err instanceof Error && isDevelopment()) {
|
|
127
|
+
console.error(err);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
76
131
|
|
|
77
132
|
// src/react.tsx
|
|
78
133
|
function Analytics({
|
|
@@ -87,6 +142,7 @@ function Analytics({
|
|
|
87
142
|
}
|
|
88
143
|
// Annotate the CommonJS export names for ESM import in node:
|
|
89
144
|
0 && (module.exports = {
|
|
90
|
-
Analytics
|
|
145
|
+
Analytics,
|
|
146
|
+
track
|
|
91
147
|
});
|
|
92
148
|
//# sourceMappingURL=index.cjs.map
|
package/dist/react/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\nexport type { AnalyticsProps } from './types';\n\nexport function Analytics({\n beforeSend,\n debug = true,\n mode = 'auto',\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug, mode });\n }, [beforeSend, debug, mode]);\n\n return null;\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\
|
|
1
|
+
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\nexport type { AnalyticsProps } from './types';\n\nexport function Analytics({\n beforeSend,\n debug = true,\n mode = 'auto',\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug, mode });\n }, [beforeSend, debug, mode]);\n\n return null;\n}\n\nexport { track } from './generic';\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n return window.vam || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\n\nexport function parseProperties(\n properties: Record<string, unknown>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | undefined {\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', ',\n )}. Only strings, numbers, booleans, and null are allowed.`,\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n","import { initQueue } from './queue';\nimport type { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\n name,\n data: props,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console\n console.error(err);\n }\n }\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA0B;;;AC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AdX;AAeE,MAAI,CAAC,UAAU;AAAG;AAElB,UAAQ,MAAM,IAAI;AAElB,YAAU;AAEV,MAAI,MAAM,YAAY;AACpB,iBAAO,OAAP,gCAAY,cAAc,MAAM;AAAA,EAClC;AAEA,QAAM,MAAM,cAAc,IACtB,uDACA;AAEJ,MAAI,SAAS,KAAK,cAAc,gBAAgB,OAAO;AAAG;AAE1D,QAAM,SAAS,SAAS,cAAc,QAAQ;AAC9C,SAAO,MAAM;AACb,SAAO,QAAQ;AAEf,MAAI,cAAc,KAAK,MAAM,UAAU,OAAO;AAC5C,WAAO,aAAa,cAAc,OAAO;AAAA,EAC3C;AAEA,WAAS,KAAK,YAAY,MAAM;AAClC;AAEO,IAAM,QAAQ,CACnB,MACA,eACS;AA7CX;AA8CE,MAAI,CAAC,YAAY;AACf,iBAAO,OAAP,gCAAY,SAAS,EAAE,KAAK;AAC5B;AAAA,EACF;AAEA,MAAI;AACF,UAAM,QAAQ,gBAAgB,YAAY;AAAA,MACxC,OAAO,aAAa;AAAA,IACtB,CAAC;AAED,iBAAO,OAAP,gCAAY,SAAS;AAAA,MACnB;AAAA,MACA,MAAM;AAAA,IACR;AAAA,EACF,SAAS,KAAP;AACA,QAAI,eAAe,SAAS,cAAc,GAAG;AAE3C,cAAQ,MAAM,GAAG;AAAA,IACnB;AAAA,EACF;AACF;;;AH5DO,SAAS,UAAU;AAAA,EACxB;AAAA,EACA,QAAQ;AAAA,EACR,OAAO;AACT,GAAyB;AACvB,8BAAU,MAAM;AACd,WAAO,EAAE,YAAY,OAAO,KAAK,CAAC;AAAA,EACpC,GAAG,CAAC,YAAY,OAAO,IAAI,CAAC;AAE5B,SAAO;AACT;","names":[]}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ interface PageViewEvent {
|
|
|
4
4
|
}
|
|
5
5
|
declare type Event = PageViewEvent;
|
|
6
6
|
declare type Mode = 'auto' | 'development' | 'production';
|
|
7
|
+
declare type AllowedPropertyValues = string | number | boolean | null;
|
|
7
8
|
declare type BeforeSend = (event: Event) => Event | null;
|
|
8
9
|
interface AnalyticsProps {
|
|
9
10
|
beforeSend?: BeforeSend;
|
|
@@ -15,9 +16,12 @@ declare global {
|
|
|
15
16
|
va?: (event: string, properties?: unknown) => void;
|
|
16
17
|
vaq?: [string, unknown?][];
|
|
17
18
|
vai?: boolean;
|
|
19
|
+
vam?: Mode;
|
|
18
20
|
}
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
declare const track: (name: string, properties?: Record<string, AllowedPropertyValues>) => void;
|
|
24
|
+
|
|
21
25
|
declare function Analytics({ beforeSend, debug, mode, }: AnalyticsProps): null;
|
|
22
26
|
|
|
23
|
-
export { Analytics, AnalyticsProps };
|
|
27
|
+
export { Analytics, AnalyticsProps, track };
|
package/dist/react/index.js
CHANGED
|
@@ -14,16 +14,50 @@ var initQueue = () => {
|
|
|
14
14
|
function isBrowser() {
|
|
15
15
|
return typeof window !== "undefined";
|
|
16
16
|
}
|
|
17
|
-
function
|
|
17
|
+
function detectEnvironment() {
|
|
18
18
|
if (typeof process === "undefined")
|
|
19
|
-
return
|
|
20
|
-
|
|
19
|
+
return "production";
|
|
20
|
+
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test")
|
|
21
|
+
return "development";
|
|
22
|
+
return "production";
|
|
21
23
|
}
|
|
22
|
-
function
|
|
24
|
+
function setMode(mode = "auto") {
|
|
23
25
|
if (mode === "auto") {
|
|
24
|
-
|
|
26
|
+
window.vam = detectEnvironment();
|
|
27
|
+
return;
|
|
25
28
|
}
|
|
26
|
-
|
|
29
|
+
window.vam = mode;
|
|
30
|
+
}
|
|
31
|
+
function getMode() {
|
|
32
|
+
return window.vam || "production";
|
|
33
|
+
}
|
|
34
|
+
function isProduction() {
|
|
35
|
+
return getMode() === "production";
|
|
36
|
+
}
|
|
37
|
+
function isDevelopment() {
|
|
38
|
+
return getMode() === "development";
|
|
39
|
+
}
|
|
40
|
+
var removeKey = (key, { [key]: _, ...rest }) => rest;
|
|
41
|
+
function parseProperties(properties, options) {
|
|
42
|
+
let props = properties;
|
|
43
|
+
const errorProperties = [];
|
|
44
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
45
|
+
if (typeof value === "object" && value !== null) {
|
|
46
|
+
if (options.strip) {
|
|
47
|
+
props = removeKey(key, props);
|
|
48
|
+
} else {
|
|
49
|
+
errorProperties.push(key);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (errorProperties.length > 0 && !options.strip) {
|
|
54
|
+
throw Error(
|
|
55
|
+
`The following properties are not valid: ${errorProperties.join(
|
|
56
|
+
", "
|
|
57
|
+
)}. Only strings, numbers, booleans, and null are allowed.`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return props;
|
|
27
61
|
}
|
|
28
62
|
|
|
29
63
|
// src/generic.ts
|
|
@@ -33,22 +67,42 @@ var inject = (props = {
|
|
|
33
67
|
var _a;
|
|
34
68
|
if (!isBrowser())
|
|
35
69
|
return;
|
|
36
|
-
|
|
70
|
+
setMode(props.mode);
|
|
37
71
|
initQueue();
|
|
38
72
|
if (props.beforeSend) {
|
|
39
73
|
(_a = window.va) == null ? void 0 : _a.call(window, "beforeSend", props.beforeSend);
|
|
40
74
|
}
|
|
41
|
-
const src =
|
|
75
|
+
const src = isDevelopment() ? "https://cdn.vercel-insights.com/v1/script.debug.js" : "/_vercel/insights/script.js";
|
|
42
76
|
if (document.head.querySelector(`script[src*="${src}"]`))
|
|
43
77
|
return;
|
|
44
78
|
const script = document.createElement("script");
|
|
45
79
|
script.src = src;
|
|
46
80
|
script.defer = true;
|
|
47
|
-
if (
|
|
81
|
+
if (isDevelopment() && props.debug === false) {
|
|
48
82
|
script.setAttribute("data-debug", "false");
|
|
49
83
|
}
|
|
50
84
|
document.head.appendChild(script);
|
|
51
85
|
};
|
|
86
|
+
var track = (name, properties) => {
|
|
87
|
+
var _a, _b;
|
|
88
|
+
if (!properties) {
|
|
89
|
+
(_a = window.va) == null ? void 0 : _a.call(window, "track", { name });
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
const props = parseProperties(properties, {
|
|
94
|
+
strip: isProduction()
|
|
95
|
+
});
|
|
96
|
+
(_b = window.va) == null ? void 0 : _b.call(window, "track", {
|
|
97
|
+
name,
|
|
98
|
+
data: props
|
|
99
|
+
});
|
|
100
|
+
} catch (err) {
|
|
101
|
+
if (err instanceof Error && isDevelopment()) {
|
|
102
|
+
console.error(err);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
52
106
|
|
|
53
107
|
// src/react.tsx
|
|
54
108
|
function Analytics({
|
|
@@ -62,6 +116,7 @@ function Analytics({
|
|
|
62
116
|
return null;
|
|
63
117
|
}
|
|
64
118
|
export {
|
|
65
|
-
Analytics
|
|
119
|
+
Analytics,
|
|
120
|
+
track
|
|
66
121
|
};
|
|
67
122
|
//# sourceMappingURL=index.js.map
|
package/dist/react/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\nexport type { AnalyticsProps } from './types';\n\nexport function Analytics({\n beforeSend,\n debug = true,\n mode = 'auto',\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug, mode });\n }, [beforeSend, debug, mode]);\n\n return null;\n}\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\
|
|
1
|
+
{"version":3,"sources":["../../src/react.tsx","../../src/queue.ts","../../src/utils.ts","../../src/generic.ts"],"sourcesContent":["import { useEffect } from 'react';\nimport { inject } from './generic';\nimport type { AnalyticsProps } from './types';\n\nexport type { AnalyticsProps } from './types';\n\nexport function Analytics({\n beforeSend,\n debug = true,\n mode = 'auto',\n}: AnalyticsProps): null {\n useEffect(() => {\n inject({ beforeSend, debug, mode });\n }, [beforeSend, debug, mode]);\n\n return null;\n}\n\nexport { track } from './generic';\n","export const initQueue = (): void => {\n // initialize va until script is loaded\n if (window.va) return;\n\n window.va = function a(...params): void {\n (window.vaq = window.vaq || []).push(params);\n };\n};\n","import type { AllowedPropertyValues, Mode } from './types';\n\nexport function isBrowser(): boolean {\n return typeof window !== 'undefined';\n}\n\nfunction detectEnvironment(): 'development' | 'production' {\n if (typeof process === 'undefined') return 'production';\n\n if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test')\n return 'development';\n\n return 'production';\n}\n\nexport function setMode(mode: Mode = 'auto'): void {\n if (mode === 'auto') {\n window.vam = detectEnvironment();\n return;\n }\n\n window.vam = mode;\n}\n\nexport function getMode(): Mode {\n return window.vam || 'production';\n}\n\nexport function isProduction(): boolean {\n return getMode() === 'production';\n}\n\nexport function isDevelopment(): boolean {\n return getMode() === 'development';\n}\n\nconst removeKey = (key: string, { [key]: _, ...rest }): Record<string, any> =>\n rest;\n\nexport function parseProperties(\n properties: Record<string, unknown>,\n options: {\n strip?: boolean;\n },\n): Error | Record<string, AllowedPropertyValues> | undefined {\n let props = properties;\n const errorProperties: string[] = [];\n for (const [key, value] of Object.entries(properties)) {\n if (typeof value === 'object' && value !== null) {\n if (options.strip) {\n props = removeKey(key, props);\n } else {\n errorProperties.push(key);\n }\n }\n }\n\n if (errorProperties.length > 0 && !options.strip) {\n throw Error(\n `The following properties are not valid: ${errorProperties.join(\n ', ',\n )}. Only strings, numbers, booleans, and null are allowed.`,\n );\n }\n return props as Record<string, AllowedPropertyValues>;\n}\n","import { initQueue } from './queue';\nimport type { AllowedPropertyValues, AnalyticsProps } from './types';\nimport {\n isBrowser,\n parseProperties,\n setMode,\n isDevelopment,\n isProduction,\n} from './utils';\n\nexport const inject = (\n props: AnalyticsProps = {\n debug: true,\n },\n): void => {\n if (!isBrowser()) return;\n\n setMode(props.mode);\n\n initQueue();\n\n if (props.beforeSend) {\n window.va?.('beforeSend', props.beforeSend);\n }\n\n const src = isDevelopment()\n ? 'https://cdn.vercel-insights.com/v1/script.debug.js'\n : '/_vercel/insights/script.js';\n\n if (document.head.querySelector(`script[src*=\"${src}\"]`)) return;\n\n const script = document.createElement('script');\n script.src = src;\n script.defer = true;\n\n if (isDevelopment() && props.debug === false) {\n script.setAttribute('data-debug', 'false');\n }\n\n document.head.appendChild(script);\n};\n\nexport const track = (\n name: string,\n properties?: Record<string, AllowedPropertyValues>,\n): void => {\n if (!properties) {\n window.va?.('track', { name });\n return;\n }\n\n try {\n const props = parseProperties(properties, {\n strip: isProduction(),\n });\n\n window.va?.('track', {\n name,\n data: props,\n });\n } catch (err) {\n if (err instanceof Error && isDevelopment()) {\n // eslint-disable-next-line no-console\n console.error(err);\n }\n }\n};\n"],"mappings":";AAAA,SAAS,iBAAiB;;;ACAnB,IAAM,YAAY,MAAY;AAEnC,MAAI,OAAO;AAAI;AAEf,SAAO,KAAK,SAAS,KAAK,QAAc;AACtC,KAAC,OAAO,MAAM,OAAO,OAAO,CAAC,GAAG,KAAK,MAAM;AAAA,EAC7C;AACF;;;ACLO,SAAS,YAAqB;AACnC,SAAO,OAAO,WAAW;AAC3B;AAEA,SAAS,oBAAkD;AACzD,MAAI,OAAO,YAAY;AAAa,WAAO;AAE3C,MAAI,QAAQ,IAAI,aAAa,iBAAiB,QAAQ,IAAI,aAAa;AACrE,WAAO;AAET,SAAO;AACT;AAEO,SAAS,QAAQ,OAAa,QAAc;AACjD,MAAI,SAAS,QAAQ;AACnB,WAAO,MAAM,kBAAkB;AAC/B;AAAA,EACF;AAEA,SAAO,MAAM;AACf;AAEO,SAAS,UAAgB;AAC9B,SAAO,OAAO,OAAO;AACvB;AAEO,SAAS,eAAwB;AACtC,SAAO,QAAQ,MAAM;AACvB;AAEO,SAAS,gBAAyB;A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