dates.ts
Language TypeScript928 B22 lines
/*** Date helpers. The API is asymmetric:* - REQUEST filters: yyyymmdd, no separators (ISO is rejected).* - orders.fetch responses: MySQL "YYYY-MM-DD HH:MM:SS".* - orders.splitShipments responses: plain ISO "YYYY-MM-DD".*//*** Convert an ISO date ("YYYY-MM-DD") or Date into the request format* `yyyymmdd` with no separators. Throws on malformed input rather than sending* something the API will reject with {"error": "Bad since date"}.*/export function toRequestDate(input: string | Date): string {const d = typeof input === "string" ? new Date(`${input}T00:00:00Z`) : input;if (Number.isNaN(d.getTime())) {throw new RangeError(`Invalid date: ${String(input)} (expected YYYY-MM-DD)`);}const yyyy = String(d.getUTCFullYear());const mm = String(d.getUTCMonth() + 1).padStart(2, "0");const dd = String(d.getUTCDate()).padStart(2, "0");return `${yyyy}${mm}${dd}`;}