import {z} from 'zod' export type Pagination = {limit?: number | undefined, offset?: number | undefined}; export type NumericMetricFilter = {field: string, operator: '>' | '>=' | '<' | '<=' | '=' | '==' | '!=', value: number | number}; export type PathFilter = {field: 'path', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type ReferralSourceFilter = {field: 'referral_source', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: 'openai' | 'none' | 'anthropic' | 'deepseek' | 'perplexity' | 'you' | 'grok' | 'microsoft' | 'gemini' | 'internal' | 'other' | Array<'openai' | 'none' | 'anthropic' | 'deepseek' | 'perplexity' | 'you' | 'grok' | 'microsoft' | 'gemini' | 'internal' | 'other'>}; export type ReferralsQuery = {date_interval?: ('hour' | 'day' | 'week' | 'month' | 'year' | 'relative_week') | undefined, dimensions?: (Array<'date' | 'path' | 'referral_source'>) | undefined, metrics: Array<'visits' | 'last_visit'>, order_by?: ({[key: string]: 'asc' | 'desc'}) | undefined, pagination?: Pagination | undefined, domain: string, start_date: string, end_date?: string | undefined, organization_id?: (string | null) | undefined, metric_filters?: Array | undefined, filters?: (Array) | undefined}; export type CreateApiV1ReportsReferralsArgs = {body: ReferralsQuery}; export type CreateApiV1ReportsReferralsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const info = z.object({total_rows: z.number().int(), query: z.record(z.string(), z.unknown()).nullable().optional()}); export const result = z.object({metrics: z.array(z.union([z.number().int(), z.number()])), dimensions: z.array(z.union([z.string(), z.string()]))}); export const response = z.object({info: info, data: z.array(result)}); 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 }; /** * Get Referrals Report V1 * * POST /v1/reports/referrals * * Also accepts auth: BearerAuth */ export const createApiV1ReportsReferrals = async (args: CreateApiV1ReportsReferralsArgs, options?: CreateApiV1ReportsReferralsOptions) => { const {baseUrl = "https://api.tryprofound.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reports/referrals${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(response, await res.json()) };