diff --git a/src/clients/PisClientV2.ts b/src/clients/PisClientV2.ts index 17bd010..54c429a 100644 --- a/src/clients/PisClientV2.ts +++ b/src/clients/PisClientV2.ts @@ -1,5 +1,6 @@ import { BaseOwlBoardClient } from "./client"; -import { ValidationError } from "../errors"; +import { PisV2_Long, PisV2_Short } from "../types/pis/PisTypesV2"; +import { validateCrs, validatePisCode, validateTiploc } from "../inputValidation/inputValidation"; export class PisClientV2 { private client: BaseOwlBoardClient; @@ -8,12 +9,26 @@ export class PisClientV2 { this.client = client; } - async getStopsByPis(code: string): Promise { - const codeRegex = /^\d{4}$/; - if (!codeRegex.test(code)) { - throw new ValidationError("Invalid input: code must be a four-character string consisting of only numerals") - } + async getStopsByPis(code: string): Promise { + validatePisCode(code); const path = `/api/v2/pis/byCode/${code}` return this.client.makeRequest("GET", path); } + + //* >> This feature is not currently exposed by the API << // + async getPisByTiplocList(tiploc_list: string[]): Promise { + for (const tiploc in tiploc_list) { + validateTiploc(tiploc); + } + const path = `/api/v2/pis/byTiplocList/${tiploc_list}` + return this.client.makeRequest("GET", path); + } + //*/ + + async getPisByStartEndCRS(startCrs: string, endCrs: string): Promise { + validateCrs(startCrs); + validateCrs(endCrs); + const path = `/api/v2/pis/byStartEnd/${startCrs}/${endCrs}` + return this.client.makeRequest("GET", path); + } } \ No newline at end of file diff --git a/src/inputValidation/inputValidation.ts b/src/inputValidation/inputValidation.ts new file mode 100644 index 0000000..5577f63 --- /dev/null +++ b/src/inputValidation/inputValidation.ts @@ -0,0 +1,25 @@ +import { ValidationError } from "../errors"; + +export function validatePisCode(code: string): boolean { + const codeRegex = /^\d{4}$/; + if (!codeRegex.test(code)) { + throw new ValidationError("Invalid input: code must be a four-character string consisting of only numerals"); + } + return true; +} + +export function validateTiploc(tiploc: string): boolean { + const tiplocRegex = /[a-zA-z0-9]{4,7}/; + if (!tiplocRegex.test(tiploc)) { + throw new ValidationError("Invalid input: TIPLOC must be between four and seven characters"); + } + return true; +} + +export function validateCrs(crs: string): boolean { + const crsRegex = /[a-zA-z]{3}/; + if (!crsRegex.test(crs)) { + throw new ValidationError("Invalid input: CRS/3ALPHA must be exactly three letters") + } + return true; +} \ No newline at end of file diff --git a/src/types/README.md b/src/types/README.md new file mode 100644 index 0000000..045005c --- /dev/null +++ b/src/types/README.md @@ -0,0 +1,5 @@ +This directory contains TS source files describing TS types which +are returned by the OwlBoard API. + +Files are organised in directories, one for each category - in each directory +there is a file for each version. \ No newline at end of file diff --git a/src/types/pis/PisTypesV2.ts b/src/types/pis/PisTypesV2.ts new file mode 100644 index 0000000..9f47525 --- /dev/null +++ b/src/types/pis/PisTypesV2.ts @@ -0,0 +1,13 @@ +export interface PisV2_Short { + code: string; + toc: string; + skipCount: number; + skipType?: string; +} + +export interface PisV2_Long { + code: string; + stops: string[]; + toc: string; + tiplocs?: string; +} \ No newline at end of file