From ed2f8527d61a9a01ca66a595798b28d8685a0519 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Sat, 2 May 2026 10:09:18 +0100 Subject: [PATCH] Implement use of a custom fetch fn --- src/lib/base.ts | 5 +++-- src/modules/locationFilter.ts | 5 +++-- src/modules/pis.ts | 8 ++++---- src/modules/stationData.ts | 4 ++-- src/modules/trains.ts | 6 ++++-- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/lib/base.ts b/src/lib/base.ts index 5cf190a..e6a8fd0 100644 --- a/src/lib/base.ts +++ b/src/lib/base.ts @@ -34,8 +34,9 @@ export class BaseClient { /** * Handles the Envelope logic + * @param fetcher - Optional custom fetch instance (e.g SvelteKit load fetch) */ - public async request(path: string, options: RequestInit = {}): Promise> { + public async request(path: string, options: RequestInit = {}, fetcher: typeof fetch = fetch): Promise> { const url = `${this.baseUrl}${path}`; console.debug(`[API DEBUG] Calling: ${url}`); @@ -43,7 +44,7 @@ export class BaseClient { headers.set('Content-Type', 'application/json'); if (this.apiKey) headers.set('X-OWL-KEY', this.apiKey); - const response = await fetch(url, { ...options, headers }); + const response = await fetcher(url, { ...options, headers }); if (!response.ok && !response.headers.get('content-type')?.includes('application/json')) { throw new Error(`HTTP ${response.status}: ${response.statusText}`); diff --git a/src/modules/locationFilter.ts b/src/modules/locationFilter.ts index dc00c7d..828848d 100644 --- a/src/modules/locationFilter.ts +++ b/src/modules/locationFilter.ts @@ -4,12 +4,13 @@ import type { BaseClient, ApiResult } from '../lib/base.js'; export class LocationFilterModule { constructor(private client: BaseClient) {} - async getLocationFilterData(): Promise> { + async getLocationFilterData(customFetch?: typeof fetch): Promise> { const path = '/locationFilter/data'; return this.client.request( path, - {method: "GET"} + {method: "GET"}, + customFetch, ); } } \ No newline at end of file diff --git a/src/modules/pis.ts b/src/modules/pis.ts index a62df71..32bbe9d 100644 --- a/src/modules/pis.ts +++ b/src/modules/pis.ts @@ -6,7 +6,7 @@ import { ValidationError } from '../lib/errors.js'; export class PisModule { constructor(private client: BaseClient) {} - async getByStartEndCrs(startCrs: string, endCrs: string): Promise> { + async getByStartEndCrs(startCrs: string, endCrs: string, customFetch?: typeof fetch): Promise> { if (!IsValidCrs(startCrs)) { throw new ValidationError("startCrs", "Invalid CRS Format") } @@ -18,10 +18,10 @@ export class PisModule { return this.client.request(path, { method: 'GET', - }); + }, customFetch); } - async getByCode(code: string): Promise> { + async getByCode(code: string, customFetch?: typeof fetch): Promise> { if (!IsValidPis(code)) { throw new ValidationError("code", "Invalid PIS Code Format") } @@ -30,6 +30,6 @@ export class PisModule { return this.client.request(path, { method: 'GET', - }) + }, customFetch) } } \ No newline at end of file diff --git a/src/modules/stationData.ts b/src/modules/stationData.ts index 9c1be6c..7163357 100644 --- a/src/modules/stationData.ts +++ b/src/modules/stationData.ts @@ -22,7 +22,7 @@ export class StationDataModule { * @param geohash Geohash as string (up to six characters), generate using this.generateGeohash() * @returns Nearest Stations API Response (CRS, Name) */ - async getNearestStations(geohash: string): Promise> { + async getNearestStations(geohash: string, customFetch?: typeof fetch): Promise> { if (!IsValidGeoHash(geohash)) { throw new ValidationError("hash", "Invalid Geohash requested"); } @@ -30,7 +30,7 @@ export class StationDataModule { const path = `/stationData/nearest/${geohash}`; return this.client.request(path, { method: 'GET', - }) + }, customFetch) } // getStationSate(crs: string){} diff --git a/src/modules/trains.ts b/src/modules/trains.ts index 8ba9f47..2a53489 100644 --- a/src/modules/trains.ts +++ b/src/modules/trains.ts @@ -7,7 +7,7 @@ import { ValidationError } from '../lib/errors.js'; export class TrainsModule { constructor(private client: BaseClient) { } - async getByHeadcode(headcode: string, date: string | Date = new Date, toc: string = ""): Promise> { + 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") } @@ -29,6 +29,8 @@ export class TrainsModule { return this.client.request(path, { method: 'GET', - }); + }, customFetch); } + + async getByRid(rid: string, customFetch?: typeof fetch) {} } \ No newline at end of file