import {z} from 'zod' export type BenefitChargeStatus = 'PENDING' | 'PAID' | 'OVERDUE' | 'CANCELLED' | 'REFUNDED'; export type GetApiBenefitsBenefitIdChargesArgs = {benefitId: string, providerServiceCode?: string | undefined, status?: BenefitChargeStatus | undefined, billingPeriodFrom?: string | undefined, billingPeriodTo?: string | undefined, page?: number | undefined, pageSize?: number | undefined}; export type GetApiBenefitsBenefitIdChargesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const money = z.number(); export const benefitChargeStatus = z.enum(["PENDING", "PAID", "OVERDUE", "CANCELLED", "REFUNDED"]); export const benefitCharge = z.object({id: z.string(), employeeId: z.number().int(), subscriptionId: z.string(), providerServiceCode: z.string(), billingPeriodStart: z.string(), billingPeriodEnd: z.string(), amount: money, status: benefitChargeStatus}); export const getApiBenefitsBenefitIdChargesResponse = z.array(benefitCharge); 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 all benefit charges for a specific subscription * * GET /benefits/{benefitId}/charges */ export const getApiBenefitsBenefitIdCharges = async (args: GetApiBenefitsBenefitIdChargesArgs, options?: GetApiBenefitsBenefitIdChargesOptions) => { const {baseUrl = "http://localhost:3000", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.providerServiceCode != null) query.append("providerServiceCode", String(args.providerServiceCode)) if (args.status != null) query.append("status", String(args.status)) if (args.billingPeriodFrom != null) query.append("billingPeriodFrom", String(args.billingPeriodFrom)) if (args.billingPeriodTo != null) query.append("billingPeriodTo", String(args.billingPeriodTo)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) const url = `${baseUrl.replace(/\/+$/, '')}/benefits/${encodeURIComponent(String(args.benefitId))}/charges${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(getApiBenefitsBenefitIdChargesResponse, await res.json()) };