import {z} from 'zod' export type GetApiTrainersArgs = {favorite_gym_name: string, store_name: string, training_start_at?: string | undefined, training_end_at?: string | undefined, budget?: number | undefined, training_menu_typ?: number | undefined}; export type GetApiTrainersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const trainer = z.object({trainer_id: z.string().optional(), trainer_name: z.string().optional(), unit_price: z.number().int().optional(), business_start_at: z.string().optional(), business_end_at: z.string().optional(), public_mail_addr: z.string().optional(), public_tel: z.string().optional(), store_name: z.string().optional(), gym_name: z.string().optional(), star_count: z.number().optional()}); export const trainers = z.object({count: z.number().int().optional(), items: z.array(trainer).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 }; /** * API007 パーソナルトレーナー検索 * * GET /trainers */ export const getApiTrainers = async (args: GetApiTrainersArgs, options: GetApiTrainersOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.favorite_gym_name != null) query.append("favorite_gym_name", String(args.favorite_gym_name)) if (args.store_name != null) query.append("store_name", String(args.store_name)) if (args.training_start_at != null) query.append("training_start_at", String(args.training_start_at)) if (args.training_end_at != null) query.append("training_end_at", String(args.training_end_at)) if (args.budget != null) query.append("budget", String(args.budget)) if (args.training_menu_typ != null) query.append("training_menu_typ", String(args.training_menu_typ)) const url = `${baseUrl.replace(/\/+$/, '')}/trainers${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(trainers, await res.json()) };