@commet/better-auth 1.3.0 → 1.3.2

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.
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/subscriptions.ts","../src/plugins/features.ts","../src/plugins/usage.ts","../src/plugins/seats.ts","../src/plugins/webhooks.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n onAfterUserCreate,\n onBeforeUserCreate,\n onUserDelete,\n onUserUpdate,\n} from \"./hooks/customer\";\nimport type { CommetEndpoints, CommetOptions } from \"./types\";\n\n// Re-export client plugin\nexport { commetClient } from \"./client\";\n\n// Re-export all plugins\nexport { portal } from \"./plugins/portal\";\nexport type { PortalConfig } from \"./plugins/portal\";\n\nexport { subscriptions } from \"./plugins/subscriptions\";\nexport type { SubscriptionsConfig } from \"./plugins/subscriptions\";\n\nexport { features } from \"./plugins/features\";\nexport type { FeaturesConfig } from \"./plugins/features\";\n\nexport { usage } from \"./plugins/usage\";\nexport type { UsageConfig } from \"./plugins/usage\";\n\nexport { seats } from \"./plugins/seats\";\nexport type { SeatsConfig } from \"./plugins/seats\";\n\nexport { webhooks } from \"./plugins/webhooks\";\nexport type { WebhooksConfig, WebhookHandler } from \"./plugins/webhooks\";\n\n// Re-export types\nexport type {\n CommetOptions,\n CommetPlugin,\n CommetPlugins,\n CommetEndpoints,\n CustomerCreateParams,\n PlanMapping,\n} from \"./types\";\n\n/**\n * Commet plugin for Better Auth\n *\n * Integrates Commet billing with Better Auth authentication.\n *\n * @example\n * ```typescript\n * import { betterAuth } from \"better-auth\";\n * import { commet, portal, subscriptions, features, usage, seats, webhooks } from \"@commet/better-auth\";\n * import { Commet } from \"@commet/node\";\n *\n * const commetClient = new Commet({\n * apiKey: process.env.COMMET_API_KEY,\n * environment: \"production\"\n * });\n *\n * const auth = betterAuth({\n * plugins: [\n * commet({\n * client: commetClient,\n * createCustomerOnSignUp: true,\n * getCustomerCreateParams: ({ user }) => ({\n * fullName: user.name,\n * metadata: { source: \"signup\" }\n * }),\n * use: [\n * portal({ returnUrl: \"/dashboard\" }),\n * subscriptions(),\n * features(),\n * usage(),\n * seats(),\n * // Webhooks are OPTIONAL - you can always query state directly\n * webhooks({\n * secret: process.env.COMMET_WEBHOOK_SECRET,\n * onSubscriptionActivated: async (payload) => { ... }\n * })\n * ]\n * })\n * ]\n * });\n * ```\n */\nexport const commet = <O extends CommetOptions>(options: O) => {\n // Compose all plugin endpoints\n const plugins = options.use\n .map((use) => use(options.client))\n .reduce((acc, plugin) => {\n Object.assign(acc, plugin);\n return acc;\n }, {} as CommetEndpoints);\n\n return {\n id: \"commet\",\n endpoints: {\n ...plugins,\n },\n init() {\n return {\n options: {\n databaseHooks: {\n user: {\n create: {\n before: onBeforeUserCreate(options),\n after: onAfterUserCreate(options),\n },\n update: {\n after: onUserUpdate(options),\n },\n delete: {\n after: onUserDelete(options),\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { CommetOptions } from \"../types\";\n\n/**\n * Hook called before a user is created in Better Auth\n * Creates a Commet customer if createCustomerOnSignUp is enabled\n */\nexport const onBeforeUserCreate =\n (options: CommetOptions) =>\n async (user: Partial<User>, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n const customParams = options.getCustomerCreateParams\n ? await options.getCustomerCreateParams({ user }, context.request)\n : {};\n\n if (!user.email) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"An email is required to create a customer\",\n });\n }\n\n // Check if customer already exists by email\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email\n );\n\n // Skip creation if customer already exists\n if (!existingCustomer) {\n await options.client.customers.create({\n email: user.email,\n fullName: customParams.fullName ?? user.name,\n domain: customParams.domain,\n metadata: customParams.metadata,\n });\n }\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer creation failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer creation failed\",\n });\n }\n };\n\n/**\n * Hook called after a user is created in Better Auth\n * Updates the Commet customer with the externalId (Better Auth user ID)\n */\nexport const onAfterUserCreate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by email and update with externalId\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email\n );\n\n if (existingCustomer && existingCustomer.externalId !== user.id) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n externalId: user.id,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer update failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer update failed\",\n });\n }\n };\n\n/**\n * Hook called when a user is updated in Better Auth\n * Syncs email and name changes to the Commet customer\n */\nexport const onUserUpdate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and update\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n email: user.email,\n fullName: user.name ?? undefined,\n });\n }\n } catch (e: unknown) {\n // Log but don't throw - update failures shouldn't break the auth flow\n if (e instanceof Error) {\n context.context.logger.error(\n `Commet customer update failed: ${e.message}`\n );\n } else {\n context.context.logger.error(\"Commet customer update failed\");\n }\n }\n };\n\n/**\n * Hook called when a user is deleted in Better Auth\n * Archives the corresponding Commet customer\n */\nexport const onUserDelete =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and archive\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.archive(existingCustomer.id);\n }\n } catch (e: unknown) {\n // Log but don't throw - archive failures shouldn't break the auth flow\n if (e instanceof Error) {\n context?.context.logger.error(\n `Commet customer archive failed: ${e.message}`\n );\n } else {\n context?.context.logger.error(\"Commet customer archive failed\");\n }\n }\n };\n","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(\"api_calls\");\n * await authClient.usage.track({ feature: \"api_call\", value: 1 });\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 * 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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${code}`, {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Check if a feature is enabled (boolean check)\n */\n check: async (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 feature: 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 seats: Record<string, number>,\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/set-all\", {\n method: \"POST\",\n body: { seats },\n ...fetchOptions,\n });\n },\n },\n };\n },\n } satisfies BetterAuthClientPlugin;\n};\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport interface PortalConfig {\n /**\n * URL to return to after leaving the customer portal\n */\n returnUrl?: string;\n}\n\n/**\n * Portal plugin - Provides customer portal access\n *\n * Endpoints:\n * - GET /customer/portal - Redirects to the Commet customer portal\n */\nexport const portal =\n ({ returnUrl }: PortalConfig = {}) =>\n (commet: Commet) => {\n return {\n portal: createAuthEndpoint(\n \"/commet/portal\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to access the customer portal\",\n });\n }\n\n try {\n const portalAccess = await commet.portal.getUrl({\n externalId: userId,\n });\n\n if (!portalAccess.success || !portalAccess.data) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n portalAccess.message || \"Failed to generate portal URL\",\n });\n }\n\n // Append return URL if configured\n let portalUrl = portalAccess.data.portalUrl;\n if (returnUrl) {\n const url = new URL(portalUrl);\n url.searchParams.set(\"return_url\", returnUrl);\n portalUrl = url.toString();\n }\n\n return ctx.json({\n url: portalUrl,\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet portal access failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to access customer portal\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport interface SubscriptionsConfig {\n /**\n * Optional plan mappings for easy slug-based plan changes\n */\n plans?: Array<{ planId: string; slug: string }>;\n}\n\nconst CancelSchema = z.object({\n reason: z.string().optional(),\n immediate: z.boolean().optional(),\n});\n\n/**\n * Subscriptions plugin - Manage customer subscriptions\n *\n * Endpoints:\n * - GET /subscription - Get active subscription for the authenticated user\n * - POST /subscription/cancel - Cancel the subscription\n */\nexport const subscriptions =\n (config: SubscriptionsConfig = {}) =>\n (commet: Commet) => {\n\n return {\n getSubscription: createAuthEndpoint(\n \"/commet/subscription\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view subscription\",\n });\n }\n\n try {\n const subscription = await commet.subscriptions.get(userId);\n\n if (!subscription.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n subscription.message || \"Failed to retrieve subscription\",\n });\n }\n\n return ctx.json(subscription.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to retrieve subscription\",\n });\n }\n },\n ),\n\n cancelSubscription: createAuthEndpoint(\n \"/commet/subscription/cancel\",\n {\n method: \"POST\",\n body: CancelSchema.optional(),\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to cancel subscription\",\n });\n }\n\n try {\n // First get the current subscription\n const currentSub = await commet.subscriptions.get(userId);\n\n if (!currentSub.success || !currentSub.data) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"No active subscription found\",\n });\n }\n\n const result = await commet.subscriptions.cancel({\n subscriptionId: currentSub.data.id,\n reason: ctx.body?.reason,\n immediate: ctx.body?.immediate,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to cancel subscription\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription cancel failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to cancel subscription\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport type FeaturesConfig = Record<string, never>;\n\n/**\n * Features plugin - Check feature access and usage\n *\n * Endpoints:\n * - GET /features - List all features for the authenticated user\n * - GET /features/:code - Get a specific feature's access/usage\n * - GET /features/:code/check - Check if feature is enabled (boolean check)\n * - GET /features/:code/can-use - Check if user can use one more unit\n */\nexport const features =\n (_config: FeaturesConfig = {}) =>\n (commet: Commet) => {\n return {\n listFeatures: createAuthEndpoint(\n \"/commet/features\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view features\",\n });\n }\n\n try {\n const result = await commet.features.list(userId);\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list features\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet features list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list features\",\n });\n }\n },\n ),\n\n getFeature: createAuthEndpoint(\n \"/commet/features/:code\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.get({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to get feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to get feature\",\n });\n }\n },\n ),\n\n checkFeature: createAuthEndpoint(\n \"/commet/features/:code/check\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.check({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature check failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature\",\n });\n }\n },\n ),\n\n canUseFeature: createAuthEndpoint(\n \"/commet/features/:code/can-use\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature usage\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.canUse({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature canUse failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type UsageConfig = Record<string, never>;\n\nconst TrackEventSchema = z.object({\n feature: z.string(),\n value: z.number().optional(),\n idempotencyKey: z.string().optional(),\n properties: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Usage plugin - Track usage events for metered billing\n *\n * Endpoints:\n * - POST /usage/track - Track a usage event for the authenticated user\n */\nexport const usage =\n (_config: UsageConfig = {}) =>\n (commet: Commet) => {\n return {\n trackUsage: createAuthEndpoint(\n \"/commet/usage/track\",\n {\n method: \"POST\",\n body: TrackEventSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to track usage\",\n });\n }\n\n try {\n const result = await commet.usage.track(\n {\n externalId: userId,\n feature: ctx.body.feature,\n value: ctx.body.value,\n idempotencyKey: ctx.body.idempotencyKey,\n properties: ctx.body.properties,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to track usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet usage track failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to track usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type SeatsConfig = Record<string, never>;\n\nconst SeatOperationSchema = z.object({\n seatType: z.string(),\n count: z.number().min(1),\n});\n\nconst SetAllSeatsSchema = z.object({\n seats: z.record(z.string(), z.number()),\n});\n\n/**\n * Seats plugin - Manage seat-based licensing\n *\n * Endpoints:\n * - GET /seats - List all seat balances for the authenticated user\n * - POST /seats/add - Add seats of a specific type\n * - POST /seats/remove - Remove seats of a specific type\n * - POST /seats/set - Set seats to a specific count\n * - POST /seats/set-all - Set all seat types at once\n */\nexport const seats =\n (_config: SeatsConfig = {}) =>\n (commet: Commet) => {\n return {\n listSeats: createAuthEndpoint(\n \"/commet/seats\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view seats\",\n });\n }\n\n try {\n const result = await commet.seats.getAllBalances({\n externalId: userId,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list seats\",\n });\n }\n },\n ),\n\n addSeats: createAuthEndpoint(\n \"/commet/seats/add\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to add seats\",\n });\n }\n\n try {\n const result = await commet.seats.add(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to add seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats add failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to add seats\",\n });\n }\n },\n ),\n\n removeSeats: createAuthEndpoint(\n \"/commet/seats/remove\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to remove seats\",\n });\n }\n\n try {\n const result = await commet.seats.remove(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to remove seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats remove failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to remove seats\",\n });\n }\n },\n ),\n\n setSeats: createAuthEndpoint(\n \"/commet/seats/set\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.set(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats set failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set seats\",\n });\n }\n },\n ),\n\n setAllSeats: createAuthEndpoint(\n \"/commet/seats/set-all\",\n {\n method: \"POST\",\n body: SetAllSeatsSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.setAll(\n {\n externalId: userId,\n seats: ctx.body.seats,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set all seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats set-all failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set all seats\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet, WebhookEvent, WebhookPayload } from \"@commet/node\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\n/**\n * Webhook handler function type\n */\nexport type WebhookHandler<T = WebhookPayload> = (payload: T) => Promise<void>;\n\n/**\n * Webhooks plugin configuration\n *\n * Note: Webhooks are OPTIONAL in Commet. You can always query the current state\n * using subscriptions.get(), features.list(), etc. Webhooks are useful if you want\n * to react immediately to events without polling.\n */\nexport interface WebhooksConfig {\n /**\n * Webhook secret for signature verification\n */\n secret: string;\n /**\n * Handler for subscription.created events\n */\n onSubscriptionCreated?: WebhookHandler;\n /**\n * Handler for subscription.activated events\n */\n onSubscriptionActivated?: WebhookHandler;\n /**\n * Handler for subscription.canceled events\n */\n onSubscriptionCanceled?: WebhookHandler;\n /**\n * Handler for subscription.updated events\n */\n onSubscriptionUpdated?: WebhookHandler;\n /**\n * Generic handler for all webhook events (catch-all)\n */\n onPayload?: WebhookHandler;\n}\n\n/**\n * Map event types to their handler keys\n */\nconst EVENT_HANDLER_MAP: Record<WebhookEvent, keyof WebhooksConfig> = {\n \"subscription.created\": \"onSubscriptionCreated\",\n \"subscription.activated\": \"onSubscriptionActivated\",\n \"subscription.canceled\": \"onSubscriptionCanceled\",\n \"subscription.updated\": \"onSubscriptionUpdated\",\n};\n\n/**\n * Webhooks plugin - Handle incoming Commet webhooks (OPTIONAL)\n *\n * You can always query the current state directly:\n * - authClient.subscription.get() for subscription status\n * - authClient.features.list() for feature access\n * - authClient.features.canUse() for usage checks\n *\n * Webhooks are useful when you want to:\n * - React immediately to changes (e.g., send email on cancellation)\n * - Avoid polling latency in critical cases\n * - Audit/log events\n *\n * Endpoint:\n * - POST /commet/webhooks - Receive and process Commet webhooks\n */\nexport const webhooks = (config: WebhooksConfig) => (commet: Commet) => {\n return {\n commetWebhooks: createAuthEndpoint(\n \"/commet/webhooks\",\n {\n method: \"POST\",\n metadata: {\n isAction: false,\n },\n cloneRequest: true,\n },\n async (ctx) => {\n if (!ctx.request?.body) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Request body is required\",\n });\n }\n\n const rawBody = await ctx.request.text();\n const signature = ctx.request.headers.get(\"x-commet-signature\");\n\n // Verify webhook signature\n const payload = commet.webhooks.verifyAndParse({\n rawBody,\n signature,\n secret: config.secret,\n });\n\n if (!payload) {\n ctx.context.logger.error(\"Invalid webhook signature\");\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Invalid webhook signature\",\n });\n }\n\n try {\n // Call specific event handler if configured\n const handlerKey = EVENT_HANDLER_MAP[payload.event];\n if (handlerKey) {\n const handler = config[handlerKey] as WebhookHandler | undefined;\n if (handler) {\n await handler(payload);\n }\n }\n\n // Always call onPayload if configured (catch-all)\n if (config.onPayload) {\n await config.onPayload(payload);\n }\n\n return ctx.json({ received: true });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet webhook handler error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\"Commet webhook handler error\");\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Webhook handler error\",\n });\n }\n },\n ),\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,iBAAyB;AAOlB,IAAM,qBACX,CAAC,YACD,OAAO,MAAqB,YAA2C;AACrE,MAAI,CAAC,WAAW,CAAC,QAAQ,wBAAwB;AAC/C;AAAA,EACF;AAEA,MAAI;AACF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B;AAAA,UACF;AAGA,cAAI,OAAO,WAAW;AACpB,kBAAM,OAAO,UAAU,OAAO;AAAA,UAChC;AAEA,iBAAO,IAAI,KAAK,EAAE,UAAU,KAAK,CAAC;AAAA,QACpC,SAAS,GAAY;AACnB,cAAI,aAAa,OAAO;AACtB,gBAAI,QAAQ,OAAO;AAAA,cACjB,iCAAiC,EAAE,OAAO;AAAA,YAC5C;AAAA,UACF,OAAO;AACL,gBAAI,QAAQ,OAAO,MAAM,8BAA8B;AAAA,UACzD;AAEA,gBAAM,IAAI,qBAAS,yBAAyB;AAAA,YAC1C,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ARpDO,IAAM,SAAS,CAA0B,YAAe;AAE7D,QAAM,UAAU,QAAQ,IACrB,IAAI,CAAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACvB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACT,GAAG,CAAC,CAAoB;AAE1B,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,WAAW;AAAA,MACT,GAAG;AAAA,IACL;AAAA,IACA,OAAO;AACL,aAAO;AAAA,QACL,SAAS;AAAA,UACP,eAAe;AAAA,YACb,MAAM;AAAA,cACJ,QAAQ;AAAA,gBACN,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cAClC;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":["seats","import_api","commet","import_api","import_plugins","commet","import_api","import_plugins","commet","import_api","import_plugins","import_zod","commet","import_api","import_plugins","import_zod","commet","import_api","commet"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/subscriptions.ts","../src/plugins/features.ts","../src/plugins/usage.ts","../src/plugins/seats.ts","../src/plugins/webhooks.ts"],"sourcesContent":["import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n onAfterUserCreate,\n onBeforeUserCreate,\n onUserDelete,\n onUserUpdate,\n} from \"./hooks/customer\";\nimport type { CommetEndpoints, CommetOptions } from \"./types\";\n\n// Re-export client plugin\nexport { commetClient } from \"./client\";\n\n// Re-export all plugins\nexport { portal } from \"./plugins/portal\";\nexport type { PortalConfig } from \"./plugins/portal\";\n\nexport { subscriptions } from \"./plugins/subscriptions\";\nexport type { SubscriptionsConfig } from \"./plugins/subscriptions\";\n\nexport { features } from \"./plugins/features\";\nexport type { FeaturesConfig } from \"./plugins/features\";\n\nexport { usage } from \"./plugins/usage\";\nexport type { UsageConfig } from \"./plugins/usage\";\n\nexport { seats } from \"./plugins/seats\";\nexport type { SeatsConfig } from \"./plugins/seats\";\n\nexport { webhooks } from \"./plugins/webhooks\";\nexport type { WebhooksConfig, WebhookHandler } from \"./plugins/webhooks\";\n\n// Re-export types\nexport type {\n CommetOptions,\n CommetPlugin,\n CommetPlugins,\n CommetEndpoints,\n CustomerCreateParams,\n PlanMapping,\n} from \"./types\";\n\n/**\n * Commet plugin for Better Auth\n *\n * Integrates Commet billing with Better Auth authentication.\n *\n * @example\n * ```typescript\n * import { betterAuth } from \"better-auth\";\n * import { commet, portal, subscriptions, features, usage, seats, webhooks } from \"@commet/better-auth\";\n * import { Commet } from \"@commet/node\";\n *\n * const commetClient = new Commet({\n * apiKey: process.env.COMMET_API_KEY,\n * environment: \"production\"\n * });\n *\n * const auth = betterAuth({\n * plugins: [\n * commet({\n * client: commetClient,\n * createCustomerOnSignUp: true,\n * getCustomerCreateParams: ({ user }) => ({\n * fullName: user.name,\n * metadata: { source: \"signup\" }\n * }),\n * use: [\n * portal({ returnUrl: \"/dashboard\" }),\n * subscriptions(),\n * features(),\n * usage(),\n * seats(),\n * // Webhooks are OPTIONAL - you can always query state directly\n * webhooks({\n * secret: process.env.COMMET_WEBHOOK_SECRET,\n * onSubscriptionActivated: async (payload) => { ... }\n * })\n * ]\n * })\n * ]\n * });\n * ```\n */\nexport const commet = <O extends CommetOptions>(options: O) => {\n // Compose all plugin endpoints\n const plugins = options.use\n .map((use) => use(options.client))\n .reduce((acc, plugin) => {\n Object.assign(acc, plugin);\n return acc;\n }, {} as CommetEndpoints);\n\n return {\n id: \"commet\",\n endpoints: {\n ...plugins,\n },\n init() {\n return {\n options: {\n databaseHooks: {\n user: {\n create: {\n before: onBeforeUserCreate(options),\n after: onAfterUserCreate(options),\n },\n update: {\n after: onUserUpdate(options),\n },\n delete: {\n after: onUserDelete(options),\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n","import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { CommetOptions } from \"../types\";\n\n/**\n * Hook called before a user is created in Better Auth\n * Creates a Commet customer if createCustomerOnSignUp is enabled\n */\nexport const onBeforeUserCreate =\n (options: CommetOptions) =>\n async (user: Partial<User>, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n const customParams = options.getCustomerCreateParams\n ? await options.getCustomerCreateParams({ user }, context.request)\n : {};\n\n if (!user.email) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"An email is required to create a customer\",\n });\n }\n\n // Check if customer already exists by email\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email,\n );\n\n // Skip creation if customer already exists\n if (!existingCustomer) {\n await options.client.customers.create({\n email: user.email,\n fullName: customParams.fullName ?? user.name,\n domain: customParams.domain,\n metadata: customParams.metadata,\n });\n }\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer creation failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer creation failed\",\n });\n }\n };\n\n/**\n * Hook called after a user is created in Better Auth\n * Updates the Commet customer with the externalId (Better Auth user ID)\n */\nexport const onAfterUserCreate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by email and update with externalId\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email,\n );\n\n if (existingCustomer && existingCustomer.externalId !== user.id) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n externalId: user.id,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer update failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer update failed\",\n });\n }\n };\n\n/**\n * Hook called when a user is updated in Better Auth\n * Syncs email and name changes to the Commet customer\n */\nexport const onUserUpdate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and update\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n email: user.email,\n fullName: user.name ?? undefined,\n });\n }\n } catch (e: unknown) {\n // Log but don't throw - update failures shouldn't break the auth flow\n if (e instanceof Error) {\n context.context.logger.error(\n `Commet customer update failed: ${e.message}`,\n );\n } else {\n context.context.logger.error(\"Commet customer update failed\");\n }\n }\n };\n\n/**\n * Hook called when a user is deleted in Better Auth\n * Archives the corresponding Commet customer\n */\nexport const onUserDelete =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and archive\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.archive(existingCustomer.id);\n }\n } catch (e: unknown) {\n // Log but don't throw - archive failures shouldn't break the auth flow\n if (e instanceof Error) {\n context?.context.logger.error(\n `Commet customer archive failed: ${e.message}`,\n );\n } else {\n context?.context.logger.error(\"Commet customer archive failed\");\n }\n }\n };\n","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(\"api_calls\");\n * await authClient.usage.track({ feature: \"api_call\", value: 1 });\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 * 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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${code}`, {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Check if a feature is enabled (boolean check)\n */\n check: async (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 feature: 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 seats: Record<string, number>,\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/set-all\", {\n method: \"POST\",\n body: { seats },\n ...fetchOptions,\n });\n },\n },\n };\n },\n } satisfies BetterAuthClientPlugin;\n};\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport interface PortalConfig {\n /**\n * URL to return to after leaving the customer portal\n */\n returnUrl?: string;\n}\n\n/**\n * Portal plugin - Provides customer portal access\n *\n * Endpoints:\n * - GET /customer/portal - Redirects to the Commet customer portal\n */\nexport const portal =\n ({ returnUrl }: PortalConfig = {}) =>\n (commet: Commet) => {\n return {\n portal: createAuthEndpoint(\n \"/commet/portal\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to access the customer portal\",\n });\n }\n\n try {\n const portalAccess = await commet.portal.getUrl({\n externalId: userId,\n });\n\n if (!portalAccess.success || !portalAccess.data) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n portalAccess.message || \"Failed to generate portal URL\",\n });\n }\n\n // Append return URL if configured\n let portalUrl = portalAccess.data.portalUrl;\n if (returnUrl) {\n const url = new URL(portalUrl);\n url.searchParams.set(\"return_url\", returnUrl);\n portalUrl = url.toString();\n }\n\n return ctx.json({\n url: portalUrl,\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet portal access failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to access customer portal\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport interface SubscriptionsConfig {\n /**\n * Optional plan mappings for easy slug-based plan changes\n */\n plans?: Array<{ planId: string; slug: string }>;\n}\n\nconst CancelSchema = z.object({\n reason: z.string().optional(),\n immediate: z.boolean().optional(),\n});\n\n/**\n * Subscriptions plugin - Manage customer subscriptions\n *\n * Endpoints:\n * - GET /subscription - Get active subscription for the authenticated user\n * - POST /subscription/cancel - Cancel the subscription\n */\nexport const subscriptions =\n (config: SubscriptionsConfig = {}) =>\n (commet: Commet) => {\n return {\n getSubscription: createAuthEndpoint(\n \"/commet/subscription\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view subscription\",\n });\n }\n\n try {\n const subscription = await commet.subscriptions.get(userId);\n\n if (!subscription.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n subscription.message || \"Failed to retrieve subscription\",\n });\n }\n\n return ctx.json(subscription.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to retrieve subscription\",\n });\n }\n },\n ),\n\n cancelSubscription: createAuthEndpoint(\n \"/commet/subscription/cancel\",\n {\n method: \"POST\",\n body: CancelSchema.optional(),\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to cancel subscription\",\n });\n }\n\n try {\n // First get the current subscription\n const currentSub = await commet.subscriptions.get(userId);\n\n if (!currentSub.success || !currentSub.data) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"No active subscription found\",\n });\n }\n\n const result = await commet.subscriptions.cancel({\n subscriptionId: currentSub.data.id,\n reason: ctx.body?.reason,\n immediate: ctx.body?.immediate,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to cancel subscription\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription cancel failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to cancel subscription\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport type FeaturesConfig = Record<string, never>;\n\n/**\n * Features plugin - Check feature access and usage\n *\n * Endpoints:\n * - GET /features - List all features for the authenticated user\n * - GET /features/:code - Get a specific feature's access/usage\n * - GET /features/:code/check - Check if feature is enabled (boolean check)\n * - GET /features/:code/can-use - Check if user can use one more unit\n */\nexport const features =\n (_config: FeaturesConfig = {}) =>\n (commet: Commet) => {\n return {\n listFeatures: createAuthEndpoint(\n \"/commet/features\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view features\",\n });\n }\n\n try {\n const result = await commet.features.list(userId);\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list features\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet features list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list features\",\n });\n }\n },\n ),\n\n getFeature: createAuthEndpoint(\n \"/commet/features/:code\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.get({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to get feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to get feature\",\n });\n }\n },\n ),\n\n checkFeature: createAuthEndpoint(\n \"/commet/features/:code/check\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.check({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature check failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature\",\n });\n }\n },\n ),\n\n canUseFeature: createAuthEndpoint(\n \"/commet/features/:code/can-use\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature usage\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.canUse({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature canUse failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type UsageConfig = Record<string, never>;\n\nconst TrackEventSchema = z.object({\n feature: z.string(),\n value: z.number().optional(),\n idempotencyKey: z.string().optional(),\n properties: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Usage plugin - Track usage events for metered billing\n *\n * Endpoints:\n * - POST /usage/track - Track a usage event for the authenticated user\n */\nexport const usage =\n (_config: UsageConfig = {}) =>\n (commet: Commet) => {\n return {\n trackUsage: createAuthEndpoint(\n \"/commet/usage/track\",\n {\n method: \"POST\",\n body: TrackEventSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to track usage\",\n });\n }\n\n try {\n const result = await commet.usage.track(\n {\n externalId: userId,\n feature: ctx.body.feature,\n value: ctx.body.value,\n idempotencyKey: ctx.body.idempotencyKey,\n properties: ctx.body.properties,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to track usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet usage track failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to track usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type SeatsConfig = Record<string, never>;\n\nconst SeatOperationSchema = z.object({\n seatType: z.string(),\n count: z.number().min(1),\n});\n\nconst SetAllSeatsSchema = z.object({\n seats: z.record(z.string(), z.number()),\n});\n\n/**\n * Seats plugin - Manage seat-based licensing\n *\n * Endpoints:\n * - GET /seats - List all seat balances for the authenticated user\n * - POST /seats/add - Add seats of a specific type\n * - POST /seats/remove - Remove seats of a specific type\n * - POST /seats/set - Set seats to a specific count\n * - POST /seats/set-all - Set all seat types at once\n */\nexport const seats =\n (_config: SeatsConfig = {}) =>\n (commet: Commet) => {\n return {\n listSeats: createAuthEndpoint(\n \"/commet/seats\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view seats\",\n });\n }\n\n try {\n const result = await commet.seats.getAllBalances({\n externalId: userId,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list seats\",\n });\n }\n },\n ),\n\n addSeats: createAuthEndpoint(\n \"/commet/seats/add\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to add seats\",\n });\n }\n\n try {\n const result = await commet.seats.add(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to add seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats add failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to add seats\",\n });\n }\n },\n ),\n\n removeSeats: createAuthEndpoint(\n \"/commet/seats/remove\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to remove seats\",\n });\n }\n\n try {\n const result = await commet.seats.remove(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to remove seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats remove failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to remove seats\",\n });\n }\n },\n ),\n\n setSeats: createAuthEndpoint(\n \"/commet/seats/set\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.set(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats set failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set seats\",\n });\n }\n },\n ),\n\n setAllSeats: createAuthEndpoint(\n \"/commet/seats/set-all\",\n {\n method: \"POST\",\n body: SetAllSeatsSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.setAll(\n {\n externalId: userId,\n seats: ctx.body.seats,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set all seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats set-all failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set all seats\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet, WebhookEvent, WebhookPayload } from \"@commet/node\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\n/**\n * Webhook handler function type\n */\nexport type WebhookHandler<T = WebhookPayload> = (payload: T) => Promise<void>;\n\n/**\n * Webhooks plugin configuration\n *\n * Note: Webhooks are OPTIONAL in Commet. You can always query the current state\n * using subscriptions.get(), features.list(), etc. Webhooks are useful if you want\n * to react immediately to events without polling.\n */\nexport interface WebhooksConfig {\n /**\n * Webhook secret for signature verification\n */\n secret: string;\n /**\n * Handler for subscription.created events\n */\n onSubscriptionCreated?: WebhookHandler;\n /**\n * Handler for subscription.activated events\n */\n onSubscriptionActivated?: WebhookHandler;\n /**\n * Handler for subscription.canceled events\n */\n onSubscriptionCanceled?: WebhookHandler;\n /**\n * Handler for subscription.updated events\n */\n onSubscriptionUpdated?: WebhookHandler;\n /**\n * Generic handler for all webhook events (catch-all)\n */\n onPayload?: WebhookHandler;\n}\n\n/**\n * Map event types to their handler keys\n */\nconst EVENT_HANDLER_MAP: Record<WebhookEvent, keyof WebhooksConfig> = {\n \"subscription.created\": \"onSubscriptionCreated\",\n \"subscription.activated\": \"onSubscriptionActivated\",\n \"subscription.canceled\": \"onSubscriptionCanceled\",\n \"subscription.updated\": \"onSubscriptionUpdated\",\n};\n\n/**\n * Webhooks plugin - Handle incoming Commet webhooks (OPTIONAL)\n *\n * You can always query the current state directly:\n * - authClient.subscription.get() for subscription status\n * - authClient.features.list() for feature access\n * - authClient.features.canUse() for usage checks\n *\n * Webhooks are useful when you want to:\n * - React immediately to changes (e.g., send email on cancellation)\n * - Avoid polling latency in critical cases\n * - Audit/log events\n *\n * Endpoint:\n * - POST /commet/webhooks - Receive and process Commet webhooks\n */\nexport const webhooks = (config: WebhooksConfig) => (commet: Commet) => {\n return {\n commetWebhooks: createAuthEndpoint(\n \"/commet/webhooks\",\n {\n method: \"POST\",\n metadata: {\n isAction: false,\n },\n cloneRequest: true,\n },\n async (ctx) => {\n if (!ctx.request?.body) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Request body is required\",\n });\n }\n\n const rawBody = await ctx.request.text();\n const signature = ctx.request.headers.get(\"x-commet-signature\");\n\n // Verify webhook signature\n const payload = commet.webhooks.verifyAndParse({\n rawBody,\n signature,\n secret: config.secret,\n });\n\n if (!payload) {\n ctx.context.logger.error(\"Invalid webhook signature\");\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Invalid webhook signature\",\n });\n }\n\n try {\n // Call specific event handler if configured\n const handlerKey = EVENT_HANDLER_MAP[payload.event];\n if (handlerKey) {\n const handler = config[handlerKey] as WebhookHandler | undefined;\n if (handler) {\n await handler(payload);\n }\n }\n\n // Always call onPayload if configured (catch-all)\n if (config.onPayload) {\n await config.onPayload(payload);\n }\n\n return ctx.json({ received: true });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet webhook handler error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\"Commet webhook handler error\");\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Webhook handler error\",\n });\n }\n },\n ),\n };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,iBAAyB;AAOlB,IAAM,qBACX,CAAC,YACD,OAAO,MAAqB,YAA2C;AACrE,MAAI,CAAC,WAAW,CAAC,QAAQ,wBAAwB;AAC/C;AAAA,EACF;AAEA,MAAI;AACF,UAAM,eAAe,QAAQ,0BACzB,MAAM,QAAQ,wBAAwB,EAAE,KAAK,GAAG,QAAQ,OAAO,IAC/D,CAAC;AAEL,QAAI,CAAC,KAAK,OAAO;AACf,YAAM,IAAI,oBAAS,eAAe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AACnB,cAAI,aAAa,OAAO;AACtB,gBAAI,QAAQ,OAAO;AAAA,cACjB,iCAAiC,EAAE,OAAO;AAAA,YAC5C;AAAA,UACF,OAAO;AACL,gBAAI,QAAQ,OAAO,MAAM,8BAA8B;AAAA,UACzD;AAEA,gBAAM,IAAI,qBAAS,yBAAyB;AAAA,YAC1C,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ARpDO,IAAM,SAAS,CAA0B,YAAe;AAE7D,QAAM,UAAU,QAAQ,IACrB,IAAI,CAAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACvB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACT,GAAG,CAAC,CAAoB;AAE1B,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,WAAW;AAAA,MACT,GAAG;AAAA,IACL;AAAA,IACA,OAAO;AACL,aAAO;AAAA,QACL,SAAS;AAAA,UACP,eAAe;AAAA,YACb,MAAM;AAAA,cACJ,QAAQ;AAAA,gBACN,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cAClC;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":["seats","import_api","commet","import_api","import_plugins","commet","import_api","import_plugins","commet","import_api","import_plugins","import_zod","commet","import_api","import_plugins","import_zod","commet","import_api","commet"]}
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/subscriptions.ts","../src/plugins/features.ts","../src/plugins/usage.ts","../src/plugins/seats.ts","../src/plugins/webhooks.ts","../src/index.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { CommetOptions } from \"../types\";\n\n/**\n * Hook called before a user is created in Better Auth\n * Creates a Commet customer if createCustomerOnSignUp is enabled\n */\nexport const onBeforeUserCreate =\n (options: CommetOptions) =>\n async (user: Partial<User>, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n const customParams = options.getCustomerCreateParams\n ? await options.getCustomerCreateParams({ user }, context.request)\n : {};\n\n if (!user.email) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"An email is required to create a customer\",\n });\n }\n\n // Check if customer already exists by email\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email\n );\n\n // Skip creation if customer already exists\n if (!existingCustomer) {\n await options.client.customers.create({\n email: user.email,\n fullName: customParams.fullName ?? user.name,\n domain: customParams.domain,\n metadata: customParams.metadata,\n });\n }\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer creation failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer creation failed\",\n });\n }\n };\n\n/**\n * Hook called after a user is created in Better Auth\n * Updates the Commet customer with the externalId (Better Auth user ID)\n */\nexport const onAfterUserCreate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by email and update with externalId\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email\n );\n\n if (existingCustomer && existingCustomer.externalId !== user.id) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n externalId: user.id,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer update failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer update failed\",\n });\n }\n };\n\n/**\n * Hook called when a user is updated in Better Auth\n * Syncs email and name changes to the Commet customer\n */\nexport const onUserUpdate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and update\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n email: user.email,\n fullName: user.name ?? undefined,\n });\n }\n } catch (e: unknown) {\n // Log but don't throw - update failures shouldn't break the auth flow\n if (e instanceof Error) {\n context.context.logger.error(\n `Commet customer update failed: ${e.message}`\n );\n } else {\n context.context.logger.error(\"Commet customer update failed\");\n }\n }\n };\n\n/**\n * Hook called when a user is deleted in Better Auth\n * Archives the corresponding Commet customer\n */\nexport const onUserDelete =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and archive\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.archive(existingCustomer.id);\n }\n } catch (e: unknown) {\n // Log but don't throw - archive failures shouldn't break the auth flow\n if (e instanceof Error) {\n context?.context.logger.error(\n `Commet customer archive failed: ${e.message}`\n );\n } else {\n context?.context.logger.error(\"Commet customer archive failed\");\n }\n }\n };\n","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(\"api_calls\");\n * await authClient.usage.track({ feature: \"api_call\", value: 1 });\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 * 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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${code}`, {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Check if a feature is enabled (boolean check)\n */\n check: async (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 feature: 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 seats: Record<string, number>,\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/set-all\", {\n method: \"POST\",\n body: { seats },\n ...fetchOptions,\n });\n },\n },\n };\n },\n } satisfies BetterAuthClientPlugin;\n};\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport interface PortalConfig {\n /**\n * URL to return to after leaving the customer portal\n */\n returnUrl?: string;\n}\n\n/**\n * Portal plugin - Provides customer portal access\n *\n * Endpoints:\n * - GET /customer/portal - Redirects to the Commet customer portal\n */\nexport const portal =\n ({ returnUrl }: PortalConfig = {}) =>\n (commet: Commet) => {\n return {\n portal: createAuthEndpoint(\n \"/commet/portal\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to access the customer portal\",\n });\n }\n\n try {\n const portalAccess = await commet.portal.getUrl({\n externalId: userId,\n });\n\n if (!portalAccess.success || !portalAccess.data) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n portalAccess.message || \"Failed to generate portal URL\",\n });\n }\n\n // Append return URL if configured\n let portalUrl = portalAccess.data.portalUrl;\n if (returnUrl) {\n const url = new URL(portalUrl);\n url.searchParams.set(\"return_url\", returnUrl);\n portalUrl = url.toString();\n }\n\n return ctx.json({\n url: portalUrl,\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet portal access failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to access customer portal\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport interface SubscriptionsConfig {\n /**\n * Optional plan mappings for easy slug-based plan changes\n */\n plans?: Array<{ planId: string; slug: string }>;\n}\n\nconst CancelSchema = z.object({\n reason: z.string().optional(),\n immediate: z.boolean().optional(),\n});\n\n/**\n * Subscriptions plugin - Manage customer subscriptions\n *\n * Endpoints:\n * - GET /subscription - Get active subscription for the authenticated user\n * - POST /subscription/cancel - Cancel the subscription\n */\nexport const subscriptions =\n (config: SubscriptionsConfig = {}) =>\n (commet: Commet) => {\n\n return {\n getSubscription: createAuthEndpoint(\n \"/commet/subscription\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view subscription\",\n });\n }\n\n try {\n const subscription = await commet.subscriptions.get(userId);\n\n if (!subscription.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n subscription.message || \"Failed to retrieve subscription\",\n });\n }\n\n return ctx.json(subscription.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to retrieve subscription\",\n });\n }\n },\n ),\n\n cancelSubscription: createAuthEndpoint(\n \"/commet/subscription/cancel\",\n {\n method: \"POST\",\n body: CancelSchema.optional(),\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to cancel subscription\",\n });\n }\n\n try {\n // First get the current subscription\n const currentSub = await commet.subscriptions.get(userId);\n\n if (!currentSub.success || !currentSub.data) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"No active subscription found\",\n });\n }\n\n const result = await commet.subscriptions.cancel({\n subscriptionId: currentSub.data.id,\n reason: ctx.body?.reason,\n immediate: ctx.body?.immediate,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to cancel subscription\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription cancel failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to cancel subscription\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport type FeaturesConfig = Record<string, never>;\n\n/**\n * Features plugin - Check feature access and usage\n *\n * Endpoints:\n * - GET /features - List all features for the authenticated user\n * - GET /features/:code - Get a specific feature's access/usage\n * - GET /features/:code/check - Check if feature is enabled (boolean check)\n * - GET /features/:code/can-use - Check if user can use one more unit\n */\nexport const features =\n (_config: FeaturesConfig = {}) =>\n (commet: Commet) => {\n return {\n listFeatures: createAuthEndpoint(\n \"/commet/features\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view features\",\n });\n }\n\n try {\n const result = await commet.features.list(userId);\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list features\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet features list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list features\",\n });\n }\n },\n ),\n\n getFeature: createAuthEndpoint(\n \"/commet/features/:code\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.get({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to get feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to get feature\",\n });\n }\n },\n ),\n\n checkFeature: createAuthEndpoint(\n \"/commet/features/:code/check\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.check({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature check failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature\",\n });\n }\n },\n ),\n\n canUseFeature: createAuthEndpoint(\n \"/commet/features/:code/can-use\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature usage\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.canUse({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature canUse failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type UsageConfig = Record<string, never>;\n\nconst TrackEventSchema = z.object({\n feature: z.string(),\n value: z.number().optional(),\n idempotencyKey: z.string().optional(),\n properties: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Usage plugin - Track usage events for metered billing\n *\n * Endpoints:\n * - POST /usage/track - Track a usage event for the authenticated user\n */\nexport const usage =\n (_config: UsageConfig = {}) =>\n (commet: Commet) => {\n return {\n trackUsage: createAuthEndpoint(\n \"/commet/usage/track\",\n {\n method: \"POST\",\n body: TrackEventSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to track usage\",\n });\n }\n\n try {\n const result = await commet.usage.track(\n {\n externalId: userId,\n feature: ctx.body.feature,\n value: ctx.body.value,\n idempotencyKey: ctx.body.idempotencyKey,\n properties: ctx.body.properties,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to track usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet usage track failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to track usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type SeatsConfig = Record<string, never>;\n\nconst SeatOperationSchema = z.object({\n seatType: z.string(),\n count: z.number().min(1),\n});\n\nconst SetAllSeatsSchema = z.object({\n seats: z.record(z.string(), z.number()),\n});\n\n/**\n * Seats plugin - Manage seat-based licensing\n *\n * Endpoints:\n * - GET /seats - List all seat balances for the authenticated user\n * - POST /seats/add - Add seats of a specific type\n * - POST /seats/remove - Remove seats of a specific type\n * - POST /seats/set - Set seats to a specific count\n * - POST /seats/set-all - Set all seat types at once\n */\nexport const seats =\n (_config: SeatsConfig = {}) =>\n (commet: Commet) => {\n return {\n listSeats: createAuthEndpoint(\n \"/commet/seats\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view seats\",\n });\n }\n\n try {\n const result = await commet.seats.getAllBalances({\n externalId: userId,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list seats\",\n });\n }\n },\n ),\n\n addSeats: createAuthEndpoint(\n \"/commet/seats/add\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to add seats\",\n });\n }\n\n try {\n const result = await commet.seats.add(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to add seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats add failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to add seats\",\n });\n }\n },\n ),\n\n removeSeats: createAuthEndpoint(\n \"/commet/seats/remove\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to remove seats\",\n });\n }\n\n try {\n const result = await commet.seats.remove(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to remove seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats remove failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to remove seats\",\n });\n }\n },\n ),\n\n setSeats: createAuthEndpoint(\n \"/commet/seats/set\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.set(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats set failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set seats\",\n });\n }\n },\n ),\n\n setAllSeats: createAuthEndpoint(\n \"/commet/seats/set-all\",\n {\n method: \"POST\",\n body: SetAllSeatsSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.setAll(\n {\n externalId: userId,\n seats: ctx.body.seats,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set all seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats set-all failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set all seats\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet, WebhookEvent, WebhookPayload } from \"@commet/node\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\n/**\n * Webhook handler function type\n */\nexport type WebhookHandler<T = WebhookPayload> = (payload: T) => Promise<void>;\n\n/**\n * Webhooks plugin configuration\n *\n * Note: Webhooks are OPTIONAL in Commet. You can always query the current state\n * using subscriptions.get(), features.list(), etc. Webhooks are useful if you want\n * to react immediately to events without polling.\n */\nexport interface WebhooksConfig {\n /**\n * Webhook secret for signature verification\n */\n secret: string;\n /**\n * Handler for subscription.created events\n */\n onSubscriptionCreated?: WebhookHandler;\n /**\n * Handler for subscription.activated events\n */\n onSubscriptionActivated?: WebhookHandler;\n /**\n * Handler for subscription.canceled events\n */\n onSubscriptionCanceled?: WebhookHandler;\n /**\n * Handler for subscription.updated events\n */\n onSubscriptionUpdated?: WebhookHandler;\n /**\n * Generic handler for all webhook events (catch-all)\n */\n onPayload?: WebhookHandler;\n}\n\n/**\n * Map event types to their handler keys\n */\nconst EVENT_HANDLER_MAP: Record<WebhookEvent, keyof WebhooksConfig> = {\n \"subscription.created\": \"onSubscriptionCreated\",\n \"subscription.activated\": \"onSubscriptionActivated\",\n \"subscription.canceled\": \"onSubscriptionCanceled\",\n \"subscription.updated\": \"onSubscriptionUpdated\",\n};\n\n/**\n * Webhooks plugin - Handle incoming Commet webhooks (OPTIONAL)\n *\n * You can always query the current state directly:\n * - authClient.subscription.get() for subscription status\n * - authClient.features.list() for feature access\n * - authClient.features.canUse() for usage checks\n *\n * Webhooks are useful when you want to:\n * - React immediately to changes (e.g., send email on cancellation)\n * - Avoid polling latency in critical cases\n * - Audit/log events\n *\n * Endpoint:\n * - POST /commet/webhooks - Receive and process Commet webhooks\n */\nexport const webhooks = (config: WebhooksConfig) => (commet: Commet) => {\n return {\n commetWebhooks: createAuthEndpoint(\n \"/commet/webhooks\",\n {\n method: \"POST\",\n metadata: {\n isAction: false,\n },\n cloneRequest: true,\n },\n async (ctx) => {\n if (!ctx.request?.body) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Request body is required\",\n });\n }\n\n const rawBody = await ctx.request.text();\n const signature = ctx.request.headers.get(\"x-commet-signature\");\n\n // Verify webhook signature\n const payload = commet.webhooks.verifyAndParse({\n rawBody,\n signature,\n secret: config.secret,\n });\n\n if (!payload) {\n ctx.context.logger.error(\"Invalid webhook signature\");\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Invalid webhook signature\",\n });\n }\n\n try {\n // Call specific event handler if configured\n const handlerKey = EVENT_HANDLER_MAP[payload.event];\n if (handlerKey) {\n const handler = config[handlerKey] as WebhookHandler | undefined;\n if (handler) {\n await handler(payload);\n }\n }\n\n // Always call onPayload if configured (catch-all)\n if (config.onPayload) {\n await config.onPayload(payload);\n }\n\n return ctx.json({ received: true });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet webhook handler error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\"Commet webhook handler error\");\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Webhook handler error\",\n });\n }\n },\n ),\n };\n};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n onAfterUserCreate,\n onBeforeUserCreate,\n onUserDelete,\n onUserUpdate,\n} from \"./hooks/customer\";\nimport type { CommetEndpoints, CommetOptions } from \"./types\";\n\n// Re-export client plugin\nexport { commetClient } from \"./client\";\n\n// Re-export all plugins\nexport { portal } from \"./plugins/portal\";\nexport type { PortalConfig } from \"./plugins/portal\";\n\nexport { subscriptions } from \"./plugins/subscriptions\";\nexport type { SubscriptionsConfig } from \"./plugins/subscriptions\";\n\nexport { features } from \"./plugins/features\";\nexport type { FeaturesConfig } from \"./plugins/features\";\n\nexport { usage } from \"./plugins/usage\";\nexport type { UsageConfig } from \"./plugins/usage\";\n\nexport { seats } from \"./plugins/seats\";\nexport type { SeatsConfig } from \"./plugins/seats\";\n\nexport { webhooks } from \"./plugins/webhooks\";\nexport type { WebhooksConfig, WebhookHandler } from \"./plugins/webhooks\";\n\n// Re-export types\nexport type {\n CommetOptions,\n CommetPlugin,\n CommetPlugins,\n CommetEndpoints,\n CustomerCreateParams,\n PlanMapping,\n} from \"./types\";\n\n/**\n * Commet plugin for Better Auth\n *\n * Integrates Commet billing with Better Auth authentication.\n *\n * @example\n * ```typescript\n * import { betterAuth } from \"better-auth\";\n * import { commet, portal, subscriptions, features, usage, seats, webhooks } from \"@commet/better-auth\";\n * import { Commet } from \"@commet/node\";\n *\n * const commetClient = new Commet({\n * apiKey: process.env.COMMET_API_KEY,\n * environment: \"production\"\n * });\n *\n * const auth = betterAuth({\n * plugins: [\n * commet({\n * client: commetClient,\n * createCustomerOnSignUp: true,\n * getCustomerCreateParams: ({ user }) => ({\n * fullName: user.name,\n * metadata: { source: \"signup\" }\n * }),\n * use: [\n * portal({ returnUrl: \"/dashboard\" }),\n * subscriptions(),\n * features(),\n * usage(),\n * seats(),\n * // Webhooks are OPTIONAL - you can always query state directly\n * webhooks({\n * secret: process.env.COMMET_WEBHOOK_SECRET,\n * onSubscriptionActivated: async (payload) => { ... }\n * })\n * ]\n * })\n * ]\n * });\n * ```\n */\nexport const commet = <O extends CommetOptions>(options: O) => {\n // Compose all plugin endpoints\n const plugins = options.use\n .map((use) => use(options.client))\n .reduce((acc, plugin) => {\n Object.assign(acc, plugin);\n return acc;\n }, {} as CommetEndpoints);\n\n return {\n id: \"commet\",\n endpoints: {\n ...plugins,\n },\n init() {\n return {\n options: {\n databaseHooks: {\n user: {\n create: {\n before: onBeforeUserCreate(options),\n after: onAfterUserCreate(options),\n },\n update: {\n after: onUserUpdate(options),\n },\n delete: {\n after: onUserDelete(options),\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n"],"mappings":";AACA,SAAS,gBAAgB;AAOlB,IAAM,qBACX,CAAC,YACD,OAAO,MAAqB,YAA2C;AACrE,MAAI,CAAC,WAAW,CAAC,QAAQ,wBAAwB;AAC/C;AAAA,EACF;AAEA,MAAI;AACF,UAAM,eAAe,QAAQ,0BACzB,MAAM,QAAQ,wBAAwB,EAAE,KAAK,GAAG,QAAQ,OAAO,IAC/D,CAAC;AAEL,QAAI,CAAC,KAAK,OAAO;AACf,YAAM,IAAI,SAAS,eAAe;AAAA,QAChC,SAAS;AAAA,MACX,CAAC;AAAA,IACH;AAGA,UAAM,oBAAoB,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,MAC5D,QAAQ,KAAK;AAAA,IACf,CAAC;AAED,UAAM,mBAAmB,kBAAkB,MAAM;AAAA,MAC/C,CAAC,MAAM,EAAE,iBAAiB,KA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Q,OAAO;AAAA,cACjB,iCAAiC,EAAE,OAAO;AAAA,YAC5C;AAAA,UACF,OAAO;AACL,gBAAI,QAAQ,OAAO,MAAM,8BAA8B;AAAA,UACzD;AAEA,gBAAM,IAAIA,UAAS,yBAAyB;AAAA,YAC1C,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ACpDO,IAAM,SAAS,CAA0B,YAAe;AAE7D,QAAM,UAAU,QAAQ,IACrB,IAAI,CAAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACvB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACT,GAAG,CAAC,CAAoB;AAE1B,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,WAAW;AAAA,MACT,GAAG;AAAA,IACL;AAAA,IACA,OAAO;AACL,aAAO;AAAA,QACL,SAAS;AAAA,UACP,eAAe;AAAA,YACb,MAAM;AAAA,cACJ,QAAQ;AAAA,gBACN,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cAClC;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":["seats","APIError","commet","APIError","sessionMiddleware","createAuthEndpoint","commet","APIError","sessionMiddleware","createAuthEndpoint","commet","APIError","sessionMiddleware","createAuthEndpoint","z","commet","APIError","sessionMiddleware","createAuthEndpoint","z","commet","APIError","createAuthEndpoint","commet"]}
1
+ {"version":3,"sources":["../src/hooks/customer.ts","../src/client.ts","../src/plugins/portal.ts","../src/plugins/subscriptions.ts","../src/plugins/features.ts","../src/plugins/usage.ts","../src/plugins/seats.ts","../src/plugins/webhooks.ts","../src/index.ts"],"sourcesContent":["import type { GenericEndpointContext, User } from \"better-auth\";\nimport { APIError } from \"better-auth/api\";\nimport type { CommetOptions } from \"../types\";\n\n/**\n * Hook called before a user is created in Better Auth\n * Creates a Commet customer if createCustomerOnSignUp is enabled\n */\nexport const onBeforeUserCreate =\n (options: CommetOptions) =>\n async (user: Partial<User>, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n const customParams = options.getCustomerCreateParams\n ? await options.getCustomerCreateParams({ user }, context.request)\n : {};\n\n if (!user.email) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"An email is required to create a customer\",\n });\n }\n\n // Check if customer already exists by email\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email,\n );\n\n // Skip creation if customer already exists\n if (!existingCustomer) {\n await options.client.customers.create({\n email: user.email,\n fullName: customParams.fullName ?? user.name,\n domain: customParams.domain,\n metadata: customParams.metadata,\n });\n }\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer creation failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer creation failed\",\n });\n }\n };\n\n/**\n * Hook called after a user is created in Better Auth\n * Updates the Commet customer with the externalId (Better Auth user ID)\n */\nexport const onAfterUserCreate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by email and update with externalId\n const existingCustomers = await options.client.customers.list({\n search: user.email,\n });\n\n const existingCustomer = existingCustomers.data?.find(\n (c) => c.billingEmail === user.email,\n );\n\n if (existingCustomer && existingCustomer.externalId !== user.id) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n externalId: user.id,\n });\n }\n } catch (e: unknown) {\n if (e instanceof Error) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: `Commet customer update failed: ${e.message}`,\n });\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Commet customer update failed\",\n });\n }\n };\n\n/**\n * Hook called when a user is updated in Better Auth\n * Syncs email and name changes to the Commet customer\n */\nexport const onUserUpdate =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and update\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.update({\n customerId: existingCustomer.id,\n email: user.email,\n fullName: user.name ?? undefined,\n });\n }\n } catch (e: unknown) {\n // Log but don't throw - update failures shouldn't break the auth flow\n if (e instanceof Error) {\n context.context.logger.error(\n `Commet customer update failed: ${e.message}`,\n );\n } else {\n context.context.logger.error(\"Commet customer update failed\");\n }\n }\n };\n\n/**\n * Hook called when a user is deleted in Better Auth\n * Archives the corresponding Commet customer\n */\nexport const onUserDelete =\n (options: CommetOptions) =>\n async (user: User, context: GenericEndpointContext | null) => {\n if (!context || !options.createCustomerOnSignUp) {\n return;\n }\n\n try {\n // Find customer by externalId and archive\n const existingCustomers = await options.client.customers.list({\n externalId: user.id,\n });\n\n const existingCustomer = existingCustomers.data?.[0];\n\n if (existingCustomer) {\n await options.client.customers.archive(existingCustomer.id);\n }\n } catch (e: unknown) {\n // Log but don't throw - archive failures shouldn't break the auth flow\n if (e instanceof Error) {\n context?.context.logger.error(\n `Commet customer archive failed: ${e.message}`,\n );\n } else {\n context?.context.logger.error(\"Commet customer archive failed\");\n }\n }\n };\n","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(\"api_calls\");\n * await authClient.usage.track({ feature: \"api_call\", value: 1 });\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 * 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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${code}`, {\n method: \"GET\",\n ...fetchOptions,\n });\n },\n\n /**\n * Check if a feature is enabled (boolean check)\n */\n check: async (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 (code: string, fetchOptions?: BetterFetchOption) => {\n return $fetch(`/commet/features/${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 feature: 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 seats: Record<string, number>,\n fetchOptions?: BetterFetchOption,\n ) => {\n return $fetch(\"/commet/seats/set-all\", {\n method: \"POST\",\n body: { seats },\n ...fetchOptions,\n });\n },\n },\n };\n },\n } satisfies BetterAuthClientPlugin;\n};\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport interface PortalConfig {\n /**\n * URL to return to after leaving the customer portal\n */\n returnUrl?: string;\n}\n\n/**\n * Portal plugin - Provides customer portal access\n *\n * Endpoints:\n * - GET /customer/portal - Redirects to the Commet customer portal\n */\nexport const portal =\n ({ returnUrl }: PortalConfig = {}) =>\n (commet: Commet) => {\n return {\n portal: createAuthEndpoint(\n \"/commet/portal\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to access the customer portal\",\n });\n }\n\n try {\n const portalAccess = await commet.portal.getUrl({\n externalId: userId,\n });\n\n if (!portalAccess.success || !portalAccess.data) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n portalAccess.message || \"Failed to generate portal URL\",\n });\n }\n\n // Append return URL if configured\n let portalUrl = portalAccess.data.portalUrl;\n if (returnUrl) {\n const url = new URL(portalUrl);\n url.searchParams.set(\"return_url\", returnUrl);\n portalUrl = url.toString();\n }\n\n return ctx.json({\n url: portalUrl,\n redirect: true,\n });\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet portal access failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to access customer portal\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport interface SubscriptionsConfig {\n /**\n * Optional plan mappings for easy slug-based plan changes\n */\n plans?: Array<{ planId: string; slug: string }>;\n}\n\nconst CancelSchema = z.object({\n reason: z.string().optional(),\n immediate: z.boolean().optional(),\n});\n\n/**\n * Subscriptions plugin - Manage customer subscriptions\n *\n * Endpoints:\n * - GET /subscription - Get active subscription for the authenticated user\n * - POST /subscription/cancel - Cancel the subscription\n */\nexport const subscriptions =\n (config: SubscriptionsConfig = {}) =>\n (commet: Commet) => {\n return {\n getSubscription: createAuthEndpoint(\n \"/commet/subscription\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view subscription\",\n });\n }\n\n try {\n const subscription = await commet.subscriptions.get(userId);\n\n if (!subscription.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message:\n subscription.message || \"Failed to retrieve subscription\",\n });\n }\n\n return ctx.json(subscription.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to retrieve subscription\",\n });\n }\n },\n ),\n\n cancelSubscription: createAuthEndpoint(\n \"/commet/subscription/cancel\",\n {\n method: \"POST\",\n body: CancelSchema.optional(),\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to cancel subscription\",\n });\n }\n\n try {\n // First get the current subscription\n const currentSub = await commet.subscriptions.get(userId);\n\n if (!currentSub.success || !currentSub.data) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"No active subscription found\",\n });\n }\n\n const result = await commet.subscriptions.cancel({\n subscriptionId: currentSub.data.id,\n reason: ctx.body?.reason,\n immediate: ctx.body?.immediate,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to cancel subscription\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet subscription cancel failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to cancel subscription\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\n\nexport type FeaturesConfig = Record<string, never>;\n\n/**\n * Features plugin - Check feature access and usage\n *\n * Endpoints:\n * - GET /features - List all features for the authenticated user\n * - GET /features/:code - Get a specific feature's access/usage\n * - GET /features/:code/check - Check if feature is enabled (boolean check)\n * - GET /features/:code/can-use - Check if user can use one more unit\n */\nexport const features =\n (_config: FeaturesConfig = {}) =>\n (commet: Commet) => {\n return {\n listFeatures: createAuthEndpoint(\n \"/commet/features\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view features\",\n });\n }\n\n try {\n const result = await commet.features.list(userId);\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list features\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet features list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list features\",\n });\n }\n },\n ),\n\n getFeature: createAuthEndpoint(\n \"/commet/features/:code\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.get({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to get feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature get failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to get feature\",\n });\n }\n },\n ),\n\n checkFeature: createAuthEndpoint(\n \"/commet/features/:code/check\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.check({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature check failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature\",\n });\n }\n },\n ),\n\n canUseFeature: createAuthEndpoint(\n \"/commet/features/:code/can-use\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n const code = ctx.params?.code;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to check feature usage\",\n });\n }\n\n if (!code) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Feature code is required\",\n });\n }\n\n try {\n const result = await commet.features.canUse({\n externalId: userId,\n code,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to check feature usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet feature canUse failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to check feature usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type UsageConfig = Record<string, never>;\n\nconst TrackEventSchema = z.object({\n feature: z.string(),\n value: z.number().optional(),\n idempotencyKey: z.string().optional(),\n properties: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Usage plugin - Track usage events for metered billing\n *\n * Endpoints:\n * - POST /usage/track - Track a usage event for the authenticated user\n */\nexport const usage =\n (_config: UsageConfig = {}) =>\n (commet: Commet) => {\n return {\n trackUsage: createAuthEndpoint(\n \"/commet/usage/track\",\n {\n method: \"POST\",\n body: TrackEventSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to track usage\",\n });\n }\n\n try {\n const result = await commet.usage.track(\n {\n externalId: userId,\n feature: ctx.body.feature,\n value: ctx.body.value,\n idempotencyKey: ctx.body.idempotencyKey,\n properties: ctx.body.properties,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to track usage\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet usage track failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to track usage\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet } from \"@commet/node\";\nimport { APIError, sessionMiddleware } from \"better-auth/api\";\nimport { createAuthEndpoint } from \"better-auth/plugins\";\nimport { z } from \"zod\";\n\nexport type SeatsConfig = Record<string, never>;\n\nconst SeatOperationSchema = z.object({\n seatType: z.string(),\n count: z.number().min(1),\n});\n\nconst SetAllSeatsSchema = z.object({\n seats: z.record(z.string(), z.number()),\n});\n\n/**\n * Seats plugin - Manage seat-based licensing\n *\n * Endpoints:\n * - GET /seats - List all seat balances for the authenticated user\n * - POST /seats/add - Add seats of a specific type\n * - POST /seats/remove - Remove seats of a specific type\n * - POST /seats/set - Set seats to a specific count\n * - POST /seats/set-all - Set all seat types at once\n */\nexport const seats =\n (_config: SeatsConfig = {}) =>\n (commet: Commet) => {\n return {\n listSeats: createAuthEndpoint(\n \"/commet/seats\",\n {\n method: \"GET\",\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to view seats\",\n });\n }\n\n try {\n const result = await commet.seats.getAllBalances({\n externalId: userId,\n });\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to list seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats list failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to list seats\",\n });\n }\n },\n ),\n\n addSeats: createAuthEndpoint(\n \"/commet/seats/add\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to add seats\",\n });\n }\n\n try {\n const result = await commet.seats.add(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to add seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats add failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to add seats\",\n });\n }\n },\n ),\n\n removeSeats: createAuthEndpoint(\n \"/commet/seats/remove\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to remove seats\",\n });\n }\n\n try {\n const result = await commet.seats.remove(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to remove seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats remove failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to remove seats\",\n });\n }\n },\n ),\n\n setSeats: createAuthEndpoint(\n \"/commet/seats/set\",\n {\n method: \"POST\",\n body: SeatOperationSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.set(\n {\n externalId: userId,\n seatType: ctx.body.seatType,\n count: ctx.body.count,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(`Commet seats set failed: ${e.message}`);\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set seats\",\n });\n }\n },\n ),\n\n setAllSeats: createAuthEndpoint(\n \"/commet/seats/set-all\",\n {\n method: \"POST\",\n body: SetAllSeatsSchema,\n use: [sessionMiddleware],\n },\n async (ctx) => {\n const userId = ctx.context.session?.user.id;\n\n if (!userId) {\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"You must be logged in to set seats\",\n });\n }\n\n try {\n const result = await commet.seats.setAll(\n {\n externalId: userId,\n seats: ctx.body.seats,\n },\n {},\n );\n\n if (!result.success) {\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: result.message || \"Failed to set all seats\",\n });\n }\n\n return ctx.json(result.data ?? null);\n } catch (e: unknown) {\n if (e instanceof APIError) {\n throw e;\n }\n\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet seats set-all failed: ${e.message}`,\n );\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Failed to set all seats\",\n });\n }\n },\n ),\n };\n };\n","import type { Commet, WebhookEvent, WebhookPayload } from \"@commet/node\";\nimport { APIError, createAuthEndpoint } from \"better-auth/api\";\n\n/**\n * Webhook handler function type\n */\nexport type WebhookHandler<T = WebhookPayload> = (payload: T) => Promise<void>;\n\n/**\n * Webhooks plugin configuration\n *\n * Note: Webhooks are OPTIONAL in Commet. You can always query the current state\n * using subscriptions.get(), features.list(), etc. Webhooks are useful if you want\n * to react immediately to events without polling.\n */\nexport interface WebhooksConfig {\n /**\n * Webhook secret for signature verification\n */\n secret: string;\n /**\n * Handler for subscription.created events\n */\n onSubscriptionCreated?: WebhookHandler;\n /**\n * Handler for subscription.activated events\n */\n onSubscriptionActivated?: WebhookHandler;\n /**\n * Handler for subscription.canceled events\n */\n onSubscriptionCanceled?: WebhookHandler;\n /**\n * Handler for subscription.updated events\n */\n onSubscriptionUpdated?: WebhookHandler;\n /**\n * Generic handler for all webhook events (catch-all)\n */\n onPayload?: WebhookHandler;\n}\n\n/**\n * Map event types to their handler keys\n */\nconst EVENT_HANDLER_MAP: Record<WebhookEvent, keyof WebhooksConfig> = {\n \"subscription.created\": \"onSubscriptionCreated\",\n \"subscription.activated\": \"onSubscriptionActivated\",\n \"subscription.canceled\": \"onSubscriptionCanceled\",\n \"subscription.updated\": \"onSubscriptionUpdated\",\n};\n\n/**\n * Webhooks plugin - Handle incoming Commet webhooks (OPTIONAL)\n *\n * You can always query the current state directly:\n * - authClient.subscription.get() for subscription status\n * - authClient.features.list() for feature access\n * - authClient.features.canUse() for usage checks\n *\n * Webhooks are useful when you want to:\n * - React immediately to changes (e.g., send email on cancellation)\n * - Avoid polling latency in critical cases\n * - Audit/log events\n *\n * Endpoint:\n * - POST /commet/webhooks - Receive and process Commet webhooks\n */\nexport const webhooks = (config: WebhooksConfig) => (commet: Commet) => {\n return {\n commetWebhooks: createAuthEndpoint(\n \"/commet/webhooks\",\n {\n method: \"POST\",\n metadata: {\n isAction: false,\n },\n cloneRequest: true,\n },\n async (ctx) => {\n if (!ctx.request?.body) {\n throw new APIError(\"BAD_REQUEST\", {\n message: \"Request body is required\",\n });\n }\n\n const rawBody = await ctx.request.text();\n const signature = ctx.request.headers.get(\"x-commet-signature\");\n\n // Verify webhook signature\n const payload = commet.webhooks.verifyAndParse({\n rawBody,\n signature,\n secret: config.secret,\n });\n\n if (!payload) {\n ctx.context.logger.error(\"Invalid webhook signature\");\n throw new APIError(\"UNAUTHORIZED\", {\n message: \"Invalid webhook signature\",\n });\n }\n\n try {\n // Call specific event handler if configured\n const handlerKey = EVENT_HANDLER_MAP[payload.event];\n if (handlerKey) {\n const handler = config[handlerKey] as WebhookHandler | undefined;\n if (handler) {\n await handler(payload);\n }\n }\n\n // Always call onPayload if configured (catch-all)\n if (config.onPayload) {\n await config.onPayload(payload);\n }\n\n return ctx.json({ received: true });\n } catch (e: unknown) {\n if (e instanceof Error) {\n ctx.context.logger.error(\n `Commet webhook handler error: ${e.message}`,\n );\n } else {\n ctx.context.logger.error(\"Commet webhook handler error\");\n }\n\n throw new APIError(\"INTERNAL_SERVER_ERROR\", {\n message: \"Webhook handler error\",\n });\n }\n },\n ),\n };\n};\n","import type { BetterAuthPlugin } from \"better-auth\";\nimport {\n onAfterUserCreate,\n onBeforeUserCreate,\n onUserDelete,\n onUserUpdate,\n} from \"./hooks/customer\";\nimport type { CommetEndpoints, CommetOptions } from \"./types\";\n\n// Re-export client plugin\nexport { commetClient } from \"./client\";\n\n// Re-export all plugins\nexport { portal } from \"./plugins/portal\";\nexport type { PortalConfig } from \"./plugins/portal\";\n\nexport { subscriptions } from \"./plugins/subscriptions\";\nexport type { SubscriptionsConfig } from \"./plugins/subscriptions\";\n\nexport { features } from \"./plugins/features\";\nexport type { FeaturesConfig } from \"./plugins/features\";\n\nexport { usage } from \"./plugins/usage\";\nexport type { UsageConfig } from \"./plugins/usage\";\n\nexport { seats } from \"./plugins/seats\";\nexport type { SeatsConfig } from \"./plugins/seats\";\n\nexport { webhooks } from \"./plugins/webhooks\";\nexport type { WebhooksConfig, WebhookHandler } from \"./plugins/webhooks\";\n\n// Re-export types\nexport type {\n CommetOptions,\n CommetPlugin,\n CommetPlugins,\n CommetEndpoints,\n CustomerCreateParams,\n PlanMapping,\n} from \"./types\";\n\n/**\n * Commet plugin for Better Auth\n *\n * Integrates Commet billing with Better Auth authentication.\n *\n * @example\n * ```typescript\n * import { betterAuth } from \"better-auth\";\n * import { commet, portal, subscriptions, features, usage, seats, webhooks } from \"@commet/better-auth\";\n * import { Commet } from \"@commet/node\";\n *\n * const commetClient = new Commet({\n * apiKey: process.env.COMMET_API_KEY,\n * environment: \"production\"\n * });\n *\n * const auth = betterAuth({\n * plugins: [\n * commet({\n * client: commetClient,\n * createCustomerOnSignUp: true,\n * getCustomerCreateParams: ({ user }) => ({\n * fullName: user.name,\n * metadata: { source: \"signup\" }\n * }),\n * use: [\n * portal({ returnUrl: \"/dashboard\" }),\n * subscriptions(),\n * features(),\n * usage(),\n * seats(),\n * // Webhooks are OPTIONAL - you can always query state directly\n * webhooks({\n * secret: process.env.COMMET_WEBHOOK_SECRET,\n * onSubscriptionActivated: async (payload) => { ... }\n * })\n * ]\n * })\n * ]\n * });\n * ```\n */\nexport const commet = <O extends CommetOptions>(options: O) => {\n // Compose all plugin endpoints\n const plugins = options.use\n .map((use) => use(options.client))\n .reduce((acc, plugin) => {\n Object.assign(acc, plugin);\n return acc;\n }, {} as CommetEndpoints);\n\n return {\n id: \"commet\",\n endpoints: {\n ...plugins,\n },\n init() {\n return {\n options: {\n databaseHooks: {\n user: {\n create: {\n before: onBeforeUserCreate(options),\n after: onAfterUserCreate(options),\n },\n update: {\n after: onUserUpdate(options),\n },\n delete: {\n after: onUserDelete(options),\n },\n },\n },\n },\n };\n },\n } satisfies BetterAuthPlugin;\n};\n"],"mappings":";AACA,SAAS,gBAAgB;AAOlB,IAAM,qBACX,CAAC,YACD,OAAO,MAAqB,YAA2C;AACrE,MAAI,CAAC,WAAW,CAAC,QAAQ,wBAAwB;AAC/C;AAAA,EACF;AAEA,MAAI;AACF,UAAM,eAAe,QAAQ,0BACzB,MAAM,QAAQ,wBAAwB,EAAE,KAAK,GAAG,QAAQ,OAAO,IAC/D,CAAC;AAEL,QAAI,CAAC,KAAK,OAAO;AACf,YAAM,IAAI,SAAS,eAAe;AAAA,QAChC,SAAS;AAAA,MACX,CAAC;AAAA,IACH;AAGA,UAAM,oBAAoB,MAAM,QAAQ,OAAO,UAAU,KAAK;AAAA,MAC5D,QAAQ,KAAK;AAAA,IACf,CAAC;AAED,UAAM,mBAAmB,kBAAkB,MAAM;AAAA,MAC/C,CAAC,MAAM,EAAE,iBAAiB,KAAK;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AAO;AAAA,cACjB,iCAAiC,EAAE,OAAO;AAAA,YAC5C;AAAA,UACF,OAAO;AACL,gBAAI,QAAQ,OAAO,MAAM,8BAA8B;AAAA,UACzD;AAEA,gBAAM,IAAIA,UAAS,yBAAyB;AAAA,YAC1C,SAAS;AAAA,UACX,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;;;ACpDO,IAAM,SAAS,CAA0B,YAAe;AAE7D,QAAM,UAAU,QAAQ,IACrB,IAAI,CAAC,QAAQ,IAAI,QAAQ,MAAM,CAAC,EAChC,OAAO,CAAC,KAAK,WAAW;AACvB,WAAO,OAAO,KAAK,MAAM;AACzB,WAAO;AAAA,EACT,GAAG,CAAC,CAAoB;AAE1B,SAAO;AAAA,IACL,IAAI;AAAA,IACJ,WAAW;AAAA,MACT,GAAG;AAAA,IACL;AAAA,IACA,OAAO;AACL,aAAO;AAAA,QACL,SAAS;AAAA,UACP,eAAe;AAAA,YACb,MAAM;AAAA,cACJ,QAAQ;AAAA,gBACN,QAAQ,mBAAmB,OAAO;AAAA,gBAClC,OAAO,kBAAkB,OAAO;AAAA,cAClC;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,cACA,QAAQ;AAAA,gBACN,OAAO,aAAa,OAAO;AAAA,cAC7B;AAAA,YACF;AAAA,UACF;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;","names":["seats","APIError","commet","APIError","sessionMiddleware","createAuthEndpoint","commet","APIError","sessionMiddleware","createAuthEndpoint","commet","APIError","sessionMiddleware","createAuthEndpoint","z","commet","APIError","sessionMiddleware","createAuthEndpoint","z","commet","APIError","createAuthEndpoint","commet"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commet/better-auth",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "Better Auth plugin for Commet integration",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -43,7 +43,7 @@
43
43
  "author": "Commet Team",
44
44
  "license": "MIT",
45
45
  "peerDependencies": {
46
- "@commet/node": "1.6.0",
46
+ "@commet/node": "1.6.2",
47
47
  "better-auth": "1.4.6",
48
48
  "zod": "3.24.2 || 4"
49
49
  },
@@ -53,7 +53,7 @@
53
53
  "tsup": "8.5.1",
54
54
  "typescript": "5.9.3",
55
55
  "zod": "4.1.12",
56
- "@commet/node": "1.6.0"
56
+ "@commet/node": "1.6.2"
57
57
  },
58
58
  "engines": {
59
59
  "node": ">=18.0.0"