api-railway
Version:
Api wrapper for api.railwayapi.site
91 lines (90 loc) • 2.86 kB
TypeScript
import HealthCheck from "./health_check.js";
import Schedules from "./schedules.js";
import Stations from "./stations.js";
import Trains from "./trains.js";
import TrainsBtwStations from "./trainsBtwStations.js";
import type { ApiError, HTTP_MEHTODS, StationCode, TrainNumber } from "./types.js";
type APIAttributes = {
trains: {
trainNumber: TrainNumber;
limit?: number;
} | {
q: string;
limit?: number;
};
stations: {
stationCode: StationCode;
limit?: number;
} | {
q: string;
limit?: number;
};
schedules: {
fullSchedule: boolean;
};
trainsBtwStations: {
fromStation: StationCode;
toStation: StationCode;
date?: string;
allTrains?: boolean;
flexible?: boolean;
};
healthCheck: {};
};
type API = keyof APIAttributes;
type SegmentTypes = {
healthCheck: "health_check";
trains: "trains" | TrainNumber;
stations: "stations" | StationCode;
schedules: "schedules" | TrainNumber;
trainsBtwStations: "trainsBtwStations";
};
type FetchOptions = {
url: string;
headers?: Record<string, string>;
method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
params: Record<string, string>;
body: BodyInit | null;
};
export declare function catchError<T, E = Error>(promise: Promise<T>): Promise<[undefined, T] | [E]>;
export declare function fetchJson<T, E = Error>(input: RequestInfo | URL, init?: RequestInit): Promise<[undefined, T] | [E]>;
export declare class URLBuilder<T extends API, U> {
private baseURL;
private segments;
private queryParameters?;
private headers;
private body;
private method;
constructor(baseURL: string, headers?: Record<string, string>);
addResource(resource: SegmentTypes[T]): URLBuilder<T, U>;
addQueryParam(value: APIAttributes[T]): URLBuilder<T, U>;
addHeader(key: string, value: string): URLBuilder<T, U>;
setBody(body: BodyInit): URLBuilder<T, U>;
setMethod(method: HTTP_MEHTODS): URLBuilder<T, U>;
buildURL(): FetchOptions;
fetch(requestInit?: RequestInit): Promise<[undefined, ApiError | U] | [Error]>;
}
type Options = {
API_VERSION?: string;
API_KEY?: string;
BASE_URL?: string;
API_TIMEOUT?: number;
PROTOCOL?: "http" | "https";
};
export declare class Client {
#private;
readonly apiKey: string | null;
readonly baseUrl: string;
readonly apiVersion: string;
readonly apiTimeout: number;
readonly protocol: "http" | "https";
readonly trains: Trains;
readonly stations: Stations;
readonly schedules: Schedules;
readonly trainsBtwStations: TrainsBtwStations;
readonly healthCheck: HealthCheck;
readonly headers: Record<string, string>;
constructor(options?: Options);
get apiCalls(): number;
}
export {};