import {z} from 'zod' export type GetApiV1BookingsBookingReferenceArgs = {bookingReference: string}; export type GetApiV1BookingsBookingReferenceOptions = {baseUrl?: string | undefined, Basic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const bookingReference = z.string().min(1).max(32); export const bookingStatus = z.enum(["Created", "PartiallyModified", "Modified", "PartiallyCancelled", "Cancelled"]); export const dateTimeUtc = z.string().min(20).max(33); export const dateTimeLocal = z.string().min(19).max(33); export const transferStatus = z.enum(["Created", "Modified", "Cancelled"]); export const supplierReference = z.string().min(1).max(50); export const transferResponse = z.object({transferDateTime: dateTimeLocal, status: transferStatus, pickupDateTime: z.string().min(19).max(33).nullable().optional(), supplierReference: supplierReference.nullable().optional()}); export const getBookingResponse = z.object({bookingReference: bookingReference, status: bookingStatus, createdAtUtc: dateTimeUtc, lastModifiedAtUtc: dateTimeUtc, transfers: z.array(transferResponse)}); 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 the current state of a Booking. * * GET /v1/bookings/{bookingReference} */ export const getApiV1BookingsBookingReference = async (args: GetApiV1BookingsBookingReferenceArgs, options?: GetApiV1BookingsBookingReferenceOptions) => { 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 (Basic !== undefined) headers.set('authorization', `Basic ${btoa(`${Basic.username}:${Basic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/bookings/${encodeURIComponent(String(args.bookingReference))}${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(getBookingResponse, await res.json()) };