import {z} from 'zod' export type Pageable = {offset?: number | undefined, limit?: number | undefined}; export type GetApiV1SpeciesUsageKeyDescriptionsArgs = {usageKey: number, arg1: Pageable, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1SpeciesUsageKeyDescriptionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const description = z.object({key: z.number().int().optional(), taxonKey: z.number().int().optional(), type: z.string().optional(), language: z.enum(["abk", "aar", "afr", "aka", "sqi", "amh", "ara", "arg", "hye", "asm", "ava", "ave", "aym", "aze", "bam", "bak", "eus", "bel", "ben", "bih", "bis", "bos", "bre", "bul", "mya", "cat", "cha", "che", "zho", "chu", "chv", "cor", "cos", "cre", "hrv", "ces", "dan", "div", "nld", "dzo", "eng", "epo", "est", "ewe", "fao", "fij", "fin", "fra", "fry", "ful", "glg", "lug", "kat", "deu", "ell", "kal", "grn", "guj", "hat", "hau", "heb", "her", "hin", "hmo", "hun", "isl", "ido", "ibo", "ind", "ina", "ile", "iku", "ipk", "gle", "ita", "jpn", "jav", "kan", "kau", "kas", "kaz", "khm", "kik", "kin", "kir", "kom", "kon", "kor", "kur", "kua", "lao", "lat", "lav", "lim", "lin", "lit", "lub", "ltz", "mkd", "mlg", "msa", "mal", "mlt", "glv", "mri", "mar", "mah", "mol", "mon", "nau", "nav", "ndo", "nep", "nde", "sme", "nob", "nno", "nor", "nya", "oci", "oji", "ori", "orm", "oss", "pli", "pan", "fas", "pol", "por", "pus", "que", "roh", "ron", "run", "rus", "smo", "sag", "san", "srd", "gla", "srp", "sna", "iii", "snd", "sin", "slk", "slv", "som", "nbl", "sot", "spa", "sun", "swa", "ssw", "swe", "tgl", "tah", "tgk", "tam", "tat", "tel", "tha", "bod", "tir", "ton", "tso", "tsn", "tur", "tuk", "twi", "uig", "ukr", "urd", "uzb", "ven", "vie", "vol", "wln", "cym", "wol", "xho", "yid", "yor", "zha", "zul", ""]).optional(), description: z.string().optional(), source: z.string().optional(), sourceTaxonKey: z.number().int().optional(), creator: z.string().optional(), contributor: z.string().optional(), license: z.string().optional()}); export const pagingResponseDescription = z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), endOfRecords: z.boolean().optional(), count: z.number().int().optional(), results: z.array(description).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 }; /** * Name usage descriptions by id * * GET /v1/species/{usageKey}/descriptions */ export const getApiV1SpeciesUsageKeyDescriptions = async (args: GetApiV1SpeciesUsageKeyDescriptionsArgs, options?: GetApiV1SpeciesUsageKeyDescriptionsOptions) => { const {baseUrl = "https://api.gbif.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.arg1 != null) query.append("arg1", JSON.stringify(args.arg1)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/species/${encodeURIComponent(String(args.usageKey))}/descriptions${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(pagingResponseDescription, await res.json()) };