import {z} from 'zod' export type PropertyShippingShipmentFromAddress = {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 PropertyShippingShipmentToAddress = {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 PropertyShippingShipmentPackages = Array; export type PropertyShippingShipmentReturnAddress = {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 ShippingCustomsItem = {description?: string | undefined, quantity?: number | undefined, amount?: number | undefined, currency?: string | undefined, weight?: number | undefined, weight_unit?: ('g' | 'kg' | 'oz' | 'lb') | undefined, harmonized_tariff_code?: string | undefined, country_of_origin?: string | undefined, sku?: string | undefined}; export type PropertyShippingShipmentCustomsItems = Array; export type PropertyShippingShipmentCustomsRestrictions = Array; export type PropertyShippingShipmentCustoms = {contents_type?: ('MERCHANDISE' | 'DOCUMENTS' | 'GIFT' | 'RETURNED_GOODS' | 'SAMPLE' | 'OTHER') | undefined, description?: string | undefined, amount?: number | undefined, currency?: string | undefined, duties_paid_by?: ('SENDER' | 'RECIPIENT' | 'THIRD_PARTY') | undefined, taxes_paid_by?: ('SENDER' | 'RECIPIENT' | 'THIRD_PARTY') | undefined, shipper_eori?: string | undefined, recipient_eori?: string | undefined, shipper_tax_number?: string | undefined, recipient_tax_number?: string | undefined, items?: PropertyShippingShipmentCustomsItems | undefined, restrictions?: PropertyShippingShipmentCustomsRestrictions | undefined, non_delivery_option?: ('RETURN' | 'ABANDON') | undefined}; export type PropertyShippingShipmentInsurance = {insured_value?: number | undefined, currency?: string | undefined, insurance_provider?: string | undefined, insurance_provider_code?: string | undefined, insurance_cost?: number | undefined, insurance_cost_currency?: string | undefined, coverage_type?: ('STANDARD' | 'PREMIUM' | 'CUSTOM') | undefined, coverage_amount?: number | undefined}; export type PropertyShippingShipmentSpecialInstructions = Array; export type ShippingShipmentLineitem = {id?: string | undefined, item_id?: string | undefined, item_variant_id?: string | undefined, order_lineitem_id?: string | undefined, quantity?: number | undefined, item_name?: string | undefined, sku?: string | undefined}; export type PropertyShippingShipmentLineitems = 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 PropertyShippingShipmentMetadata = Array; export type ShippingShipment = {id?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, order_id?: string | undefined, organization_id?: string | undefined, from_address?: PropertyShippingShipmentFromAddress | undefined, to_address?: PropertyShippingShipmentToAddress | undefined, packages?: PropertyShippingShipmentPackages | undefined, carrier_id?: string | undefined, service_code?: string | undefined, status?: ('PENDING' | 'PROCESSING' | 'IN_TRANSIT' | 'DELIVERED' | 'EXCEPTION' | 'CANCELLED' | 'LABEL_CREATED' | 'PICKED_UP' | 'OUT_FOR_DELIVERY' | 'DELIVERY_ATTEMPTED' | 'RETURNED_TO_SENDER' | 'HELD_AT_LOCATION' | 'CUSTOMS_CLEARANCE' | 'EXCEPTION_RESOLVED') | undefined, rate_id?: string | undefined, label_id?: string | undefined, tracking_id?: string | undefined, shipped_at?: string | undefined, rate_amount?: number | undefined, rate_currency?: string | undefined, rate_service_name?: string | undefined, rate_estimated_days?: number | undefined, rate_estimated_delivery_at?: string | undefined, is_rate_guaranteed?: boolean | undefined, return_address?: PropertyShippingShipmentReturnAddress | undefined, return_authorization_number?: string | undefined, warehouse_location_id?: string | undefined, warehouse_location_name?: string | undefined, customs?: PropertyShippingShipmentCustoms | undefined, is_international?: boolean | undefined, insurance?: PropertyShippingShipmentInsurance | undefined, special_instructions?: PropertyShippingShipmentSpecialInstructions | undefined, is_signature_required?: boolean | undefined, is_adult_signature_required?: boolean | undefined, reference_number?: string | undefined, is_return?: boolean | undefined, original_shipment_id?: string | undefined, return_reason?: string | undefined, return_type?: ('CUSTOMER' | 'VENDOR' | 'WARRANTY' | 'DEFECTIVE' | 'OTHER') | undefined, carrier_name?: string | undefined, tracking_url?: string | undefined, lineitems?: PropertyShippingShipmentLineitems | undefined, metadata?: PropertyShippingShipmentMetadata | undefined, raw?: {[key: string]: unknown} | undefined}; export type UpdateApiShippingConnectionIdShipmentIdArgs = {fields?: (Array<'id' | 'created_at' | 'updated_at' | 'order_id' | 'organization_id' | 'from_address' | 'to_address' | 'packages' | 'carrier_id' | 'service_code' | 'status' | 'rate_id' | 'label_id' | 'tracking_id' | 'shipped_at' | 'rate_amount' | 'rate_currency' | 'rate_service_name' | 'rate_estimated_days' | 'rate_estimated_delivery_at' | 'is_rate_guaranteed' | 'return_address' | 'return_authorization_number' | 'warehouse_location_id' | 'warehouse_location_name' | 'customs' | 'is_international' | 'insurance' | 'special_instructions' | 'is_signature_required' | 'is_adult_signature_required' | 'reference_number' | 'is_return' | 'original_shipment_id' | 'return_reason' | 'return_type' | 'carrier_name' | 'tracking_url' | 'lineitems' | 'metadata' | 'raw'>) | undefined, raw?: string | undefined, connection_id: string, id: string, body: ShippingShipment}; export type UpdateApiShippingConnectionIdShipmentIdOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const propertyShippingShipmentFromAddress = 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 propertyShippingShipmentToAddress = 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 propertyShippingShipmentPackages = z.array(shippingPackage); export const propertyShippingShipmentReturnAddress = 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 shippingCustomsItem = z.object({description: z.string().optional(), quantity: z.number().optional(), amount: z.number().optional(), currency: z.string().optional(), weight: z.number().optional(), weight_unit: z.enum(["g", "kg", "oz", "lb"]).optional(), harmonized_tariff_code: z.string().optional(), country_of_origin: z.string().optional(), sku: z.string().optional()}); export const propertyShippingShipmentCustomsItems = z.array(shippingCustomsItem); export const propertyShippingShipmentCustomsRestrictions = z.array(z.string()); export const propertyShippingShipmentCustoms = z.object({contents_type: z.enum(["MERCHANDISE", "DOCUMENTS", "GIFT", "RETURNED_GOODS", "SAMPLE", "OTHER"]).optional(), description: z.string().optional(), amount: z.number().optional(), currency: z.string().optional(), duties_paid_by: z.enum(["SENDER", "RECIPIENT", "THIRD_PARTY"]).optional(), taxes_paid_by: z.enum(["SENDER", "RECIPIENT", "THIRD_PARTY"]).optional(), shipper_eori: z.string().optional(), recipient_eori: z.string().optional(), shipper_tax_number: z.string().optional(), recipient_tax_number: z.string().optional(), items: propertyShippingShipmentCustomsItems.optional(), restrictions: propertyShippingShipmentCustomsRestrictions.optional(), non_delivery_option: z.enum(["RETURN", "ABANDON"]).optional()}); export const propertyShippingShipmentInsurance = z.object({insured_value: z.number().optional(), currency: z.string().optional(), insurance_provider: z.string().optional(), insurance_provider_code: z.string().optional(), insurance_cost: z.number().optional(), insurance_cost_currency: z.string().optional(), coverage_type: z.enum(["STANDARD", "PREMIUM", "CUSTOM"]).optional(), coverage_amount: z.number().optional()}); export const propertyShippingShipmentSpecialInstructions = z.array(z.string()); export const shippingShipmentLineitem = z.object({id: z.string().optional(), item_id: z.string().optional(), item_variant_id: z.string().optional(), order_lineitem_id: z.string().optional(), quantity: z.number().optional(), item_name: z.string().optional(), sku: z.string().optional()}); export const propertyShippingShipmentLineitems = z.array(shippingShipmentLineitem); 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 propertyShippingShipmentMetadata = z.array(shippingMetadata); export const shippingShipment = z.object({id: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), order_id: z.string().optional(), organization_id: z.string().optional(), from_address: propertyShippingShipmentFromAddress.optional(), to_address: propertyShippingShipmentToAddress.optional(), packages: propertyShippingShipmentPackages.optional(), carrier_id: z.string().optional(), service_code: z.string().optional(), status: z.enum(["PENDING", "PROCESSING", "IN_TRANSIT", "DELIVERED", "EXCEPTION", "CANCELLED", "LABEL_CREATED", "PICKED_UP", "OUT_FOR_DELIVERY", "DELIVERY_ATTEMPTED", "RETURNED_TO_SENDER", "HELD_AT_LOCATION", "CUSTOMS_CLEARANCE", "EXCEPTION_RESOLVED"]).optional(), rate_id: z.string().optional(), label_id: z.string().optional(), tracking_id: z.string().optional(), shipped_at: z.string().optional(), rate_amount: z.number().optional(), rate_currency: z.string().optional(), rate_service_name: z.string().optional(), rate_estimated_days: z.number().optional(), rate_estimated_delivery_at: z.string().optional(), is_rate_guaranteed: z.boolean().optional(), return_address: propertyShippingShipmentReturnAddress.optional(), return_authorization_number: z.string().optional(), warehouse_location_id: z.string().optional(), warehouse_location_name: z.string().optional(), customs: propertyShippingShipmentCustoms.optional(), is_international: z.boolean().optional(), insurance: propertyShippingShipmentInsurance.optional(), special_instructions: propertyShippingShipmentSpecialInstructions.optional(), is_signature_required: z.boolean().optional(), is_adult_signature_required: z.boolean().optional(), reference_number: z.string().optional(), is_return: z.boolean().optional(), original_shipment_id: z.string().optional(), return_reason: z.string().optional(), return_type: z.enum(["CUSTOMER", "VENDOR", "WARRANTY", "DEFECTIVE", "OTHER"]).optional(), carrier_name: z.string().optional(), tracking_url: z.string().optional(), lineitems: propertyShippingShipmentLineitems.optional(), metadata: propertyShippingShipmentMetadata.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 }; /** * Update a shipment * * PUT /shipping/{connection_id}/shipment/{id} */ export const updateApiShippingConnectionIdShipmentId = async (args: UpdateApiShippingConnectionIdShipmentIdArgs, options?: UpdateApiShippingConnectionIdShipmentIdOptions) => { 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))}/shipment/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(shippingShipment, await res.json()) };