import {z} from 'zod' export type GetApiDomainNamePpcSpendTotalArgs = {api_key?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, country?: string | undefined, granularity?: string | undefined, main_domain_only?: string | undefined, domain_name: string}; export type GetApiDomainNamePpcSpendTotalOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiDomainNamePpcSpendTotalResponse = z.object({meta: z.object({request: z.object({granularity: z.string().optional(), main_domain_only: z.boolean().optional(), format: z.string().optional(), domain: z.string().optional(), start_date: z.string().optional(), end_date: z.string().optional(), country: z.string().optional()}).optional(), status: z.string().optional(), last_updated: z.string().optional(), device: z.string().optional()}).optional(), breakdown: z.array(z.object({date: z.string().optional(), currencies: z.object({aud: z.number().optional(), eur: z.number().optional(), gbp: z.number().optional(), jpy: z.number().optional(), usd: z.number().optional()}).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 }; /** * PPC Spend - All Traffic * * GET /{domain_name}/ppc-spend/total */ export const getApiDomainNamePpcSpendTotal = async (args: GetApiDomainNamePpcSpendTotalArgs, options?: GetApiDomainNamePpcSpendTotalOptions) => { const {baseUrl = "https://api.similarweb.com/v1/website", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.api_key != null) query.append("api_key", String(args.api_key)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.country != null) query.append("country", String(args.country)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.main_domain_only != null) query.append("main_domain_only", String(args.main_domain_only)) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.domain_name))}/ppc-spend/total${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(getApiDomainNamePpcSpendTotalResponse, await res.json()) };