import {z} from 'zod' export type GetApiV1LoansArgs = {sessionId?: string | undefined, authorizationId?: string | undefined}; export type GetApiV1LoansOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const categoryId = z.object({expense: z.string().nullable().optional(), income: z.string().nullable().optional()}); export const loanDetails = z.object({averageMonthlyOutgoingLoanPayment: z.number().nullable().optional(), categoryId: categoryId.optional(), categoryTitle: z.string().nullable().optional(), daysSinceLastIncomingLoan: z.number().int().nullable().optional(), monthlyIncomingLoanTrend: z.number().nullable().optional(), monthlyOutgoingLoanTrend: z.number().nullable().optional(), numberOfIncomingLoanSources: z.number().int().nullable().optional(), numberOfOutgoingLoanSources: z.number().int().nullable().optional(), ratioOfOutgoingLoanCategoryToLoans: z.number().nullable().optional()}); export const loan = z.object({averageMonthlyOutgoingLoanPayment: z.number().nullable().optional(), calendarMonths: z.number().int().nullable().optional(), calendarMonthsWithIncomingLoans: z.number().int().nullable().optional(), calendarMonthsWithOutgoingLoans: z.number().int().nullable().optional(), daysSinceLastIncomingLoan: z.number().int().nullable().optional(), loansByCategory: z.array(loanDetails).nullable().optional(), monthlyIncomingLoanTrend: z.number().nullable().optional(), monthlyOutgoingLoanTrend: z.number().nullable().optional(), ratioOfIncomingLoansToAllIncome: z.number().nullable().optional(), ratioOfOutgoingLoansToAllExpenses: z.number().nullable().optional()}); export const loansResponse = z.object({loans: z.array(loan).nullable().optional(), overallAmount: z.number().nullable().optional(), weightedAverageInterestRate: z.number().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 }; /** * Get loans. * * GET /v1/loans */ export const getApiV1Loans = async (args: GetApiV1LoansArgs, options: GetApiV1LoansOptions) => { 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${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(loansResponse, await res.json()) };