Implement use of a custom fetch fn

This commit is contained in:
2026-05-02 10:09:18 +01:00
parent 50c2e5f427
commit ed2f8527d6
5 changed files with 16 additions and 12 deletions

View File

@@ -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<T>(path: string, options: RequestInit = {}): Promise<ApiResult<T>> {
public async request<T>(path: string, options: RequestInit = {}, fetcher: typeof fetch = fetch): Promise<ApiResult<T>> {
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}`);

View File

@@ -4,12 +4,13 @@ import type { BaseClient, ApiResult } from '../lib/base.js';
export class LocationFilterModule {
constructor(private client: BaseClient) {}
async getLocationFilterData(): Promise<ApiResult<ApiLocationFilter.LocationFilterObject[]>> {
async getLocationFilterData(customFetch?: typeof fetch): Promise<ApiResult<ApiLocationFilter.LocationFilterObject[]>> {
const path = '/locationFilter/data';
return this.client.request<ApiLocationFilter.LocationFilterObject[]>(
path,
{method: "GET"}
{method: "GET"},
customFetch,
);
}
}

View File

@@ -6,7 +6,7 @@ import { ValidationError } from '../lib/errors.js';
export class PisModule {
constructor(private client: BaseClient) {}
async getByStartEndCrs(startCrs: string, endCrs: string): Promise<ApiResult<ApiPisObject.PisObjects[]>> {
async getByStartEndCrs(startCrs: string, endCrs: string, customFetch?: typeof fetch): Promise<ApiResult<ApiPisObject.PisObjects[]>> {
if (!IsValidCrs(startCrs)) {
throw new ValidationError("startCrs", "Invalid CRS Format")
}
@@ -18,10 +18,10 @@ export class PisModule {
return this.client.request<ApiPisObject.PisObjects[]>(path, {
method: 'GET',
});
}, customFetch);
}
async getByCode(code: string): Promise<ApiResult<ApiPisObject.PisObjects[]>> {
async getByCode(code: string, customFetch?: typeof fetch): Promise<ApiResult<ApiPisObject.PisObjects[]>> {
if (!IsValidPis(code)) {
throw new ValidationError("code", "Invalid PIS Code Format")
}
@@ -30,6 +30,6 @@ export class PisModule {
return this.client.request<ApiPisObject.PisObjects[]>(path, {
method: 'GET',
})
}, customFetch)
}
}

View File

@@ -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<ApiResult<ApiStationsNearestStations.StationsNearestStations[]>> {
async getNearestStations(geohash: string, customFetch?: typeof fetch): Promise<ApiResult<ApiStationsNearestStations.StationsNearestStations[]>> {
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<ApiStationsNearestStations.StationsNearestStations[]>(path, {
method: 'GET',
})
}, customFetch)
}
// getStationSate(crs: string){}

View File

@@ -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<ApiResult<ApiTrainsTrainByHeadcode.TrainByHeadcodeResponse[]>> {
async getByHeadcode(headcode: string, date: string | Date = new Date, toc: string = "", customFetch?: typeof fetch): Promise<ApiResult<ApiTrainsTrainByHeadcode.TrainByHeadcodeResponse[]>> {
if (!IsValidHeadcode(headcode)) {
throw new ValidationError("headcode", "Invalid headcode format")
}
@@ -29,6 +29,8 @@ export class TrainsModule {
return this.client.request<ApiTrainsTrainByHeadcode.TrainByHeadcodeResponse[]>(path, {
method: 'GET',
});
}, customFetch);
}
async getByRid(rid: string, customFetch?: typeof fetch) {}
}