import { ApiTrainsTrainByHeadcode, ApiTrainsTrainDetails } from '@owlboard/api-schema-types'; import type { BaseClient, ApiResult } from '../lib/base.js'; import { IsValidHeadcode, IsValidRid, IsValidToc, IsValidDateStr } from '../lib/validation.js'; import { ensureDateString } from '../lib/helpers.js'; import { ValidationError } from '../lib/errors.js'; export class TrainsModule { constructor(private client: BaseClient) { } async getByHeadcode(headcode: string, date: string | Date = new Date, toc: string = "", customFetch?: typeof fetch): Promise> { if (!IsValidHeadcode(headcode)) { throw new ValidationError("headcode", "Invalid headcode format") } if (toc !== "" && !IsValidToc(toc)) { throw new ValidationError("TOC", "Invalid TOC Format, needs to be two characters"); } const dateStr = ensureDateString(date); if (!IsValidDateStr(dateStr)) { throw new ValidationError("Date", "Invalid date format, need YYYY-MM-DD"); } const searchParams = new URLSearchParams(); searchParams.append('d', dateStr); searchParams.append('h', headcode); if (toc) searchParams.append('t', toc); const path = `/trains?${searchParams.toString()}`; return this.client.request(path, { method: 'GET', }, customFetch); } async getByRid(rid: string, customFetch?: typeof fetch): Promise> { if (!IsValidRid(rid)) { throw new ValidationError("RID", "Invalid RID format, must be 15-16 numerals") } const path = `/train/${rid}` return this.client.request(path, { method: 'GET', }, customFetch); } }