create-db 1.0.4-pr48-DC-4894-posthog-fix-17272348089.0 → 1.0.4

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.
Files changed (3) hide show
  1. package/analytics.js +63 -0
  2. package/index.js +35 -40
  3. package/package.json +1 -1
package/analytics.js ADDED
@@ -0,0 +1,63 @@
1
+ import { randomUUID } from "crypto";
2
+
3
+ class EventCaptureError extends Error {
4
+ constructor(event, status) {
5
+ super(`Failed to submit PostHog event '${event}': ${status}`);
6
+ }
7
+ }
8
+
9
+ class PosthogEventCapture {
10
+ async capture(eventName, properties = {}) {
11
+ const POSTHOG_API_HOST = process.env.POSTHOG_API_HOST;
12
+ const POSTHOG_KEY = process.env.POSTHOG_API_KEY;
13
+
14
+ if (
15
+ !POSTHOG_API_HOST ||
16
+ !POSTHOG_KEY ||
17
+ POSTHOG_API_HOST.trim() === "" ||
18
+ POSTHOG_KEY.trim() === ""
19
+ ) {
20
+ if (process.env.NODE_ENV === "development") {
21
+ console.warn(
22
+ "Analytics disabled: missing POSTHOG_API_HOST or POSTHOG_API_KEY."
23
+ );
24
+ }
25
+ return;
26
+ }
27
+
28
+ const POSTHOG_CAPTURE_URL = `${POSTHOG_API_HOST.replace(/\/+$/, "")}/capture`;
29
+
30
+ const payload = {
31
+ api_key: POSTHOG_KEY,
32
+ event: eventName,
33
+ distinct_id: randomUUID(),
34
+ properties: {
35
+ $process_person_profile: false,
36
+ ...properties,
37
+ },
38
+ };
39
+
40
+ try {
41
+ const response = await fetch(POSTHOG_CAPTURE_URL, {
42
+ method: "POST",
43
+ headers: {
44
+ "Content-Type": "application/json",
45
+ },
46
+ body: JSON.stringify(payload),
47
+ });
48
+
49
+ if (!response.ok) {
50
+ throw new EventCaptureError(eventName, response.statusText);
51
+ }
52
+ } catch (error) {
53
+ if (process.env.NODE_ENV === "development") {
54
+ console.error("Analytics error:", error.message);
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ // Create a singleton instance
61
+ const analytics = new PosthogEventCapture();
62
+
63
+ export { analytics, EventCaptureError };
package/index.js CHANGED
@@ -9,32 +9,13 @@ dotenv.config();
9
9
  import { select, spinner, intro, outro, log, cancel } from "@clack/prompts";
10
10
  import chalk from "chalk";
11
11
  import terminalLink from "terminal-link";
12
+ import { analytics } from "./analytics.js";
12
13
 
13
14
  const CREATE_DB_WORKER_URL =
14
15
  process.env.CREATE_DB_WORKER_URL || "https://create-db-temp.prisma.io";
15
16
  const CLAIM_DB_WORKER_URL =
16
17
  process.env.CLAIM_DB_WORKER_URL || "https://create-db.prisma.io";
17
18
 
18
- async function sendAnalyticsToWorker(
19
- eventName,
20
- properties,
21
- { timeoutMs = 2000 }
22
- ) {
23
- const controller = new AbortController();
24
- const timer = setTimeout(() => controller.abort(), timeoutMs);
25
- try {
26
- await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
27
- method: "POST",
28
- headers: { "Content-Type": "application/json" },
29
- body: JSON.stringify({ eventName, properties }),
30
- signal: controller.signal,
31
- });
32
- } catch (error) {
33
- } finally {
34
- clearTimeout(timer);
35
- }
36
- }
37
-
38
19
  async function detectUserLocation() {
39
20
  try {
40
21
  const response = await fetch("https://ipapi.co/json/", {
@@ -62,6 +43,7 @@ async function detectUserLocation() {
62
43
  }
63
44
  }
64
45
 
46
+ // Region coordinates (latitude, longitude)
65
47
  const REGION_COORDINATES = {
66
48
  "ap-southeast-1": { lat: 1.3521, lng: 103.8198 }, // Singapore
67
49
  "ap-northeast-1": { lat: 35.6762, lng: 139.6503 }, // Tokyo
@@ -378,12 +360,14 @@ async function promptForRegion(defaultRegion, userAgent) {
378
360
  }
379
361
 
380
362
  try {
381
- await sendAnalyticsToWorker("create_db:region_selected", {
363
+ const analyticsProps = {
382
364
  command: CLI_NAME,
383
365
  region: region,
384
366
  "selection-method": "interactive",
385
367
  "user-agent": userAgent,
386
- });
368
+ };
369
+
370
+ await analytics.capture("create_db:region_selected", analyticsProps);
387
371
  } catch (error) {}
388
372
 
389
373
  return region;
@@ -423,13 +407,18 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
423
407
  }
424
408
 
425
409
  try {
426
- await sendAnalyticsToWorker("create_db:database_creation_failed", {
410
+ const analyticsProps = {
427
411
  command: CLI_NAME,
428
412
  region: region,
429
413
  "error-type": "rate_limit",
430
414
  "status-code": 429,
431
415
  "user-agent": userAgent,
432
- });
416
+ };
417
+
418
+ await analytics.capture(
419
+ "create_db:database_creation_failed",
420
+ analyticsProps
421
+ );
433
422
  } catch (error) {}
434
423
 
435
424
  process.exit(1);
@@ -453,13 +442,18 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
453
442
  s.stop("Unexpected response from create service.");
454
443
  }
455
444
  try {
456
- await sendAnalyticsToWorker("create_db:database_creation_failed", {
445
+ const analyticsProps = {
457
446
  command: CLI_NAME,
458
447
  region,
459
448
  "error-type": "invalid_json",
460
449
  "status-code": resp.status,
461
450
  "user-agent": userAgent,
462
- });
451
+ };
452
+
453
+ await analytics.capture(
454
+ "create_db:database_creation_failed",
455
+ analyticsProps
456
+ );
463
457
  } catch (error) {}
464
458
  process.exit(1);
465
459
  }
@@ -524,13 +518,18 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
524
518
  }
525
519
 
526
520
  try {
527
- await sendAnalyticsToWorker("create_db:database_creation_failed", {
521
+ const analyticsProps = {
528
522
  command: CLI_NAME,
529
523
  region: region,
530
524
  "error-type": "api_error",
531
525
  "error-message": result.error.message,
532
526
  "user-agent": userAgent,
533
- });
527
+ };
528
+
529
+ await analytics.capture(
530
+ "create_db:database_creation_failed",
531
+ analyticsProps
532
+ );
534
533
  } catch (error) {}
535
534
  process.exit(1);
536
535
  }
@@ -582,14 +581,6 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
582
581
  )
583
582
  )
584
583
  );
585
-
586
- try {
587
- await sendAnalyticsToWorker("create_db:database_created", {
588
- command: CLI_NAME,
589
- region,
590
- utm_source: CLI_NAME,
591
- });
592
- } catch {}
593
584
  }
594
585
 
595
586
  async function main() {
@@ -605,7 +596,7 @@ async function main() {
605
596
  }
606
597
 
607
598
  try {
608
- await sendAnalyticsToWorker("create_db:cli_command_ran", {
599
+ const analyticsProps = {
609
600
  command: CLI_NAME,
610
601
  "full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
611
602
  "has-region-flag":
@@ -620,7 +611,9 @@ async function main() {
620
611
  platform: process.platform,
621
612
  arch: process.arch,
622
613
  "user-agent": userAgent,
623
- });
614
+ };
615
+
616
+ await analytics.capture("create_db:cli_command_ran", analyticsProps);
624
617
  } catch (error) {
625
618
  console.error("Error:", error.message);
626
619
  }
@@ -647,12 +640,14 @@ async function main() {
647
640
  region = flags.region;
648
641
 
649
642
  try {
650
- await sendAnalyticsToWorker("create_db:region_selected", {
643
+ const analyticsProps = {
651
644
  command: CLI_NAME,
652
645
  region: region,
653
646
  "selection-method": "flag",
654
647
  "user-agent": userAgent,
655
- });
648
+ };
649
+
650
+ await analytics.capture("create_db:region_selected", analyticsProps);
656
651
  } catch (error) {}
657
652
  }
658
653
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "1.0.4-pr48-DC-4894-posthog-fix-17272348089.0",
3
+ "version": "1.0.4",
4
4
  "description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
5
5
  "main": "index.js",
6
6
  "author": "",