import {z} from 'zod' export type GetApiV2FreightShipmentsFreightShipmentIdDocumentsOptions = {baseUrl?: string | undefined, apiKeys?: string | undefined, fetch?: typeof fetch | undefined}; export const freightDocument = 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(), created_at: z.string().nullable().optional(), url: z.string().nullable().optional()}); export const listFreightShipmentDocumentsResponseBody = z.object({documents: z.array(freightDocument).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 }; /** * List freight shipment documents * * GET /v2/freight/shipments/{freight_shipment_id}/documents */ export const getApiV2FreightShipmentsFreightShipmentIdDocuments = async (options?: GetApiV2FreightShipmentsFreightShipmentIdDocumentsOptions) => { 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))}/documents${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(listFreightShipmentDocumentsResponseBody, await res.json()) };