import {z} from 'zod' export type Pagination = {limit?: number | undefined, offset?: number | undefined}; export type MethodFilter = {field: 'method', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type HostFilter = {field: 'host', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; 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 StatusCodeFilter = {field: 'status_code', operator: 'is' | 'not_is' | 'in' | 'not_in', value: number | Array}; export type IpFilter = {field: 'ip', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type UserAgentFilter = {field: 'user_agent', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type RefererFilter = {field: 'referer', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type ProviderFilter = {field: 'provider', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type QueryParamsFilter = {field: 'query_params', operator: 'is' | 'not_is' | 'in' | 'not_in' | 'contains' | 'not_contains' | 'matches' | 'contains_case_insensitive' | 'not_contains_case_insensitive', value: string | Array}; export type BytesSentFilter = {field: 'bytes_sent', operator: 'is' | 'not_is' | 'in' | 'not_in', value: number | Array}; export type DurationMsFilter = {field: 'duration_ms', operator: 'is' | 'not_is' | 'in' | 'not_in', value: number | Array}; export type LogsQuery = {date_interval?: ('hour' | 'day' | 'week' | 'month' | 'year' | 'relative_week') | undefined, dimensions?: (Array<'timestamp' | 'method' | 'host' | 'path' | 'status_code' | 'ip' | 'user_agent' | 'referer' | 'bytes_sent' | 'duration_ms' | 'query_params'>) | undefined, metrics: Array<'count'>, order_by?: ({[key: string]: 'asc' | 'desc'}) | undefined, pagination?: Pagination | undefined, domain: string, start_date: string, end_date?: string | undefined, organization_id?: (string | null) | undefined, filters?: (Array) | undefined}; export type CreateApiV1LogsRawArgs = {body: LogsQuery}; export type CreateApiV1LogsRawOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const logVisit = z.object({timestamp: z.string(), org_id: z.string(), method: z.string(), host: z.string(), path: z.string(), status_code: z.number().int(), ip: z.string(), user_agent: z.string(), referer: z.string().optional(), bytes_sent: z.number().int().optional(), duration_ms: z.number().int().optional(), query_params: z.record(z.string(), z.string()).optional()}); 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 createApiV1LogsRawResponse = z.union([z.array(logVisit), response]); 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 Logs * * POST /v1/logs/raw * * Also accepts auth: BearerAuth */ export const createApiV1LogsRaw = async (args: CreateApiV1LogsRawArgs, options?: CreateApiV1LogsRawOptions) => { 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/logs/raw${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(createApiV1LogsRawResponse, await res.json()) };