create-db 1.0.4-pr48-DC-4894-posthog-fix-17271643844.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 +34 -22
  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,16 +9,7 @@ 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
-
13
- async function sendAnalyticsToWorker(eventName, properties = {}) {
14
- try {
15
- await fetch(`${CREATE_DB_WORKER_URL}/analytics`, {
16
- method: "POST",
17
- headers: { "Content-Type": "application/json" },
18
- body: JSON.stringify({ eventName, properties }),
19
- });
20
- } catch (error) {}
21
- }
12
+ import { analytics } from "./analytics.js";
22
13
 
23
14
  const CREATE_DB_WORKER_URL =
24
15
  process.env.CREATE_DB_WORKER_URL || "https://create-db-temp.prisma.io";
@@ -369,12 +360,14 @@ async function promptForRegion(defaultRegion, userAgent) {
369
360
  }
370
361
 
371
362
  try {
372
- await sendAnalyticsToWorker("create_db:region_selected", {
363
+ const analyticsProps = {
373
364
  command: CLI_NAME,
374
365
  region: region,
375
366
  "selection-method": "interactive",
376
367
  "user-agent": userAgent,
377
- });
368
+ };
369
+
370
+ await analytics.capture("create_db:region_selected", analyticsProps);
378
371
  } catch (error) {}
379
372
 
380
373
  return region;
@@ -414,13 +407,18 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
414
407
  }
415
408
 
416
409
  try {
417
- await sendAnalyticsToWorker("create_db:database_creation_failed", {
410
+ const analyticsProps = {
418
411
  command: CLI_NAME,
419
412
  region: region,
420
413
  "error-type": "rate_limit",
421
414
  "status-code": 429,
422
415
  "user-agent": userAgent,
423
- });
416
+ };
417
+
418
+ await analytics.capture(
419
+ "create_db:database_creation_failed",
420
+ analyticsProps
421
+ );
424
422
  } catch (error) {}
425
423
 
426
424
  process.exit(1);
@@ -444,13 +442,18 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
444
442
  s.stop("Unexpected response from create service.");
445
443
  }
446
444
  try {
447
- await sendAnalyticsToWorker("create_db:database_creation_failed", {
445
+ const analyticsProps = {
448
446
  command: CLI_NAME,
449
447
  region,
450
448
  "error-type": "invalid_json",
451
449
  "status-code": resp.status,
452
450
  "user-agent": userAgent,
453
- });
451
+ };
452
+
453
+ await analytics.capture(
454
+ "create_db:database_creation_failed",
455
+ analyticsProps
456
+ );
454
457
  } catch (error) {}
455
458
  process.exit(1);
456
459
  }
@@ -515,13 +518,18 @@ async function createDatabase(name, region, userAgent, returnJson = false) {
515
518
  }
516
519
 
517
520
  try {
518
- await sendAnalyticsToWorker("create_db:database_creation_failed", {
521
+ const analyticsProps = {
519
522
  command: CLI_NAME,
520
523
  region: region,
521
524
  "error-type": "api_error",
522
525
  "error-message": result.error.message,
523
526
  "user-agent": userAgent,
524
- });
527
+ };
528
+
529
+ await analytics.capture(
530
+ "create_db:database_creation_failed",
531
+ analyticsProps
532
+ );
525
533
  } catch (error) {}
526
534
  process.exit(1);
527
535
  }
@@ -588,7 +596,7 @@ async function main() {
588
596
  }
589
597
 
590
598
  try {
591
- await sendAnalyticsToWorker("create_db:cli_command_ran", {
599
+ const analyticsProps = {
592
600
  command: CLI_NAME,
593
601
  "full-command": `${CLI_NAME} ${rawArgs.join(" ")}`.trim(),
594
602
  "has-region-flag":
@@ -603,7 +611,9 @@ async function main() {
603
611
  platform: process.platform,
604
612
  arch: process.arch,
605
613
  "user-agent": userAgent,
606
- });
614
+ };
615
+
616
+ await analytics.capture("create_db:cli_command_ran", analyticsProps);
607
617
  } catch (error) {
608
618
  console.error("Error:", error.message);
609
619
  }
@@ -630,12 +640,14 @@ async function main() {
630
640
  region = flags.region;
631
641
 
632
642
  try {
633
- await sendAnalyticsToWorker("create_db:region_selected", {
643
+ const analyticsProps = {
634
644
  command: CLI_NAME,
635
645
  region: region,
636
646
  "selection-method": "flag",
637
647
  "user-agent": userAgent,
638
- });
648
+ };
649
+
650
+ await analytics.capture("create_db:region_selected", analyticsProps);
639
651
  } catch (error) {}
640
652
  }
641
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-17271643844.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": "",