import {z} from 'zod' export type GetApiListArgs = {url: string, fields?: ('links' | 'extlinks' | 'meta' | 'querystring' | 'breadcrumb') | undefined, timeout?: number | undefined, callback?: string | undefined, proxy?: string | undefined, proxyAuth?: string | undefined, useProxy?: string | undefined, renderDelay?: number | undefined, scroll?: ('slow' | 'fast') | undefined}; export type GetApiListOptions = {baseUrl?: string | undefined, tokenscheme?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiListResponse = z.object({request: z.object({pageUrl: z.string().optional(), api: z.string().optional(), version: z.number().int().optional()}).optional(), objects: z.array(z.object({diffbotUri: z.string().optional(), icon: z.string().optional(), pageUrl: z.string().optional(), type: z.string().optional(), title: z.string().optional(), items: z.array(z.object({date: z.string().optional(), summary: z.string().optional(), link: z.string().optional(), title: z.string().optional()})).optional()})).optional()}); 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 }; /** * List * * GET /list */ export const getApiList = async (args: GetApiListArgs, options?: GetApiListOptions) => { const {baseUrl = "https://llm.diffbot.com/api/v1/web_search", fetch: fetchFn = fetch, tokenscheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.url != null) query.append("url", String(args.url)) if (args.fields != null) query.append("fields", String(args.fields)) if (args.timeout != null) query.append("timeout", String(args.timeout)) if (args.callback != null) query.append("callback", String(args.callback)) if (args.proxy != null) query.append("proxy", String(args.proxy)) if (args.proxyAuth != null) query.append("proxyAuth", String(args.proxyAuth)) if (args.useProxy != null) query.append("useProxy", String(args.useProxy)) if (args.renderDelay != null) query.append("renderDelay", String(args.renderDelay)) if (args.scroll != null) query.append("scroll", String(args.scroll)) if (tokenscheme !== undefined) query.set("token", tokenscheme) const url = `${baseUrl.replace(/\/+$/, '')}/list${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(getApiListResponse, await res.json()) };