import {z} from 'zod' export type GetApiV1ChatAlertsLogsArgs = {alert_id?: (string | null) | undefined, user_id?: (string | null) | undefined, page?: number | undefined, per_page?: number | undefined, all?: boolean | undefined, start_timestamp?: (number | null) | undefined, end_timestamp?: (number | null) | undefined}; export type GetApiV1ChatAlertsLogsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const alertLog = z.object({id: z.string(), created_at: z.string(), alert_id: z.string(), user_id: z.string(), alert: z.boolean(), reasoning: z.string(), report: z.string().nullable().optional(), report_url: z.string().nullable().optional(), article_ids: z.array(z.string()), webhook: z.record(z.string(), z.unknown()).nullable().optional()}); export const paginatedResponseAlertLog = z.object({items: z.array(alertLog), count: z.number().int(), next_page: z.number().int().nullable(), previous_page: z.number().int().nullable()}); 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 all alert logs * * GET /v1/chat/alerts/logs */ export const getApiV1ChatAlertsLogs = async (args: GetApiV1ChatAlertsLogsArgs, options?: GetApiV1ChatAlertsLogsOptions) => { const {baseUrl = "https://api.asknews.app", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.alert_id != null) query.append("alert_id", String(args.alert_id)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.all != null) query.append("all", String(args.all)) if (args.start_timestamp != null) query.append("start_timestamp", String(args.start_timestamp)) if (args.end_timestamp != null) query.append("end_timestamp", String(args.end_timestamp)) if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/chat/alerts/logs${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(paginatedResponseAlertLog, await res.json()) };