@betterstore/sdk
Version:
E-commerce for Developers
571 lines (549 loc) • 17.8 kB
text/typescript
import * as axios from 'axios';
declare const createApiClient: (apiKey: string, proxy?: string) => axios.AxiosInstance;
type ShippingRate = ZasilkovnaRate;
interface BaseRate {
provider: string;
name: string;
price: number;
}
interface ZasilkovnaRate extends BaseRate {
provider: "zasilkovna";
clientSecret: string;
}
type ProductData = Pick<Product, "title" | "description" | "images" | "category" | "tags" | "sku" | "barcode" | "vendor" | "isPhysical" | "weightInGrams" | "heightInCm" | "widthInCm" | "lengthInCm" | "priceInCents" | "billingType" | "billingInterval" | "billingIntervalCount"> & {
productId: string;
selectedVariant: Pick<ProductVariant, "title" | "description" | "sku" | "barcode" | "images" | "isPhysical" | "weightInGrams" | "heightInCm" | "widthInCm" | "lengthInCm" | "priceInCents" | "billingType" | "billingInterval" | "billingIntervalCount" | "variantOptions" | "metadata">;
};
interface LineItem {
quantity: number;
variantOptions: {
name: string;
value: string;
}[];
productData: ProductData;
metadata?: string;
}
interface LineItemCreate extends Omit<LineItem, "productData" | "product" | "metadata" | "variantOptions"> {
variantOptions?: {
name: string;
value: string;
}[];
productId: string;
product?: ProductWithoutVariants;
metadata?: Record<string, any>;
}
type Currency = string;
interface CheckoutCreateParams {
type: "hosted" | "embed";
customerId?: string;
lineItems: LineItemCreate[];
currency?: Currency;
discountCodes?: string[];
}
interface CheckoutUpdateParams {
customerId?: string;
shipmentData?: {
provider: string;
name?: string;
pickupPointId?: string;
trackingNumber?: string;
trackingUrl?: string;
};
}
type ShipmentData = {
provider: string;
name?: string;
service?: string;
pickupPointId?: string;
trackingId?: string;
trackingUrl?: string;
};
interface CheckoutSession {
id: string;
testmode: boolean;
clientSecret: string;
customer?: {
id: string;
address?: Address;
email?: string;
};
lineItems: LineItem[];
tax: number | null;
shipping: number | null;
discountAmount: number | null;
appliedDiscounts: {
id: string;
amount: number;
allowedLineItems: {
productId: string;
quantity: number;
}[];
discount: Discount;
}[];
currency: string;
exchangeRate: number | null;
shipmentData: ShipmentData | null;
status: "IN_PROGRESS" | "PAYMENT_PENDING" | "ABANDONED" | "CANCELED" | "FAILED";
}
type SortOrder = "asc" | "desc";
type GetListParams<A, B> = {
sortBy?: A;
sortOrder?: SortOrder;
query?: B & {
NOT?: B | B[];
OR?: B[];
};
};
type EnumQueryType<T> = {
equals?: T;
in?: T[];
not?: T;
notIn?: T[];
};
type StringArrayQueryType<T = string> = {
equals?: T[];
has?: T;
hasEvery?: T[];
hasSome?: T[];
isEmpty?: boolean;
};
type DateQueryType = {
equals?: Date;
in?: Date[];
not?: Date;
notIn?: Date[];
gt?: Date;
gte?: Date;
lt?: Date;
lte?: Date;
};
type OptionalDateQueryType = DateQueryType & {
isSet: boolean;
};
type ArrayModelQueryType<T> = {
some?: T;
every?: T;
none?: T;
};
interface VariantOption {
name: string;
value: string;
}
interface ProductVariant {
title: string;
description?: string;
images: string[];
trackInventory: boolean;
sku?: string;
barcode?: string;
stockAvailable: number;
stockCommited: number;
stockUnavailable: number;
isPhysical: boolean;
weightInGrams?: number;
heightInCm?: number;
widthInCm?: number;
lengthInCm?: number;
priceInCents: number;
billingType: ProductBillingType;
billingInterval: ProductBillingInterval;
billingIntervalCount: number;
metadata?: Record<string, any>;
variantOptions: VariantOption[];
}
interface ProductOption {
name: string;
values: string[];
}
type ProductStatus = "DRAFT" | "ACTIVE" | "ARCHIVED";
type ProductBillingInterval = "DAY" | "WEEK" | "MONTH" | "YEAR";
type ProductBillingType = "ONE_TIME" | "SUBSCRIPTION";
interface Product {
id: string;
createdAt: Date;
updatedAt: Date;
title: string;
description?: string;
images: string[];
category: string;
tags: string[];
isPhysical: boolean;
weightInGrams?: number;
heightInCm?: number;
widthInCm?: number;
lengthInCm?: number;
priceInCents: number;
billingType: ProductBillingType;
billingInterval: ProductBillingInterval;
billingIntervalCount: number;
trackInventory: boolean;
sku?: string;
barcode?: string;
stockAvailable: number;
stockCommited: number;
stockUnavailable: number;
seoPageTitle?: string;
seoDescription?: string;
seoHandle?: string;
vendor?: string;
metadata?: Record<string, any>;
status: ProductStatus;
options: ProductOption[];
productVariants: ProductVariant[];
}
interface ProductWithoutVariants extends Omit<Product, "productVariants"> {
}
type ListProductsQuery = {
collectionIDs?: StringArrayQueryType;
collections?: ArrayModelQueryType<{
seoHandle?: StringArrayQueryType;
}>;
tags?: StringArrayQueryType;
createdAt?: DateQueryType;
updatedAt?: DateQueryType;
};
type ListProductsSortBy = "createdAt" | "updatedAt" | "title" | "stockAvailable" | "stockCommited" | "priceInCents";
type ListProductsParams = GetListParams<ListProductsSortBy, ListProductsQuery>;
type RetrieveProductParams = {
seoHandle: string;
} | {
id: string;
};
interface Collection {
id: string;
title: string;
description?: string;
images: string[];
tags: string[];
seoPageTitle?: string;
seoDescription?: string;
seoHandle?: string;
}
interface CollectionWithProducts extends Collection {
products: ProductWithoutVariants[];
}
type ListCollectionsQuery = undefined;
type ListCollectionsSortBy = "createdAt" | "updatedAt" | "title";
type ListCollectionsParams = GetListParams<ListCollectionsSortBy, ListCollectionsQuery>;
type RetrieveCollectionParams = {
seoHandle: string;
} | {
id: string;
};
type Address = {
name: string;
company?: string;
line1: string;
line2?: string;
city: string;
state?: string;
country: string;
countryCode: string;
zipCode: string;
phone: string;
};
interface CustomerCreateParams {
firstName?: string;
lastName?: string;
email: string;
phone?: string;
address?: Address;
image?: string;
password?: string;
metadata?: Record<string, any>;
tags?: string[];
notes?: string;
isSubscribedEmail?: boolean;
isSubscribedSMS?: boolean;
}
interface CustomerUpdateParams extends Omit<CustomerCreateParams, "email"> {
email?: string;
}
interface Customer$1 extends CustomerCreateParams {
id: string;
createdAt: string;
updatedAt: string;
}
interface CustomerSubscription {
cancelAtPeriodEnd: boolean;
}
type CustomerSubscriptionUpdateParams = Partial<Pick<CustomerSubscription, "cancelAtPeriodEnd">>;
interface Discount {
id: string;
createdAt: Date;
updatedAt: Date;
type: "AMOUNT_OFF_PRODUCTS" | "BUY_X_GET_Y" | "AMOUNT_OFF_ORDER" | "FREE_SHIPPING";
method: "CODE" | "AUTOMATIC";
code?: string | null;
title?: string | null;
value: number;
valueType: "PERCENTAGE" | "FIXED_AMOUNT" | "FREE";
discountScope: "PRODUCTS" | "COLLECTIONS";
allowedProductIDs: string[];
allowedCollectionIDs: string[];
allowedCombinations: ("ORDER" | "PRODUCT" | "SHIPPING")[];
minimumRequirementsType: "NONE" | "MINIMUM_ORDER_AMOUNT" | "MINIMUM_PRODUCT_QUANTITY";
minimumRequirementsValue?: number | null;
requiredProductIDs: string[];
requiredCollectionIDs: string[];
minimumRequirementsScope: "PRODUCTS" | "COLLECTIONS";
maxUses?: number | null;
maxUsesPerCustomer?: number | null;
maxAllowedProductQuantity?: number | null;
uses: number;
subscriptionDiscountDurationType: "ONE_TIME" | "RECURRING" | "FOREVER";
subscriptionDiscountDurationValue: number;
stripeDiscountId?: string | null;
startsAt: Date;
expiresAt?: Date | null;
status: "ACTIVE" | "EXPIRED" | "SCHEDULED";
organizationId: string;
}
type ListDiscountsQuery = {
type?: EnumQueryType<Discount["type"]>;
valueType?: EnumQueryType<Discount["valueType"]>;
method?: EnumQueryType<Discount["method"]>;
status?: EnumQueryType<Discount["status"]>;
minimumRequirementsType?: EnumQueryType<Discount["minimumRequirementsType"]>;
minimumRequirementsScope?: EnumQueryType<Discount["minimumRequirementsScope"]>;
discountScope?: EnumQueryType<Discount["discountScope"]>;
subscriptionDiscountDurationType?: EnumQueryType<Discount["subscriptionDiscountDurationType"]>;
allowedCombinations?: StringArrayQueryType<Discount["allowedCombinations"]>;
allowedProductIDs?: StringArrayQueryType<Discount["allowedProductIDs"]>;
allowedCollectionIDs?: StringArrayQueryType<Discount["allowedCollectionIDs"]>;
requiredProductIDs?: StringArrayQueryType<Discount["requiredProductIDs"]>;
requiredCollectionIDs?: StringArrayQueryType<Discount["requiredCollectionIDs"]>;
startsAt?: DateQueryType;
expiresAt?: OptionalDateQueryType;
createdAt?: DateQueryType;
updatedAt?: DateQueryType;
};
type ListDiscountsSortBy = "createdAt" | "updatedAt" | "expiresAt" | "startsAt";
type ListDiscountsParams = GetListParams<ListDiscountsSortBy, ListDiscountsQuery>;
type RetrieveDiscountParams = {
id: string;
} | {
code: string;
};
interface CustomerSession {
customerId: string;
expiresAt: Date;
testmode: boolean;
token: string;
customer: {
createdAt: Date;
updatedAt: Date;
firstName: string;
lastName: string;
email: string;
phone?: string;
password?: string;
image?: string;
metadata?: string;
stripeCustomerId: string;
isSubscribedEmail: boolean;
isSubscribedSMS: boolean;
address: Address;
};
}
interface OTPLoginParams {
email: string;
}
interface OTPSignupParams extends CustomerCreateParams {
}
interface OTPVerifyParams {
email: string;
otp: string;
}
type OTPLoginResponse = {
success: true;
token: string;
} | {
success: false;
code: "BAD_REQUEST" | "CUSTOMER_NOT_FOUND";
error: string;
};
type OTPSignupResponse = {
success: true;
token: string;
} | {
success: false;
code: "BAD_REQUEST" | "CUSTOMER_ALREADY_EXISTS";
error: string;
};
type OTPVerifyResponse = {
success: true;
customerSession: CustomerSession;
} | {
success: false;
code: "BAD_REQUEST";
error: string;
};
declare class OTP {
private apiClient;
constructor(apiClient: ReturnType<typeof createApiClient>);
signup(params: OTPSignupParams): Promise<OTPSignupResponse>;
login(params: OTPLoginParams): Promise<OTPLoginResponse>;
verify(params: OTPVerifyParams): Promise<OTPVerifyResponse>;
}
declare class Auth {
private apiClient;
otp: OTP;
constructor(apiKey: string, proxy?: string);
retrieveSession(id: string): Promise<CustomerSession | null>;
}
declare class Checkout {
private apiClient;
constructor(apiKey: string, proxy?: string);
/**
* Create a new checkout session
*/
create(params: CheckoutCreateParams): Promise<CheckoutSession>;
/**
* Retrieve a checkout session by ID
*/
retrieve(checkoutId: string): Promise<CheckoutSession | null>;
/**
* Update a checkout session
*/
update(checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession | null>;
/**
* Apply a discount code to a checkout session
*/
applyDiscountCode(checkoutId: string, discountCode: string): Promise<CheckoutSession>;
/**
* Remove a discount from a checkout session
*/
removeDiscount(checkoutId: string, discountId: string): Promise<CheckoutSession | null>;
/**
* Revalidate a checkout session
*/
revalidateDiscounts(checkoutId: string): Promise<CheckoutSession | null>;
/**
* Get shipping rates for a checkout session
*/
getShippingRates(checkoutId: string): Promise<ShippingRate[]>;
/**
* Generate payment secret for a checkout session
*/
generatePaymentSecret(checkoutId: string): Promise<{
paymentSecret: string;
publicKey: string;
checkoutSession: CheckoutSession;
}>;
}
declare class Client {
proxy?: string;
constructor(proxy?: string);
/**
* Retrieve a checkout session by ID
*/
retrieveCheckout(clientSecret: string, checkoutId: string): Promise<CheckoutSession | null>;
/**
* Update a checkout session
*/
updateCheckout(clientSecret: string, checkoutId: string, params: CheckoutUpdateParams): Promise<CheckoutSession | null>;
/**
* Apply a discount code to a checkout session
*/
applyDiscountCode(clientSecret: string, checkoutId: string, discountCode: string): Promise<CheckoutSession>;
/**
* Remove a discount code from a checkout session
*/
removeDiscount(clientSecret: string, checkoutId: string, discountId: string): Promise<CheckoutSession>;
/**
* Revalidate a checkout session
*/
revalidateDiscounts(clientSecret: string, checkoutId: string): Promise<CheckoutSession | null>;
/**
* Get shipping rates for a checkout session
*/
getCheckoutShippingRates(clientSecret: string, checkoutId: string): Promise<ShippingRate[]>;
/**
* Generate payment secret for a checkout session
*/
generateCheckoutPaymentSecret(clientSecret: string, checkoutId: string): Promise<{
paymentSecret: string;
publicKey: string;
checkoutSession: CheckoutSession;
}>;
/**
* Create a new customer
*/
createCustomer(clientSecret: string, params: CustomerCreateParams): Promise<Customer$1>;
/**
* Retrieve a customer by ID or email
*/
retrieveCustomer(clientSecret: string, idOrEmail: string): Promise<Customer$1 | null>;
/**
* Update a customer
*/
updateCustomer(clientSecret: string, customerId: string, params: CustomerUpdateParams): Promise<Customer$1 | null>;
}
declare class Collections {
private apiClient;
constructor(apiKey: string, proxy?: string);
list(params?: ListCollectionsParams): Promise<Collection[]>;
retrieve(params: RetrieveCollectionParams): Promise<CollectionWithProducts | null>;
}
declare class Customer {
private apiClient;
constructor(apiKey: string, proxy?: string);
/**
* Create a new customer
*/
create(params: CustomerCreateParams): Promise<Customer$1>;
/**
* Retrieve a customer by ID or email
*/
retrieve(idOrEmail: string): Promise<Customer$1 | null>;
/**
* Update a customer
*/
update(customerId: string, params: CustomerUpdateParams): Promise<Customer$1 | null>;
/**
* Delete a customer
*/
delete(customerId: string): Promise<void>;
/**
* Update a customer subscription
*/
updateCustomerSubscription(stripeSubscriptionId: string, params: CustomerSubscriptionUpdateParams): Promise<CustomerSubscription | null>;
}
declare class Discounts {
private apiClient;
constructor(apiKey: string, proxy?: string);
list(params?: ListDiscountsParams): Promise<Discount[]>;
retrieve(params: RetrieveDiscountParams): Promise<Discount | null>;
}
declare class Helpers {
proxy?: string;
constructor(proxy?: string);
formatCurrency(currency: string): string;
formatPrice(priceInCents: number, currency: string, exchangeRate?: number | null): string;
getExchangeRate(baseCurrency: string, targetCurrency: string): Promise<number>;
}
declare class Products {
private apiClient;
constructor(apiKey: string, proxy?: string);
list(params?: ListProductsParams): Promise<ProductWithoutVariants[]>;
retrieve(params: RetrieveProductParams): Promise<Product | null>;
}
declare function createBetterStore(config: {
apiKey: string;
proxy?: string;
}): {
checkout: Checkout;
customer: Customer;
discounts: Discounts;
collections: Collections;
products: Products;
auth: Auth;
};
declare function createStoreClient(config?: {
proxy?: string;
}): Client;
declare function createStoreHelpers(config?: {
proxy?: string;
}): Helpers;
export { type Address, type CheckoutCreateParams, type CheckoutSession, type CheckoutUpdateParams, type Collection, type CollectionWithProducts, type Currency, type Customer$1 as Customer, type CustomerCreateParams, type CustomerSession, type CustomerSubscription, type CustomerSubscriptionUpdateParams, type CustomerUpdateParams, type Discount, type LineItem, type LineItemCreate, type ListCollectionsParams, type ListCollectionsQuery, type ListCollectionsSortBy, type ListDiscountsParams, type ListDiscountsQuery, type ListDiscountsSortBy, type ListProductsParams, type ListProductsQuery, type ListProductsSortBy, type OTPLoginParams, type OTPLoginResponse, type OTPSignupParams, type OTPSignupResponse, type OTPVerifyParams, type OTPVerifyResponse, type Product, type ProductBillingInterval, type ProductBillingType, type ProductOption, type ProductStatus, type ProductVariant, type ProductWithoutVariants, type RetrieveCollectionParams, type RetrieveDiscountParams, type RetrieveProductParams, type ShippingRate, type VariantOption, createStoreClient, createStoreHelpers, createBetterStore as default };