import {z} from 'zod' export type CreateApiAppAnalyzeGHealthArgs = {body: {linking_id: number, period_days?: (30 | 60 | 90) | undefined, channel?: ('PERFORMANCE_MAX' | 'SEARCH' | 'DEMAND_GEN') | undefined}}; export type CreateApiAppAnalyzeGHealthOptions = {baseUrl: string, token?: string | undefined, fetch?: typeof fetch | undefined}; export const appAnalyzeGoogleAdsHealthMetricsSchema = z.object({cost: z.number(), clicks: z.number().int(), impressions: z.number().int(), conversions: z.number(), conversionValue: z.number(), cpa: z.number(), roas: z.number(), ctr: z.number(), cpc: z.number(), convRate: z.number()}); export const appAnalyzeGoogleAdsHealthAccountSchema = z.object({campaignCount: z.number().int(), hasConversionValue: z.boolean(), totals: appAnalyzeGoogleAdsHealthMetricsSchema}); export const appAnalyzeGoogleAdsHealthDeltasSchema = z.object({cost: z.number().nullable(), conversions: z.number().nullable(), conversionValue: z.number().nullable(), cpa: z.number().nullable(), roas: z.number().nullable(), convRate: z.number().nullable(), ctr: z.number().nullable(), cpc: z.number().nullable()}); export const appAnalyzeGoogleAdsHealthFlagSchema = z.object({level: z.enum(["GREEN", "YELLOW", "RED", "INSUFFICIENT_DATA"]), area: z.string(), message: z.string()}); export const appAnalyzeGoogleAdsHealthCampaignSchema = z.object({campaignId: z.number().int(), campaignName: z.string(), goalType: z.enum(["LEADGEN", "ECOMMERCE"]), status: z.enum(["GOOD", "STABLE", "NEEDS_ATTENTION", "ISSUE", "INSUFFICIENT_DATA"]), trend: z.enum(["IMPROVING", "STABLE", "DECLINING", "INSUFFICIENT_DATA"]), score: z.number().int().nullable(), topIssue: z.string().nullable(), current: appAnalyzeGoogleAdsHealthMetricsSchema, previous: appAnalyzeGoogleAdsHealthMetricsSchema, deltas: appAnalyzeGoogleAdsHealthDeltasSchema, flags: z.array(appAnalyzeGoogleAdsHealthFlagSchema)}); export const appAnalyzeGoogleAdsHealthReportSchema = z.object({linkingId: z.number().int(), linkingCustomerId: z.number().int(), linkingName: z.string(), periodDays: z.number().int(), account: appAnalyzeGoogleAdsHealthAccountSchema, campaigns: z.array(appAnalyzeGoogleAdsHealthCampaignSchema)}); export const createApiAppAnalyzeGHealthResponse = z.object({data: appAnalyzeGoogleAdsHealthReportSchema}); 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 }; /** * POST /app/analyze/g/health */ export const createApiAppAnalyzeGHealth = async (args: CreateApiAppAnalyzeGHealthArgs, options: CreateApiAppAnalyzeGHealthOptions) => { const {baseUrl, fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set("authorization", token) const url = `${baseUrl.replace(/\/+$/, '')}/app/analyze/g/health${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiAppAnalyzeGHealthResponse, await res.json()) };