import {z} from 'zod' export type GetApiV2ServicesServiceServiceNumberDateArgs = {service_number: string, date: string, language?: ('nl' | 'en') | undefined}; export type GetApiV2ServicesServiceServiceNumberDateOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const stationObject = z.object({code: z.string().optional(), short: z.string().optional(), medium: z.string().optional(), long: z.string().optional()}); export const service = z.object({company: z.string().optional(), id: z.string().optional(), journey_planner: z.boolean().optional(), parts: z.array(z.object({remarks: z.array(z.string()).optional(), service_number: z.string().optional(), stops: z.array(z.object({arrival_cancelled: z.boolean().optional(), arrival_delay: z.number().int().gte(0).optional(), arrival_platform_actual: z.string().optional(), arrival_platform_planned: z.string().optional(), arrival_time: z.string().optional(), assistance_available: z.boolean().optional(), departure_cancelled: z.boolean().optional(), departure_delay: z.number().int().gte(0).optional(), departure_platform_actual: z.string().optional(), departure_platform_planned: z.string().optional(), departure_time: z.string().optional(), do_not_board: z.boolean().optional(), material: z.array(z.object({accessible: z.boolean().optional(), destination: z.string().optional(), destination_code: z.string().optional(), number: z.string().optional(), position: z.number().int().gte(0).optional(), remains_behind: z.boolean().optional(), type: z.string().optional()})).optional(), remarks: z.array(z.string()).optional(), station: stationObject.optional(), station_accessible: z.boolean().optional(), stop_type: z.string().optional(), stopping_actual: z.boolean().optional(), stopping_planned: z.boolean().optional(), tips: z.array(z.string()).optional()})).optional(), tips: z.array(z.string()).optional()})).optional(), remarks: z.array(z.string()).optional(), tips: z.array(z.string()).optional(), reservation_required: z.boolean().optional(), with_supplement: z.boolean().optional(), service_date: z.string().optional(), service_number: z.string().optional(), timestamp: z.string().optional(), type: z.string().optional(), type_code: z.string().optional()}); export const statusField = z.enum(["UNKNOWN", "DOWN", "RECOVERING", "UP"]); export const getApiV2ServicesServiceServiceNumberDateResponse = z.object({service: service.optional(), status: statusField.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Retrieve service details * * GET /v2/services/service/{service_number}/{date} */ export const getApiV2ServicesServiceServiceNumberDate = async (args: GetApiV2ServicesServiceServiceNumberDateArgs, options?: GetApiV2ServicesServiceServiceNumberDateOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.language != null) query.append("language", String(args.language)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/services/service/${encodeURIComponent(String(args.service_number))}/${encodeURIComponent(String(args.date))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV2ServicesServiceServiceNumberDateResponse, await res.json()) };