import {z} from 'zod' export type GetApiPeoplePersonIdArgs = {person_id: string}; export type GetApiPeoplePersonIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const linkWithFollowers = z.object({url: z.string().optional(), followers: z.number().int().optional()}); export const personLinks = z.object({github: z.array(linkWithFollowers).optional(), linkedin: z.array(linkWithFollowers).optional(), medium: z.array(linkWithFollowers).optional(), twitter: z.array(linkWithFollowers).optional(), additional: z.array(linkWithFollowers).optional()}); export const coinAndPosition = z.object({coin_id: z.string().optional(), coin_name: z.string().optional(), position: z.string().optional()}); export const personById = z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), teams_count: z.number().int().optional(), links: personLinks.optional(), positions: z.array(coinAndPosition).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 }; /** * Get person by ID * * GET /people/{person_id} */ export const getApiPeoplePersonId = async (args: GetApiPeoplePersonIdArgs, options?: GetApiPeoplePersonIdOptions) => { const {baseUrl = "https://api.coinpaprika.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/people/${encodeURIComponent(String(args.person_id))}${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(personById, await res.json()) };