@commet/better-auth 1.3.2 → 1.3.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.
package/dist/index.cjs CHANGED
@@ -308,185 +308,33 @@ var commetClient = () => {
308
308
  };
309
309
  };
310
310
 
311
- // src/plugins/portal.ts
312
- var import_api2 = require("better-auth/api");
313
- var import_plugins = require("better-auth/plugins");
314
- var portal = ({ returnUrl } = {}) => (commet2) => {
315
- return {
316
- portal: (0, import_plugins.createAuthEndpoint)(
317
- "/commet/portal",
318
- {
319
- method: "GET",
320
- use: [import_api2.sessionMiddleware]
321
- },
322
- async (ctx) => {
323
- const userId = ctx.context.session?.user.id;
324
- if (!userId) {
325
- throw new import_api2.APIError("UNAUTHORIZED", {
326
- message: "You must be logged in to access the customer portal"
327
- });
328
- }
329
- try {
330
- const portalAccess = await commet2.portal.getUrl({
331
- externalId: userId
332
- });
333
- if (!portalAccess.success || !portalAccess.data) {
334
- throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
335
- message: portalAccess.message || "Failed to generate portal URL"
336
- });
337
- }
338
- let portalUrl = portalAccess.data.portalUrl;
339
- if (returnUrl) {
340
- const url = new URL(portalUrl);
341
- url.searchParams.set("return_url", returnUrl);
342
- portalUrl = url.toString();
343
- }
344
- return ctx.json({
345
- url: portalUrl,
346
- redirect: true
347
- });
348
- } catch (e) {
349
- if (e instanceof import_api2.APIError) {
350
- throw e;
351
- }
352
- if (e instanceof Error) {
353
- ctx.context.logger.error(
354
- `Commet portal access failed: ${e.message}`
355
- );
356
- }
357
- throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
358
- message: "Failed to access customer portal"
359
- });
360
- }
361
- }
362
- )
363
- };
364
- };
365
-
366
- // src/plugins/subscriptions.ts
367
- var import_api3 = require("better-auth/api");
368
- var import_plugins2 = require("better-auth/plugins");
369
- var import_zod = require("zod");
370
- var CancelSchema = import_zod.z.object({
371
- reason: import_zod.z.string().optional(),
372
- immediate: import_zod.z.boolean().optional()
373
- });
374
- var subscriptions = (config = {}) => (commet2) => {
375
- return {
376
- getSubscription: (0, import_plugins2.createAuthEndpoint)(
377
- "/commet/subscription",
378
- {
379
- method: "GET",
380
- use: [import_api3.sessionMiddleware]
381
- },
382
- async (ctx) => {
383
- const userId = ctx.context.session?.user.id;
384
- if (!userId) {
385
- throw new import_api3.APIError("UNAUTHORIZED", {
386
- message: "You must be logged in to view subscription"
387
- });
388
- }
389
- try {
390
- const subscription = await commet2.subscriptions.get(userId);
391
- if (!subscription.success) {
392
- throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
393
- message: subscription.message || "Failed to retrieve subscription"
394
- });
395
- }
396
- return ctx.json(subscription.data ?? null);
397
- } catch (e) {
398
- if (e instanceof import_api3.APIError) {
399
- throw e;
400
- }
401
- if (e instanceof Error) {
402
- ctx.context.logger.error(
403
- `Commet subscription get failed: ${e.message}`
404
- );
405
- }
406
- throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
407
- message: "Failed to retrieve subscription"
408
- });
409
- }
410
- }
411
- ),
412
- cancelSubscription: (0, import_plugins2.createAuthEndpoint)(
413
- "/commet/subscription/cancel",
414
- {
415
- method: "POST",
416
- body: CancelSchema.optional(),
417
- use: [import_api3.sessionMiddleware]
418
- },
419
- async (ctx) => {
420
- const userId = ctx.context.session?.user.id;
421
- if (!userId) {
422
- throw new import_api3.APIError("UNAUTHORIZED", {
423
- message: "You must be logged in to cancel subscription"
424
- });
425
- }
426
- try {
427
- const currentSub = await commet2.subscriptions.get(userId);
428
- if (!currentSub.success || !currentSub.data) {
429
- throw new import_api3.APIError("BAD_REQUEST", {
430
- message: "No active subscription found"
431
- });
432
- }
433
- const result = await commet2.subscriptions.cancel({
434
- subscriptionId: currentSub.data.id,
435
- reason: ctx.body?.reason,
436
- immediate: ctx.body?.immediate
437
- });
438
- if (!result.success) {
439
- throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
440
- message: result.message || "Failed to cancel subscription"
441
- });
442
- }
443
- return ctx.json(result.data ?? null);
444
- } catch (e) {
445
- if (e instanceof import_api3.APIError) {
446
- throw e;
447
- }
448
- if (e instanceof Error) {
449
- ctx.context.logger.error(
450
- `Commet subscription cancel failed: ${e.message}`
451
- );
452
- }
453
- throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
454
- message: "Failed to cancel subscription"
455
- });
456
- }
457
- }
458
- )
459
- };
460
- };
461
-
462
311
  // src/plugins/features.ts
463
- var import_api4 = require("better-auth/api");
464
- var import_plugins3 = require("better-auth/plugins");
312
+ var import_api2 = require("better-auth/api");
465
313
  var features = (_config = {}) => (commet2) => {
466
314
  return {
467
- listFeatures: (0, import_plugins3.createAuthEndpoint)(
315
+ listFeatures: (0, import_api2.createAuthEndpoint)(
468
316
  "/commet/features",
469
317
  {
470
318
  method: "GET",
471
- use: [import_api4.sessionMiddleware]
319
+ use: [import_api2.sessionMiddleware]
472
320
  },
473
321
  async (ctx) => {
474
322
  const userId = ctx.context.session?.user.id;
475
323
  if (!userId) {
476
- throw new import_api4.APIError("UNAUTHORIZED", {
324
+ throw new import_api2.APIError("UNAUTHORIZED", {
477
325
  message: "You must be logged in to view features"
478
326
  });
479
327
  }
480
328
  try {
481
329
  const result = await commet2.features.list(userId);
482
330
  if (!result.success) {
483
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
331
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
484
332
  message: result.message || "Failed to list features"
485
333
  });
486
334
  }
487
335
  return ctx.json(result.data ?? null);
488
336
  } catch (e) {
489
- if (e instanceof import_api4.APIError) {
337
+ if (e instanceof import_api2.APIError) {
490
338
  throw e;
491
339
  }
492
340
  if (e instanceof Error) {
@@ -494,28 +342,28 @@ var features = (_config = {}) => (commet2) => {
494
342
  `Commet features list failed: ${e.message}`
495
343
  );
496
344
  }
497
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
345
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
498
346
  message: "Failed to list features"
499
347
  });
500
348
  }
501
349
  }
502
350
  ),
503
- getFeature: (0, import_plugins3.createAuthEndpoint)(
351
+ getFeature: (0, import_api2.createAuthEndpoint)(
504
352
  "/commet/features/:code",
505
353
  {
506
354
  method: "GET",
507
- use: [import_api4.sessionMiddleware]
355
+ use: [import_api2.sessionMiddleware]
508
356
  },
509
357
  async (ctx) => {
510
358
  const userId = ctx.context.session?.user.id;
511
359
  const code = ctx.params?.code;
512
360
  if (!userId) {
513
- throw new import_api4.APIError("UNAUTHORIZED", {
361
+ throw new import_api2.APIError("UNAUTHORIZED", {
514
362
  message: "You must be logged in to view feature"
515
363
  });
516
364
  }
517
365
  if (!code) {
518
- throw new import_api4.APIError("BAD_REQUEST", {
366
+ throw new import_api2.APIError("BAD_REQUEST", {
519
367
  message: "Feature code is required"
520
368
  });
521
369
  }
@@ -525,13 +373,13 @@ var features = (_config = {}) => (commet2) => {
525
373
  code
526
374
  });
527
375
  if (!result.success) {
528
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
376
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
529
377
  message: result.message || "Failed to get feature"
530
378
  });
531
379
  }
532
380
  return ctx.json(result.data ?? null);
533
381
  } catch (e) {
534
- if (e instanceof import_api4.APIError) {
382
+ if (e instanceof import_api2.APIError) {
535
383
  throw e;
536
384
  }
537
385
  if (e instanceof Error) {
@@ -539,28 +387,28 @@ var features = (_config = {}) => (commet2) => {
539
387
  `Commet feature get failed: ${e.message}`
540
388
  );
541
389
  }
542
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
390
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
543
391
  message: "Failed to get feature"
544
392
  });
545
393
  }
546
394
  }
547
395
  ),
548
- checkFeature: (0, import_plugins3.createAuthEndpoint)(
396
+ checkFeature: (0, import_api2.createAuthEndpoint)(
549
397
  "/commet/features/:code/check",
550
398
  {
551
399
  method: "GET",
552
- use: [import_api4.sessionMiddleware]
400
+ use: [import_api2.sessionMiddleware]
553
401
  },
554
402
  async (ctx) => {
555
403
  const userId = ctx.context.session?.user.id;
556
404
  const code = ctx.params?.code;
557
405
  if (!userId) {
558
- throw new import_api4.APIError("UNAUTHORIZED", {
406
+ throw new import_api2.APIError("UNAUTHORIZED", {
559
407
  message: "You must be logged in to check feature"
560
408
  });
561
409
  }
562
410
  if (!code) {
563
- throw new import_api4.APIError("BAD_REQUEST", {
411
+ throw new import_api2.APIError("BAD_REQUEST", {
564
412
  message: "Feature code is required"
565
413
  });
566
414
  }
@@ -570,13 +418,13 @@ var features = (_config = {}) => (commet2) => {
570
418
  code
571
419
  });
572
420
  if (!result.success) {
573
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
421
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
574
422
  message: result.message || "Failed to check feature"
575
423
  });
576
424
  }
577
425
  return ctx.json(result.data ?? null);
578
426
  } catch (e) {
579
- if (e instanceof import_api4.APIError) {
427
+ if (e instanceof import_api2.APIError) {
580
428
  throw e;
581
429
  }
582
430
  if (e instanceof Error) {
@@ -584,28 +432,28 @@ var features = (_config = {}) => (commet2) => {
584
432
  `Commet feature check failed: ${e.message}`
585
433
  );
586
434
  }
587
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
435
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
588
436
  message: "Failed to check feature"
589
437
  });
590
438
  }
591
439
  }
592
440
  ),
593
- canUseFeature: (0, import_plugins3.createAuthEndpoint)(
441
+ canUseFeature: (0, import_api2.createAuthEndpoint)(
594
442
  "/commet/features/:code/can-use",
595
443
  {
596
444
  method: "GET",
597
- use: [import_api4.sessionMiddleware]
445
+ use: [import_api2.sessionMiddleware]
598
446
  },
599
447
  async (ctx) => {
600
448
  const userId = ctx.context.session?.user.id;
601
449
  const code = ctx.params?.code;
602
450
  if (!userId) {
603
- throw new import_api4.APIError("UNAUTHORIZED", {
451
+ throw new import_api2.APIError("UNAUTHORIZED", {
604
452
  message: "You must be logged in to check feature usage"
605
453
  });
606
454
  }
607
455
  if (!code) {
608
- throw new import_api4.APIError("BAD_REQUEST", {
456
+ throw new import_api2.APIError("BAD_REQUEST", {
609
457
  message: "Feature code is required"
610
458
  });
611
459
  }
@@ -615,13 +463,13 @@ var features = (_config = {}) => (commet2) => {
615
463
  code
616
464
  });
617
465
  if (!result.success) {
618
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
466
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
619
467
  message: result.message || "Failed to check feature usage"
620
468
  });
621
469
  }
622
470
  return ctx.json(result.data ?? null);
623
471
  } catch (e) {
624
- if (e instanceof import_api4.APIError) {
472
+ if (e instanceof import_api2.APIError) {
625
473
  throw e;
626
474
  }
627
475
  if (e instanceof Error) {
@@ -629,7 +477,7 @@ var features = (_config = {}) => (commet2) => {
629
477
  `Commet feature canUse failed: ${e.message}`
630
478
  );
631
479
  }
632
- throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
480
+ throw new import_api2.APIError("INTERNAL_SERVER_ERROR", {
633
481
  message: "Failed to check feature usage"
634
482
  });
635
483
  }
@@ -638,60 +486,53 @@ var features = (_config = {}) => (commet2) => {
638
486
  };
639
487
  };
640
488
 
641
- // src/plugins/usage.ts
642
- var import_api5 = require("better-auth/api");
643
- var import_plugins4 = require("better-auth/plugins");
644
- var import_zod2 = require("zod");
645
- var TrackEventSchema = import_zod2.z.object({
646
- feature: import_zod2.z.string(),
647
- value: import_zod2.z.number().optional(),
648
- idempotencyKey: import_zod2.z.string().optional(),
649
- properties: import_zod2.z.record(import_zod2.z.string(), import_zod2.z.string()).optional()
650
- });
651
- var usage = (_config = {}) => (commet2) => {
489
+ // src/plugins/portal.ts
490
+ var import_api3 = require("better-auth/api");
491
+ var portal = ({ returnUrl } = {}) => (commet2) => {
652
492
  return {
653
- trackUsage: (0, import_plugins4.createAuthEndpoint)(
654
- "/commet/usage/track",
493
+ portal: (0, import_api3.createAuthEndpoint)(
494
+ "/commet/portal",
655
495
  {
656
- method: "POST",
657
- body: TrackEventSchema,
658
- use: [import_api5.sessionMiddleware]
496
+ method: "GET",
497
+ use: [import_api3.sessionMiddleware]
659
498
  },
660
499
  async (ctx) => {
661
500
  const userId = ctx.context.session?.user.id;
662
501
  if (!userId) {
663
- throw new import_api5.APIError("UNAUTHORIZED", {
664
- message: "You must be logged in to track usage"
502
+ throw new import_api3.APIError("UNAUTHORIZED", {
503
+ message: "You must be logged in to access the customer portal"
665
504
  });
666
505
  }
667
506
  try {
668
- const result = await commet2.usage.track(
669
- {
670
- externalId: userId,
671
- feature: ctx.body.feature,
672
- value: ctx.body.value,
673
- idempotencyKey: ctx.body.idempotencyKey,
674
- properties: ctx.body.properties
675
- },
676
- {}
677
- );
678
- if (!result.success) {
679
- throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
680
- message: result.message || "Failed to track usage"
507
+ const portalAccess = await commet2.portal.getUrl({
508
+ externalId: userId
509
+ });
510
+ if (!portalAccess.success || !portalAccess.data) {
511
+ throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
512
+ message: portalAccess.message || "Failed to generate portal URL"
681
513
  });
682
514
  }
683
- return ctx.json(result.data ?? null);
515
+ let portalUrl = portalAccess.data.portalUrl;
516
+ if (returnUrl) {
517
+ const url = new URL(portalUrl);
518
+ url.searchParams.set("return_url", returnUrl);
519
+ portalUrl = url.toString();
520
+ }
521
+ return ctx.json({
522
+ url: portalUrl,
523
+ redirect: true
524
+ });
684
525
  } catch (e) {
685
- if (e instanceof import_api5.APIError) {
526
+ if (e instanceof import_api3.APIError) {
686
527
  throw e;
687
528
  }
688
529
  if (e instanceof Error) {
689
530
  ctx.context.logger.error(
690
- `Commet usage track failed: ${e.message}`
531
+ `Commet portal access failed: ${e.message}`
691
532
  );
692
533
  }
693
- throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
694
- message: "Failed to track usage"
534
+ throw new import_api3.APIError("INTERNAL_SERVER_ERROR", {
535
+ message: "Failed to access customer portal"
695
536
  });
696
537
  }
697
538
  }
@@ -700,28 +541,27 @@ var usage = (_config = {}) => (commet2) => {
700
541
  };
701
542
 
702
543
  // src/plugins/seats.ts
703
- var import_api6 = require("better-auth/api");
704
- var import_plugins5 = require("better-auth/plugins");
705
- var import_zod3 = require("zod");
706
- var SeatOperationSchema = import_zod3.z.object({
707
- seatType: import_zod3.z.string(),
708
- count: import_zod3.z.number().min(1)
544
+ var import_api4 = require("better-auth/api");
545
+ var import_zod = require("zod");
546
+ var SeatOperationSchema = import_zod.z.object({
547
+ seatType: import_zod.z.string(),
548
+ count: import_zod.z.number().min(1)
709
549
  });
710
- var SetAllSeatsSchema = import_zod3.z.object({
711
- seats: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.number())
550
+ var SetAllSeatsSchema = import_zod.z.object({
551
+ seats: import_zod.z.record(import_zod.z.string(), import_zod.z.number())
712
552
  });
713
553
  var seats = (_config = {}) => (commet2) => {
714
554
  return {
715
- listSeats: (0, import_plugins5.createAuthEndpoint)(
555
+ listSeats: (0, import_api4.createAuthEndpoint)(
716
556
  "/commet/seats",
717
557
  {
718
558
  method: "GET",
719
- use: [import_api6.sessionMiddleware]
559
+ use: [import_api4.sessionMiddleware]
720
560
  },
721
561
  async (ctx) => {
722
562
  const userId = ctx.context.session?.user.id;
723
563
  if (!userId) {
724
- throw new import_api6.APIError("UNAUTHORIZED", {
564
+ throw new import_api4.APIError("UNAUTHORIZED", {
725
565
  message: "You must be logged in to view seats"
726
566
  });
727
567
  }
@@ -730,13 +570,13 @@ var seats = (_config = {}) => (commet2) => {
730
570
  externalId: userId
731
571
  });
732
572
  if (!result.success) {
733
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
573
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
734
574
  message: result.message || "Failed to list seats"
735
575
  });
736
576
  }
737
577
  return ctx.json(result.data ?? null);
738
578
  } catch (e) {
739
- if (e instanceof import_api6.APIError) {
579
+ if (e instanceof import_api4.APIError) {
740
580
  throw e;
741
581
  }
742
582
  if (e instanceof Error) {
@@ -744,23 +584,23 @@ var seats = (_config = {}) => (commet2) => {
744
584
  `Commet seats list failed: ${e.message}`
745
585
  );
746
586
  }
747
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
587
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
748
588
  message: "Failed to list seats"
749
589
  });
750
590
  }
751
591
  }
752
592
  ),
753
- addSeats: (0, import_plugins5.createAuthEndpoint)(
593
+ addSeats: (0, import_api4.createAuthEndpoint)(
754
594
  "/commet/seats/add",
755
595
  {
756
596
  method: "POST",
757
597
  body: SeatOperationSchema,
758
- use: [import_api6.sessionMiddleware]
598
+ use: [import_api4.sessionMiddleware]
759
599
  },
760
600
  async (ctx) => {
761
601
  const userId = ctx.context.session?.user.id;
762
602
  if (!userId) {
763
- throw new import_api6.APIError("UNAUTHORIZED", {
603
+ throw new import_api4.APIError("UNAUTHORIZED", {
764
604
  message: "You must be logged in to add seats"
765
605
  });
766
606
  }
@@ -774,35 +614,35 @@ var seats = (_config = {}) => (commet2) => {
774
614
  {}
775
615
  );
776
616
  if (!result.success) {
777
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
617
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
778
618
  message: result.message || "Failed to add seats"
779
619
  });
780
620
  }
781
621
  return ctx.json(result.data ?? null);
782
622
  } catch (e) {
783
- if (e instanceof import_api6.APIError) {
623
+ if (e instanceof import_api4.APIError) {
784
624
  throw e;
785
625
  }
786
626
  if (e instanceof Error) {
787
627
  ctx.context.logger.error(`Commet seats add failed: ${e.message}`);
788
628
  }
789
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
629
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
790
630
  message: "Failed to add seats"
791
631
  });
792
632
  }
793
633
  }
794
634
  ),
795
- removeSeats: (0, import_plugins5.createAuthEndpoint)(
635
+ removeSeats: (0, import_api4.createAuthEndpoint)(
796
636
  "/commet/seats/remove",
797
637
  {
798
638
  method: "POST",
799
639
  body: SeatOperationSchema,
800
- use: [import_api6.sessionMiddleware]
640
+ use: [import_api4.sessionMiddleware]
801
641
  },
802
642
  async (ctx) => {
803
643
  const userId = ctx.context.session?.user.id;
804
644
  if (!userId) {
805
- throw new import_api6.APIError("UNAUTHORIZED", {
645
+ throw new import_api4.APIError("UNAUTHORIZED", {
806
646
  message: "You must be logged in to remove seats"
807
647
  });
808
648
  }
@@ -816,13 +656,13 @@ var seats = (_config = {}) => (commet2) => {
816
656
  {}
817
657
  );
818
658
  if (!result.success) {
819
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
659
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
820
660
  message: result.message || "Failed to remove seats"
821
661
  });
822
662
  }
823
663
  return ctx.json(result.data ?? null);
824
664
  } catch (e) {
825
- if (e instanceof import_api6.APIError) {
665
+ if (e instanceof import_api4.APIError) {
826
666
  throw e;
827
667
  }
828
668
  if (e instanceof Error) {
@@ -830,23 +670,23 @@ var seats = (_config = {}) => (commet2) => {
830
670
  `Commet seats remove failed: ${e.message}`
831
671
  );
832
672
  }
833
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
673
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
834
674
  message: "Failed to remove seats"
835
675
  });
836
676
  }
837
677
  }
838
678
  ),
839
- setSeats: (0, import_plugins5.createAuthEndpoint)(
679
+ setSeats: (0, import_api4.createAuthEndpoint)(
840
680
  "/commet/seats/set",
841
681
  {
842
682
  method: "POST",
843
683
  body: SeatOperationSchema,
844
- use: [import_api6.sessionMiddleware]
684
+ use: [import_api4.sessionMiddleware]
845
685
  },
846
686
  async (ctx) => {
847
687
  const userId = ctx.context.session?.user.id;
848
688
  if (!userId) {
849
- throw new import_api6.APIError("UNAUTHORIZED", {
689
+ throw new import_api4.APIError("UNAUTHORIZED", {
850
690
  message: "You must be logged in to set seats"
851
691
  });
852
692
  }
@@ -860,35 +700,35 @@ var seats = (_config = {}) => (commet2) => {
860
700
  {}
861
701
  );
862
702
  if (!result.success) {
863
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
703
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
864
704
  message: result.message || "Failed to set seats"
865
705
  });
866
706
  }
867
707
  return ctx.json(result.data ?? null);
868
708
  } catch (e) {
869
- if (e instanceof import_api6.APIError) {
709
+ if (e instanceof import_api4.APIError) {
870
710
  throw e;
871
711
  }
872
712
  if (e instanceof Error) {
873
713
  ctx.context.logger.error(`Commet seats set failed: ${e.message}`);
874
714
  }
875
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
715
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
876
716
  message: "Failed to set seats"
877
717
  });
878
718
  }
879
719
  }
880
720
  ),
881
- setAllSeats: (0, import_plugins5.createAuthEndpoint)(
721
+ setAllSeats: (0, import_api4.createAuthEndpoint)(
882
722
  "/commet/seats/set-all",
883
723
  {
884
724
  method: "POST",
885
725
  body: SetAllSeatsSchema,
886
- use: [import_api6.sessionMiddleware]
726
+ use: [import_api4.sessionMiddleware]
887
727
  },
888
728
  async (ctx) => {
889
729
  const userId = ctx.context.session?.user.id;
890
730
  if (!userId) {
891
- throw new import_api6.APIError("UNAUTHORIZED", {
731
+ throw new import_api4.APIError("UNAUTHORIZED", {
892
732
  message: "You must be logged in to set seats"
893
733
  });
894
734
  }
@@ -901,13 +741,13 @@ var seats = (_config = {}) => (commet2) => {
901
741
  {}
902
742
  );
903
743
  if (!result.success) {
904
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
744
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
905
745
  message: result.message || "Failed to set all seats"
906
746
  });
907
747
  }
908
748
  return ctx.json(result.data ?? null);
909
749
  } catch (e) {
910
- if (e instanceof import_api6.APIError) {
750
+ if (e instanceof import_api4.APIError) {
911
751
  throw e;
912
752
  }
913
753
  if (e instanceof Error) {
@@ -915,7 +755,7 @@ var seats = (_config = {}) => (commet2) => {
915
755
  `Commet seats set-all failed: ${e.message}`
916
756
  );
917
757
  }
918
- throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
758
+ throw new import_api4.APIError("INTERNAL_SERVER_ERROR", {
919
759
  message: "Failed to set all seats"
920
760
  });
921
761
  }
@@ -924,6 +764,161 @@ var seats = (_config = {}) => (commet2) => {
924
764
  };
925
765
  };
926
766
 
767
+ // src/plugins/subscriptions.ts
768
+ var import_api5 = require("better-auth/api");
769
+ var import_zod2 = require("zod");
770
+ var CancelSchema = import_zod2.z.object({
771
+ reason: import_zod2.z.string().optional(),
772
+ immediate: import_zod2.z.boolean().optional()
773
+ });
774
+ var subscriptions = (_config = {}) => (commet2) => {
775
+ return {
776
+ getSubscription: (0, import_api5.createAuthEndpoint)(
777
+ "/commet/subscription",
778
+ {
779
+ method: "GET",
780
+ use: [import_api5.sessionMiddleware]
781
+ },
782
+ async (ctx) => {
783
+ const userId = ctx.context.session?.user.id;
784
+ if (!userId) {
785
+ throw new import_api5.APIError("UNAUTHORIZED", {
786
+ message: "You must be logged in to view subscription"
787
+ });
788
+ }
789
+ try {
790
+ const subscription = await commet2.subscriptions.get(userId);
791
+ if (!subscription.success) {
792
+ throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
793
+ message: subscription.message || "Failed to retrieve subscription"
794
+ });
795
+ }
796
+ return ctx.json(subscription.data ?? null);
797
+ } catch (e) {
798
+ if (e instanceof import_api5.APIError) {
799
+ throw e;
800
+ }
801
+ if (e instanceof Error) {
802
+ ctx.context.logger.error(
803
+ `Commet subscription get failed: ${e.message}`
804
+ );
805
+ }
806
+ throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
807
+ message: "Failed to retrieve subscription"
808
+ });
809
+ }
810
+ }
811
+ ),
812
+ cancelSubscription: (0, import_api5.createAuthEndpoint)(
813
+ "/commet/subscription/cancel",
814
+ {
815
+ method: "POST",
816
+ body: CancelSchema.optional(),
817
+ use: [import_api5.sessionMiddleware]
818
+ },
819
+ async (ctx) => {
820
+ const userId = ctx.context.session?.user.id;
821
+ if (!userId) {
822
+ throw new import_api5.APIError("UNAUTHORIZED", {
823
+ message: "You must be logged in to cancel subscription"
824
+ });
825
+ }
826
+ try {
827
+ const currentSub = await commet2.subscriptions.get(userId);
828
+ if (!currentSub.success || !currentSub.data) {
829
+ throw new import_api5.APIError("BAD_REQUEST", {
830
+ message: "No active subscription found"
831
+ });
832
+ }
833
+ const result = await commet2.subscriptions.cancel({
834
+ subscriptionId: currentSub.data.id,
835
+ reason: ctx.body?.reason,
836
+ immediate: ctx.body?.immediate
837
+ });
838
+ if (!result.success) {
839
+ throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
840
+ message: result.message || "Failed to cancel subscription"
841
+ });
842
+ }
843
+ return ctx.json(result.data ?? null);
844
+ } catch (e) {
845
+ if (e instanceof import_api5.APIError) {
846
+ throw e;
847
+ }
848
+ if (e instanceof Error) {
849
+ ctx.context.logger.error(
850
+ `Commet subscription cancel failed: ${e.message}`
851
+ );
852
+ }
853
+ throw new import_api5.APIError("INTERNAL_SERVER_ERROR", {
854
+ message: "Failed to cancel subscription"
855
+ });
856
+ }
857
+ }
858
+ )
859
+ };
860
+ };
861
+
862
+ // src/plugins/usage.ts
863
+ var import_api6 = require("better-auth/api");
864
+ var import_zod3 = require("zod");
865
+ var TrackEventSchema = import_zod3.z.object({
866
+ feature: import_zod3.z.string(),
867
+ value: import_zod3.z.number().optional(),
868
+ idempotencyKey: import_zod3.z.string().optional(),
869
+ properties: import_zod3.z.record(import_zod3.z.string(), import_zod3.z.string()).optional()
870
+ });
871
+ var usage = (_config = {}) => (commet2) => {
872
+ return {
873
+ trackUsage: (0, import_api6.createAuthEndpoint)(
874
+ "/commet/usage/track",
875
+ {
876
+ method: "POST",
877
+ body: TrackEventSchema,
878
+ use: [import_api6.sessionMiddleware]
879
+ },
880
+ async (ctx) => {
881
+ const userId = ctx.context.session?.user.id;
882
+ if (!userId) {
883
+ throw new import_api6.APIError("UNAUTHORIZED", {
884
+ message: "You must be logged in to track usage"
885
+ });
886
+ }
887
+ try {
888
+ const result = await commet2.usage.track(
889
+ {
890
+ externalId: userId,
891
+ feature: ctx.body.feature,
892
+ value: ctx.body.value,
893
+ idempotencyKey: ctx.body.idempotencyKey,
894
+ properties: ctx.body.properties
895
+ },
896
+ {}
897
+ );
898
+ if (!result.success) {
899
+ throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
900
+ message: result.message || "Failed to track usage"
901
+ });
902
+ }
903
+ return ctx.json(result.data ?? null);
904
+ } catch (e) {
905
+ if (e instanceof import_api6.APIError) {
906
+ throw e;
907
+ }
908
+ if (e instanceof Error) {
909
+ ctx.context.logger.error(
910
+ `Commet usage track failed: ${e.message}`
911
+ );
912
+ }
913
+ throw new import_api6.APIError("INTERNAL_SERVER_ERROR", {
914
+ message: "Failed to track usage"
915
+ });
916
+ }
917
+ }
918
+ )
919
+ };
920
+ };
921
+
927
922
  // src/plugins/webhooks.ts
928
923
  var import_api7 = require("better-auth/api");
929
924
  var EVENT_HANDLER_MAP = {