autumn-js
Version:
Autumn JS Library
664 lines (638 loc) • 20.8 kB
TypeScript
import * as react_jsx_runtime from 'react/jsx-runtime';
import { C as CheckFeatureResult, a as CheckProductResult, P as ProductItem, b as Product, c as ProductScenario, d as CustomerProduct, e as CustomerFeature, f as CustomerInvoice, g as CustomerData, h as CreateCustomerParams, i as Customer, B as BillingPortalResult, j as CustomerExpandOption } from './AutumnProvider-BKgOhoik.js';
export { A as AutumnProvider } from './AutumnProvider-BKgOhoik.js';
import { z } from 'zod';
import * as swr from 'swr';
import { SWRConfiguration } from 'swr';
import * as React$1 from 'react';
import { NextRequest, NextResponse } from 'next/server';
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 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, {
code: string;
customer_id: string;
id: string;
feature_id?: string | undefined;
event_name?: string | undefined;
}, {
code: string;
customer_id: 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, {
code: string;
message: string;
customer_id: string;
product_ids: string[];
customer_data?: any;
checkout_url?: string | undefined;
}, {
code: string;
message: string;
customer_id: 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;
}
type EntityExpandOption = "invoices";
interface DeleteEntityResult {
success: boolean;
}
interface GetEntityParams$1 {
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 CreateReferralCodeResult {
code: string;
customer_id: string;
created_at: number;
}
interface RedeemReferralCodeResult {
id: string;
customer_id: string;
reward_id: string;
applied: boolean;
}
declare const NextAutumnProvider: ({ encryptedCustomerId, customerData, children, }: {
encryptedCustomerId?: string;
customerData?: CustomerData;
children: React.ReactNode;
}) => react_jsx_runtime.JSX.Element;
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 AutumnClientConfig {
backendUrl?: string;
getBearerToken?: () => Promise<string | null | undefined>;
customerData?: CustomerData;
includeCredentials?: boolean;
betterAuthUrl?: string;
}
declare class AutumnClient {
readonly backendUrl?: string;
protected readonly getBearerToken?: () => Promise<string | null | undefined>;
protected readonly customerData?: CustomerData;
protected includeCredentials?: boolean;
readonly prefix: string;
constructor({ backendUrl, getBearerToken, customerData, includeCredentials, betterAuthUrl, }: AutumnClientConfig);
/**
* Detects if the backend supports CORS credentials by making an OPTIONS request
*/
private detectCors;
/**
* Automatically determines whether to include credentials based on CORS detection
*/
private shouldIncludeCredentials;
getHeaders(): Promise<any>;
handleFetch({ path, method, body, }: {
path: string;
method: string;
body?: any;
}): Promise<Result<any, AutumnError>>;
post(path: string, body: any): Promise<Result<any, AutumnError>>;
get(path: string): Promise<Result<any, AutumnError>>;
delete(path: string): Promise<Result<any, AutumnError>>;
createCustomer(params: Omit<CreateCustomerParams, "id" | "data"> & {
errorOnNotFound?: boolean;
}): Promise<Result<Customer, AutumnError>>;
getPricingTable(): Promise<Result<{
list: PricingTableProduct[];
}, AutumnError>>;
attach: (params: {
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;
}) => AutumnPromise<{
code: string;
message: string;
customer_id: string;
product_ids: string[];
customer_data?: any;
checkout_url?: string | undefined;
}>;
checkout: (params: {
productId: string;
options?: {
quantity: number;
featureId: string;
}[] | undefined;
entityId?: string | undefined;
dialog?: any;
successUrl?: string | undefined;
openInNewTab?: boolean | undefined;
}) => AutumnPromise<CheckoutResult>;
cancel: (params: {
productId: string;
entityId?: string | undefined;
cancelImmediately?: boolean | undefined;
}) => AutumnPromise<{
customer_id: string;
product_id: string;
success: boolean;
}>;
check: (params: {
productId?: string | undefined;
entityId?: string | undefined;
featureId?: string | undefined;
requiredBalance?: number | undefined;
sendEvent?: boolean | undefined;
withPreview?: boolean | undefined;
dialog?: any;
entityData?: any;
}) => AutumnPromise<CheckResult>;
track: (params: {
value?: number | undefined;
entityId?: string | undefined;
featureId?: string | undefined;
entityData?: any;
eventName?: string | undefined;
idempotencyKey?: string | undefined;
}) => AutumnPromise<{
code: string;
customer_id: string;
id: string;
feature_id?: string | undefined;
event_name?: string | undefined;
}>;
openBillingPortal: (params?: OpenBillingPortalParams | undefined) => AutumnPromise<BillingPortalResult>;
setupPayment: (params: SetupPaymentParams) => AutumnPromise<SetupPaymentResult>;
entities: {
create: (params: CreateEntityParams | CreateEntityParams[]) => AutumnPromise<Entity | Entity[]>;
get: (entityId: string, params?: GetEntityParams | undefined) => AutumnPromise<Entity>;
delete: (entityId: string) => AutumnPromise<DeleteEntityResult>;
};
referrals: {
createCode: (params: {
programId: string;
}) => AutumnPromise<CreateReferralCodeResult>;
redeemCode: (params: {
code: string;
}) => AutumnPromise<RedeemReferralCodeResult>;
};
products: {
list: () => AutumnPromise<{
list: Product[];
}>;
};
}
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;
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;
}[];
}
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$1) => {
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;
}>;
};
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 AutumnDialogContext {
props: any;
setProps: (props: any) => void;
open: boolean;
setOpen: (open: boolean) => void;
setComponent: (component: any) => void;
}
interface AutumnContextParams {
initialized: boolean;
disableDialogs: boolean;
client: AutumnClient;
paywallDialog: AutumnDialogContext;
attachDialog: AutumnDialogContext;
}
declare const AutumnContext: React$1.Context<AutumnContextParams>;
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, NextAutumnProvider, autumnHandler, useAutumn, useCustomer, useEntity, usePricingTable };