import {z} from 'zod' export type StorefrontCheckoutVariantItem = {type: 'variant', variant_id: number, quantity: number}; export type StorefrontCheckoutBundlePriceOptionItem = {type: 'bundle_price_option', bundle_price_option_id: number, quantity: number}; export type StorefrontCheckoutItem = StorefrontCheckoutVariantItem | StorefrontCheckoutBundlePriceOptionItem; export type StorefrontCheckoutDestination = {location_id: number | string, postal_code: string}; export type StorefrontSelectedShippingOption = {courier_service_id?: (number | string) | undefined, warehouse_unique_id?: string | undefined, courier_aggregator_code?: (string | null) | undefined} | {[key: string]: unknown}; export type StorefrontMoneyValue = string | null | number; export type StorefrontCheckoutSummaryRequest = {items?: Array | undefined, destination?: StorefrontCheckoutDestination | undefined, payment_method?: string | undefined, courier_service_id?: (number | string) | undefined, warehouse_unique_id?: string | undefined, courier_aggregator_code?: (string | null) | undefined, shipping_option?: StorefrontSelectedShippingOption | undefined, selected_shipping_option?: StorefrontSelectedShippingOption | undefined, shipping_cost?: StorefrontMoneyValue | undefined, discount_code_code?: (string | null) | undefined} | {[key: string]: unknown}; export type CreateApiV3StoresStoreIdPublicCheckoutSummaryArgs = {body: StorefrontCheckoutSummaryRequest}; export type CreateApiV3StoresStoreIdPublicCheckoutSummaryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const storefrontMoneyValue = z.union([z.string().nullable(), z.number()]); export const storefrontCheckoutSummaryResponse = z.object({product_price: storefrontMoneyValue, shipping_cost: storefrontMoneyValue, other_income: storefrontMoneyValue, other_income_name: z.string().nullable(), service_fee: storefrontMoneyValue, gross_revenue: storefrontMoneyValue}); 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 }; /** * Compute guest checkout summary * * POST /v3/stores/{store_id}/public/checkout/summary */ export const createApiV3StoresStoreIdPublicCheckoutSummary = async (args: CreateApiV3StoresStoreIdPublicCheckoutSummaryArgs, options?: CreateApiV3StoresStoreIdPublicCheckoutSummaryOptions) => { const {baseUrl = "https://api.scalev.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v3/stores/${encodeURIComponent(String(args.store_id))}/public/checkout/summary${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(storefrontCheckoutSummaryResponse, await res.json()) };