UNPKG

autumn-js

Version:

Autumn JS Library

428 lines (415 loc) 13.1 kB
import { R as Result, A as AutumnError } from './response-ByHPEnHs.js'; declare enum AppEnv { Sandbox = "sandbox", Live = "live" } 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"; declare enum ProductStatus { Active = "active", Expired = "expired", Trialing = "trialing", Scheduled = "scheduled", PastDue = "past_due" } type CustomerExpandOption = "invoices" | "rewards" | "trials_used" | "entities"; interface CustomerFeature { id: string; name: string; unlimited?: boolean; interval?: ProductItemInterval | null; balance?: number; usage?: number; included_usage?: number; next_reset_at?: number | null; breakdown?: { interval: ProductItemInterval; balance?: number; usage?: number; included_usage?: number; next_reset_at?: number; }[]; } interface CustomerProduct { id: string; name: string | null; group: string | null; status: ProductStatus; started_at: number; canceled_at: number | null; 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; } 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[]; } interface CustomerData { name?: string; email?: string; fingerprint?: string; } interface GetCustomerParams { expand?: CustomerExpandOption[]; } interface CreateCustomerParams { id?: string | null; email?: string | null; name?: string | null; fingerprint?: string | null; metadata?: Record<string, any>; expand?: CustomerExpandOption[]; } interface UpdateCustomerParams { id?: string | null; name?: string | null; email?: string | null; fingerprint?: string | null; } interface BillingPortalParams { return_url?: string; } interface BillingPortalResponse { customer_id: string; url: string; } interface CustomerInvoice { product_ids: string[]; stripe_id: string; status: string; total: number; currency: string; created_at: number; } type EntityExpandOption = "invoices"; interface CreateEntityParams { id: string; name: string; feature_id: string; customer_data?: CustomerData; } interface CreateEntityResult { success: boolean; } interface DeleteEntityResult { success: boolean; } interface GetEntityParams { expand?: EntityExpandOption[]; } interface Entity { id: string; name: string; customer_id: string; created_at: number; env: string; products: CustomerProduct[]; features: Record<string, CustomerFeature>; invoices?: CustomerInvoice[]; } interface EntityData { name?: string; feature_id: string; } interface ProductItem { feature_id?: string; included_usage?: number | typeof Infinite; interval?: ProductItemIntervalType; usage_model?: UsageModel; price?: number; billing_units?: number; entity_feature_id?: string; reset_usage_on_billing?: boolean; reset_usage_when_enabled?: boolean; } interface FreeTrial { duration: FreeTrialDuration; length: number; unique_fingerprint: boolean; } interface Product { autumn_id: string; created_at: number; id: string; name: string; env: AppEnv; is_add_on: boolean; is_default: boolean; group: string; version: number; items: ProductItem[]; free_trial: FreeTrial | null; } interface CreateProductParams { id: string; name?: string; is_add_on?: boolean; is_default?: boolean; items?: ProductItem[]; free_trial?: FreeTrial; } interface ListProductsParams { } type CheckFeatureScenario = "usage_limit" | "feature_flag"; interface CheckFeatureFormattedPreview { scenario: CheckFeatureScenario; title: string; message: string; feature_id: string; feature_name: string; products: Product[]; } type CheckProductScenario = "scheduled" | "active" | "new" | "renew" | "upgrade" | "downgrade" | "cancel"; interface CheckProductFormattedPreview { scenario: CheckProductScenario; 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; }; } interface AttachFeatureOptions { feature_id: string; quantity: number; } interface AttachParams { customer_id: string; product_id?: string; entity_id?: string; options?: AttachFeatureOptions[]; product_ids?: string[]; free_trial?: boolean; success_url?: string; metadata?: Record<string, string>; force_checkout?: boolean; customer_data?: CustomerData; entity_data?: EntityData; checkout_session_params?: Record<string, any>; reward?: string; } interface CancelParams { customer_id: string; product_id: string; entity_id?: string; cancel_immediately?: boolean; } interface CancelResult { success: boolean; customer_id: string; product_id: string; } interface AttachResult { checkout_url?: string; customer_id: string; product_ids: string[]; code: string; message: string; customer_data?: CustomerData; } interface TrackParams { customer_id: string; value?: number; feature_id?: string; event_name?: string; entity_id?: string; customer_data?: CustomerData; idempotency_key?: string; entity_data?: EntityData; } interface TrackResult { id: string; code: string; customer_id: string; feature_id?: string; event_name?: string; } interface CheckParams { customer_id: string; feature_id?: string; product_id?: string; entity_id?: string; customer_data?: CustomerData; required_balance?: number; send_event?: boolean; with_preview?: "raw" | "formatted"; entity_data?: EntityData; } interface CheckResult { customer_id: string; allowed: boolean; code: string; feature_id?: string; required_balance?: number; unlimited?: boolean; balance?: number | null; product_id?: string; status?: string; preview?: CheckProductFormattedPreview | CheckFeatureFormattedPreview; } interface UsageParams { customer_id: string; feature_id: string; value: number; customer_data?: CustomerData; } interface UsageResult { code: string; customer_id: string; feature_id: string; } declare class Autumn { private readonly secretKey; private readonly publishableKey; private level; private headers; private url; constructor(options?: { secretKey?: string; publishableKey?: string; url?: string; version?: string; headers?: Record<string, string>; }); getLevel(): "secret" | "publishable"; get(path: string): Promise<Result<any, AutumnError>>; post(path: string, body: any): Promise<Result<any, AutumnError>>; delete(path: string): Promise<Result<any, AutumnError>>; static customers: { get: (id: string, params?: GetCustomerParams) => Promise<Result<Customer, AutumnError>>; create: (params?: CreateCustomerParams) => Promise<Result<Customer, AutumnError>>; update: (id: string, params: UpdateCustomerParams) => Promise<Result<Customer, AutumnError>>; delete: (id: string) => Promise<Result<Customer, AutumnError>>; billingPortal: (id: string, params?: BillingPortalParams) => Promise<Result<BillingPortalResponse, AutumnError>>; }; static products: { get: (id: string) => Promise<Result<Product, AutumnError>>; create: (params?: CreateProductParams) => Promise<Result<Product, AutumnError>>; list: (params?: ListProductsParams) => Promise<Result<Product[], AutumnError>>; }; static entities: { get: (customer_id: string, entity_id: string, params?: GetEntityParams) => Promise<Result<Entity, AutumnError>>; create: (customer_id: string, params?: CreateEntityParams | CreateEntityParams[]) => Promise<Result<CreateEntityResult, AutumnError>>; delete: (customer_id: string, entity_id: string) => Promise<Result<DeleteEntityResult, AutumnError>>; }; static referrals: { createCode: (params: CreateReferralCodeParams) => Promise<Result<any, AutumnError>>; redeemCode: (params: RedeemReferralCodeParams) => Promise<Result<any, AutumnError>>; }; customers: { get: (id: string, params?: GetCustomerParams) => Promise<Result<Customer, AutumnError>>; create: (params?: CreateCustomerParams) => Promise<Result<Customer, AutumnError>>; update: (id: string, params: UpdateCustomerParams) => Promise<Result<Customer, AutumnError>>; delete: (id: string) => Promise<Result<Customer, AutumnError>>; billingPortal: (id: string, params?: BillingPortalParams) => Promise<Result<BillingPortalResponse, AutumnError>>; }; products: { get: (id: string) => Promise<Result<Product, AutumnError>>; create: (params?: CreateProductParams) => Promise<Result<Product, AutumnError>>; list: (params?: ListProductsParams) => Promise<Result<Product[], AutumnError>>; }; entities: { get: (customer_id: string, entity_id: string, params?: GetEntityParams) => Promise<Result<Entity, AutumnError>>; create: (customer_id: string, params?: CreateEntityParams | CreateEntityParams[]) => Promise<Result<CreateEntityResult, AutumnError>>; delete: (customer_id: string, entity_id: string) => Promise<Result<DeleteEntityResult, AutumnError>>; }; referrals: { createCode: (params: CreateReferralCodeParams) => Promise<Result<any, AutumnError>>; redeemCode: (params: RedeemReferralCodeParams) => Promise<Result<any, AutumnError>>; }; static attach: (params: AttachParams) => Promise<Result<AttachResult, AutumnError>>; static usage: (params: UsageParams) => Promise<Result<UsageResult, AutumnError>>; attach(params: AttachParams): Promise<Result<AttachResult, AutumnError>>; static cancel: (params: CancelParams) => Promise<Result<CancelResult, AutumnError>>; cancel(params: CancelParams): Promise<Result<CancelResult, AutumnError>>; /** * @deprecated This method is deprecated and will be removed in a future version. * Please use the new check() method instead. */ static entitled: (params: CheckParams) => Promise<Result<CheckResult, AutumnError>>; /** * @deprecated This method is deprecated and will be removed in a future version. * Please use the new check() method instead. */ entitled(params: CheckParams): Promise<Result<CheckResult, AutumnError>>; static check: (params: CheckParams) => Promise<Result<CheckResult, AutumnError>>; check(params: CheckParams): Promise<Result<CheckResult, AutumnError>>; /** * @deprecated This method is deprecated and will be removed in a future version. * Please use the new track() method instead. */ static event: (params: TrackParams) => Promise<Result<TrackResult, AutumnError>>; /** * @deprecated This method is deprecated and will be removed in a future version. * Please use the new track() method instead. */ event(params: TrackParams): Promise<Result<TrackResult, AutumnError>>; static track: (params: TrackParams) => Promise<Result<TrackResult, AutumnError>>; track(params: TrackParams): Promise<Result<TrackResult, AutumnError>>; usage(params: UsageParams): Promise<Result<UsageResult, AutumnError>>; } interface CreateReferralCodeParams { customer_id: string; program_id: string; } interface RedeemReferralCodeParams { code: string; customer_id: string; } export { Autumn as A, type CustomerData as C };