import {z} from 'zod' export type AddressData = {postal_code?: (string | null) | undefined, city?: (string | null) | undefined, federal_tax_id?: (string | null) | undefined, state_tax_id?: (string | null) | undefined, person_name?: (string | null) | undefined, company_name?: (string | null) | undefined, country_code: 'AC' | 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AN' | 'AO' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BM' | 'BN' | 'BO' | 'BR' | 'BS' | 'BT' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HN' | 'HR' | 'HT' | 'HU' | 'IC' | 'ID' | 'IE' | 'IL' | 'IN' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KV' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PR' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SY' | 'SZ' | 'TC' | 'TD' | 'TG' | 'TH' | 'TJ' | 'TL' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WS' | 'XB' | 'XC' | 'XE' | 'XM' | 'XN' | 'XS' | 'XY' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW' | 'EH' | 'IM' | 'BL' | 'MF' | 'SX', email?: (string | null) | undefined, phone_number?: (string | null) | undefined, state_code?: (string | null) | undefined, residential?: (boolean | null) | undefined, street_number?: (string | null) | undefined, address_line1?: (string | null) | undefined, address_line2?: (string | null) | undefined, validate_location?: (boolean | null) | undefined}; export type CommodityData = {weight: number, weight_unit: 'KG' | 'LB' | 'OZ' | 'G', title?: (string | null) | undefined, description?: (string | null) | undefined, quantity?: number | undefined, sku?: (string | null) | undefined, hs_code?: (string | null) | undefined, value_amount?: (number | null) | undefined, value_currency?: ('EUR' | 'AED' | 'USD' | 'XCD' | 'AMD' | 'ANG' | 'AOA' | 'ARS' | 'AUD' | 'AWG' | 'AZN' | 'BAM' | 'BBD' | 'BDT' | 'XOF' | 'BGN' | 'BHD' | 'BIF' | 'BMD' | 'BND' | 'BOB' | 'BRL' | 'BSD' | 'BTN' | 'BWP' | 'BYN' | 'BZD' | 'CAD' | 'CDF' | 'XAF' | 'CHF' | 'NZD' | 'CLP' | 'CNY' | 'COP' | 'CRC' | 'CUC' | 'CVE' | 'CZK' | 'DJF' | 'DKK' | 'DOP' | 'DZD' | 'EGP' | 'ERN' | 'ETB' | 'FJD' | 'GBP' | 'GEL' | 'GHS' | 'GMD' | 'GNF' | 'GTQ' | 'GYD' | 'HKD' | 'HNL' | 'HRK' | 'HTG' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'IRR' | 'ISK' | 'JMD' | 'JOD' | 'JPY' | 'KES' | 'KGS' | 'KHR' | 'KMF' | 'KPW' | 'KRW' | 'KWD' | 'KYD' | 'KZT' | 'LAK' | 'LKR' | 'LRD' | 'LSL' | 'LYD' | 'MAD' | 'MDL' | 'MGA' | 'MKD' | 'MMK' | 'MNT' | 'MOP' | 'MRO' | 'MUR' | 'MVR' | 'MWK' | 'MXN' | 'MYR' | 'MZN' | 'NAD' | 'XPF' | 'NGN' | 'NIO' | 'NOK' | 'NPR' | 'OMR' | 'PEN' | 'PGK' | 'PHP' | 'PKR' | 'PLN' | 'PYG' | 'QAR' | 'RSD' | 'RUB' | 'RWF' | 'SAR' | 'SBD' | 'SCR' | 'SDG' | 'SEK' | 'SGD' | 'SHP' | 'SLL' | 'SOS' | 'SRD' | 'SSP' | 'STD' | 'SYP' | 'SZL' | 'THB' | 'TJS' | 'TND' | 'TOP' | 'TRY' | 'TTD' | 'TWD' | 'TZS' | 'UAH' | 'UYU' | 'UZS' | 'VEF' | 'VND' | 'VUV' | 'WST' | 'YER' | 'ZAR' | 'null' | null) | undefined, origin_country?: ('AC' | 'AD' | 'AE' | 'AF' | 'AG' | 'AI' | 'AL' | 'AM' | 'AN' | 'AO' | 'AR' | 'AS' | 'AT' | 'AU' | 'AW' | 'AZ' | 'BA' | 'BB' | 'BD' | 'BE' | 'BF' | 'BG' | 'BH' | 'BI' | 'BJ' | 'BM' | 'BN' | 'BO' | 'BR' | 'BS' | 'BT' | 'BW' | 'BY' | 'BZ' | 'CA' | 'CD' | 'CF' | 'CG' | 'CH' | 'CI' | 'CK' | 'CL' | 'CM' | 'CN' | 'CO' | 'CR' | 'CU' | 'CV' | 'CY' | 'CZ' | 'DE' | 'DJ' | 'DK' | 'DM' | 'DO' | 'DZ' | 'EC' | 'EE' | 'EG' | 'ER' | 'ES' | 'ET' | 'FI' | 'FJ' | 'FK' | 'FM' | 'FO' | 'FR' | 'GA' | 'GB' | 'GD' | 'GE' | 'GF' | 'GG' | 'GH' | 'GI' | 'GL' | 'GM' | 'GN' | 'GP' | 'GQ' | 'GR' | 'GT' | 'GU' | 'GW' | 'GY' | 'HK' | 'HN' | 'HR' | 'HT' | 'HU' | 'IC' | 'ID' | 'IE' | 'IL' | 'IN' | 'IQ' | 'IR' | 'IS' | 'IT' | 'JE' | 'JM' | 'JO' | 'JP' | 'KE' | 'KG' | 'KH' | 'KI' | 'KM' | 'KN' | 'KP' | 'KR' | 'KV' | 'KW' | 'KY' | 'KZ' | 'LA' | 'LB' | 'LC' | 'LI' | 'LK' | 'LR' | 'LS' | 'LT' | 'LU' | 'LV' | 'LY' | 'MA' | 'MC' | 'MD' | 'ME' | 'MG' | 'MH' | 'MK' | 'ML' | 'MM' | 'MN' | 'MO' | 'MP' | 'MQ' | 'MR' | 'MS' | 'MT' | 'MU' | 'MV' | 'MW' | 'MX' | 'MY' | 'MZ' | 'NA' | 'NC' | 'NE' | 'NG' | 'NI' | 'NL' | 'NO' | 'NP' | 'NR' | 'NU' | 'NZ' | 'OM' | 'PA' | 'PE' | 'PF' | 'PG' | 'PH' | 'PK' | 'PL' | 'PR' | 'PT' | 'PW' | 'PY' | 'QA' | 'RE' | 'RO' | 'RS' | 'RU' | 'RW' | 'SA' | 'SB' | 'SC' | 'SD' | 'SE' | 'SG' | 'SH' | 'SI' | 'SK' | 'SL' | 'SM' | 'SN' | 'SO' | 'SR' | 'SS' | 'ST' | 'SV' | 'SY' | 'SZ' | 'TC' | 'TD' | 'TG' | 'TH' | 'TJ' | 'TL' | 'TN' | 'TO' | 'TR' | 'TT' | 'TV' | 'TW' | 'TZ' | 'UA' | 'UG' | 'US' | 'UY' | 'UZ' | 'VA' | 'VC' | 'VE' | 'VG' | 'VI' | 'VN' | 'VU' | 'WS' | 'XB' | 'XC' | 'XE' | 'XM' | 'XN' | 'XS' | 'XY' | 'YE' | 'YT' | 'ZA' | 'ZM' | 'ZW' | 'EH' | 'IM' | 'BL' | 'MF' | 'SX' | 'null' | null) | undefined, parent_id?: (string | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined}; export type ParcelData = {weight: number, width?: (number | null) | undefined, height?: (number | null) | undefined, length?: (number | null) | undefined, packaging_type?: (string | null) | undefined, package_preset?: (string | null) | undefined, description?: (string | null) | undefined, content?: (string | null) | undefined, is_document?: (boolean | null) | undefined, weight_unit: 'KG' | 'LB' | 'OZ' | 'G', dimension_unit?: ('CM' | 'IN' | 'null' | null) | undefined, items?: Array | undefined, reference_number?: (string | null) | undefined, freight_class?: (string | null) | undefined, options?: {[key: string]: unknown} | undefined}; export type RateRequest = {shipper: AddressData, recipient: AddressData, parcels: Array, services?: (Array | null) | undefined, options?: {[key: string]: unknown} | undefined, reference?: (string | null) | undefined, carrier_ids?: (Array | null) | undefined}; export type CreateApiV1ProxyRatesArgs = {body: RateRequest}; export type CreateApiV1ProxyRatesOptions = {baseUrl: string, TokenBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const message = z.object({message: z.string().optional(), code: z.string().optional(), details: z.record(z.string(), z.unknown()).optional(), carrier_name: z.string().optional(), carrier_id: z.string().optional()}); export const charge = z.object({name: z.string().nullable().optional(), amount: z.number().nullable().optional(), currency: z.string().nullable().optional()}); export const rate = z.object({id: z.string().optional(), object_type: z.string().optional(), carrier_name: z.string(), carrier_id: z.string(), currency: z.string().optional(), service: z.string().nullable().optional(), total_charge: z.number().optional(), transit_days: z.number().int().nullable().optional(), extra_charges: z.array(charge).optional(), estimated_delivery: z.string().nullable().optional(), meta: z.record(z.string(), z.unknown()).nullable().optional(), test_mode: z.boolean()}); export const rateResponse = z.object({messages: z.array(message).optional(), rates: z.array(rate)}); 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 }; /** * Fetch shipment rates * * POST /v1/proxy/rates * * Also accepts auth: Token, OAuth2, JWT */ export const createApiV1ProxyRates = async (args: CreateApiV1ProxyRatesArgs, options: CreateApiV1ProxyRatesOptions) => { const {baseUrl, fetch: fetchFn = fetch, TokenBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (TokenBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${TokenBasic.username}:${TokenBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/proxy/rates${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(rateResponse, await res.json()) };