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