import {z} from 'zod' export type GetApiInstitutionsArgs = {before?: string | undefined, after?: string | undefined, serviceProviders?: string | undefined, limit?: string | undefined, name?: string | undefined, type?: string | undefined, regions?: string | undefined}; export type GetApiInstitutionsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const images = z.object({icon: z.string().optional(), tile: z.string().optional(), wide: z.string().optional()}); export const provider = z.object({categories: z.array(z.string()).optional(), id: z.string().optional(), oauth: z.boolean().optional(), products: z.array(z.string()).optional(), regions: z.array(z.string()).optional()}); export const institution = z.object({aliases: z.array(z.string()).optional(), domain: z.string().optional(), id: z.string().optional(), images: images.optional(), isTest: z.boolean().optional(), key: z.string().optional(), name: z.string().optional(), providers: z.record(z.string(), provider).optional(), rank: z.number().int().optional(), subName: z.string().optional(), type: z.string().optional()}); export const institutionPage = z.object({count: z.number().int().optional(), institutions: z.array(institution).optional(), remaining: z.number().int().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 }; /** * Search institutions * * GET /institutions */ export const getApiInstitutions = async (args: GetApiInstitutionsArgs, options?: GetApiInstitutionsOptions) => { const {baseUrl = "https://api-sb.meld.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.serviceProviders != null) query.append("serviceProviders", String(args.serviceProviders)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.name != null) query.append("name", String(args.name)) if (args.type != null) query.append("type", String(args.type)) if (args.regions != null) query.append("regions", String(args.regions)) if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/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(institutionPage, await res.json()) };