/** * Svelte bindings for Autumn billing integration * * @module */ import { createAutumnClient, setAutumnContext, getAutumnContext, hasAutumnContext, } from "./client.svelte.js"; import type { AutumnConvexApi } from "./types.js"; /** * Initialize Autumn for Svelte. * * This function sets up Autumn billing integration for your Svelte application. * It should be called in your root layout or App component. * * @param convexApi - The Autumn API from your Convex backend (e.g., api.autumn) * @returns The Autumn client instance that can be used to access billing methods * @example * ```svelte * * * * ``` */ export function setupAutumn({ convexApi, }: { /** Autumn Convex API object (e.g., api.autumn from generated types) */ convexApi: AutumnConvexApi; }) { const autumn = createAutumnClient({ convexApi }); setAutumnContext(autumn); return autumn; } /** * Hook for accessing customer data and Autumn billing operations. * * Must be called after `setupAutumn()` has been called in a parent component. * * Note: Customer data is fetched once on initialization and automatically * refreshed after mutations (checkout, track, attach, cancel, createEntity). * Use `refetch()` to manually refresh customer data. * * @returns Customer data and billing methods * @returns {Customer | null | undefined} customer - Customer data (null if not found, undefined if loading) * @returns {boolean} isLoading - Whether customer data is currently loading * @returns {Error | null | undefined} error - Error if customer fetch failed * @returns {function(CheckParams): LocalCheckResult} allowed - Local check for feature access without consuming usage * @returns {function(CheckParams): Promise} check - Server-side check with usage tracking * @returns {function(CheckoutParams): Promise} checkout - Initiate checkout; returns a hosted url or a preview to confirm with attach * @returns {function(TrackParams): Promise} track - Track usage of a feature * @returns {function(AttachParams): Promise} attach - Attach a product to the customer * @returns {function(CancelParams): Promise} cancel - Cancel a product subscription * @returns {function(BillingPortalParams): Promise} openBillingPortal - Open Stripe billing portal * @returns {function(CreateEntityParams): Promise} createEntity - Create a new entity * @returns {function(GetEntityParams): Promise} getEntity - Get entity by ID * @returns {function(SetupPaymentParams): Promise} setupPayment - Setup payment method without charging * @returns {function(CreateReferralCodeParams): Promise} createReferralCode - Create a referral code * @returns {function(RedeemReferralCodeParams): Promise} redeemReferralCode - Redeem a referral code for rewards * @returns {function(): Promise} listProducts - List all available products * @returns {function(SetUsageParams): Promise} usage - Set usage to an absolute value (not a query - use customer.features for reading) * @returns {function(QueryParams): Promise} query - Query customer data with custom parameters * @returns {function(EventListParams): Promise} listEvents - List raw Autumn usage events * @returns {function(EventAggregateParams): Promise} aggregateEvents - Aggregate Autumn usage events * @returns {function(): Promise} refetch - Manually refresh customer data * * @example * ```svelte * * * * {#if isLoading} *

Loading...

* {:else if error} *

Error: {error.message}

* {:else if customer} *

Welcome {customer.name}!

*

Messages: {customer.features?.messages?.balance}

* {#if !canUpload} * * {/if} * {/if} * ``` * * @example * ```svelte * * * * * * ``` * * @example * ```svelte * * * * * * * * {#if messagesFeature} *

Usage: {messagesUsed} / {messagesTotal} (Remaining: {messagesRemaining})

* {/if} * ``` */ export function useCustomer() { const autumn = getAutumnContext(); if (!autumn) { throw new Error( "No Autumn client found in context. Did you forget to call setupAutumn()?", ); } return { get customer() { return autumn.customer; }, get isLoading() { return autumn.isLoading; }, get error() { return autumn.error; }, allowed: autumn.allowed, check: autumn.check, checkout: autumn.checkout, track: autumn.track, attach: autumn.attach, cancel: autumn.cancel, openBillingPortal: autumn.openBillingPortal, createEntity: autumn.createEntity, getEntity: autumn.getEntity, setupPayment: autumn.setupPayment, createReferralCode: autumn.createReferralCode, redeemReferralCode: autumn.redeemReferralCode, listProducts: autumn.listProducts, usage: autumn.usage, query: autumn.query, listEvents: autumn.listEvents, aggregateEvents: autumn.aggregateEvents, refetch: autumn.refetch, }; } /** * Check if Autumn has been set up in the current context. */ export function isAutumnSetup(): boolean { return hasAutumnContext(); } export type { Customer, Entity, Feature, Product, ProductItem, CheckParams, CheckResult, CheckoutParams, CheckoutResult, TrackParams, TrackResult, AttachParams, AttachResult, AttachFeatureOptions, CancelParams, BillingPortalParams, BillingPortalResult, CreateCustomerParams, CreateEntityParams, GetEntityParams, SetupPaymentParams, SetupPaymentResult, CreateReferralCodeParams, CreateReferralCodeResult, RedeemReferralCodeParams, RedeemReferralCodeResult, SetUsageParams, SetUsageResult, FeatureUsageData, QueryParams, QueryResult, EventRecord, EventListParams, EventListResult, EventAggregateParams, LocalCheckResult, RefetchOptions, AutumnConvexApi, AutumnActionResponse, } from "./types.js"; export { AutumnError, unwrapAutumnResponse } from "./types.js";