import {z} from 'zod' export type CreateApiAppAnalyzeGReportingQuerierArgs = {body: {linkingId: number, resource: string, select: Array, where?: (Array<{column: string, operator?: ('=' | '!=' | '<' | '<=' | '>' | '>=' | 'LIKE' | 'IN' | 'NOT_IN' | 'DURING' | null) | undefined, value?: (boolean | string | number | Array | null | null) | undefined, boolean?: ('AND' | 'OR' | null) | undefined}> | null) | undefined, sort?: (Array<{column: string, direction?: ('ASC' | 'DESC' | null) | undefined}> | null) | undefined, limit?: (number | null) | undefined, perPage?: (number | null) | undefined, cursor?: (string | null) | undefined}}; export type CreateApiAppAnalyzeGReportingQuerierOptions = {baseUrl: string, token?: string | undefined, fetch?: typeof fetch | undefined}; export const appAnalyzeGoogleAdsReportingRowSchema = z.record(z.string(), z.unknown()); export const createApiAppAnalyzeGReportingQuerierResponse = z.object({items: z.array(appAnalyzeGoogleAdsReportingRowSchema), perPage: z.number().int(), next: z.string().nullable()}); 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/reporting/querier */ export const createApiAppAnalyzeGReportingQuerier = async (args: CreateApiAppAnalyzeGReportingQuerierArgs, options: CreateApiAppAnalyzeGReportingQuerierOptions) => { 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/reporting/querier${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(createApiAppAnalyzeGReportingQuerierResponse, await res.json()) };