import {z} from 'zod' export type GetApiAppYelpBusinessesArgs = {q?: string | undefined, status?: string | undefined}; export type GetApiAppYelpBusinessesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appYelpBusinessSchema = z.object({id: z.string(), yelpBusinessId: z.string().nullable(), partnerBusinessId: z.string().nullable(), name: z.string(), city: z.string().nullable(), state: z.string().nullable(), rating: z.number().nullable(), reviewCount: z.number().int(), status: z.string(), activePrograms: z.number().int(), spendMtdCents: z.number().int(), leadsMtd: z.number().int(), lastSyncedAt: z.string().nullable(), serviceTags: z.array(z.string()), categories: z.array(z.string()), addressLines: z.array(z.string()), phone: z.string().nullable(), yelpUrl: z.string().nullable(), imageUrl: z.string().nullable(), vertical: z.string(), verticalLabel: z.string(), verticalSource: z.string().nullable(), aiProfileSummary: z.string().nullable(), aiServiceFocus: z.array(z.string()), aiDemandTriggers: z.array(z.string()), aiRecommendedSignals: z.array(z.string()), aiCustomerIntentNotes: z.string().nullable(), aiProfileSource: z.string().nullable(), aiProfileGeneratedAt: z.string().nullable(), latestListingUpdate: z.object({}).nullable(), enabled: z.boolean(), enabledAt: z.string().nullable(), spendMtdAvailable: z.boolean()}); export const getApiAppYelpBusinessesResponse = z.array(appYelpBusinessSchema); 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 }; /** * List Yelp businesses synced for the current user * * GET /app/yelp/businesses */ export const getApiAppYelpBusinesses = async (args: GetApiAppYelpBusinessesArgs, options: GetApiAppYelpBusinessesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.status != null) query.append("status", String(args.status)) const url = `${baseUrl.replace(/\/+$/, '')}/app/yelp/businesses${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(getApiAppYelpBusinessesResponse, await res.json()) };