import {z} from 'zod' export type GetApiGatewayV1SearchFundraisingArgs = {q?: string | undefined, from?: string | undefined, to?: string | undefined, source?: ('social' | 'news') | undefined, lang?: ('en' | 'zh' | 'ja' | 'kr' | 'ko') | undefined, min_importance?: number | undefined, sort_by?: ('recency' | 'relevance' | 'importance') | undefined, order?: ('asc' | 'desc') | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiGatewayV1SearchFundraisingOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const fundraisingSearchSource = z.object({name: z.string().optional(), type: z.enum(["social", "news"]), url: z.string().optional()}); export const fundraisingSearchItem = z.object({event_at: z.number().int(), id: z.string(), importance: z.number().int().gte(1).lte(5).optional(), published_at: z.number().int(), source: fundraisingSearchSource, source_count: z.number().int(), summary: z.string(), title: z.string()}); 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 dataResponseFundraisingSearchItem = z.object({$schema: z.string().optional(), data: z.array(fundraisingSearchItem).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 }; /** * Fundraising Event Search * * GET /gateway/v1/search/fundraising */ export const getApiGatewayV1SearchFundraising = async (args: GetApiGatewayV1SearchFundraisingArgs, options?: GetApiGatewayV1SearchFundraisingOptions) => { 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.source != null) query.append("source", String(args.source)) if (args.lang != null) query.append("lang", String(args.lang)) if (args.min_importance != null) query.append("min_importance", String(args.min_importance)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.order != null) query.append("order", String(args.order)) 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/fundraising${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(dataResponseFundraisingSearchItem, await res.json()) };