mattrguerra/meridian-orders-mcpmainPrivate on GitHub, published here in full

dates.ts

Language TypeScript928 B22 lines

mcp-server/src/lib/dates.ts

  1. /**
  2. * Date helpers. The API is asymmetric:
  3. * - REQUEST filters: yyyymmdd, no separators (ISO is rejected).
  4. * - orders.fetch responses: MySQL "YYYY-MM-DD HH:MM:SS".
  5. * - orders.splitShipments responses: plain ISO "YYYY-MM-DD".
  6. */
  7. /**
  8. * Convert an ISO date ("YYYY-MM-DD") or Date into the request format
  9. * `yyyymmdd` with no separators. Throws on malformed input rather than sending
  10. * something the API will reject with {"error": "Bad since date"}.
  11. */
  12. export function toRequestDate(input: string | Date): string {
  13. const d = typeof input === "string" ? new Date(`${input}T00:00:00Z`) : input;
  14. if (Number.isNaN(d.getTime())) {
  15. throw new RangeError(`Invalid date: ${String(input)} (expected YYYY-MM-DD)`);
  16. }
  17. const yyyy = String(d.getUTCFullYear());
  18. const mm = String(d.getUTCMonth() + 1).padStart(2, "0");
  19. const dd = String(d.getUTCDate()).padStart(2, "0");
  20. return `${yyyy}${mm}${dd}`;
  21. }