/** * Server-side handlers for Autumn in SvelteKit. */ import type { ConvexHttpClient } from "convex/browser"; import type { RequestEvent } from "@sveltejs/kit"; import type { AutumnConvexApi, Customer, Entity } from "../../svelte/types.js"; /** * Options for creating Autumn handlers. */ export interface AutumnHandlersOptions { /** Autumn Convex API object (e.g., api.autumn). */ convexApi: AutumnConvexApi; /** * Factory function to create an authenticated Convex HTTP client. * * This function receives the SvelteKit RequestEvent and should return * a ConvexHttpClient with authentication already configured. * * Works with any auth solution: * - Convex Auth: Pass `createConvexAuthHandlers().createConvexHttpClient` * - BetterAuth: Create client with your BetterAuth token * - Custom: Implement your own client factory * * @example * ```typescript * // With Convex Auth * const authHandlers = createConvexAuthHandlers({ convexUrl: PUBLIC_CONVEX_URL }); * const autumnHandlers = createAutumnHandlers({ * convexApi: api.autumn, * createClient: authHandlers.createConvexHttpClient * }); * ``` */ createClient: (event: RequestEvent) => Promise | ConvexHttpClient; } /** * Create server-side handlers for Autumn in SvelteKit. * * This function creates helpers for fetching Autumn billing data during SSR. * It delegates authentication to your auth solution by accepting a client factory function. * * @param options - Configuration options for Autumn handlers * @param options.convexApi - Autumn Convex API object (e.g., api.autumn) * @param options.createClient - Factory function to create authenticated Convex client * * @returns Server-side handler functions * @returns {function(RequestEvent): Promise} getCustomer - Fetch customer data (returns null on error or unauthenticated) * @returns {function(RequestEvent, string): Promise} getEntity - Fetch entity data by ID (returns null on error) * @returns {function(RequestEvent): Promise} getConvexClient - Get authenticated Convex client from factory * * @example * ```typescript * // +layout.server.ts - With Convex Auth * import { createConvexAuthHandlers } from '@stickerdaniel/convex-autumn-svelte/sveltekit/server'; * import { createAutumnHandlers } from '@stickerdaniel/convex-autumn-svelte/autumn/sveltekit/server'; * import { api } from '../../convex/_generated/api'; * import { PUBLIC_CONVEX_URL } from '$env/static/public'; * import type { LayoutServerLoad } from './$types'; * * const authHandlers = createConvexAuthHandlers({ convexUrl: PUBLIC_CONVEX_URL }); * * export const load: LayoutServerLoad = async (event) => { * const { getCustomer } = createAutumnHandlers({ * convexApi: api.autumn, * createClient: authHandlers.createConvexHttpClient * }); * * const customer = await getCustomer(event); * * return { * autumnState: { * customer, * _timeFetched: Date.now() * } * }; * }; * ``` * * @example * ```typescript * // +page.server.ts - Using getEntity and custom queries * import { createConvexAuthHandlers } from '@stickerdaniel/convex-autumn-svelte/sveltekit/server'; * import { createAutumnHandlers } from '@stickerdaniel/convex-autumn-svelte/autumn/sveltekit/server'; * import { api } from '../../convex/_generated/api'; * import type { PageServerLoad } from './$types'; * * const authHandlers = createConvexAuthHandlers(); * * export const load: PageServerLoad = async (event) => { * const { getEntity, getConvexClient } = createAutumnHandlers({ * convexApi: api.autumn, * createClient: authHandlers.createConvexHttpClient * }); * * // Fetch specific entity * const entity = await getEntity(event, 'entity-123'); * * // Make custom Convex queries/mutations * const convex = await getConvexClient(event); * const customData = await convex.query(api.myQuery, {}); * * return { entity, customData }; * }; * ``` * * @example * ```typescript * // +layout.server.ts - With BetterAuth * import { auth } from '../../auth'; // Your BetterAuth instance * import { createAutumnHandlers } from '@stickerdaniel/convex-autumn-svelte/autumn/sveltekit/server'; * import { ConvexHttpClient } from 'convex/browser'; * import { PUBLIC_CONVEX_URL } from '$env/static/public'; * import { api } from '../../convex/_generated/api'; * import type { LayoutServerLoad } from './$types'; * * export const load: LayoutServerLoad = async (event) => { * const { getCustomer } = createAutumnHandlers({ * convexApi: api.autumn, * createClient: async (event) => { * const session = await auth.api.getSession({ * headers: event.request.headers * }); * const client = new ConvexHttpClient(PUBLIC_CONVEX_URL); * if (session?.token) { * client.setAuth(session.token); * } * return client; * } * }); * * const customer = await getCustomer(event); * * return { * autumnState: { customer, _timeFetched: Date.now() } * }; * }; * ``` */ export declare function createAutumnHandlers({ convexApi, createClient, }: AutumnHandlersOptions): { getCustomer: (event: RequestEvent) => Promise; getEntity: (event: RequestEvent, entityId: string) => Promise; getConvexClient: (event: RequestEvent) => Promise; }; export type { Customer, Entity, Feature, Product, CheckParams, CheckResult, CheckoutParams, TrackParams, TrackResult, AttachParams, CancelParams, BillingPortalParams, BillingPortalResult, CreateEntityParams, GetEntityParams, SetupPaymentParams, SetupPaymentResult, CreateReferralCodeParams, CreateReferralCodeResult, RedeemReferralCodeParams, RedeemReferralCodeResult, SetUsageParams, SetUsageResult, QueryParams, QueryResult, } from "../../svelte/types.js"; //# sourceMappingURL=index.d.ts.map