import {z} from 'zod' export type GetApiRestaurantsArgs = {filter?: unknown | undefined, q?: unknown | undefined, latitude?: unknown | undefined, longitude?: unknown | undefined, radius?: unknown | undefined, sort?: unknown | undefined, sortby?: unknown | undefined, city?: unknown | undefined, cuisine?: Array | undefined}; export type GetApiRestaurantsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const restaurant = z.object({id: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), name: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), mobile: z.string().optional(), fax: z.string().optional(), contact_name: z.string().optional(), contact_phone: z.string().optional(), website: z.string().optional(), address1: z.string().optional(), address: z.string().optional(), zip_code: z.string().optional(), latitude: z.number().optional(), longitude: z.number().optional(), sales_tax: z.number().optional(), minimum_order_for_booking: z.number().optional(), is_allow_users_to_door_delivery_order: z.number().int().optional(), estimated_time_to_delivery: z.number().int().optional(), delivery_charge: z.number().optional(), delivery_miles: z.number().int().optional(), slug: z.string().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 }; /** * Fetch all restaurants * * GET /restaurants */ export const getApiRestaurants = async (args: GetApiRestaurantsArgs, options: GetApiRestaurantsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter != null) query.append("filter", String(args.filter)) if (args.q != null) query.append("q", String(args.q)) if (args.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.radius != null) query.append("radius", String(args.radius)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.sortby != null) query.append("sortby", String(args.sortby)) if (args.city != null) query.append("city", String(args.city)) for (const item of args.cuisine ?? []) query.append("cuisine", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/restaurants${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(restaurant, await res.json()) };