import {z} from 'zod' export type MoneySchema = {amount?: string | undefined, currency?: string | undefined}; export type StorefrontCartSchema = {lineItems: Array<{coupons?: Array<{code: string, discountAmount: MoneySchema, id: string}> | undefined, id: string, originalPrice?: MoneySchema | undefined, priceTotal: MoneySchema, product: {id: string}, quantity: number, variant: {id: string}}>, priceTotal: MoneySchema}; export type StorefrontCustomerSchema = {id: string}; export type LengthSchema = {unit: 'in' | 'cm', value: number}; export type WeightSchema = {kg?: number | undefined}; export type CreateApiStoresStoreIdCoverageProductsArgs = {storeId: string, body: {cart: StorefrontCartSchema, customer?: StorefrontCustomerSchema | undefined, products?: Array<{id: string}> | undefined, variants?: (Array<{height?: LengthSchema | undefined, id: string, length?: LengthSchema | undefined, title?: string | undefined, weight?: WeightSchema | undefined, width?: LengthSchema | undefined}>) | undefined}}; export type CreateApiStoresStoreIdCoverageProductsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const moneySchema = z.object({amount: z.string().optional(), currency: z.string().optional()}); export const coverageProductSchema = z.object({coverages: z.array(z.object({lineItems: z.array(z.object({id: z.string().optional()})), type: z.enum(["shipping_failure", "return_shipping"])})).optional(), id: z.string(), price: moneySchema}); export const createApiStoresStoreIdCoverageProductsResponse = z.object({coverageProducts: z.array(coverageProductSchema)}); 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 }; /** * Coverage Products * * POST /stores/{storeId}/coverage-products */ export const createApiStoresStoreIdCoverageProducts = async (args: CreateApiStoresStoreIdCoverageProductsArgs, options?: CreateApiStoresStoreIdCoverageProductsOptions) => { const {baseUrl = "https://api.getredo.com/v2.2", 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(/\/+$/, '')}/stores/${encodeURIComponent(String(args.storeId))}/coverage-products${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(createApiStoresStoreIdCoverageProductsResponse, await res.json()) };