import {z} from 'zod' export type GetApiBookingsTraineeIdArgs = {trainee_id: string, start_at?: string | undefined, end_at?: string | undefined}; export type GetApiBookingsTraineeIdOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const booking = z.object({booking_id: z.string().optional(), menu_id: z.string().optional(), trainer_id: z.string().optional(), display_order: z.string().optional(), menu_name: z.string().optional(), menu_description: z.string().optional(), required_time: z.number().int().optional(), price: z.number().int().optional(), start_at: z.string().optional(), end_at: z.string().optional(), training_start_at: z.string().optional(), training_end_at: z.string().optional()}); export const bookings = z.object({bokkings: z.array(booking).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 }; /** * API013 予約済みトレーニング取得 * * GET /bookings/{trainee_id} */ export const getApiBookingsTraineeId = async (args: GetApiBookingsTraineeIdArgs, options: GetApiBookingsTraineeIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_at != null) query.append("start_at", String(args.start_at)) if (args.end_at != null) query.append("end_at", String(args.end_at)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/bookings/${encodeURIComponent(String(args.trainee_id))}${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(bookings, await res.json()) };