autumn-js
Version:
Autumn JS Library
308 lines (286 loc) • 8.23 kB
TypeScript
import * as React$1 from 'react';
import React__default from 'react';
import * as swr from 'swr';
import * as react_jsx_runtime from 'react/jsx-runtime';
import { NextRequest, NextResponse } from 'next/server';
interface EntityDataParams {
name?: string;
featureId: string;
}
interface AttachParams {
productId: string;
entityId?: string;
options?: {
featureId: string;
quantity: number;
}[];
successUrl?: string;
forceCheckout?: boolean;
metadata?: Record<string, string>;
dialog?: () => JSX.Element | React__default.ReactNode;
callback?: () => Promise<void>;
withPreview?: "formatted" | "raw";
entityData?: EntityDataParams;
openInNewTab?: boolean;
reward?: string;
}
interface CreateEntityParams {
id: string;
name: string;
featureId: string;
}
interface CancelParams {
productId: string;
entityId?: string;
cancelImmediately?: boolean;
}
interface TrackParams {
featureId?: string;
eventName?: string;
entityId?: string;
value?: number;
idempotencyKey?: string;
entityData?: EntityDataParams;
}
declare const useAutumn: () => {
attach: ({ productId, entityId, options, successUrl, forceCheckout, metadata, dialog, callback, entityData, openInNewTab, }: AttachParams) => Promise<any>;
check: ({ featureId, productId, entityId, requiredBalance, sendEvent, withPreview, dialog, entityData, }: {
featureId?: string;
productId?: string;
entityId?: string;
requiredBalance?: number;
sendEvent?: boolean;
withPreview?: "formatted" | "raw";
dialog?: (data: any) => JSX.Element | React.ReactNode;
entityData?: EntityDataParams;
}) => Promise<any>;
track: (params: TrackParams) => Promise<any>;
cancel: (params: CancelParams) => Promise<any>;
openBillingPortal: (options?: {
returnUrl?: string;
}) => Promise<any>;
};
declare enum AppEnv {
Sandbox = "sandbox",
Live = "live"
}
declare enum ProductItemInterval {
Minute = "minute",
Hour = "hour",
Day = "day",
Week = "week",
Month = "month",
Quarter = "quarter",
SemiAnnual = "semi_annual",
Year = "year",
Multiple = "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 CustomerInvoice {
product_ids: string[];
stripe_id: string;
status: string;
total: number;
currency: string;
created_at: number;
}
type EntityExpandOption = "invoices";
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 PricingTableProduct {
id: string;
name: string;
buttonText: string;
price: {
primaryText: string;
secondaryText?: string;
};
items: {
primaryText: string;
secondaryText?: string;
}[];
}
declare class AutumnClientError extends Error {
code: string;
constructor({ message, code }: {
message: string;
code: string;
});
toString(): string;
toJSON(): {
message: string;
code: string;
};
}
interface AutumnContextParams {
encryptedCustomerId?: string;
customerData?: CustomerData;
authProvider?: "clerk" | "better-auth";
customer: Customer | null;
setCustomer: (customer: Customer | null) => void;
entity: Entity | null;
setEntity: (entity: Entity | null) => void;
entityId?: string;
prodChangeDialog: {
found: boolean;
setProps: (props: any) => void;
setOpen: (open: boolean) => void;
setComponent: (component: any) => void;
};
paywallDialog: {
found: boolean;
setProps: (props: any) => void;
setOpen: (open: boolean) => void;
setComponent: (component: any) => void;
};
pricingTableProducts: PricingTableProduct[] | null;
setPricingTableProducts: (products: PricingTableProduct[]) => void;
}
interface UseCustomerProps {
expand?: CustomerExpandOption[];
autoCreate?: boolean;
encryptedCustomerId?: string;
customerData?: CustomerData;
errorOnNotFound?: boolean;
}
declare const useCustomer: (options?: UseCustomerProps) => {
customer: Customer | null;
isLoading: boolean;
error: AutumnClientError | null;
refetch: () => Promise<void>;
createEntity: (params: CreateEntityParams | CreateEntityParams[]) => Promise<any>;
deleteEntity: (entityId: string) => Promise<any>;
};
declare const usePricingTable: (options?: {
productDetails: {
id: string;
description?: string;
buttonText?: string;
buttonUrl?: string;
recommendText?: string;
}[];
}) => {
products: any[] | null;
isLoading: boolean;
error: any;
refetch: () => Promise<PricingTableProduct[] | null>;
};
declare const useEntity: (entityId?: string, params?: GetEntityParams) => {
entity: any;
isLoading: boolean;
error: any;
refetch: swr.KeyedMutator<any>;
};
declare const AutumnContext: React$1.Context<AutumnContextParams>;
declare const useAutumnContext: () => AutumnContextParams;
declare const withAuth: <T extends {
customerId?: string;
customerData?: CustomerData;
authProvider?: string;
}>({ fn, withCustomerData, requireCustomer, }: {
fn: (args: T) => Promise<any>;
withCustomerData?: boolean;
requireCustomer?: boolean;
}) => (args: Omit<T, "customerId"> & {
encryptedCustomerId?: string;
}, request?: Request) => Promise<any>;
interface AuthPluginOptions {
provider: "better-auth" | "supabase" | "clerk";
instance?: any;
useOrg?: boolean;
useUser?: boolean;
}
declare global {
var __autumnAuth: AuthPluginOptions;
}
interface AutumnProviderProps {
customerId?: string;
customerData?: CustomerData;
authPlugin?: AuthPluginOptions;
children?: React.ReactNode;
}
declare const AutumnProvider: ({ customerId, customerData, authPlugin, children, }: AutumnProviderProps) => react_jsx_runtime.JSX.Element;
type AuthResult = Promise<{
customerId?: string;
customerData?: {
name?: string;
email?: string;
};
} | null>;
declare function autumnHandler(options: {
identify: (request: NextRequest) => AuthResult;
url?: string;
secretKey?: string;
}): {
GET: (request: NextRequest) => Promise<NextResponse<any>>;
POST: (request: NextRequest) => Promise<NextResponse<any>>;
};
export { AutumnContext, AutumnProvider, type UseCustomerProps, autumnHandler, useAutumn, useAutumnContext, useCustomer, useEntity, usePricingTable, withAuth };