import {z} from 'zod' export type GetApiAppYelpAdsJobsArgs = {businessId?: string | undefined, programRef?: string | undefined, status?: string | undefined, limit?: number | undefined}; export type GetApiAppYelpAdsJobsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appYelpAdsJobSchema = z.object({id: z.number().int(), recommendationId: z.number().int().nullable(), recommendationTitle: z.string().nullable(), businessId: z.string().nullable(), businessName: z.string().nullable(), programRef: z.string().nullable(), action: z.string(), status: z.string(), requestedDirection: z.string().nullable(), requestedDeltaPct: z.number().int().nullable(), currentBudgetCents: z.number().int().nullable(), targetBudgetCents: z.number().int().nullable(), guardrailsPassed: z.boolean().nullable(), partnerWriteEnabled: z.boolean().nullable(), blockReason: z.string().nullable(), message: z.string().nullable(), submittedAt: z.string().nullable(), checkedAt: z.string().nullable(), completedAt: z.string().nullable(), createdAt: z.string().nullable()}); export const getApiAppYelpAdsJobsResponse = z.array(appYelpAdsJobSchema); 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 guarded Yelp ads jobs for the current user * * GET /app/yelp/ads-jobs */ export const getApiAppYelpAdsJobs = async (args: GetApiAppYelpAdsJobsArgs, options: GetApiAppYelpAdsJobsOptions) => { 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.programRef != null) query.append("programRef", String(args.programRef)) if (args.status != null) query.append("status", String(args.status)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/app/yelp/ads-jobs${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(getApiAppYelpAdsJobsResponse, await res.json()) };