import {z} from 'zod' export type GoogleAdsSearchads360V23CommonKeywordPlanAggregateMetrics = {aggregateMetricTypes?: (Array<'UNSPECIFIED' | 'UNKNOWN' | 'DEVICE'>) | undefined}; export type GoogleAdsSearchads360V23CommonYearMonth = {month?: ('UNSPECIFIED' | 'UNKNOWN' | 'JANUARY' | 'FEBRUARY' | 'MARCH' | 'APRIL' | 'MAY' | 'JUNE' | 'JULY' | 'AUGUST' | 'SEPTEMBER' | 'OCTOBER' | 'NOVEMBER' | 'DECEMBER') | undefined, year?: unknown | undefined}; export type GoogleAdsSearchads360V23CommonYearMonthRange = {end?: GoogleAdsSearchads360V23CommonYearMonth | undefined, start?: GoogleAdsSearchads360V23CommonYearMonth | undefined}; export type GoogleAdsSearchads360V23CommonHistoricalMetricsOptions = {includeAverageCpc?: boolean | undefined, yearMonthRange?: GoogleAdsSearchads360V23CommonYearMonthRange | undefined}; export type GoogleAdsSearchads360V23ServicesGenerateKeywordHistoricalMetricsRequest = {aggregateMetrics?: GoogleAdsSearchads360V23CommonKeywordPlanAggregateMetrics | undefined, geoTargetConstants?: Array | undefined, historicalMetricsOptions?: GoogleAdsSearchads360V23CommonHistoricalMetricsOptions | undefined, includeAdultKeywords?: boolean | undefined, keywordPlanNetwork?: ('UNSPECIFIED' | 'UNKNOWN' | 'GOOGLE_SEARCH' | 'GOOGLE_SEARCH_AND_PARTNERS') | undefined, keywords?: Array | undefined, language?: string | undefined}; export type CreateApiV23CustomersCustomersIdGenerateKeywordHistoricalMetricsArgs = {customersId: string, body: GoogleAdsSearchads360V23ServicesGenerateKeywordHistoricalMetricsRequest}; export type CreateApiV23CustomersCustomersIdGenerateKeywordHistoricalMetricsOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const googleAdsSearchads360V23CommonKeywordPlanDeviceSearches = z.object({device: z.enum(["UNSPECIFIED", "UNKNOWN", "MOBILE", "TABLET", "DESKTOP", "CONNECTED_TV", "OTHER"]).optional(), searchCount: z.string().optional()}); export const googleAdsSearchads360V23CommonKeywordPlanAggregateMetricResults = z.object({deviceSearches: z.array(googleAdsSearchads360V23CommonKeywordPlanDeviceSearches).optional()}); export const googleAdsSearchads360V23CommonMonthlySearchVolume = z.object({month: z.enum(["UNSPECIFIED", "UNKNOWN", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"]).optional(), monthlySearches: z.string().optional(), year: z.string().optional()}); export const googleAdsSearchads360V23CommonKeywordPlanHistoricalMetrics = z.object({averageCpcMicros: z.string().optional(), avgMonthlySearches: z.string().optional(), competition: z.enum(["UNSPECIFIED", "UNKNOWN", "LOW", "MEDIUM", "HIGH"]).optional(), competitionIndex: z.string().optional(), highTopOfPageBidMicros: z.string().optional(), lowTopOfPageBidMicros: z.string().optional(), monthlySearchVolumes: z.array(googleAdsSearchads360V23CommonMonthlySearchVolume).optional()}); export const googleAdsSearchads360V23ServicesGenerateKeywordHistoricalMetricsResult = z.object({closeVariants: z.array(z.string()).optional(), keywordMetrics: googleAdsSearchads360V23CommonKeywordPlanHistoricalMetrics.optional(), text: z.string().optional()}); export const googleAdsSearchads360V23ServicesGenerateKeywordHistoricalMetricsResponse = z.object({aggregateMetricResults: googleAdsSearchads360V23CommonKeywordPlanAggregateMetricResults.optional(), results: z.array(googleAdsSearchads360V23ServicesGenerateKeywordHistoricalMetricsResult).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 }; /** * Returns a list of keyword historical metrics. List of thrown errors: [AuthenticationError]() [AuthorizationError]() [CollectionSizeError]() [HeaderError]() [InternalError]() [QuotaError]() [RequestError]() * * POST /v23/customers/{customersId}:generateKeywordHistoricalMetrics */ export const createApiV23CustomersCustomersIdGenerateKeywordHistoricalMetrics = async (args: CreateApiV23CustomersCustomersIdGenerateKeywordHistoricalMetricsArgs, options?: CreateApiV23CustomersCustomersIdGenerateKeywordHistoricalMetricsOptions) => { const {baseUrl = "https://searchads360.googleapis.com", 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(/\/+$/, '')}/v23/customers/${encodeURIComponent(String(args.customersId))}:generateKeywordHistoricalMetrics${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(googleAdsSearchads360V23ServicesGenerateKeywordHistoricalMetricsResponse, await res.json()) };