import {z} from 'zod' export type GetApiPolicyTenantLocatorTransactionsLocatorPreferencesArgs = {tenantLocator: string, locator: unknown}; export type GetApiPolicyTenantLocatorTransactionsLocatorPreferencesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const installmentPreferences = z.object({cadence: z.enum(["none", "fullPay", "weekly", "everyOtherWeek", "monthly", "quarterly", "semiannually", "annually", "thirtyDays", "everyNDays"]).optional(), anchorMode: z.enum(["generateDay", "termStartDay", "dueDay"]).optional(), generateLeadDays: z.number().int().optional(), dueLeadDays: z.number().int().optional(), installmentWeights: z.array(z.number()), maxInstallmentsPerTerm: z.number().int().optional(), installmentPlanName: z.string().optional(), anchorType: z.enum(["none", "dayOfMonth", "anchorTime", "dayOfWeek", "weekOfMonth"]).optional(), dayOfMonth: z.number().int().optional(), dayOfWeek: z.enum(["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]).optional(), weekOfMonth: z.enum(["none", "first", "second", "third", "fourth", "fifth"]).optional(), anchorTime: z.string().optional(), autopayLeadDays: z.number().optional()}); export const preferences = z.object({installmentPreferences: installmentPreferences.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 /policy/{tenantLocator}/transactions/{locator}/preferences */ export const getApiPolicyTenantLocatorTransactionsLocatorPreferences = async (args: GetApiPolicyTenantLocatorTransactionsLocatorPreferencesArgs, options: GetApiPolicyTenantLocatorTransactionsLocatorPreferencesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/policy/${encodeURIComponent(String(args.tenantLocator))}/transactions/${encodeURIComponent(String(args.locator))}/preferences${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(preferences, await res.json()) };