UNPKG

autumn-js

Version:

Autumn JS Library

755 lines (725 loc) 21.7 kB
import * as react_jsx_runtime from 'react/jsx-runtime'; import { z } from 'zod'; import * as swr from 'swr'; import { SWRConfiguration } from 'swr'; interface ErrorResponse { message: string; code: string; } declare class AutumnError extends Error { readonly message: string; readonly code: string; constructor(response: ErrorResponse); static fromError(error: any): AutumnError; toString(): string; toJSON(): { message: string; code: string; }; } declare const Infinite = "inf"; declare enum FreeTrialDuration { Day = "day" } declare enum UsageModel { Prepaid = "prepaid", PayPerUse = "pay_per_use" } type UsageModelType = "prepaid" | "pay_per_use"; declare enum ProductItemInterval { Minute = "minute", Hour = "hour", Day = "day", Week = "week", Month = "month", Quarter = "quarter", SemiAnnual = "semi_annual", Year = "year", Multiple = "multiple" } type ProductItemIntervalType = "minute" | "hour" | "day" | "week" | "month" | "quarter" | "semi_annual" | "year" | "multiple"; type CheckFeatureScenario = "usage_limit" | "feature_flag"; interface CheckFeatureResult extends CoreCustomerFeature { allowed: boolean; feature_id: string; customer_id: string; entity_id?: string; required_balance: number; code: string; usage_limit?: number; preview?: CheckFeaturePreview; } interface CheckFeaturePreview { scenario: CheckFeatureScenario; title: string; message: string; feature_id: string; feature_name: string; products: Product[]; } type ProductScenario = "scheduled" | "active" | "new" | "renew" | "upgrade" | "downgrade" | "cancel"; interface CheckProductResult { allowed: boolean; customer_id: string; product_id: string; code: string; status?: string; preview?: CheckProductPreview; } interface CheckProductPreview { scenario: ProductScenario; product_id: string; product_name: string; recurring: boolean; error_on_attach?: boolean; next_cycle_at?: number; current_product_name?: string; items?: { price: string; description: string; usage_model?: UsageModelType; }[]; options?: { feature_id: string; feature_name: string; billing_units: number; price?: number; tiers?: { to: number | string; amount: number; }[]; }[]; due_today?: { price: number; currency: string; }; due_next_cycle?: { price: number; currency: string; }; } declare enum AppEnv { Sandbox = "sandbox", Live = "live" } interface Feature { id: string; name: string; type: "boolean" | "continuous_use" | "single_use" | "credit_system"; } interface ProductItem { type?: "feature" | "priced_feature" | "price"; feature_id?: string; included_usage?: number | typeof Infinite; interval?: ProductItemIntervalType; feature?: Feature; usage_model?: UsageModel; price?: number; billing_units?: number; entity_feature_id?: string; reset_usage_when_enabled?: boolean; quantity?: number; next_cycle_quantity?: number; display?: { primary_text?: string; secondary_text?: string; }; } interface FreeTrial { duration: FreeTrialDuration; length: number; unique_fingerprint: boolean; trial_available?: boolean; } interface Product { id: string; created_at: number; name: string; env: AppEnv; is_add_on: boolean; is_default: boolean; group: string; version: number; items: ProductItem[]; free_trial: FreeTrial | null; scenario?: ProductScenario; base_variant_id: string | null; properties: { is_free: boolean; is_one_off: boolean; interval_group: string; has_trial: boolean; updateable: boolean; }; display?: { name?: string; description?: string; button_text?: string; recommend_text?: string; everything_from?: string; button_url?: string; }; } declare enum ProductStatus { Active = "active", Expired = "expired", Trialing = "trialing", Scheduled = "scheduled", PastDue = "past_due" } type CustomerExpandOption = "invoices" | "rewards" | "trials_used" | "entities" | "referrals" | "payment_method"; interface CoreCustomerFeature { unlimited?: boolean; interval?: ProductItemInterval | null; balance?: number; usage?: number; included_usage?: number; next_reset_at?: number | null; overage_allowed?: boolean; usage_limit?: number; breakdown?: { interval: ProductItemInterval; balance?: number; usage?: number; included_usage?: number; next_reset_at?: number; }[]; credit_schema?: { feature_id: string; credit_amount: number; }[]; } interface CustomerFeature extends CoreCustomerFeature { id: string; name: string; type: "static" | "single_use" | "continuous_use"; } interface CustomerProduct { id: string; name: string | null; group: string | null; status: ProductStatus; started_at: number; canceled_at: number | null; version: number; subscription_ids?: string[] | null; current_period_start?: number | null; current_period_end?: number | null; trial_ends_at?: number; entity_id?: string; is_add_on: boolean; is_default: boolean; items: ProductItem[]; } interface Customer { id: string | null; created_at: number; name: string | null; email: string | null; fingerprint: string | null; stripe_id: string | null; env: AppEnv; metadata: Record<string, any>; products: CustomerProduct[]; features: Record<string, CustomerFeature>; invoices?: CustomerInvoice[]; } declare const CustomerDataSchema: z.ZodObject<{ name: z.ZodOptional<z.ZodNullable<z.ZodString>>; email: z.ZodOptional<z.ZodNullable<z.ZodString>>; fingerprint: z.ZodOptional<z.ZodNullable<z.ZodString>>; }, "strip", z.ZodTypeAny, { name?: string | null | undefined; email?: string | null | undefined; fingerprint?: string | null | undefined; }, { name?: string | null | undefined; email?: string | null | undefined; fingerprint?: string | null | undefined; }>; type CustomerData = z.infer<typeof CustomerDataSchema>; interface BillingPortalResult { customer_id: string; url: string; } interface CustomerInvoice { product_ids: string[]; stripe_id: string; status: string; total: number; currency: string; created_at: number; hosted_invoice_url: string; } declare const CancelResultSchema: z.ZodObject<{ success: z.ZodBoolean; customer_id: z.ZodString; product_id: z.ZodString; }, "strip", z.ZodTypeAny, { customer_id: string; product_id: string; success: boolean; }, { customer_id: string; product_id: string; success: boolean; }>; type CancelResult = z.infer<typeof CancelResultSchema>; declare const TrackResultSchema: z.ZodObject<{ id: z.ZodString; code: z.ZodString; customer_id: z.ZodString; feature_id: z.ZodOptional<z.ZodString>; event_name: z.ZodOptional<z.ZodString>; }, "strip", z.ZodTypeAny, { customer_id: string; code: string; id: string; feature_id?: string | undefined; event_name?: string | undefined; }, { customer_id: string; code: string; id: string; feature_id?: string | undefined; event_name?: string | undefined; }>; type TrackResult = z.infer<typeof TrackResultSchema>; type CheckResult = CheckFeatureResult & CheckProductResult; interface SetupPaymentResult { customer_id: string; url: string; } declare const AttachFeatureOptionsSchema: z.ZodObject<{ feature_id: z.ZodString; quantity: z.ZodNumber; }, "strip", z.ZodTypeAny, { feature_id: string; quantity: number; }, { feature_id: string; quantity: number; }>; type AttachFeatureOptions = z.infer<typeof AttachFeatureOptionsSchema>; declare const AttachResultSchema: z.ZodObject<{ checkout_url: z.ZodOptional<z.ZodString>; customer_id: z.ZodString; product_ids: z.ZodArray<z.ZodString, "many">; code: z.ZodString; message: z.ZodString; customer_data: z.ZodOptional<z.ZodAny>; }, "strip", z.ZodTypeAny, { customer_id: string; code: string; message: string; product_ids: string[]; customer_data?: any; checkout_url?: string | undefined; }, { customer_id: string; code: string; message: string; product_ids: string[]; customer_data?: any; checkout_url?: string | undefined; }>; type AttachResult = z.infer<typeof AttachResultSchema>; type CheckoutResult = { url?: string; customer_id: string; has_prorations: boolean; lines: { description: string; amount: number; item: ProductItem; }[]; total: number; currency: string; options: AttachFeatureOptions[]; product: Product; current_product: Product; next_cycle?: { starts_at: number; total: number; }; }; type Success<T> = { data: T; error: null; statusCode?: number; }; type Failure<E> = { data: null; error: E; statusCode?: number; }; type Result<T, E> = Success<T> | Failure<E>; type AutumnPromise<T> = Promise<Result<T, AutumnError>>; interface PricingTableProduct { id: string; name: string; description?: string; button_text?: string; recommend_text?: string; everything_from?: string; price: { primary_text: string; secondary_text?: string; } & ProductItem; items: ({ primary_text: string; secondary_text?: string; } & ProductItem)[]; scenario: ProductScenario; } interface Entity { id: string; name: string; customer_id: string; created_at: number; env: string; products: CustomerProduct[]; features: Record<string, CustomerFeature>; invoices?: CustomerInvoice[]; } interface CreateReferralCodeResult { code: string; customer_id: string; created_at: number; } interface RedeemReferralCodeResult { id: string; customer_id: string; reward_id: string; applied: boolean; } declare const ReactAutumnProvider: ({ children, getBearerToken, backendUrl, customerData, includeCredentials, betterAuthUrl, }: { children: React.ReactNode; getBearerToken?: () => Promise<string | null | undefined>; backendUrl?: string; customerData?: CustomerData; includeCredentials?: boolean; betterAuthUrl?: string; }) => react_jsx_runtime.JSX.Element; interface ProductDetails { id?: string; name?: string; description?: string; buttonText?: string; buttonUrl?: string; recommendText?: string; everythingFrom?: string; price?: { primaryText: string; secondaryText?: string; }; items?: { featureId?: string; primaryText?: string; secondaryText?: string; }[]; } interface CreateEntityParams { id: string; name?: string; featureId: string; } interface GetEntityParams { expand?: string[]; } declare const CreateReferralCodeParamsSchema: z.ZodObject<{ programId: z.ZodString; }, "strip", z.ZodTypeAny, { programId: string; }, { programId: string; }>; type CreateReferralCodeParams = z.infer<typeof CreateReferralCodeParamsSchema>; declare const RedeemReferralCodeParamsSchema: z.ZodObject<{ code: z.ZodString; }, "strip", z.ZodTypeAny, { code: string; }, { code: string; }>; type RedeemReferralCodeParams = z.infer<typeof RedeemReferralCodeParamsSchema>; declare const CancelParamsSchema: z.ZodObject<{ productId: z.ZodString; entityId: z.ZodOptional<z.ZodString>; cancelImmediately: z.ZodOptional<z.ZodBoolean>; }, "strip", z.ZodTypeAny, { productId: string; entityId?: string | undefined; cancelImmediately?: boolean | undefined; }, { productId: string; entityId?: string | undefined; cancelImmediately?: boolean | undefined; }>; type CancelParams = z.infer<typeof CancelParamsSchema>; declare const CheckParamsSchema: z.ZodObject<{ featureId: z.ZodOptional<z.ZodString>; productId: z.ZodOptional<z.ZodString>; entityId: z.ZodOptional<z.ZodString>; requiredBalance: z.ZodOptional<z.ZodNumber>; sendEvent: z.ZodOptional<z.ZodBoolean>; withPreview: z.ZodOptional<z.ZodBoolean>; dialog: z.ZodOptional<z.ZodAny>; entityData: z.ZodOptional<z.ZodAny>; }, "strip", z.ZodTypeAny, { productId?: string | undefined; entityId?: string | undefined; featureId?: string | undefined; requiredBalance?: number | undefined; sendEvent?: boolean | undefined; withPreview?: boolean | undefined; dialog?: any; entityData?: any; }, { productId?: string | undefined; entityId?: string | undefined; featureId?: string | undefined; requiredBalance?: number | undefined; sendEvent?: boolean | undefined; withPreview?: boolean | undefined; dialog?: any; entityData?: any; }>; type CheckParams = z.infer<typeof CheckParamsSchema>; declare const TrackParamsSchema: z.ZodObject<{ featureId: z.ZodOptional<z.ZodString>; eventName: z.ZodOptional<z.ZodString>; entityId: z.ZodOptional<z.ZodString>; value: z.ZodOptional<z.ZodNumber>; idempotencyKey: z.ZodOptional<z.ZodString>; entityData: z.ZodOptional<z.ZodAny>; }, "strip", z.ZodTypeAny, { value?: number | undefined; entityId?: string | undefined; featureId?: string | undefined; entityData?: any; eventName?: string | undefined; idempotencyKey?: string | undefined; }, { value?: number | undefined; entityId?: string | undefined; featureId?: string | undefined; entityData?: any; eventName?: string | undefined; idempotencyKey?: string | undefined; }>; type TrackParams = z.infer<typeof TrackParamsSchema>; interface OpenBillingPortalParams { returnUrl?: string; openInNewTab?: boolean; } interface SetupPaymentParams { successUrl?: string; checkoutSessionParams?: Record<string, any>; openInNewTab?: boolean; } interface AllowedParams { featureId?: string; productId?: string; requiredBalance?: number; } declare const AttachParamsSchema: z.ZodObject<{ productId: z.ZodOptional<z.ZodString>; entityId: z.ZodOptional<z.ZodString>; options: z.ZodOptional<z.ZodArray<z.ZodObject<{ featureId: z.ZodString; quantity: z.ZodNumber; }, "strip", z.ZodTypeAny, { quantity: number; featureId: string; }, { quantity: number; featureId: string; }>, "many">>; productIds: z.ZodOptional<z.ZodArray<z.ZodString, "many">>; freeTrial: z.ZodOptional<z.ZodBoolean>; successUrl: z.ZodOptional<z.ZodString>; metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>; forceCheckout: z.ZodOptional<z.ZodBoolean>; /** * @deprecated This field is deprecated and will be removed in a future version. */ dialog: z.ZodOptional<z.ZodAny>; entityData: z.ZodOptional<z.ZodAny>; openInNewTab: z.ZodOptional<z.ZodBoolean>; reward: z.ZodOptional<z.ZodString>; checkoutSessionParams: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>; }, "strip", z.ZodTypeAny, { options?: { quantity: number; featureId: string; }[] | undefined; metadata?: Record<string, string> | undefined; reward?: string | undefined; productId?: string | undefined; entityId?: string | undefined; dialog?: any; entityData?: any; productIds?: string[] | undefined; freeTrial?: boolean | undefined; successUrl?: string | undefined; forceCheckout?: boolean | undefined; openInNewTab?: boolean | undefined; checkoutSessionParams?: Record<string, any> | undefined; }, { options?: { quantity: number; featureId: string; }[] | undefined; metadata?: Record<string, string> | undefined; reward?: string | undefined; productId?: string | undefined; entityId?: string | undefined; dialog?: any; entityData?: any; productIds?: string[] | undefined; freeTrial?: boolean | undefined; successUrl?: string | undefined; forceCheckout?: boolean | undefined; openInNewTab?: boolean | undefined; checkoutSessionParams?: Record<string, any> | undefined; }>; type AttachParams = z.infer<typeof AttachParamsSchema>; declare const CheckoutParamsSchema: z.ZodObject<{ productId: z.ZodString; entityId: z.ZodOptional<z.ZodString>; options: z.ZodOptional<z.ZodArray<z.ZodObject<{ featureId: z.ZodString; quantity: z.ZodNumber; }, "strip", z.ZodTypeAny, { quantity: number; featureId: string; }, { quantity: number; featureId: string; }>, "many">>; successUrl: z.ZodOptional<z.ZodString>; openInNewTab: z.ZodOptional<z.ZodBoolean>; dialog: z.ZodOptional<z.ZodAny>; }, "strip", z.ZodTypeAny, { productId: string; options?: { quantity: number; featureId: string; }[] | undefined; entityId?: string | undefined; dialog?: any; successUrl?: string | undefined; openInNewTab?: boolean | undefined; }, { productId: string; options?: { quantity: number; featureId: string; }[] | undefined; entityId?: string | undefined; dialog?: any; successUrl?: string | undefined; openInNewTab?: boolean | undefined; }>; type CheckoutParams = z.infer<typeof CheckoutParamsSchema>; interface UseCustomerResult { customer: Customer | null; isLoading: boolean; error: AutumnError | null; attach: (params: AttachParams) => AutumnPromise<AttachResult>; check: (params: CheckParams) => AutumnPromise<CheckResult>; track: (params: TrackParams) => AutumnPromise<TrackResult>; cancel: (params: CancelParams) => AutumnPromise<CancelResult>; setupPayment: (params: SetupPaymentParams) => AutumnPromise<SetupPaymentResult>; openBillingPortal: (params?: OpenBillingPortalParams) => AutumnPromise<BillingPortalResult>; checkout: (params: CheckoutParams) => AutumnPromise<CheckoutResult>; refetch: () => Promise<Customer | null>; createEntity: (params: CreateEntityParams | CreateEntityParams[]) => AutumnPromise<Entity | Entity[]>; createReferralCode: (params: CreateReferralCodeParams) => AutumnPromise<CreateReferralCodeResult>; redeemReferralCode: (params: RedeemReferralCodeParams) => AutumnPromise<RedeemReferralCodeResult>; allowed: (params: AllowedParams) => boolean; } interface UseCustomerParams { errorOnNotFound?: boolean; expand?: CustomerExpandOption[]; swrConfig?: SWRConfiguration; } declare const useCustomer: (params?: UseCustomerParams) => UseCustomerResult; declare const usePricingTable: (params?: { productDetails?: ProductDetails[]; }) => { products: Product[] | null; isLoading: boolean; error: AutumnError | undefined; refetch: swr.KeyedMutator<Product[]>; }; declare const useEntity: (entityId: string | null, params?: GetEntityParams) => { entity: Entity | null; isLoading: boolean; error: any; refetch: swr.KeyedMutator<Entity | null>; allowed: (params: AllowedParams) => boolean; check: (params: CheckParams) => AutumnPromise<CheckResult>; attach: (params: AttachParams) => Promise<Result<{ customer_id: string; code: string; message: string; product_ids: string[]; customer_data?: any; checkout_url?: string | undefined; } | CheckResult, AutumnError>>; cancel: (params: CancelParams) => AutumnPromise<{ customer_id: string; product_id: string; success: boolean; }>; track: (params: TrackParams) => AutumnPromise<{ customer_id: string; code: string; id: string; feature_id?: string | undefined; event_name?: string | undefined; }>; }; /** * @deprecated The functions exported from this hook can now be found in useCustomer */ declare const useAutumn: () => { attach: (params: AttachParams) => Promise<Result<{ customer_id: string; code: string; message: string; product_ids: string[]; customer_data?: any; checkout_url?: string | undefined; } | CheckResult, AutumnError>>; check: (params: CheckParams) => AutumnPromise<CheckResult>; track: (params: TrackParams) => AutumnPromise<TrackResult>; cancel: (params: CancelParams) => AutumnPromise<CancelResult>; openBillingPortal: (params?: OpenBillingPortalParams) => AutumnPromise<BillingPortalResult>; setupPayment: (params?: SetupPaymentParams) => AutumnPromise<SetupPaymentResult>; checkout: (params: CheckoutParams) => Promise<{ data: null; error: AutumnError; } | { data: CheckoutResult; error: null; }>; }; interface AttachDialogProps { open: boolean; setOpen: (open: boolean) => void; checkoutResult: CheckoutResult; } declare function AttachDialog(params: AttachDialogProps): react_jsx_runtime.JSX.Element; interface CheckDialogProps { open: boolean; setOpen: (open: boolean) => void; preview: CheckFeaturePreview; } declare function CheckDialog(params?: CheckDialogProps): react_jsx_runtime.JSX.Element; declare function PricingTable({ productDetails, }: { productDetails?: any; }): react_jsx_runtime.JSX.Element; export { AttachDialog, ReactAutumnProvider as AutumnProvider, CheckDialog, PricingTable, type PricingTableProduct, type ProductDetails, useAutumn, useCustomer, useEntity, usePricingTable };