import type { BetterAuthPlugin } from "better-auth/plugins";
import {
DEFAULT_CART_TABLE_NAME,
DEFAULT_MAX_CART_ITEMS,
DEFAULT_MAX_ITEM_PRICE,
DEFAULT_MAX_ITEM_QUANTITY,
createErrorCodes,
} from "./constants";
import { createEndpoints } from "./routes";
import { CartPluginOptions } from "./types";
/**
* Cart plugin for BetterAuth
* Provides shopping cart functionality with database storage
*/
export const cart = (options?: CartPluginOptions): BetterAuthPlugin => {
// Set defaults
const cartTableName = options?.cartTableName || DEFAULT_CART_TABLE_NAME;
const maxCartItems = options?.maxCartItems || DEFAULT_MAX_CART_ITEMS;
const maxItemPrice = options?.maxItemPrice || DEFAULT_MAX_ITEM_PRICE;
const maxItemQuantity = options?.maxItemQuantity || DEFAULT_MAX_ITEM_QUANTITY;
// Create error codes
const ERROR_CODES = createErrorCodes(
maxCartItems,
maxItemPrice,
maxItemQuantity,
);
// Define database schema
const schema = {
[cartTableName]: {
fields: {
userId: {
type: "string" as const,
required: true,
},
productId: {
type: "string" as const,
required: true,
},
name: {
type: "string" as const,
required: true,
},
price: {
type: "number" as const,
required: true,
},
quantity: {
type: "number" as const,
required: true,
},
image: {
type: "string" as const,
required: false,
},
createdAt: {
type: "date" as const,
required: true,
},
updatedAt: {
type: "date" as const,
required: true,
},
},
modelName: cartTableName,
},
};
// Create endpoints
const endpoints = createEndpoints(cartTableName, ERROR_CODES, {
maxCartItems,
maxItemPrice,
maxItemQuantity,
onCheckout: options?.onCheckout,
});
return {
id: "cart",
schema,
endpoints,
$ERROR_CODES: ERROR_CODES,
};
};
// Re-export types
export * from "./types";
import type { BetterAuthPlugin } from "better-auth/plugins";
import {
DEFAULT_CART_TABLE_NAME,
DEFAULT_MAX_CART_ITEMS,
DEFAULT_MAX_ITEM_PRICE,
DEFAULT_MAX_ITEM_QUANTITY,
createErrorCodes,
} from "./constants";
import { createEndpoints } from "./routes";
import { CartPluginOptions } from "./types";
/**
* Cart plugin for BetterAuth
* Provides shopping cart functionality with database storage
*/
export const cart = (options?: CartPluginOptions): BetterAuthPlugin => {
// Set defaults
const cartTableName = options?.cartTableName || DEFAULT_CART_TABLE_NAME;
const maxCartItems = options?.maxCartItems || DEFAULT_MAX_CART_ITEMS;
const maxItemPrice = options?.maxItemPrice || DEFAULT_MAX_ITEM_PRICE;
const maxItemQuantity = options?.maxItemQuantity || DEFAULT_MAX_ITEM_QUANTITY;
// Create error codes
const ERROR_CODES = createErrorCodes(
maxCartItems,
maxItemPrice,
maxItemQuantity,
);
// Define database schema
const schema = {
[cartTableName]: {
fields: {
userId: {
type: "string" as const,
required: true,
},
productId: {
type: "string" as const,
required: true,
},
name: {
type: "string" as const,
required: true,
},
price: {
type: "number" as const,
required: true,
},
quantity: {
type: "number" as const,
required: true,
},
image: {
type: "string" as const,
required: false,
},
createdAt: {
type: "date" as const,
required: true,
},
updatedAt: {
type: "date" as const,
required: true,
},
},
modelName: cartTableName,
},
};
// Create endpoints
const endpoints = createEndpoints(cartTableName, ERROR_CODES, {
maxCartItems,
maxItemPrice,
maxItemQuantity,
onCheckout: options?.onCheckout,
});
return {
id: "cart",
schema,
endpoints,
$ERROR_CODES: ERROR_CODES,
};
};
// Re-export types
export * from "./types";