import {z} from 'zod' export type GetApiAnnouncementsArgs = {placement?: ('users/dashboard#sidebar' | 'users/dashboard' | 'welcome/index' | 'mobile/home' | 'mobile') | undefined, client?: ('inat-ios' | 'inat-android' | 'seek' | 'inatrn') | undefined, locale?: string | undefined, fields: unknown, 'X-HTTP-Method-Override'?: string | undefined}; export type GetApiAnnouncementsOptions = {baseUrl?: string | undefined, userJwtOptional?: string | undefined, fetch?: typeof fetch | undefined}; export const announcement = z.object({id: z.number().int(), body: z.string().optional(), placement: z.string().optional(), clients: z.array(z.string()).optional(), dismissible: z.boolean().optional(), locales: z.array(z.string()).optional(), start: z.string().optional(), end: z.string().optional()}); export const resultsAnnouncements = z.object({total_results: z.number().int(), page: z.number().int(), per_page: z.number().int(), results: z.array(announcement)}); 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 }; /** * Search announcements * * GET /announcements */ export const getApiAnnouncements = async (args: GetApiAnnouncementsArgs, options?: GetApiAnnouncementsOptions) => { const {baseUrl = "https://api.inaturalist.org/v2", fetch: fetchFn = fetch, userJwtOptional} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.placement != null) query.append("placement", String(args.placement)) if (args.client != null) query.append("client", String(args.client)) if (args.locale != null) query.append("locale", String(args.locale)) if (args.fields != null) query.append("fields", String(args.fields)) if (args['X-HTTP-Method-Override'] != null) headers.set("X-HTTP-Method-Override", String(args['X-HTTP-Method-Override'])) if (userJwtOptional !== undefined) headers.set("Authorization", userJwtOptional) const url = `${baseUrl.replace(/\/+$/, '')}/announcements${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(resultsAnnouncements, await res.json()) };