import {z} from 'zod' export type GetApiV1GoogleScholarProfilesArgs = {mauthors: string, hl?: string | undefined, after_author?: (string | null) | undefined, before_author?: (string | null) | undefined}; export type GetApiV1GoogleScholarProfilesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const scholarProfile = z.object({position: z.number().int().nullable().optional(), author_id: z.string().nullable().optional(), name: z.string(), link: z.string().nullable().optional(), affiliation: z.string().nullable().optional(), email_domain: z.string().nullable().optional(), cited_by: z.number().int().nullable().optional(), interests: z.array(z.string()).optional(), thumbnail: z.string().nullable().optional()}); export const scholarProfilesResponse = z.object({profiles: z.array(scholarProfile).optional(), query: z.string(), language: z.string().optional(), before_author: z.string().nullable().optional(), after_author: z.string().nullable().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 }; /** * Search Google Scholar author profiles * * GET /v1/google/scholar/profiles */ export const getApiV1GoogleScholarProfiles = async (args: GetApiV1GoogleScholarProfilesArgs, options?: GetApiV1GoogleScholarProfilesOptions) => { const {baseUrl = "https://scrapebadger.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.mauthors != null) query.append("mauthors", String(args.mauthors)) if (args.hl != null) query.append("hl", String(args.hl)) if (args.after_author != null) query.append("after_author", String(args.after_author)) if (args.before_author != null) query.append("before_author", String(args.before_author)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/google/scholar/profiles${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(scholarProfilesResponse, await res.json()) };