import {z} from 'zod' export type TrackingInfo = {carrier_tracking_link?: (string | null) | undefined, customer_name?: (string | null) | undefined, expected_delivery?: (string | null) | undefined, note?: (string | null) | undefined, order_date?: (string | null) | undefined, order_id?: (string | null) | undefined, package_weight?: (string | null) | undefined, package_weight_unit?: (string | null) | undefined, shipment_package_count?: (string | null) | undefined, shipment_pickup_date?: (string | null) | undefined, shipment_delivery_date?: (string | null) | undefined, shipment_service?: (string | null) | undefined, shipment_origin_country?: (string | null) | undefined, shipment_origin_postal_code?: (string | null) | undefined, shipment_destination_country?: (string | null) | undefined, shipment_destination_postal_code?: (string | null) | undefined, shipping_date?: (string | null) | undefined, signed_by?: (string | null) | undefined, source?: (string | null) | undefined}; export type TrackingData = {tracking_number: string, carrier_name: 'allied_express' | 'allied_express_local' | 'amazon_shipping' | 'aramex' | 'asendia_us' | 'australiapost' | 'boxknight' | 'bpost' | 'canadapost' | 'canpar' | 'chronopost' | 'colissimo' | 'dhl_express' | 'dhl_parcel_de' | 'dhl_poland' | 'dhl_universal' | 'dicom' | 'dpd' | 'dpdhl' | 'fedex' | 'fedex_ws' | 'generic' | 'geodis' | 'hay_post' | 'laposte' | 'locate2u' | 'nationex' | 'purolator' | 'roadie' | 'royalmail' | 'seko' | 'sendle' | 'tge' | 'tnt' | 'ups' | 'usps' | 'usps_international' | 'usps_wt' | 'usps_wt_international' | 'zoom2u', account_number?: (string | null) | undefined, reference?: (string | null) | undefined, info?: (TrackingInfo | null) | undefined, metadata?: {[key: string]: unknown} | undefined}; export type BatchTrackerData = {trackers: Array}; export type CreateApiV1BatchesTrackersArgs = {body: BatchTrackerData}; export type CreateApiV1BatchesTrackersOptions = {baseUrl: string, TokenBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const batchObject = z.object({id: z.string().optional(), status: z.enum(["queued", "running", "failed", "completed", "completed_with_errors"]), errors: z.record(z.string(), z.unknown()).nullable().optional()}); export const batchOperation = z.object({id: z.string().optional(), status: z.enum(["queued", "running", "failed", "completed", "completed_with_errors"]), resource_type: z.enum(["orders", "shipments", "trackers", "billing"]), resources: z.array(batchObject), created_at: z.string(), updated_at: z.string(), test_mode: z.boolean()}); 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 tracker batch * * POST /v1/batches/trackers * * Also accepts auth: Token, OAuth2, JWT */ export const createApiV1BatchesTrackers = async (args: CreateApiV1BatchesTrackersArgs, options: CreateApiV1BatchesTrackersOptions) => { 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/batches/trackers${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(batchOperation, await res.json()) };