import {z} from 'zod' export type GetApiCartProductsProductIdArgs = {product_id: string, 'x-trackstar-api-key': string, 'x-trackstar-access-token': string}; export type GetApiCartProductsProductIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const productMeasurements = z.object({length: z.number().nullable(), width: z.number().nullable(), height: z.number().nullable(), unit: z.enum(["cm", "in"]).nullable(), weight: z.number().nullable(), weight_unit: z.enum(["kg", "oz", "lb"]).nullable()}); export const cartProductApiItemSchema = z.object({id: z.string(), created_date: z.string(), updated_date: z.string(), name: z.string(), description: z.string().nullable(), sku: z.string().nullable(), gtin: z.string().nullable(), parent_product_id: z.string(), is_kit: z.boolean(), active: z.boolean(), image_url: z.string().nullable(), price: z.number().nullable(), unit_cost: z.number().nullable(), currency_code: z.string().nullable(), inventory: z.number().int().nullable(), inventory_by_warehouse_id: z.record(z.string(), z.number().int()), measurements: productMeasurements, tags: z.array(z.string()), categories: z.array(z.string()), external_system_url: z.string().nullable(), trackstar_tags: z.array(z.unknown()).nullable(), additional_fields: z.record(z.string(), z.unknown()), trackstar_created_date: z.string(), trackstar_updated_date: z.string()}); export const cartProductsSingleItemEndpoint = z.object({data: cartProductApiItemSchema.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 }; /** * Get Cart Product * * GET /cart/products/{product_id} */ export const getApiCartProductsProductId = async (args: GetApiCartProductsProductIdArgs, options?: GetApiCartProductsProductIdOptions) => { const {baseUrl = "https://production.trackstarhq.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-trackstar-api-key'] != null) headers.set("x-trackstar-api-key", String(args['x-trackstar-api-key'])) if (args['x-trackstar-access-token'] != null) headers.set("x-trackstar-access-token", String(args['x-trackstar-access-token'])) const url = `${baseUrl.replace(/\/+$/, '')}/cart/products/${encodeURIComponent(String(args.product_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(cartProductsSingleItemEndpoint, await res.json()) };