import {z} from 'zod' export type GetApiV2FreightShipmentsFreightShipmentIdOptions = {baseUrl?: string | undefined, apiKeys?: string | undefined, fetch?: typeof fetch | undefined}; export const seId = z.string().min(1).max(25); export const freightShipmentStatus = z.enum(["pending", "quoted", "booked", "in_transit", "delivered", "cancelled", "exception"]); export const freightShipmentDocument = z.object({type: z.enum(["bill_of_lading", "bol_pallet_label_combined", "certificate_of_insurance", "certificate_of_insurance_instructions", "certificate_of_origin_us", "certificate_of_origin_usmca", "certificate_of_origin_usmca_instructions", "international_commercial_invoice", "packing_list", "pallet_label", "pallet_label_4x6", "proof_of_delivery", "quote", "weight_and_inspection"]).optional(), url: z.string().nullable().optional()}); export const freightNotification = z.object({events: z.array(z.enum(["quote_ready", "exception", "booked", "delivered", "in_transit", "out_for_delivery", "pickup_scheduled", "voided"])), email_addresses: z.array(z.string())}); export const freightShipmentResponseBody = z.object({freight_shipment_id: seId.optional(), shipment_id: seId.nullable().optional(), freight_provider_account_id: seId.optional(), freight_provider_code: z.string().optional(), status: freightShipmentStatus.optional(), bol_number: z.string().nullable().optional(), pro_number: z.string().nullable().optional(), confirmation_number: z.string().nullable().optional(), created_at: z.string().optional(), documents: z.array(freightShipmentDocument).nullable().optional(), notifications: z.array(freightNotification).nullable().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 }; /** * Get freight shipment by ID * * GET /v2/freight/shipments/{freight_shipment_id} */ export const getApiV2FreightShipmentsFreightShipmentId = async (options?: GetApiV2FreightShipmentsFreightShipmentIdOptions) => { const {baseUrl = "https://api.shipstation.com", fetch: fetchFn = fetch, apiKeys} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeys !== undefined) headers.set("api-key", apiKeys) const url = `${baseUrl.replace(/\/+$/, '')}/v2/freight/shipments/${encodeURIComponent(String(args.freight_shipment_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(freightShipmentResponseBody, await res.json()) };