import {z} from 'zod' export type GetApiV1LoansEventsArgs = {sessionId?: string | undefined, authorizationId?: string | undefined}; export type GetApiV1LoansEventsOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const loanType = z.enum(["BLANCO", "CREDIT", "LAND", "MEMBERSHIP", "MORTGAGE", "OTHER", "STUDENT", "VEHICLE"]); export const loanEvent = z.object({accountId: z.string().nullable().optional(), balance: z.number().nullable().optional(), interestRate: z.number().nullable().optional(), interestRateChange: z.number().nullable().optional(), loanName: z.string().nullable().optional(), loanType: loanType.optional(), name: z.string().nullable().optional(), nextTermsModificationDay: z.number().int().nullable().optional(), providerId: z.string().nullable().optional(), timestamp: z.number().int().nullable().optional(), type: z.string().nullable().optional()}); export const loanEventsResponse = z.object({events: z.array(loanEvent).nullable().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 }; /** * List events that effects loans. * * GET /v1/loans/events */ export const getApiV1LoansEvents = async (args: GetApiV1LoansEventsArgs, options: GetApiV1LoansEventsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sessionId != null) headers.set("sessionId", String(args.sessionId)) if (args.authorizationId != null) headers.set("authorizationId", String(args.authorizationId)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/v1/loans/events${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(loanEventsResponse, await res.json()) };