import {z} from 'zod' export type GetApiAppRecommendationsArgs = {businessId?: string | undefined, domain?: string | undefined, status?: string | undefined}; export type GetApiAppRecommendationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appRecommendationSchema = z.object({id: z.number().int(), businessId: z.string().nullable(), businessName: z.string().nullable(), domain: z.string(), title: z.string(), rationale: z.string(), direction: z.enum(["increase", "decrease", "hold", "action", "pause", "launch"]), action: z.enum(["budget_adjustment", "program_pause", "program_resume", "program_create"]).nullable(), deltaPct: z.number().int().nullable(), service: z.string().nullable(), confidence: z.string(), status: z.string(), source: z.string(), createdAt: z.string().nullable(), acceptedAt: z.string().nullable(), dismissedAt: z.string().nullable(), outcomeCheckedAt: z.string().nullable(), outcomeWindowDays: z.number().int().nullable(), outcomeMetrics: z.object({}).nullable(), yelpAdsJobId: z.number().int().nullable(), yelpAdsJobStatus: z.string().nullable(), yelpAdsJobProgramRef: z.string().nullable(), yelpAdsJobMessage: z.string().nullable()}); export const getApiAppRecommendationsResponse = z.array(appRecommendationSchema); 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 the current user's stored recommendations * * GET /app/recommendations */ export const getApiAppRecommendations = async (args: GetApiAppRecommendationsArgs, options: GetApiAppRecommendationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.businessId != null) query.append("businessId", String(args.businessId)) if (args.domain != null) query.append("domain", String(args.domain)) if (args.status != null) query.append("status", String(args.status)) const url = `${baseUrl.replace(/\/+$/, '')}/app/recommendations${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(getApiAppRecommendationsResponse, await res.json()) };