import {z} from 'zod' export type GetApiPaymentDistributionNationalIdScheduleTypeArgs = {nationalId: string, scheduleType: string, totalAmount: number, monthAmount: number, monthCount: number}; export type GetApiPaymentDistributionNationalIdScheduleTypeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const errors = z.object({type: z.string().optional(), title: z.string(), detail: z.string(), param: z.string().optional(), traceId: z.string().optional()}); export const problemDetailsDT = z.object({type: z.string().optional(), title: z.string(), status: z.string(), detail: z.string(), instance: z.string().optional(), errors: z.array(errors).optional()}); export const paymentsDT = z.object({dueDate: z.string(), payment: z.number().int(), accumulated: z.number().int()}); export const paymentDistributionDT = z.object({nationalId: z.string(), scheduleType: z.string(), payments: z.array(paymentsDT)}); export const paymentDistributionnationalIdscheduleTypeGETResponse = z.object({error: problemDetailsDT.optional(), paymentDistribution: paymentDistributionDT}); 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 /paymentDistribution/{nationalId}/{scheduleType} */ export const getApiPaymentDistributionNationalIdScheduleType = async (args: GetApiPaymentDistributionNationalIdScheduleTypeArgs, options?: GetApiPaymentDistributionNationalIdScheduleTypeOptions) => { const {baseUrl = "http://wmdev03.Dev.local:5000/rad/TBRIslandis.api.v1:paymentSchedule_v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.totalAmount != null) query.append("totalAmount", String(args.totalAmount)) if (args.monthAmount != null) query.append("monthAmount", String(args.monthAmount)) if (args.monthCount != null) query.append("monthCount", String(args.monthCount)) const url = `${baseUrl.replace(/\/+$/, '')}/paymentDistribution/${encodeURIComponent(String(args.nationalId))}/${encodeURIComponent(String(args.scheduleType))}${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(paymentDistributionnationalIdscheduleTypeGETResponse, await res.json()) };