import {z} from 'zod' export type GetApiCustomerBusinessesHandleEventsArgs = {handle: string, page?: number | undefined, per_page?: number | undefined}; export type GetApiCustomerBusinessesHandleEventsOptions = {baseUrl?: string | undefined, http?: string | undefined, fetch?: typeof fetch | undefined}; export const image = z.object({id: z.string(), path: z.string(), filename: z.string(), original_filename: z.string(), mime_type: z.string().nullable(), size: z.number().int().nullable(), disk: z.string(), alt_text: z.string().nullable(), imageable_type: z.string(), imageable_id: z.string(), sort_order: z.number().int(), created_at: z.string().nullable(), updated_at: z.string().nullable(), url: z.string()}); export const eventResource = z.object({id: z.string(), title: z.string(), description: z.string().nullable(), starts_at: z.string(), ends_at: z.string(), image: image}); export const paginationResource = z.object({current_page: z.string(), per_page: z.string(), total: z.string(), has_more: z.string()}); export const eventResponseResource = z.object({events: z.array(eventResource), pagination: paginationResource}); export const getApiCustomerBusinessesHandleEventsResponse = z.object({data: eventResponseResource}); 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 events * * GET /customer/businesses/{handle}/events */ export const getApiCustomerBusinessesHandleEvents = async (args: GetApiCustomerBusinessesHandleEventsArgs, options?: GetApiCustomerBusinessesHandleEventsOptions) => { 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 (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (http !== undefined) headers.set('authorization', `Bearer ${http}`) const url = `${baseUrl.replace(/\/+$/, '')}/customer/businesses/${encodeURIComponent(String(args.handle))}/events${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(getApiCustomerBusinessesHandleEventsResponse, await res.json()) };