import {z} from 'zod' export type GetApiV3DiaryShortNameAllocationsArgs = {shortName: string, preferredDate: string, appointmentType: string, lettings?: boolean | undefined, propertyIdentifier?: string | undefined, branchID?: string | undefined}; export type GetApiV3DiaryShortNameAllocationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const diaryBookingModel = z.object({Start: z.string().optional(), End: z.string().optional(), StaffName: z.string().optional(), StaffID: z.string().optional()}); export const getApiV3DiaryShortNameAllocationsResponse = z.array(diaryBookingModel); 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 }; /** * Get a list of all available allocations for a date + 7 days for a specified appointment type * * GET /v3/diary/{shortName}/allocations */ export const getApiV3DiaryShortNameAllocations = async (args: GetApiV3DiaryShortNameAllocationsArgs, options?: GetApiV3DiaryShortNameAllocationsOptions) => { const {baseUrl = "https://live-api.letmc.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.preferredDate != null) query.append("preferredDate", String(args.preferredDate)) if (args.appointmentType != null) query.append("appointmentType", String(args.appointmentType)) if (args.lettings != null) query.append("lettings", String(args.lettings)) if (args.propertyIdentifier != null) query.append("propertyIdentifier", String(args.propertyIdentifier)) if (args.branchID != null) query.append("branchID", String(args.branchID)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/diary/${encodeURIComponent(String(args.shortName))}/allocations${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(getApiV3DiaryShortNameAllocationsResponse, await res.json()) };