getSplitShipments.ts
Language TypeScript1.9 KB48 lines
import { z } from "zod";import type { ToolModule } from "./types.js";import { plinthRequest } from "../lib/http.js";import { PlinthApiError } from "../lib/errors.js";/*** get_split_shipments: per-order multi-address shipment breakdown (Plinth* method `orders.splitShipments`).** Wire notes:* - The ONLY method whose top level is a bare JSON ARRAY (one entry per* destination).* - {"notice":"No match"} for a valid single-address order means "not a* multi-address order": treat it as an empty result, not a hard error* (PlinthApiError.raw lets you distinguish).* - `split_lines` can be an array OR an ordinal-keyed object within one* response (lib/collections.ts).* - Destination count is not guaranteed to equal the shipment-entry count on* orders.fetch for the same order.*/export const getSplitShipments: ToolModule = {name: "get_split_shipments",config: {title: "Get multi-address shipments",description:"Per-order multi-address shipment breakdown, keyed on order_ref. Read-only. Returns 'not multi-address' for single-address orders.",inputSchema: {orderId: z.number().int().positive().describe("Raw integer order_ref."),},},async handler(args, ctx) {const { orderId } = args as { orderId: number };try {const body = await plinthRequest(ctx, "orders.splitShipments", { order_ref: orderId });return { content: [{ type: "text", text: JSON.stringify(body, null, 2) }] };} catch (err) {// "No match" here means "not a multi-address order", not a failure.if (err instanceof PlinthApiError && /no match/i.test(err.message)) {return {content: [{ type: "text", text: `Order ${orderId} is not a multi-address order (single-address or no split shipment).` },],};}throw err;}},};