import {z} from 'zod' export type GetApiV2ShipmentsShipmentIdLabelsOptions = {baseUrl?: string | undefined, apiKeys?: string | undefined, fetch?: typeof fetch | undefined}; export const documentType = z.enum(["commercial_invoice", "certificate_of_origin", "usmca_commercial_invoice_certification_of_origin", "usmca_certification_of_origin", "pro_forma_invoice", "other", "authorization_form", "export_accompanying_document", "export_license", "import_permit", "one_time_nafta", "power_of_attorney", "packing_list", "sed_document", "shippers_letter_of_instruction", "declaration", "section_232_customs_affidavit"]); export const documentStatusValue = z.enum(["awaiting_upload", "processing", "processing_claimed", "invalid", "uploaded", "queued", "sent", "failed"]); export const documentStatus = z.object({document_id: z.string().optional(), type: documentType.optional(), file_name: z.string().optional(), status: documentStatusValue.optional(), error: z.string().nullable().optional(), external_id: z.string().nullable().optional()}); export const orderShipment = z.object({shipment_id: z.string().optional(), tracking_number: z.string().optional(), documents: z.array(documentStatus).optional()}); export const listShipmentLabelsResponseBody = z.array(orderShipment); 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 }; /** * List shipments and their document statuses * * GET /v2/shipments/{shipment_id}/labels */ export const getApiV2ShipmentsShipmentIdLabels = async (options?: GetApiV2ShipmentsShipmentIdLabelsOptions) => { 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/shipments/${encodeURIComponent(String(args.shipment_id))}/labels${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(listShipmentLabelsResponseBody, await res.json()) };