@codaco/analytics 6.0.0-alpha.1 → 6.0.0-alpha.2
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/package.json +1 -1
- package/src/index.ts +10 -5
- package/src/utils.ts +14 -6
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { NextResponse, type NextRequest } from 'next/server';
|
|
2
|
-
import { ensureError, getBaseUrl } from './utils';
|
|
2
|
+
import { ensureError, getBaseUrl, strictBooleanSchema } from './utils';
|
|
3
3
|
import z from 'zod';
|
|
4
4
|
|
|
5
5
|
// Todo: it would be great to work out a way to support arbitrary types here.
|
|
@@ -90,9 +90,13 @@ export const createRouteHandler = ({
|
|
|
90
90
|
try {
|
|
91
91
|
const incomingEvent = (await request.json()) as unknown;
|
|
92
92
|
|
|
93
|
+
const disableAnalytics = strictBooleanSchema.parse(
|
|
94
|
+
// eslint-disable-next-line no-process-env
|
|
95
|
+
process.env.DISABLE_ANALYTICS,
|
|
96
|
+
);
|
|
97
|
+
|
|
93
98
|
// Check if analytics is disabled
|
|
94
|
-
|
|
95
|
-
if (process.env.DISABLE_ANALYTICS) {
|
|
99
|
+
if (disableAnalytics) {
|
|
96
100
|
// eslint-disable-next-line no-console
|
|
97
101
|
console.info('🛑 Analytics disabled. Payload not sent.');
|
|
98
102
|
try {
|
|
@@ -206,14 +210,15 @@ export const createRouteHandler = ({
|
|
|
206
210
|
};
|
|
207
211
|
|
|
208
212
|
export const makeEventTracker =
|
|
209
|
-
(
|
|
213
|
+
(options?: { endpoint?: string }) =>
|
|
210
214
|
async (
|
|
211
215
|
event: RawEvent,
|
|
212
216
|
): Promise<{
|
|
213
217
|
error: string | null;
|
|
214
218
|
success: boolean;
|
|
215
219
|
}> => {
|
|
216
|
-
const endpointWithHost =
|
|
220
|
+
const endpointWithHost =
|
|
221
|
+
getBaseUrl() + options?.endpoint ?? '/api/analytics';
|
|
217
222
|
|
|
218
223
|
const eventWithTimeStamp: TrackableEvent = {
|
|
219
224
|
...event,
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
/* eslint-disable no-process-env */
|
|
2
|
+
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
|
|
2
5
|
// Helper function that ensures that a value is an Error
|
|
3
6
|
export function ensureError(value: unknown): Error {
|
|
4
7
|
if (!value) return new Error('No value was thrown');
|
|
@@ -24,18 +27,23 @@ export function ensureError(value: unknown): Error {
|
|
|
24
27
|
}
|
|
25
28
|
|
|
26
29
|
export function getBaseUrl() {
|
|
27
|
-
if (typeof window !== 'undefined')
|
|
28
|
-
// browser should use relative path
|
|
29
|
-
return '';
|
|
30
|
-
|
|
31
30
|
if (process.env.VERCEL_URL)
|
|
32
31
|
// reference for vercel.com
|
|
33
32
|
return `https://${process.env.VERCEL_URL}`;
|
|
34
33
|
|
|
35
|
-
if (process.env.
|
|
34
|
+
if (process.env.PUBLIC_URL)
|
|
36
35
|
// Manually set deployment URL from env
|
|
37
|
-
return process.env.
|
|
36
|
+
return process.env.PUBLIC_URL;
|
|
38
37
|
|
|
39
38
|
// assume localhost
|
|
40
39
|
return `http://127.0.0.1:3000`;
|
|
41
40
|
}
|
|
41
|
+
|
|
42
|
+
// this is a workaround for this issue:https://github.com/colinhacks/zod/issues/1630
|
|
43
|
+
// z.coerce.boolean() doesn't work as expected
|
|
44
|
+
export const strictBooleanSchema = z
|
|
45
|
+
.enum(['true', 'false', 'True', 'False', 'TRUE', 'FALSE'])
|
|
46
|
+
.default('false')
|
|
47
|
+
.transform(
|
|
48
|
+
(value) => value === 'true' || value === 'True' || value === 'TRUE',
|
|
49
|
+
);
|