UNPKG

@vansite/ts-sharetribe-flex-sdk

Version:

This is a TypeScript SDK for Sharetribe Flex API. It reduces the complexity of the API and provides a more user-friendly interface.

51 lines (50 loc) 1.76 kB
/** * @fileoverview Client for managing listing stock in the Sharetribe Marketplace API. * * Use this to safely update stock levels using atomic compare-and-set operations. * Prevents race conditions when multiple users try to book simultaneously. * * @see https://www.sharetribe.com/api-reference/marketplace.html#stock */ import type { AxiosResponse } from "axios"; import MarketplaceApi from "./index"; import { ExtraParameter, StockCompareAndSetParameter, StockResponse } from "../../types"; /** * Stock API client (own listings only) */ declare class Stock { readonly authRequired = true; private readonly axios; private readonly endpoint; private readonly headers; constructor(api: MarketplaceApi); /** * Atomically update stock using compare-and-set * * Fails if current stock doesn't match `oldTotal` — prevents overselling. * * @template P * @template EP * @param {P & StockCompareAndSetParameter} params * @param {EP} [extraParams] - Optional extra parameters (e.g. `expand: true`) * @returns {Promise<AxiosResponse<StockResponse<"compareAndSet", EP>>>} * * @example * // Reserve 2 units if current stock is 10 * await sdk.stock.compareAndSet({ * listingId: "listing-abc123", * oldTotal: 10, * newTotal: 8 * }); * * @example * // Restock from 5 to 50 * await sdk.stock.compareAndSet({ * listingId: "listing-abc123", * oldTotal: 5, * newTotal: 50 * }); */ compareAndSet<P extends StockCompareAndSetParameter, EP extends ExtraParameter | undefined = undefined>(params: P, extraParams?: EP): Promise<AxiosResponse<StockResponse<"compareAndSet", EP>>>; } export default Stock;