import {z} from 'zod' export type GetApiCustomerBusinessesHandleArgs = {handle: string}; export type GetApiCustomerBusinessesHandleOptions = {baseUrl?: string | undefined, http?: string | undefined, fetch?: typeof fetch | undefined}; export const businessProfileResource = z.object({id: z.string(), name: z.string(), handle: z.string(), description: z.string(), category: z.string(), address: z.string(), city: z.string(), state: z.string(), zip: z.string(), latitude: z.number(), longitude: z.number(), phone: z.string(), website: z.string(), logo: z.string(), cover_photo: z.string(), hours: z.record(z.string(), z.object({isOpen: z.boolean(), openTime: z.string(), closeTime: z.string()}))}); export const getApiCustomerBusinessesHandleResponse = z.object({data: businessProfileResource}); 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 business profile * * GET /customer/businesses/{handle} */ export const getApiCustomerBusinessesHandle = async (args: GetApiCustomerBusinessesHandleArgs, options?: GetApiCustomerBusinessesHandleOptions) => { const {baseUrl = "http://51eat.co/api/v1", fetch: fetchFn = fetch, http} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (http !== undefined) headers.set('authorization', `Bearer ${http}`) const url = `${baseUrl.replace(/\/+$/, '')}/customer/businesses/${encodeURIComponent(String(args.handle))}${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(getApiCustomerBusinessesHandleResponse, await res.json()) };