Separate input validation and extend PIS functions
This commit is contained in:
parent
071328bdd0
commit
d20d981190
@ -1,5 +1,6 @@
|
|||||||
import { BaseOwlBoardClient } from "./client";
|
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 {
|
export class PisClientV2 {
|
||||||
private client: BaseOwlBoardClient;
|
private client: BaseOwlBoardClient;
|
||||||
@ -8,12 +9,26 @@ export class PisClientV2 {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getStopsByPis(code: string): Promise<any> {
|
async getStopsByPis(code: string): Promise<PisV2_Long> {
|
||||||
const codeRegex = /^\d{4}$/;
|
validatePisCode(code);
|
||||||
if (!codeRegex.test(code)) {
|
|
||||||
throw new ValidationError("Invalid input: code must be a four-character string consisting of only numerals")
|
|
||||||
}
|
|
||||||
const path = `/api/v2/pis/byCode/${code}`
|
const path = `/api/v2/pis/byCode/${code}`
|
||||||
return this.client.makeRequest("GET", path);
|
return this.client.makeRequest("GET", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//* >> This feature is not currently exposed by the API << //
|
||||||
|
async getPisByTiplocList(tiploc_list: string[]): Promise<PisV2_Short> {
|
||||||
|
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<PisV2_Short> {
|
||||||
|
validateCrs(startCrs);
|
||||||
|
validateCrs(endCrs);
|
||||||
|
const path = `/api/v2/pis/byStartEnd/${startCrs}/${endCrs}`
|
||||||
|
return this.client.makeRequest("GET", path);
|
||||||
|
}
|
||||||
}
|
}
|
25
src/inputValidation/inputValidation.ts
Normal file
25
src/inputValidation/inputValidation.ts
Normal file
@ -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;
|
||||||
|
}
|
5
src/types/README.md
Normal file
5
src/types/README.md
Normal file
@ -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.
|
13
src/types/pis/PisTypesV2.ts
Normal file
13
src/types/pis/PisTypesV2.ts
Normal file
@ -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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user