import {z} from 'zod' export type GetApiGatewayV1ProjectAiNewsArgs = {id?: string | undefined, q?: string | undefined, limit?: number | undefined, offset?: number | undefined, lang?: ('en' | 'zh' | 'ja' | 'kr') | undefined}; export type GetApiGatewayV1ProjectAiNewsOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const projectPulseItem = z.object({id: z.string(), signal_type: z.string(), slug: z.string().optional(), sources: z.array(z.string()).nullable().optional(), subtitle: z.string().optional(), timestamp: z.number().int(), title: z.string(), tldr: z.array(z.string()).nullable().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 dataResponseProjectPulseItem = z.object({$schema: z.string().optional(), data: z.array(projectPulseItem).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 }; /** * Project AI News * * GET /gateway/v1/project/ai-news */ export const getApiGatewayV1ProjectAiNews = async (args: GetApiGatewayV1ProjectAiNewsArgs, options?: GetApiGatewayV1ProjectAiNewsOptions) => { 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.id != null) query.append("id", String(args.id)) if (args.q != null) query.append("q", String(args.q)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.lang != null) query.append("lang", String(args.lang)) if (AccessToken !== undefined) headers.set('authorization', `Bearer ${AccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/v1/project/ai-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(dataResponseProjectPulseItem, await res.json()) };