import {z} from 'zod' export type SortDirection = 'Asc' | 'Desc'; export type DateTimeLocal = string; export type GetApiV1BookingsBookingReferenceEventsArgs = {bookingReference: string, leg: number, sort: SortDirection, startingAt?: DateTimeLocal | undefined, endingAt?: DateTimeLocal | undefined}; export type GetApiV1BookingsBookingReferenceEventsOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bookingReference = z.string().min(1).max(32); export const bookingEventType = z.enum(["DriverDepartedToPickup", "DriverArrivedAtPickup", "DriverSubmittedCustomerNoShow", "DriverDepartedToDropoff", "DriverArrivedAtDropoff", "DriverLiveLocation"]); export const dateTimeLocal = z.string().min(19).max(33); export const bookingEvent = z.object({eventType: bookingEventType, occurredAt: dateTimeLocal, latitude: z.number().gte(-90).lte(90).nullable().optional(), longitude: z.number().gte(-180).lte(180).nullable().optional()}); export const getBookingEventResponse = z.object({bookingReference: bookingReference, leg: z.number().int().gte(1).lte(2), items: z.array(bookingEvent)}); 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 }; /** * Gets Booking Events for a Booking leg. * * GET /v1/bookings/{bookingReference}/events */ export const getApiV1BookingsBookingReferenceEvents = async (args: GetApiV1BookingsBookingReferenceEventsArgs, options?: GetApiV1BookingsBookingReferenceEventsOptions) => { const {baseUrl = "https://your.domain.com", fetch: fetchFn = fetch, Basic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.leg != null) query.append("leg", String(args.leg)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.startingAt != null) query.append("startingAt", String(args.startingAt)) if (args.endingAt != null) query.append("endingAt", String(args.endingAt)) if (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/bookings/${encodeURIComponent(String(args.bookingReference))}/events${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(getBookingEventResponse, await res.json()) };