import {z} from 'zod' export type GetApiProviderAuthorizationsArgs = {'X-HTTP-Method-Override'?: string | undefined, fields?: string | undefined}; export type GetApiProviderAuthorizationsOptions = {baseUrl?: string | undefined, userJwtRequired?: string | undefined, fetch?: typeof fetch | undefined}; export const providerAuthorization = z.object({created_at: z.string().optional(), id: z.number().int(), provider_name: z.enum(["apple", "facebook", "flickr", "google_oauth2", "open_id", "orcid", "soundcloud", "twitter"]).optional(), provider_uid: z.string().nullable().optional(), scope: z.string().nullable().optional(), updated_at: z.string().optional(), user_id: z.number().int().optional()}); export const resultsProviderAuthorizations = z.object({total_results: z.number().int(), page: z.number().int(), per_page: z.number().int(), results: z.array(providerAuthorization)}); 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 }; /** * Third-party accounts the authenticated user has authorized iNat to access on their behalf * * GET /provider_authorizations */ export const getApiProviderAuthorizations = async (args: GetApiProviderAuthorizationsArgs, options?: GetApiProviderAuthorizationsOptions) => { const {baseUrl = "https://api.inaturalist.org/v2", fetch: fetchFn = fetch, userJwtRequired} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-HTTP-Method-Override'] != null) headers.set("X-HTTP-Method-Override", String(args['X-HTTP-Method-Override'])) if (args.fields != null) query.append("fields", String(args.fields)) if (userJwtRequired !== undefined) headers.set("Authorization", userJwtRequired) const url = `${baseUrl.replace(/\/+$/, '')}/provider_authorizations${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(resultsProviderAuthorizations, await res.json()) };