// Type definitions for swell-js // Project: https://github.com/swellstores/swell-js#readme // Thanks to contributor Gus Fune // Credit Stackoverflow user ford04 for SnakeToCamelCase function // https://stackoverflow.com/questions/60269936/typescript-convert-generic-object-from-snake-to-camel-case import type { AccountCase, AddressCase, PasswordTokenInputCase, } from './account'; import type { AttributeCase } from './attribute'; import type { CardCase, InputCreateTokenCase, TokenResponseCase } from './card'; import type { CartCase, CartItemCase } from './cart'; import type { CategoryCase } from './category'; import type { ContentCase } from './content'; import type { SelectCurrencyReturnCase, EnabledCurrencyCase, FormatInputCase, } from './currency'; import type { InvoiceCase } from './invoice'; import type { Locale } from './locale'; import type { OrderCase } from './order'; import type { ProductCase, FlexibleProductInputCase, PriceRange, } from './product'; import type { InputPaymentElementCard, InputPaymentElementIdeal, InputPaymentElementPaypal, InputPaymentElementGoogle, InputPaymentElementApple, InputPaymentElementSezzle, InputPaymentRedirect, PaymentCase, } from './payment'; import type { Settings } from './settings'; import type { SubscriptionCase } from './subscription'; import type { MakeCase } from './utils'; export type * from './account'; export type * from './attribute'; export type * from './billing'; export type * from './card'; export type * from './cart'; export type * from './category'; export type * from './content'; export type * from './coupon'; export type * from './currency'; export type * from './discount'; export type * from './giftcard'; export type * from './invoice'; export type * from './locale'; export type * from './order'; export type * from './payment'; export type * from './product'; export type * from './promotion'; export type * from './purchase_link'; export type * from './refund'; export type * from './settings'; export type * from './shipment_rating'; export type * from './subscription'; export type SnakeToCamelCase = S extends `${infer T}_${infer U}` ? `${T}${Capitalize>}` : S; export type ConvertSnakeToCamelCase = { [K in keyof T as SnakeToCamelCase]: T[K]; }; export interface BaseModel { id?: string; date_created?: string; date_updated?: string; } export interface Query { limit?: number; page?: number; where?: Record; expand?: string[] | string; search?: string; [key: string]: unknown; } export interface IncludeQuery { [key: string]: { url: string; data?: Query; params?: Query; }; } export interface InitOptions { currency?: string; key?: string; locale?: string; previewContent?: boolean; session?: string; store?: string; timeout?: number; useCamelCase?: boolean; url?: string; vaultUrl?: string; setCookie?: (key: string, value: string) => void; getCookie?: (key: string) => string | undefined; headers?: Record; getCart?: () => Promise; /** * When overriding `updateCart` you should throw an exception * if the server returns an error, instead of silently returning the error. * This is necessary for the correct processing of payment forms * and payment integrations such as Google Pay and Apple Pay. */ updateCart?: (input: object) => Promise; } export interface InitOptionsCamel extends InitOptions<'camel'> { useCamelCase: true; } export interface ResultsResponse { count: number; limit: number; page: number; pages?: Record< number, { start: number; end: number; } >; page_count?: number; results: T[]; } export type ResultsResponseCamel = ConvertSnakeToCamelCase< ResultsResponse >; type ResultsResponseCase = MakeCase< ResultsResponse, ResultsResponseCamel >; export interface Tax { id?: string; name?: string; amount?: number; priority?: number; rate?: number; } export interface ItemDiscount { id?: string; amount?: number; } export interface Image { caption?: string; file?: { content_type?: string; date_uploaded?: string; filename?: string; height?: number; length?: number; metadata?: object; md5?: string; private?: boolean; url?: string; width?: number; }; id?: string; } export type ImageCamel = ConvertSnakeToCamelCase; export interface SwellError { code: string; message: string; } export interface ServerError { error: SwellError; } export interface ErrorResponse { errors: Record; } export interface ProductQuery extends Query { category?: string; categories?: string[]; $filters?: unknown; } export interface SwellClient { version: string; init(storeId: string, publicKey: string, options?: InitOptions): void; account: { create(input: AccountCase[C]): Promise; login( user: string, password: string | PasswordTokenInputCase[C], ): Promise; logout(): Promise; recover(input: object): Promise; update(input: AccountCase[C]): Promise; get(query?: object): Promise; listAddresses( input?: object, ): Promise[C]>; /** @deprecated use `listAddresses` instead */ getAddresses( input?: object, ): Promise[C]>; createAddress( input: AddressCase[C], ): Promise; updateAddress( id: string, input: AddressCase[C], ): Promise; deleteAddress(id: string): Promise; listCards(query?: object): Promise[C]>; /** @deprecated use `listCards` instead */ getCards(query?: object): Promise[C]>; createCard(input: CardCase[C]): Promise; updateCard( id: string, input: CardCase[C], ): Promise; deleteCard(id: string): Promise; listOrders(query?: object): Promise[C]>; /** @deprecated use `listOrders` instead */ getOrders(query?: object): Promise[C]>; getOrder(id: string): Promise; }; attributes: { get(id: string, query?: object): Promise; list(query?: object): Promise[C]>; }; card: { createToken(input: InputCreateTokenCase[C]): Promise; validateCVC(code: string): boolean; validateExpiry(month: string, year: string): boolean; validateNumber(cardNumber: string): boolean; }; cart: { get(): Promise; update(input: object): Promise; recover(input: string): Promise; getSettings(): Promise; getShippingRates(): Promise; addItem( input: Partial, ): Promise; setItems( input: Partial[], ): Promise; removeItem(id: string): Promise; updateItem( id: string, input: Partial, ): Promise; applyCoupon(code: string): Promise; removeCoupon(): Promise; applyGiftcard(code: string): Promise; removeGiftcard(id: string): Promise; submitOrder(): Promise; getOrder(checkoutId?: string): Promise; }; categories: { get(id: string, query?: object): Promise; list(query?: object): Promise[C]>; }; content: { get( type: string, id: string, query?: Query, ): Promise[C]>; list( type: string, query?: Query, ): Promise[C]>; }; currency: { format(amount: number, format: FormatInputCase[C]): string; set(code?: string): EnabledCurrencyCase[C]; get(): EnabledCurrencyCase[C]; list(): EnabledCurrencyCase[C][] | Promise; select(currency: string): Promise; selected(): string; }; locale: { get(): Locale; set(code: string): Locale; list(): Locale[] | Promise; select(locale: string): Promise<{ locale: string }>; selected(): string; }; payment: { get(id: string): Promise; methods(): Promise; createElements(input: { card?: InputPaymentElementCard; ideal?: InputPaymentElementIdeal; paypal?: InputPaymentElementPaypal; google?: InputPaymentElementGoogle; apple?: InputPaymentElementApple; sezzle?: InputPaymentElementSezzle; }): Promise; tokenize(input?: { card?: object; ideal?: object; klarna?: object; bancontact?: object; paysafecard?: object; amazon?: object; }): Promise; handleRedirect(input?: { card?: InputPaymentRedirect; paysafecard?: InputPaymentRedirect; klarna?: InputPaymentRedirect; bancontact?: InputPaymentRedirect; }): Promise; authenticate(id: string): Promise; resetAsyncPayment(id: string): Promise; createIntent(input: { gateway: string; intent: object }): Promise; updateIntent(input: { gateway: string; intent: object }): Promise; authorizeGateway(input: { gateway: string; params?: object; }): Promise; }; products: { categories(products: FlexibleProductInputCase[C]): CategoryCase[C][]; filters(products: FlexibleProductInputCase[C], options?: object): object[]; filterableAttributeFilters( products: ProductCase[C][], options?: object, ): Promise; get(id: string, query?: ProductQuery): Promise; list(query?: ProductQuery): Promise[C]>; priceRange(product: FlexibleProductInputCase[C]): PriceRange; attributes(products: FlexibleProductInputCase[C]): AttributeCase[C][]; variation( product: ProductCase[C], options: object, purchaseOption?: object, ): ProductCase[C]; }; settings: { get( id?: string, defaultValue?: string | number | Settings, ): Settings | Promise; refresh(): Promise; getCurrentLocale(): string; getStoreLocale(): string; getStoreLocales(): Locale[]; load(): Promise; menus(id?: string, defaultValue?: unknown): Settings | Promise; payments( id?: string, defaultValue?: string | number | Settings, ): Settings | Promise; subscriptions( id?: string, defaultValue?: string | number | Settings, ): Settings | Promise; session( id?: string, defaultValue?: string | number | Settings, ): Settings | Promise; }; subscriptions: { create(input: object): Promise; update( id: string, input: object, ): Promise; list(query?: object): Promise[C]>; get(id: string, query?: object): Promise; addItem( id: string, input: object, ): Promise; setItems( id: string, input: object[], ): Promise; updateItem( id: string, itemId: string, input: object, ): Promise; removeItem( id: string, itemId: string, ): Promise; }; invoices: { get(id: string, query?: object): Promise; list(query?: object): Promise[C]>; }; session: { get(): Promise>; getCookie(): string | undefined; setCookie(value: string): void; }; functions: { request( method: string, appId: string, functionName: string, data?: unknown, options?: unknown, ): Promise; get( appId: string, functionName: string, data?: unknown, options?: unknown, ): Promise; put( appId: string, functionName: string, data?: unknown, options?: unknown, ): Promise; post( appId: string, functionName: string, data?: unknown, options?: unknown, ): Promise; }; utils: { get: typeof import('lodash-es/get').default; set: typeof import('lodash-es/set').default; uniq: typeof import('lodash-es/uniq').default; find: typeof import('lodash-es/find').default; round: typeof import('lodash-es/round').default; pick: typeof import('lodash-es/pick').default; findIndex: typeof import('lodash-es/findIndex').default; cloneDeep: typeof import('lodash-es/cloneDeep').default; toNumber: typeof import('lodash-es/toNumber').default; toLower: typeof import('lodash-es/toLower').default; isEqual: typeof import('lodash-es/isEqual').default; isEmpty: typeof import('lodash-es/isEmpty').default; merge: typeof import('deepmerge'); map(arr: T[], mapper: (item: T) => R): R[]; reduce( arr: T[], reducer: (acc: R, item: T, index: number) => R, init: R, ): R; toSnake(obj: T): R; toCamel(obj: T): R; snakeCase(str: string): string; camelCase(str: string): string; trimStart(str: string): string; trimBoth(str: string): string; trimEnd(str: string): string; stringifyQuery(query: object): string; base64Encode(input: string): string; }; // Backward compatible functions auth: SwellClient['init']; request( method: string, url: string, id?: string, data?: unknown, options?: unknown, ): Promise; get(url: string, query?: Query): Promise; put(url: string, data?: unknown): Promise; post(url: string, data?: unknown): Promise; delete(url: string, data?: unknown): Promise; getCookie(key: string): string | undefined; setCookie(key: string, value: string, options?: Record): void; } export interface SwellClientDefault extends SwellClient { create( store?: string, key?: string, options?: InitOptions, ): SwellClient; create( store?: string, key?: string, options?: InitOptionsCamel, ): SwellClient<'camel'>; default: SwellClientDefault<'snake'>; } declare const swell: SwellClientDefault<'snake'>; export as namespace swell; export = swell;