import {z} from 'zod' export type AddressByIdInput = {id: string}; export type AddressByPlaceInput = {place_id: string}; export type AddressNewInput = {name: string, street: string, street_number: string, floor?: (string | null) | undefined, apartment?: (string | null) | undefined, reference?: (string | null) | undefined, city_id?: number | undefined, city?: string | undefined, state_id?: number | undefined, state?: string | undefined, country_id?: number | undefined, country?: string | undefined, zipcode?: (string | null) | undefined, phone?: (string | null) | undefined, email?: (string | null) | undefined, document_type?: (string | null) | undefined, document_number?: (string | null) | undefined, geo_latitude?: (number | null) | undefined, geo_longitude?: (number | null) | undefined}; export type AddressInput = AddressByIdInput | AddressByPlaceInput | AddressNewInput; export type PackageInput = {id?: number | undefined, external_reference?: (string | null) | undefined, description?: (string | null) | undefined, label_code?: string | undefined, length?: number | undefined, width?: number | undefined, height?: number | undefined, original_length?: number | undefined, length_unit?: ('cm' | 'm' | 'in' | 'ft') | undefined, original_width?: number | undefined, width_unit?: ('cm' | 'm' | 'in' | 'ft') | undefined, original_height?: number | undefined, height_unit?: ('cm' | 'm' | 'in' | 'ft') | undefined, grams?: number | undefined, original_weight?: number | undefined, weight_unit?: ('g' | 'kg' | 'lbs' | 'oz') | undefined, un_code?: string | undefined, classification?: string | undefined, is_flammable?: boolean | undefined, is_corrosive?: boolean | undefined, is_fragile?: boolean | undefined, temperature_min?: number | undefined, temperature_max?: number | undefined, original_temperature_min?: number | undefined, temperature_min_unit?: ('°C' | '°F') | undefined, original_temperature_max?: number | undefined, temperature_max_unit?: ('°C' | '°F') | undefined}; export type CreateApiShipmentsQuotesArgs = {body: {origin: AddressInput, destination: AddressInput, packages: Array, declared_value?: (number | null) | undefined, shipper_id?: (string | null) | undefined, carrier_id?: (string | null) | undefined}}; export type CreateApiShipmentsQuotesOptions = {baseUrl?: string | undefined, orgToken?: string | undefined, fetch?: typeof fetch | undefined}; export const quote = z.object({service_type_id: z.number().int().optional(), service_type_name: z.string().optional(), price: z.number().optional(), currency: z.string().optional(), estimated_delivery_days: z.number().int().nullable().optional()}); export const createApiShipmentsQuotesResponse = z.object({quotes: z.array(quote).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 }; /** * Obtener cotizaciones de envío * * POST /shipments/quotes */ export const createApiShipmentsQuotes = async (args: CreateApiShipmentsQuotesArgs, options?: CreateApiShipmentsQuotesOptions) => { const {baseUrl = "https://mi-organizacion.routes.zipnova.app/api/v1", fetch: fetchFn = fetch, orgToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (orgToken !== undefined) headers.set('authorization', `Bearer ${orgToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/shipments/quotes${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(createApiShipmentsQuotesResponse, await res.json()) };