import {z} from 'zod' export type GetApiCustomerBusinessesHandleBulletinsArgs = {handle: string, page?: number | undefined, per_page?: number | undefined}; export type GetApiCustomerBusinessesHandleBulletinsOptions = {baseUrl?: string | undefined, http?: string | undefined, fetch?: typeof fetch | undefined}; export const bulletinResource = z.object({id: z.string(), title: z.string(), content: z.string(), published_at: z.string().nullable()}); export const paginationResource = z.object({current_page: z.string(), per_page: z.string(), total: z.string(), has_more: z.string()}); export const bulletinResponseResource = z.object({bulletins: z.array(bulletinResource), pagination: paginationResource}); export const getApiCustomerBusinessesHandleBulletinsResponse = z.object({data: bulletinResponseResource}); 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 bulletins * * GET /customer/businesses/{handle}/bulletins */ export const getApiCustomerBusinessesHandleBulletins = async (args: GetApiCustomerBusinessesHandleBulletinsArgs, options?: GetApiCustomerBusinessesHandleBulletinsOptions) => { 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))}/bulletins${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(getApiCustomerBusinessesHandleBulletinsResponse, await res.json()) };