import {z} from 'zod' export type PropertyShippingRateFromAddress = {address1?: string | undefined, address2?: string | undefined, city?: string | undefined, region?: string | undefined, region_code?: string | undefined, postal_code?: string | undefined, country?: string | undefined, country_code?: string | undefined, name?: string | undefined, company_name?: string | undefined, telephone?: string | undefined, email?: string | undefined, is_residential?: boolean | undefined, is_validated?: boolean | undefined, delivery_instructions?: string | undefined}; export type PropertyShippingRateToAddress = {address1?: string | undefined, address2?: string | undefined, city?: string | undefined, region?: string | undefined, region_code?: string | undefined, postal_code?: string | undefined, country?: string | undefined, country_code?: string | undefined, name?: string | undefined, company_name?: string | undefined, telephone?: string | undefined, email?: string | undefined, is_residential?: boolean | undefined, is_validated?: boolean | undefined, delivery_instructions?: string | undefined}; export type ShippingPackage = {weight?: number | undefined, weight_unit?: ('g' | 'kg' | 'oz' | 'lb') | undefined, length?: number | undefined, width?: number | undefined, height?: number | undefined, size_unit?: ('cm' | 'inch') | undefined, description?: string | undefined, value?: number | undefined, currency?: string | undefined, tracking_number?: string | undefined, insured_amount?: number | undefined}; export type PropertyShippingRatePackages = Array; export type ShippingRateSurcharge = {code?: string | undefined, name?: string | undefined, amount?: number | undefined, description?: string | undefined}; export type PropertyShippingRateRateSurcharges = Array; export type ShippingRateRate = {title: string, description?: string | undefined, amount?: number | undefined, currency?: string | undefined, base_amount?: number | undefined, tax_amount?: number | undefined, discount_amount?: number | undefined, surcharges?: PropertyShippingRateRateSurcharges | undefined, rate_source?: string | undefined, is_negotiated_rate?: boolean | undefined, estimated_days?: number | undefined, delivery_days?: number | undefined, estimated_delivery_end_at?: string | undefined, estimated_delivery_start_at?: string | undefined, delivery_terms?: string | undefined, transit_hours?: number | undefined, is_guaranteed?: boolean | undefined, package_type?: string | undefined, is_trackable?: boolean | undefined, is_active?: boolean | undefined}; export type PropertyShippingRateRates = Array; export type ShippingMetadata = {id?: string | undefined, slug?: string | undefined, value?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined, namespace?: string | undefined, format?: ('TEXT' | 'NUMBER' | 'DATE' | 'BOOLEAN' | 'FILE' | 'TEXTAREA' | 'SINGLE_SELECT' | 'MULTIPLE_SELECT' | 'MEASUREMENT' | 'PRICE' | 'YES_NO' | 'CURRENCY' | 'URL') | undefined, extra_data?: ({[key: string]: unknown} | string | number | boolean | Array | string | number | boolean>) | undefined}; export type PropertyShippingRateMetadata = Array; export type ShippingRate = {shipment_id?: string | undefined, currency?: string | undefined, from_address?: PropertyShippingRateFromAddress | undefined, to_address?: PropertyShippingRateToAddress | undefined, packages?: PropertyShippingRatePackages | undefined, ship_by_at?: string | undefined, carrier_id?: string | undefined, id?: string | undefined, rates?: PropertyShippingRateRates | undefined, metadata?: PropertyShippingRateMetadata | undefined, raw?: {[key: string]: unknown} | undefined}; export type CreateApiShippingConnectionIdRateArgs = {fields?: (Array<'shipment_id' | 'currency' | 'from_address' | 'to_address' | 'packages' | 'ship_by_at' | 'carrier_id' | 'id' | 'rates' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, body: ShippingRate}; export type CreateApiShippingConnectionIdRateOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyShippingRateFromAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional(), name: z.string().optional(), company_name: z.string().optional(), telephone: z.string().optional(), email: z.string().optional(), is_residential: z.boolean().optional(), is_validated: z.boolean().optional(), delivery_instructions: z.string().optional()}); export const propertyShippingRateToAddress = z.object({address1: z.string().optional(), address2: z.string().optional(), city: z.string().optional(), region: z.string().optional(), region_code: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), country_code: z.string().optional(), name: z.string().optional(), company_name: z.string().optional(), telephone: z.string().optional(), email: z.string().optional(), is_residential: z.boolean().optional(), is_validated: z.boolean().optional(), delivery_instructions: z.string().optional()}); export const shippingPackage = z.object({weight: z.number().optional(), weight_unit: z.enum(["g", "kg", "oz", "lb"]).optional(), length: z.number().optional(), width: z.number().optional(), height: z.number().optional(), size_unit: z.enum(["cm", "inch"]).optional(), description: z.string().optional(), value: z.number().optional(), currency: z.string().optional(), tracking_number: z.string().optional(), insured_amount: z.number().optional()}); export const propertyShippingRatePackages = z.array(shippingPackage); export const shippingRateSurcharge = z.object({code: z.string().optional(), name: z.string().optional(), amount: z.number().optional(), description: z.string().optional()}); export const propertyShippingRateRateSurcharges = z.array(shippingRateSurcharge); export const shippingRateRate = z.object({title: z.string(), description: z.string().optional(), amount: z.number().optional(), currency: z.string().optional(), base_amount: z.number().optional(), tax_amount: z.number().optional(), discount_amount: z.number().optional(), surcharges: propertyShippingRateRateSurcharges.optional(), rate_source: z.string().optional(), is_negotiated_rate: z.boolean().optional(), estimated_days: z.number().optional(), delivery_days: z.number().optional(), estimated_delivery_end_at: z.string().optional(), estimated_delivery_start_at: z.string().optional(), delivery_terms: z.string().optional(), transit_hours: z.number().optional(), is_guaranteed: z.boolean().optional(), package_type: z.string().optional(), is_trackable: z.boolean().optional(), is_active: z.boolean().optional()}); export const propertyShippingRateRates = z.array(shippingRateRate); export const shippingMetadata = z.object({id: z.string().optional(), slug: z.string().optional(), value: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional(), namespace: z.string().optional(), format: z.enum(["TEXT", "NUMBER", "DATE", "BOOLEAN", "FILE", "TEXTAREA", "SINGLE_SELECT", "MULTIPLE_SELECT", "MEASUREMENT", "PRICE", "YES_NO", "CURRENCY", "URL"]).optional(), extra_data: z.union([z.record(z.string(), z.unknown()), z.string(), z.number(), z.boolean(), z.array(z.union([z.object({}), z.string(), z.number(), z.boolean()]))]).optional()}); export const propertyShippingRateMetadata = z.array(shippingMetadata); export const shippingRate = z.object({shipment_id: z.string().optional(), currency: z.string().optional(), from_address: propertyShippingRateFromAddress.optional(), to_address: propertyShippingRateToAddress.optional(), packages: propertyShippingRatePackages.optional(), ship_by_at: z.string().optional(), carrier_id: z.string().optional(), id: z.string().optional(), rates: propertyShippingRateRates.optional(), metadata: propertyShippingRateMetadata.optional(), raw: z.record(z.string(), z.unknown()).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 }; /** * Create a rate * * POST /shipping/{connection_id}/rate */ export const createApiShippingConnectionIdRate = async (args: CreateApiShippingConnectionIdRateArgs, options?: CreateApiShippingConnectionIdRateOptions) => { const {baseUrl = "https://api.unified.to", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.raw != null) query.append("raw", String(args.raw)) if (jwt !== undefined) headers.set("authorization", jwt) const url = `${baseUrl.replace(/\/+$/, '')}/shipping/${encodeURIComponent(String(args.connection_id))}/rate${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(shippingRate, await res.json()) };