import {z} from 'zod' export type GetApiApiV1LoanProductsIdFloatingRatesArgs = {id: string, productVersionId: string, asOf: string}; export type GetApiApiV1LoanProductsIdFloatingRatesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const floatingRatePeriodHumaBody = z.object({annualRateBps: z.number().int(), effectiveDate: z.string(), endDate: z.string().optional(), id: z.string()}); export const floatingRateTableInnerHumaBody = z.object({id: z.string(), name: z.string(), periods: z.array(floatingRatePeriodHumaBody).nullable()}); export const floatingRateTermsHumaBody = z.object({fixedAnnualRateBps: z.number().int(), floatingRateTableId: z.string().optional(), floatingSpreadBps: z.number().int(), rateMode: z.string(), requiresFloatingRate: z.boolean(), versionId: z.string()}); export const floatingRateTableHumaBody = z.object({period: floatingRatePeriodHumaBody.optional(), rateMode: z.string(), table: floatingRateTableInnerHumaBody.optional(), terms: floatingRateTermsHumaBody.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 loan product floating rate table * * GET /api/v1/loan-products/{id}/floating-rates */ export const getApiApiV1LoanProductsIdFloatingRates = async (args: GetApiApiV1LoanProductsIdFloatingRatesArgs, options?: GetApiApiV1LoanProductsIdFloatingRatesOptions) => { const {baseUrl = "https://identity.sandbox.lerian.net", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.productVersionId != null) query.append("productVersionId", String(args.productVersionId)) if (args.asOf != null) query.append("asOf", String(args.asOf)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/loan-products/${encodeURIComponent(String(args.id))}/floating-rates${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(floatingRateTableHumaBody, await res.json()) };