import {z} from 'zod' export type GetApiSiteExplorerTotalSearchVolumeHistoryArgs = {mode?: ('exact' | 'prefix' | 'domain' | 'subdomains') | undefined, target: string, portfolio_id?: number | undefined, protocol?: ('both' | 'http' | 'https') | undefined, country?: ('ad' | 'ae' | 'af' | 'ag' | 'ai' | 'al' | 'am' | 'ao' | 'ar' | 'as' | 'at' | 'au' | 'aw' | 'az' | 'ba' | 'bb' | 'bd' | 'be' | 'bf' | 'bg' | 'bh' | 'bi' | 'bj' | 'bn' | 'bo' | 'br' | 'bs' | 'bt' | 'bw' | 'by' | 'bz' | 'ca' | 'cd' | 'cf' | 'cg' | 'ch' | 'ci' | 'ck' | 'cl' | 'cm' | 'cn' | 'co' | 'cr' | 'cu' | 'cv' | 'cy' | 'cz' | 'de' | 'dj' | 'dk' | 'dm' | 'do' | 'dz' | 'ec' | 'ee' | 'eg' | 'es' | 'et' | 'fi' | 'fj' | 'fm' | 'fo' | 'fr' | 'ga' | 'gb' | 'gd' | 'ge' | 'gf' | 'gg' | 'gh' | 'gi' | 'gl' | 'gm' | 'gn' | 'gp' | 'gq' | 'gr' | 'gt' | 'gu' | 'gy' | 'hk' | 'hn' | 'hr' | 'ht' | 'hu' | 'id' | 'ie' | 'il' | 'im' | 'in' | 'iq' | 'is' | 'it' | 'je' | 'jm' | 'jo' | 'jp' | 'ke' | 'kg' | 'kh' | 'ki' | 'kn' | 'kr' | 'kw' | 'ky' | 'kz' | 'la' | 'lb' | 'lc' | 'li' | 'lk' | 'ls' | 'lt' | 'lu' | 'lv' | 'ly' | 'ma' | 'mc' | 'md' | 'me' | 'mg' | 'mk' | 'ml' | 'mm' | 'mn' | 'mq' | 'mr' | 'ms' | 'mt' | 'mu' | 'mv' | 'mw' | 'mx' | 'my' | 'mz' | 'na' | 'nc' | 'ne' | 'ng' | 'ni' | 'nl' | 'no' | 'np' | 'nr' | 'nu' | 'nz' | 'om' | 'pa' | 'pe' | 'pf' | 'pg' | 'ph' | 'pk' | 'pl' | 'pn' | 'pr' | 'ps' | 'pt' | 'py' | 'qa' | 're' | 'ro' | 'rs' | 'ru' | 'rw' | 'sa' | 'sb' | 'sc' | 'se' | 'sg' | 'sh' | 'si' | 'sk' | 'sl' | 'sm' | 'sn' | 'so' | 'sr' | 'st' | 'sv' | 'td' | 'tg' | 'th' | 'tj' | 'tk' | 'tl' | 'tm' | 'tn' | 'to' | 'tr' | 'tt' | 'tw' | 'tz' | 'ua' | 'ug' | 'us' | 'uy' | 'uz' | 'vc' | 've' | 'vg' | 'vi' | 'vn' | 'vu' | 'ws' | 'ye' | 'yt' | 'za' | 'zm' | 'zw') | undefined, date_from: string, date_to?: string | undefined, history_grouping?: ('daily' | 'weekly' | 'monthly') | undefined, top_positions?: ('top_10' | 'top_100') | undefined, volume_mode?: ('monthly' | 'average') | undefined, output?: ('json' | 'csv' | 'xml' | 'php') | undefined}; export type GetApiSiteExplorerTotalSearchVolumeHistoryOptions = {baseUrl?: string | undefined, http?: string | undefined, fetch?: typeof fetch | undefined}; export const siteExplorerTotalSearchVolumeHistory = z.object({metrics: z.array(z.object({date: z.string().optional(), total_search_volume: z.number().int().optional()})).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 }; /** * Total search volume history * * GET /site-explorer/total-search-volume-history */ export const getApiSiteExplorerTotalSearchVolumeHistory = async (args: GetApiSiteExplorerTotalSearchVolumeHistoryArgs, options?: GetApiSiteExplorerTotalSearchVolumeHistoryOptions) => { const {baseUrl = "https://api.ahrefs.com/v3", fetch: fetchFn = fetch, http} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.mode != null) query.append("mode", String(args.mode)) if (args.target != null) query.append("target", String(args.target)) if (args.portfolio_id != null) query.append("portfolio_id", String(args.portfolio_id)) if (args.protocol != null) query.append("protocol", String(args.protocol)) if (args.country != null) query.append("country", String(args.country)) if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.history_grouping != null) query.append("history_grouping", String(args.history_grouping)) if (args.top_positions != null) query.append("top_positions", String(args.top_positions)) if (args.volume_mode != null) query.append("volume_mode", String(args.volume_mode)) if (args.output != null) query.append("output", String(args.output)) if (http !== undefined) headers.set('authorization', `Bearer ${http}`) const url = `${baseUrl.replace(/\/+$/, '')}/site-explorer/total-search-volume-history${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(siteExplorerTotalSearchVolumeHistory, await res.json()) };