autumn-js
Version:
Autumn JS Library
743 lines (729 loc) • 24.1 kB
TypeScript
import { z } from 'zod';
import { R as Result, A as AutumnError } from './response-CV6KGWH5.js';
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;
};
}
interface CreateProductParams {
id: string;
name?: string;
is_add_on?: boolean;
is_default?: boolean;
items?: ProductItem[];
free_trial?: FreeTrial;
}
interface ListProductsParams {
customer_id?: 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 GetCustomerParams {
expand?: CustomerExpandOption[];
}
declare const CreateCustomerParamsSchema: z.ZodObject<{
id: z.ZodOptional<z.ZodNullable<z.ZodString>>;
email: z.ZodOptional<z.ZodNullable<z.ZodString>>;
name: z.ZodOptional<z.ZodNullable<z.ZodString>>;
fingerprint: z.ZodOptional<z.ZodNullable<z.ZodString>>;
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
expand: z.ZodOptional<z.ZodArray<z.ZodEnum<["invoices", "rewards", "trials_used", "entities", "referrals", "payment_method"]>, "many">>;
}, "strip", z.ZodTypeAny, {
id?: string | null | undefined;
name?: string | null | undefined;
email?: string | null | undefined;
fingerprint?: string | null | undefined;
metadata?: Record<string, any> | undefined;
expand?: ("invoices" | "rewards" | "trials_used" | "entities" | "referrals" | "payment_method")[] | undefined;
}, {
id?: string | null | undefined;
name?: string | null | undefined;
email?: string | null | undefined;
fingerprint?: string | null | undefined;
metadata?: Record<string, any> | undefined;
expand?: ("invoices" | "rewards" | "trials_used" | "entities" | "referrals" | "payment_method")[] | undefined;
}>;
type CreateCustomerParams = z.infer<typeof CreateCustomerParamsSchema>;
interface UpdateCustomerParams {
id?: string | null;
name?: string | null;
email?: string | null;
fingerprint?: string | null;
}
declare const BillingPortalParamsSchema: z.ZodObject<{
return_url: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
return_url?: string | undefined;
}, {
return_url?: string | undefined;
}>;
type BillingPortalParams = z.infer<typeof BillingPortalParamsSchema>;
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 CancelParamsSchema: z.ZodObject<{
customer_id: z.ZodString;
product_id: z.ZodString;
entity_id: z.ZodOptional<z.ZodString>;
cancel_immediately: z.ZodOptional<z.ZodBoolean>;
}, "strip", z.ZodTypeAny, {
customer_id: string;
product_id: string;
entity_id?: string | undefined;
cancel_immediately?: boolean | undefined;
}, {
customer_id: string;
product_id: string;
entity_id?: string | undefined;
cancel_immediately?: boolean | undefined;
}>;
type CancelParams = z.infer<typeof CancelParamsSchema>;
declare const TrackParamsSchema: z.ZodObject<{
customer_id: z.ZodString;
value: z.ZodOptional<z.ZodNumber>;
feature_id: z.ZodOptional<z.ZodString>;
event_name: z.ZodOptional<z.ZodString>;
entity_id: z.ZodOptional<z.ZodString>;
customer_data: z.ZodOptional<z.ZodAny>;
idempotency_key: z.ZodOptional<z.ZodString>;
entity_data: z.ZodOptional<z.ZodAny>;
}, "strip", z.ZodTypeAny, {
customer_id: string;
feature_id?: string | undefined;
entity_id?: string | undefined;
value?: number | undefined;
customer_data?: any;
entity_data?: any;
event_name?: string | undefined;
idempotency_key?: string | undefined;
}, {
customer_id: string;
feature_id?: string | undefined;
entity_id?: string | undefined;
value?: number | undefined;
customer_data?: any;
entity_data?: any;
event_name?: string | undefined;
idempotency_key?: string | undefined;
}>;
type TrackParams = z.infer<typeof TrackParamsSchema>;
declare const CheckParamsSchema: z.ZodObject<{
customer_id: z.ZodString;
feature_id: z.ZodOptional<z.ZodString>;
product_id: z.ZodOptional<z.ZodString>;
entity_id: z.ZodOptional<z.ZodString>;
customer_data: z.ZodOptional<z.ZodAny>;
required_balance: z.ZodOptional<z.ZodNumber>;
send_event: z.ZodOptional<z.ZodBoolean>;
with_preview: z.ZodOptional<z.ZodBoolean>;
entity_data: z.ZodOptional<z.ZodObject<{
name: z.ZodOptional<z.ZodString>;
feature_id: z.ZodString;
}, "strip", z.ZodTypeAny, {
feature_id: string;
name?: string | undefined;
}, {
feature_id: string;
name?: string | undefined;
}>>;
}, "strip", z.ZodTypeAny, {
customer_id: string;
feature_id?: string | undefined;
entity_id?: string | undefined;
required_balance?: number | undefined;
product_id?: string | undefined;
customer_data?: any;
entity_data?: {
feature_id: string;
name?: string | undefined;
} | undefined;
send_event?: boolean | undefined;
with_preview?: boolean | undefined;
}, {
customer_id: string;
feature_id?: string | undefined;
entity_id?: string | undefined;
required_balance?: number | undefined;
product_id?: string | undefined;
customer_data?: any;
entity_data?: {
feature_id: string;
name?: string | undefined;
} | undefined;
send_event?: boolean | undefined;
with_preview?: boolean | undefined;
}>;
type CheckParams = z.infer<typeof CheckParamsSchema>;
type CheckResult = CheckFeatureResult & CheckProductResult;
interface UsageParams {
customer_id: string;
feature_id: string;
value: number;
customer_data?: CustomerData;
}
interface UsageResult {
code: string;
customer_id: string;
feature_id: string;
}
interface SetupPaymentParams {
customer_id: string;
success_url?: string;
checkout_session_params?: Record<string, any>;
}
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 AttachParamsSchema: z.ZodObject<{
customer_id: z.ZodString;
product_id: z.ZodOptional<z.ZodString>;
entity_id: z.ZodOptional<z.ZodString>;
options: z.ZodOptional<z.ZodArray<z.ZodObject<{
feature_id: z.ZodString;
quantity: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
feature_id: string;
quantity: number;
}, {
feature_id: string;
quantity: number;
}>, "many">>;
product_ids: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
free_trial: z.ZodOptional<z.ZodBoolean>;
success_url: z.ZodOptional<z.ZodString>;
metadata: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
force_checkout: z.ZodOptional<z.ZodBoolean>;
customer_data: z.ZodOptional<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;
}>>;
entity_data: z.ZodOptional<z.ZodAny>;
checkout_session_params: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
reward: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
customer_id: string;
entity_id?: string | undefined;
options?: {
feature_id: string;
quantity: number;
}[] | undefined;
metadata?: Record<string, string> | undefined;
product_id?: string | undefined;
product_ids?: string[] | undefined;
free_trial?: boolean | undefined;
success_url?: string | undefined;
force_checkout?: boolean | undefined;
customer_data?: {
name?: string | null | undefined;
email?: string | null | undefined;
fingerprint?: string | null | undefined;
} | undefined;
entity_data?: any;
checkout_session_params?: Record<string, any> | undefined;
reward?: string | undefined;
}, {
customer_id: string;
entity_id?: string | undefined;
options?: {
feature_id: string;
quantity: number;
}[] | undefined;
metadata?: Record<string, string> | undefined;
product_id?: string | undefined;
product_ids?: string[] | undefined;
free_trial?: boolean | undefined;
success_url?: string | undefined;
force_checkout?: boolean | undefined;
customer_data?: {
name?: string | null | undefined;
email?: string | null | undefined;
fingerprint?: string | null | undefined;
} | undefined;
entity_data?: any;
checkout_session_params?: Record<string, any> | undefined;
reward?: string | undefined;
}>;
type AttachParams = z.infer<typeof AttachParamsSchema>;
declare const CheckoutParamsSchema: z.ZodObject<{
customer_id: z.ZodString;
product_id: z.ZodString;
entity_id: z.ZodOptional<z.ZodString>;
success_url: z.ZodOptional<z.ZodString>;
customer_data: z.ZodOptional<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;
}>>;
options: z.ZodOptional<z.ZodArray<z.ZodObject<{
feature_id: z.ZodString;
quantity: z.ZodNumber;
}, "strip", z.ZodTypeAny, {
feature_id: string;
quantity: number;
}, {
feature_id: string;
quantity: number;
}>, "many">>;
}, "strip", z.ZodTypeAny, {
customer_id: string;
product_id: string;
entity_id?: string | undefined;
options?: {
feature_id: string;
quantity: number;
}[] | undefined;
success_url?: string | undefined;
customer_data?: {
name?: string | null | undefined;
email?: string | null | undefined;
fingerprint?: string | null | undefined;
} | undefined;
}, {
customer_id: string;
product_id: string;
entity_id?: string | undefined;
options?: {
feature_id: string;
quantity: number;
}[] | undefined;
success_url?: string | undefined;
customer_data?: {
name?: string | null | undefined;
email?: string | null | undefined;
fingerprint?: string | null | undefined;
} | undefined;
}>;
type CheckoutParams = z.infer<typeof CheckoutParamsSchema>;
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;
};
};
declare class Autumn {
private readonly secretKey;
private readonly publishableKey;
private headers;
private url;
private logger;
constructor(options?: {
secretKey?: string;
publishableKey?: string;
url?: string;
version?: string;
headers?: Record<string, string>;
logLevel?: string;
});
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<BillingPortalResult, AutumnError>>;
};
static products: {
get: (id: string) => Promise<Result<Product, AutumnError>>;
create: (params?: CreateProductParams) => Promise<Result<Product, AutumnError>>;
list: (params?: ListProductsParams) => Promise<Result<{
list: 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<BillingPortalResult, AutumnError>>;
};
products: {
get: (id: string) => Promise<Result<Product, AutumnError>>;
create: (params?: CreateProductParams) => Promise<Result<Product, AutumnError>>;
list: (params?: ListProductsParams) => Promise<Result<{
list: 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 checkout: (params: CheckoutParams) => Promise<Result<CheckoutResult, AutumnError>>;
checkout(params: CheckoutParams): Promise<Result<CheckoutResult, AutumnError>>;
static attach: (params: AttachParams) => Promise<Result<{
customer_id: string;
code: string;
message: string;
product_ids: string[];
customer_data?: any;
checkout_url?: string | undefined;
}, AutumnError>>;
static usage: (params: UsageParams) => Promise<Result<UsageResult, AutumnError>>;
attach(params: AttachParams): Promise<Result<{
customer_id: string;
code: string;
message: string;
product_ids: string[];
customer_data?: any;
checkout_url?: string | undefined;
}, AutumnError>>;
static setupPayment: (params: SetupPaymentParams) => Promise<Result<SetupPaymentResult, AutumnError>>;
setupPayment(params: SetupPaymentParams): Promise<Result<SetupPaymentResult, AutumnError>>;
static cancel: (params: CancelParams) => Promise<Result<{
customer_id: string;
product_id: string;
success: boolean;
}, AutumnError>>;
cancel(params: CancelParams): Promise<Result<{
customer_id: string;
product_id: string;
success: boolean;
}, AutumnError>>;
static check: (params: CheckParams) => Promise<Result<CheckResult, AutumnError>>;
check(params: CheckParams): Promise<Result<CheckResult, AutumnError>>;
static track: (params: TrackParams) => Promise<Result<{
customer_id: string;
code: string;
id: string;
feature_id?: string | undefined;
event_name?: string | undefined;
}, AutumnError>>;
track(params: TrackParams): Promise<Result<{
customer_id: string;
code: string;
id: string;
feature_id?: string | undefined;
event_name?: string | undefined;
}, AutumnError>>;
usage(params: UsageParams): Promise<Result<UsageResult, AutumnError>>;
}
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[];
}
declare const CreateReferralCodeParamsSchema: z.ZodObject<{
customer_id: z.ZodString;
program_id: z.ZodString;
}, "strip", z.ZodTypeAny, {
customer_id: string;
program_id: string;
}, {
customer_id: string;
program_id: string;
}>;
type CreateReferralCodeParams = z.infer<typeof CreateReferralCodeParamsSchema>;
declare const RedeemReferralCodeParamsSchema: z.ZodObject<{
code: z.ZodString;
customer_id: z.ZodString;
}, "strip", z.ZodTypeAny, {
customer_id: string;
code: string;
}, {
customer_id: string;
code: string;
}>;
type RedeemReferralCodeParams = z.infer<typeof RedeemReferralCodeParamsSchema>;
export { Autumn as A, type CustomerData as C };