import {z} from 'zod' export type GetApiV3StoresStoreIdPublicCartOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const guestCartVariantItem = z.object({id: z.number().int(), type: z.literal("variant"), variant_id: z.number().int(), bundle_price_option_id: z.number().int().nullable(), quantity: z.number().int().gte(1)}).and(z.record(z.string(), z.unknown())); export const guestCartBundlePriceOptionItem = z.object({id: z.number().int(), type: z.literal("bundle_price_option"), variant_id: z.number().int().nullable(), bundle_price_option_id: z.number().int(), bundle_price_option_slug: z.string().optional(), bundle_price_option_unique_id: z.string().optional(), quantity: z.number().int().gte(1)}).and(z.record(z.string(), z.unknown())); export const guestCartItem = z.discriminatedUnion("type", [guestCartVariantItem, guestCartBundlePriceOptionItem]); export const guestCart = z.object({id: z.number().int().optional(), item_count: z.number().int().optional(), total: z.union([z.string(), z.number()]).optional(), items: z.array(guestCartItem).optional()}).and(z.record(z.string(), z.unknown())); 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 the guest storefront cart * * GET /v3/stores/{store_id}/public/cart */ export const getApiV3StoresStoreIdPublicCart = async (options?: GetApiV3StoresStoreIdPublicCartOptions) => { const {baseUrl = "https://api.scalev.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v3/stores/${encodeURIComponent(String(args.store_id))}/public/cart${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(guestCart, await res.json()) };