@commet/better-auth 1.0.0

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/client.js ADDED
@@ -0,0 +1,172 @@
1
+ // src/client.ts
2
+ var commetClient = () => {
3
+ return {
4
+ id: "commet-client",
5
+ $InferServerPlugin: {},
6
+ getActions: ($fetch) => {
7
+ return {
8
+ // Customer Portal
9
+ customer: {
10
+ /**
11
+ * Redirect to the Commet customer portal
12
+ */
13
+ portal: async (fetchOptions) => {
14
+ const res = await $fetch("/commet/portal", {
15
+ method: "GET",
16
+ ...fetchOptions
17
+ });
18
+ if (res.error) {
19
+ throw new Error(res.error.message);
20
+ }
21
+ const data = res.data;
22
+ if (data.redirect && typeof window !== "undefined") {
23
+ window.location.href = data.url;
24
+ }
25
+ return data;
26
+ }
27
+ },
28
+ // Subscription management
29
+ subscription: {
30
+ /**
31
+ * Get the current subscription for the authenticated user
32
+ */
33
+ get: async (fetchOptions) => {
34
+ return $fetch("/commet/subscription", {
35
+ method: "GET",
36
+ ...fetchOptions
37
+ });
38
+ },
39
+ /**
40
+ * Change the subscription plan (upgrade/downgrade)
41
+ */
42
+ changePlan: async (data, fetchOptions) => {
43
+ return $fetch("/commet/subscription/change-plan", {
44
+ method: "POST",
45
+ body: data,
46
+ ...fetchOptions
47
+ });
48
+ },
49
+ /**
50
+ * Cancel the subscription
51
+ */
52
+ cancel: async (data, fetchOptions) => {
53
+ return $fetch("/commet/subscription/cancel", {
54
+ method: "POST",
55
+ body: data ?? {},
56
+ ...fetchOptions
57
+ });
58
+ }
59
+ },
60
+ // Feature access
61
+ features: {
62
+ /**
63
+ * List all features for the authenticated user
64
+ */
65
+ list: async (fetchOptions) => {
66
+ return $fetch("/commet/features", {
67
+ method: "GET",
68
+ ...fetchOptions
69
+ });
70
+ },
71
+ /**
72
+ * Get a specific feature's access/usage
73
+ */
74
+ get: async (data, fetchOptions) => {
75
+ return $fetch(`/commet/features/${data.code}`, {
76
+ method: "GET",
77
+ ...fetchOptions
78
+ });
79
+ },
80
+ /**
81
+ * Check if a feature is enabled (boolean check)
82
+ */
83
+ check: async (data, fetchOptions) => {
84
+ return $fetch(`/features/${data.code}/check`, {
85
+ method: "GET",
86
+ ...fetchOptions
87
+ });
88
+ },
89
+ /**
90
+ * Check if user can use one more unit of a feature
91
+ * Returns { allowed: boolean, willBeCharged: boolean }
92
+ */
93
+ canUse: async (data, fetchOptions) => {
94
+ return $fetch(`/features/${data.code}/can-use`, {
95
+ method: "GET",
96
+ ...fetchOptions
97
+ });
98
+ }
99
+ },
100
+ // Usage tracking
101
+ usage: {
102
+ /**
103
+ * Track a usage event for the authenticated user
104
+ */
105
+ track: async (data, fetchOptions) => {
106
+ return $fetch("/commet/usage/track", {
107
+ method: "POST",
108
+ body: data,
109
+ ...fetchOptions
110
+ });
111
+ }
112
+ },
113
+ // Seat management
114
+ seats: {
115
+ /**
116
+ * List all seat balances for the authenticated user
117
+ */
118
+ list: async (fetchOptions) => {
119
+ return $fetch("/commet/seats", {
120
+ method: "GET",
121
+ ...fetchOptions
122
+ });
123
+ },
124
+ /**
125
+ * Add seats of a specific type
126
+ */
127
+ add: async (data, fetchOptions) => {
128
+ return $fetch("/commet/seats/add", {
129
+ method: "POST",
130
+ body: data,
131
+ ...fetchOptions
132
+ });
133
+ },
134
+ /**
135
+ * Remove seats of a specific type
136
+ */
137
+ remove: async (data, fetchOptions) => {
138
+ return $fetch("/commet/seats/remove", {
139
+ method: "POST",
140
+ body: data,
141
+ ...fetchOptions
142
+ });
143
+ },
144
+ /**
145
+ * Set seats to a specific count
146
+ */
147
+ set: async (data, fetchOptions) => {
148
+ return $fetch("/commet/seats/set", {
149
+ method: "POST",
150
+ body: data,
151
+ ...fetchOptions
152
+ });
153
+ },
154
+ /**
155
+ * Set all seat types at once
156
+ */
157
+ setAll: async (data, fetchOptions) => {
158
+ return $fetch("/commet/seats/set-all", {
159
+ method: "POST",
160
+ body: data,
161
+ ...fetchOptions
162
+ });
163
+ }
164
+ }
165
+ };
166
+ }
167
+ };
168
+ };
169
+ export {
170
+ commetClient
171
+ };
172
+ //# sourceMappingURL=client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { BetterAuthClientPlugin } from \"better-auth\";\nimport type { BetterFetchOption } from \"better-auth/client\";\nimport type { commet } from \"./index\";\n\n/**\n * Commet client plugin for Better Auth\n *\n * Provides client-side methods to interact with Commet billing features.\n *\n * @example\n * ```typescript\n * import { createAuthClient } from \"better-auth/react\";\n * import { commetClient } from \"@commet/better-auth\";\n *\n * export const authClient = createAuthClient({\n * plugins: [commetClient()]\n * });\n *\n * // Usage - you can always query state directly (no webhooks needed)\n * const { data: subscription } = await authClient.subscription.get();\n * const { data: features } = await authClient.features.list();\n * const { data: canUse } = await authClient.features.canUse({ code: \"api_calls\" });\n * await authClient.usage.track({ eventType: \"api_call\" });\n * await authClient.customer.portal(); // Redirect to portal\n * ```\n */\nexport const commetClient = () => {\n return {\n id: \"commet-client\",\n $InferServerPlugin: {} as ReturnType<typeof commet>,\n getActions: ($fetch) => {\n return {\n // Customer Portal\n customer: {\n /**\n * Redirect to the Commet customer portal\n */\n portal: async (fetchOptions?: BetterFetchOption) => {\n const res = await $fetch(\"/commet/portal\", {\n method: \"GET\",\n ...fetchOptions,\n });\n\n if (res.error) {\n throw new Error(res.error.message);\n }\n\n const data = res.data as { url: string; redirect: boolean };\n\n if (data.redirect && typeof window !== \"undefined\") {\n window.location.href = data.url;\n }\n\n return data;\n },\n },\n\n // Subscription management\n subscription: {\n /**\n * Get the current subscription for the authenticated user\n */\n get: async (fetchOptions?: BetterFetchOption) => {\n return $fetch(\"/commet/subscription\", {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Change the subscription plan (upgrade/downgrade)\n */\n changePlan: async (\n data: {\n planId?: string;\n slug?: string;\n billingInterval?: \"monthly\" | \"quarterly\" | \"yearly\";\n },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/subscription/change-plan\", {\n method: \"POST\",\n body: data,\n ...fetchOptions,\n });\n },\n\n /**\n * Cancel the subscription\n */\n cancel: async (\n data?: { reason?: string; immediate?: boolean },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/subscription/cancel\", {\n method: \"POST\",\n body: data ?? {},\n ...fetchOptions,\n });\n },\n },\n\n // Feature access\n features: {\n /**\n * List all features for the authenticated user\n */\n list: async (fetchOptions?: BetterFetchOption) => {\n return $fetch(\"/commet/features\", {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Get a specific feature's access/usage\n */\n get: async (\n data: { code: string },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(`/commet/features/${data.code}`, {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Check if a feature is enabled (boolean check)\n */\n check: async (\n data: { code: string },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(`/features/${data.code}/check`, {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Check if user can use one more unit of a feature\n * Returns { allowed: boolean, willBeCharged: boolean }\n */\n canUse: async (\n data: { code: string },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(`/features/${data.code}/can-use`, {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n },\n\n // Usage tracking\n usage: {\n /**\n * Track a usage event for the authenticated user\n */\n track: async (\n data: {\n eventType: string;\n value?: number;\n idempotencyKey?: string;\n properties?: Record<string, string>;\n },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/usage/track\", {\n method: \"POST\",\n body: data,\n ...fetchOptions,\n });\n },\n },\n\n // Seat management\n seats: {\n /**\n * List all seat balances for the authenticated user\n */\n list: async (fetchOptions?: BetterFetchOption) => {\n return $fetch(\"/commet/seats\", {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Add seats of a specific type\n */\n add: async (\n data: { seatType: string; count: number },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/add\", {\n method: \"POST\",\n body: data,\n ...fetchOptions,\n });\n },\n\n /**\n * Remove seats of a specific type\n */\n remove: async (\n data: { seatType: string; count: number },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/remove\", {\n method: \"POST\",\n body: data,\n ...fetchOptions,\n });\n },\n\n /**\n * Set seats to a specific count\n */\n set: async (\n data: { seatType: string; count: number },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/set\", {\n method: \"POST\",\n body: data,\n ...fetchOptions,\n });\n },\n\n /**\n * Set all seat types at once\n */\n setAll: async (\n data: { seats: Record<string, number> },\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/set-all\", {\n method: \"POST\",\n body: data,\n ...fetchOptions,\n });\n },\n },\n };\n },\n } satisfies BetterAuthClientPlugin;\n};\n\n"],"mappings":";AA0BO,IAAM,eAAe,MAAM;AAChC,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,oBAAoB,CAAC;AAAA,IACrB,YAAY,CAAC,WAAW;AACtB,aAAO;AAAA;AAAA,QAEL,UAAU;AAAA;AAAA;AAAA;AAAA,UAIR,QAAQ,OAAO,iBAAqC;AAClD,kBAAM,MAAM,MAAM,OAAO,kBAAkB;AAAA,cACzC,QAAQ;AAAA,cACR,GAAG;AAAA,YACL,CAAC;AAED,gBAAI,IAAI,OAAO;AACb,oBAAM,IAAI,MAAM,IAAI,MAAM,OAAO;AAAA,YACnC;AAEA,kBAAM,OAAO,IAAI;AAEjB,gBAAI,KAAK,YAAY,OAAO,WAAW,aAAa;AAClD,qBAAO,SAAS,OAAO,KAAK;AAAA,YAC9B;AAEA,mBAAO;AAAA,UACT;AAAA,QACF;AAAA;AAAA,QAGA,cAAc;AAAA;AAAA;AAAA;AAAA,UAIZ,KAAK,OAAO,iBAAqC;AAC/C,mBAAO,OAAO,wBAAwB;AAAA,cACpC,QAAQ;AAAA,cACR,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