import {z} from 'zod' export type Pageable = {offset?: number | undefined, limit?: number | undefined}; export type GetApiV1SpeciesUsageKeyIdentifierArgs = {usageKey: number, arg1: Pageable, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1SpeciesUsageKeyIdentifierOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const identifier = z.object({identifier: z.string().optional(), title: z.string().optional(), type: z.enum(["URL", "LSID", "HANDLER", "DOI", "UUID", "FTP", "URI", "UNKNOWN", "GBIF_PORTAL", "GBIF_NODE", "GBIF_PARTICIPANT", "GRSCICOLL_ID", "GRSCICOLL_URI", "IH_IRN", "ROR", "GRID", "CITES", "SYMBIOTA_UUID", "WIKIDATA", "NCBI_BIOCOLLECTION", "ISIL", "CLB_DATASET_KEY"]).optional(), identifierLink: z.string().optional()}); export const pagingResponseIdentifier = z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), endOfRecords: z.boolean().optional(), count: z.number().int().optional(), results: z.array(identifier).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 identifiers by id * * GET /v1/species/{usageKey}/identifier */ export const getApiV1SpeciesUsageKeyIdentifier = async (args: GetApiV1SpeciesUsageKeyIdentifierArgs, options?: GetApiV1SpeciesUsageKeyIdentifierOptions) => { 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))}/identifier${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(pagingResponseIdentifier, await res.json()) };