import {z} from 'zod' export type GetApiV1SpeciesMatchArgs = {usageKey?: string | undefined, taxonID?: string | undefined, taxonConceptID?: string | undefined, scientificNameID?: string | undefined, name?: string | undefined, scientificName?: string | undefined, authorship?: string | undefined, scientificNameAuthorship?: string | undefined, rank?: ('DOMAIN' | 'SUPERKINGDOM' | 'KINGDOM' | 'SUBKINGDOM' | 'INFRAKINGDOM' | 'SUPERPHYLUM' | 'PHYLUM' | 'SUBPHYLUM' | 'INFRAPHYLUM' | 'SUPERCLASS' | 'CLASS' | 'SUBCLASS' | 'INFRACLASS' | 'PARVCLASS' | 'SUPERLEGION' | 'LEGION' | 'SUBLEGION' | 'INFRALEGION' | 'SUPERCOHORT' | 'COHORT' | 'SUBCOHORT' | 'INFRACOHORT' | 'MAGNORDER' | 'SUPERORDER' | 'GRANDORDER' | 'ORDER' | 'SUBORDER' | 'INFRAORDER' | 'PARVORDER' | 'SUPERFAMILY' | 'FAMILY' | 'SUBFAMILY' | 'INFRAFAMILY' | 'SUPERTRIBE' | 'TRIBE' | 'SUBTRIBE' | 'INFRATRIBE' | 'SUPRAGENERIC_NAME' | 'GENUS' | 'SUBGENUS' | 'INFRAGENUS' | 'SECTION' | 'SUBSECTION' | 'SERIES' | 'SUBSERIES' | 'INFRAGENERIC_NAME' | 'SPECIES_AGGREGATE' | 'SPECIES' | 'INFRASPECIFIC_NAME' | 'GREX' | 'SUBSPECIES' | 'CULTIVAR_GROUP' | 'CONVARIETY' | 'INFRASUBSPECIFIC_NAME' | 'PROLES' | 'RACE' | 'NATIO' | 'ABERRATION' | 'MORPH' | 'VARIETY' | 'SUBVARIETY' | 'FORM' | 'SUBFORM' | 'PATHOVAR' | 'BIOVAR' | 'CHEMOVAR' | 'MORPHOVAR' | 'PHAGOVAR' | 'SEROVAR' | 'CHEMOFORM' | 'FORMA_SPECIALIS' | 'CULTIVAR' | 'STRAIN' | 'OTHER' | 'UNRANKED') | undefined, taxonRank?: string | undefined, genericName?: string | undefined, specificEpithet?: string | undefined, infraspecificEpithet?: string | undefined, exclude?: Array | undefined, strict?: boolean | undefined, verbose?: boolean | undefined, kingdom?: string | undefined, phylum?: string | undefined, order?: string | undefined, class?: string | undefined, family?: string | undefined, genus?: string | undefined}; export type GetApiV1SpeciesMatchOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1SpeciesMatchResponse = z.object({}); 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 }; /** * Legacy fuzzy name match service (Version 1 - flat format) * * GET /v1/species/match */ export const getApiV1SpeciesMatch = async (args: GetApiV1SpeciesMatchArgs, options?: GetApiV1SpeciesMatchOptions) => { 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.usageKey != null) query.append("usageKey", String(args.usageKey)) if (args.taxonID != null) query.append("taxonID", String(args.taxonID)) if (args.taxonConceptID != null) query.append("taxonConceptID", String(args.taxonConceptID)) if (args.scientificNameID != null) query.append("scientificNameID", String(args.scientificNameID)) if (args.name != null) query.append("name", String(args.name)) if (args.scientificName != null) query.append("scientificName", String(args.scientificName)) if (args.authorship != null) query.append("authorship", String(args.authorship)) if (args.scientificNameAuthorship != null) query.append("scientificNameAuthorship", String(args.scientificNameAuthorship)) if (args.rank != null) query.append("rank", String(args.rank)) if (args.taxonRank != null) query.append("taxonRank", String(args.taxonRank)) if (args.genericName != null) query.append("genericName", String(args.genericName)) if (args.specificEpithet != null) query.append("specificEpithet", String(args.specificEpithet)) if (args.infraspecificEpithet != null) query.append("infraspecificEpithet", String(args.infraspecificEpithet)) for (const item of args.exclude ?? []) query.append("exclude", String(item)) if (args.strict != null) query.append("strict", String(args.strict)) if (args.verbose != null) query.append("verbose", String(args.verbose)) if (args.kingdom != null) query.append("kingdom", String(args.kingdom)) if (args.phylum != null) query.append("phylum", String(args.phylum)) if (args.order != null) query.append("order", String(args.order)) if (args.class != null) query.append("class", String(args.class)) if (args.family != null) query.append("family", String(args.family)) if (args.genus != null) query.append("genus", String(args.genus)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/species/match${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(getApiV1SpeciesMatchResponse, await res.json()) };