import {z} from 'zod' export type InventoryMoney = {amount: number, currency: string}; export type InventorySetProductRequest = {title: string, description: string, product_url: string, image_url: string, additional_image_urls?: Array | undefined, video_url?: string | undefined, model_3d_url?: string | undefined, sku?: string | undefined, gtin?: string | undefined, mpn?: string | undefined, brand?: string | undefined, category?: string | undefined, price?: InventoryMoney | undefined, sale_price?: InventoryMoney | undefined, sale_price_starts_at?: string | undefined, sale_price_ends_at?: string | undefined, group_id?: string | undefined, group_title?: string | undefined, color?: string | undefined, size?: string | undefined, size_system?: string | undefined, gender?: string | undefined, condition?: ('new' | 'used' | 'refurbished') | undefined, material?: string | undefined, age_group?: string | undefined, length?: number | undefined, width?: number | undefined, height?: number | undefined, dimension_unit?: string | undefined, weight?: number | undefined, weight_unit?: string | undefined, expiration_date?: string | undefined, harmonized_system_code?: string | undefined, country_of_origin?: string | undefined, seller_name?: string | undefined, seller_url?: string | undefined, seller_privacy_policy?: string | undefined, seller_tos?: string | undefined}; export type UpdateApiInventoryVariantIdProductArgs = {variant_id: string, body: InventorySetProductRequest}; export type UpdateApiInventoryVariantIdProductOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const inventoryMoney = z.object({amount: z.number().int(), currency: z.string().min(3).max(3)}); export const inventoryHalLink = z.object({href: z.string().optional(), actions: z.array(z.string()).optional(), types: z.array(z.string()).optional()}); export const inventoryProductKnowledge = z.object({variant_id: z.string(), title: z.string(), description: z.string(), product_url: z.string(), image_url: z.string(), additional_image_urls: z.array(z.string()).optional(), video_url: z.string().optional(), model_3d_url: z.string().optional(), sku: z.string().optional(), gtin: z.string().optional(), mpn: z.string().optional(), brand: z.string().optional(), category: z.string().optional(), price: inventoryMoney.optional(), sale_price: inventoryMoney.optional(), sale_price_starts_at: z.string().optional(), sale_price_ends_at: z.string().optional(), group_id: z.string().optional(), group_title: z.string().optional(), color: z.string().optional(), size: z.string().optional(), size_system: z.string().optional(), gender: z.string().optional(), condition: z.enum(["new", "used", "refurbished"]), material: z.string().optional(), age_group: z.string().optional(), length: z.number().optional(), width: z.number().optional(), height: z.number().optional(), dimension_unit: z.string().optional(), weight: z.number().optional(), weight_unit: z.string().optional(), expiration_date: z.string().optional(), harmonized_system_code: z.string().optional(), country_of_origin: z.string().optional(), seller_name: z.string().optional(), seller_url: z.string().optional(), seller_privacy_policy: z.string().optional(), seller_tos: z.string().optional(), created_on: z.string(), modified_on: z.string(), _links: z.object({self: inventoryHalLink.optional(), set: inventoryHalLink.optional(), delete: inventoryHalLink.optional()})}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Set product knowledge * * PUT /inventory/{variant_id}/product */ export const updateApiInventoryVariantIdProduct = async (args: UpdateApiInventoryVariantIdProductArgs, options: UpdateApiInventoryVariantIdProductOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/inventory/${encodeURIComponent(String(args.variant_id))}/product${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(inventoryProductKnowledge, await res.json()) };