import {z} from 'zod' export type GetApiGatewayV1SearchNewsArgs = {q: string, from?: string | undefined, to?: string | undefined, sort_by?: ('relevance' | 'recency') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiGatewayV1SearchNewsOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const newsArticleItem = z.object({highlights: z.record(z.string(), z.array(z.string()).nullable()).optional(), id: z.string(), project_id: z.string().optional(), project_name: z.string().optional(), published_at: z.number().int(), source: z.string().optional(), summary: z.string().optional(), title: z.string(), url: z.string().optional()}); export const offsetMeta = z.object({cached: z.boolean(), credits_used: z.number().int(), empty_reason: z.string().optional(), has_more: z.boolean().optional(), limit: z.number().int(), offset: z.number().int(), total: z.number().int().optional(), watermark: z.number().int().optional()}); export const dataResponseNewsArticleItem = z.object({$schema: z.string().optional(), data: z.array(newsArticleItem).nullable(), meta: offsetMeta}); 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 }; /** * News Article Search * * GET /gateway/v1/search/news */ export const getApiGatewayV1SearchNews = async (args: GetApiGatewayV1SearchNewsArgs, options?: GetApiGatewayV1SearchNewsOptions) => { const {baseUrl = "https://api.asksurf.ai", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.q != null) query.append("q", String(args.q)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (AccessToken !== undefined) headers.set('authorization', `Bearer ${AccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/v1/search/news${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(dataResponseNewsArticleItem, await res.json()) };