import {z} from 'zod' export type GetApiApiV3InstitutionsArgs = {ids?: Array | undefined, limit?: (number | null) | undefined, institution_type?: (string | null) | undefined, account_types?: (Array | null) | undefined, states?: (Array | null) | undefined, counties?: (Array | null) | undefined, cities?: (Array | null) | undefined, population_min?: (number | null) | undefined, population_max?: (number | null) | undefined, search_query?: (string | null) | undefined}; export type GetApiApiV3InstitutionsOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV3InstitutionsResponse = z.unknown(); 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 }; /** * Get Institutions * * GET /api/v3/institutions */ export const getApiApiV3Institutions = async (args: GetApiApiV3InstitutionsArgs, options: GetApiApiV3InstitutionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.ids ?? []) query.append("ids", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.institution_type != null) query.append("institution_type", String(args.institution_type)) for (const item of args.account_types ?? []) query.append("account_types", String(item)) for (const item of args.states ?? []) query.append("states", String(item)) for (const item of args.counties ?? []) query.append("counties", String(item)) for (const item of args.cities ?? []) query.append("cities", String(item)) if (args.population_min != null) query.append("population_min", String(args.population_min)) if (args.population_max != null) query.append("population_max", String(args.population_max)) if (args.search_query != null) query.append("search_query", String(args.search_query)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v3/institutions${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(getApiApiV3InstitutionsResponse, await res.json()) };