import {z} from 'zod' export type DateRange = {endDate?: string | undefined, kind?: string | undefined, relativeDateRange?: ('TODAY' | 'YESTERDAY' | 'WEEK_TO_DATE' | 'MONTH_TO_DATE' | 'QUARTER_TO_DATE' | 'YEAR_TO_DATE' | 'PREVIOUS_WEEK' | 'PREVIOUS_MONTH' | 'PREVIOUS_QUARTER' | 'PREVIOUS_YEAR' | 'LAST_7_DAYS' | 'LAST_30_DAYS' | 'LAST_90_DAYS' | 'LAST_365_DAYS' | 'LAST_24_MONTHS' | 'LAST_14_DAYS' | 'LAST_60_DAYS') | undefined, startDate?: string | undefined}; export type DimensionValue = {dimensionName?: string | undefined, etag?: string | undefined, id?: string | undefined, kind?: string | undefined, matchType?: ('EXACT' | 'BEGINS_WITH' | 'CONTAINS' | 'WILDCARD_EXPRESSION') | undefined, value?: string | undefined}; export type SortBy = {name?: string | undefined, sortOrder?: ('ASCENDING' | 'DESCENDING') | undefined}; export type ReportDataQueryRequest = {dateRange?: DateRange | undefined, dimensionFilters?: Array | undefined, dimensionNames?: Array | undefined, maxResults?: number | undefined, metricNames?: Array | undefined, pageToken?: string | undefined, sortBys?: Array | undefined}; export type CreateApiUserprofilesProfileIdReportdataQueryArgs = {profileId: unknown, body: ReportDataQueryRequest}; export type CreateApiUserprofilesProfileIdReportdataQueryOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const columnHeader = z.object({name: z.string().optional(), type: z.enum(["COLUMN_TYPE_UNSPECIFIED", "DIMENSION", "METRIC"]).optional()}); export const reportDataRow = z.object({values: z.array(z.string()).optional()}); export const reportDataResponse = z.object({columnHeaders: z.array(columnHeader).optional(), nextPageToken: z.string().optional(), rows: z.array(reportDataRow).optional(), totalRow: reportDataRow.optional()}); 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 }; /** * Executes an ad-hoc query and returns structured JSON payload data. * * POST /userprofiles/{profileId}/reportdata/query */ export const createApiUserprofilesProfileIdReportdataQuery = async (args: CreateApiUserprofilesProfileIdReportdataQueryArgs, options?: CreateApiUserprofilesProfileIdReportdataQueryOptions) => { const {baseUrl = "https://dfareporting.googleapis.com/dfareporting/v5", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/userprofiles/${encodeURIComponent(String(args.profileId))}/reportdata/query${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(reportDataResponse, await res.json()) };