import {z} from 'zod' export type Pageable = {offset?: number | undefined, limit?: number | undefined}; export type GetApiV1SpeciesUsageKeyDistributionsArgs = {usageKey: number, arg1: Pageable, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1SpeciesUsageKeyDistributionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const distribution = z.object({taxonKey: z.number().int().optional(), locationId: z.string().optional(), locality: z.string().optional(), country: z.enum(["AF", "AX", "AL", "DZ", "AS", "AD", "AO", "AI", "AQ", "AG", "AR", "AM", "AW", "AU", "AT", "AZ", "BS", "BH", "BD", "BB", "BY", "BE", "BZ", "BJ", "BM", "BT", "BO", "BQ", "BA", "BW", "BV", "BR", "IO", "BN", "BG", "BF", "BI", "KH", "CM", "CA", "CV", "KY", "CF", "TD", "CL", "CN", "CX", "CC", "CO", "KM", "CD", "CG", "CK", "CR", "CI", "HR", "CU", "CW", "CY", "CZ", "DK", "DJ", "DM", "DO", "EC", "EG", "SV", "GQ", "ER", "EE", "ET", "FK", "FO", "FJ", "FI", "FR", "GF", "PF", "TF", "GA", "GM", "GE", "DE", "GH", "GI", "GR", "GL", "GD", "GP", "GU", "GT", "GG", "GN", "GW", "GY", "HT", "HM", "VA", "HN", "HK", "HU", "IS", "IN", "ID", "IR", "IQ", "IE", "IM", "IL", "IT", "JM", "JP", "JE", "JO", "KZ", "KE", "KI", "KP", "KR", "KW", "KG", "LA", "LV", "LB", "LS", "LR", "LY", "LI", "LT", "LU", "MO", "MK", "MG", "MW", "MY", "MV", "ML", "MT", "MH", "MQ", "MR", "MU", "YT", "MX", "FM", "MD", "MC", "MN", "ME", "MS", "MA", "MZ", "MM", "NA", "NR", "NP", "NL", "NC", "NZ", "NI", "NE", "NG", "NU", "NF", "MP", "NO", "OM", "PK", "PW", "PS", "PA", "PG", "PY", "PE", "PH", "PN", "PL", "PT", "PR", "QA", "RE", "RO", "RU", "RW", "BL", "SH", "KN", "LC", "MF", "PM", "VC", "WS", "SM", "ST", "SA", "SN", "RS", "SC", "SL", "SG", "SX", "SK", "SI", "SB", "SO", "ZA", "GS", "SS", "ES", "LK", "SD", "SR", "SJ", "SZ", "SE", "CH", "SY", "TW", "TJ", "TZ", "TH", "TL", "TG", "TK", "TO", "TT", "TN", "TR", "TM", "TC", "TV", "UG", "UA", "AE", "GB", "US", "UM", "UY", "UZ", "VU", "VE", "VN", "VG", "VI", "WF", "EH", "YE", "ZM", "ZW", "AA", "XK", "XZ", "ZZ"]).optional(), status: z.enum(["PRESENT", "COMMON", "RARE", "IRREGULAR", "DOUBTFUL", "EXCLUDED", "ABSENT"]).optional(), lifeStage: z.enum(["ZYGOTE", "EMRYO", "LARVA", "JUVENILE", "ADULT", "SPOROPHYTE", "SPORE", "GAMETOPHYTE", "GAMETE", "PUPA"]).optional(), temporal: z.string().optional(), threatStatus: z.enum(["EXTINCT", "EXTINCT_IN_THE_WILD", "REGIONALLY_EXTINCT", "CRITICALLY_ENDANGERED", "ENDANGERED", "VULNERABLE", "NEAR_THREATENED", "LEAST_CONCERN", "DATA_DEFICIENT", "NOT_APPLICABLE", "NOT_EVALUATED"]).optional(), establishmentMeans: z.enum(["NATIVE", "INTRODUCED", "NATURALISED", "INVASIVE", "MANAGED", "UNCERTAIN"]).optional(), appendixCites: z.enum(["I", "II", "III"]).optional(), source: z.string().optional(), sourceTaxonKey: z.number().int().optional(), startDayOfYear: z.number().int().optional(), endDayOfYear: z.number().int().optional(), remarks: z.string().optional()}); export const pagingResponseDistribution = z.object({offset: z.number().int().optional(), limit: z.number().int().optional(), endOfRecords: z.boolean().optional(), count: z.number().int().optional(), results: z.array(distribution).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 distributions by id * * GET /v1/species/{usageKey}/distributions */ export const getApiV1SpeciesUsageKeyDistributions = async (args: GetApiV1SpeciesUsageKeyDistributionsArgs, options?: GetApiV1SpeciesUsageKeyDistributionsOptions) => { 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))}/distributions${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(pagingResponseDistribution, await res.json()) };