import {z} from 'zod' export type GetApiBillpayOffersOfferIdBillArgs = {offerId: string, billIdentifier?: string | undefined, accountNumber?: string | undefined}; export type GetApiBillpayOffersOfferIdBillOptions = {baseUrl: string, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoPriceType = z.enum(["FIXED", "RANGE"]); export const dtoBillPayBillResponse = z.object({cost: z.number().int().optional(), costCurrency: z.string().optional(), costCurrencyDivisor: z.number().int().optional(), costFee: z.number().int().optional(), costFeePct: z.number().int().optional(), costFeeTotal: z.number().int().optional(), costTotal: z.number().int().optional(), createdAt: z.string().optional(), price: z.number().int().optional(), priceCurrency: z.string().optional(), priceCurrencyDivisor: z.number().int().optional(), priceFee: z.number().int().optional(), priceFx: z.number().optional(), priceType: dtoPriceType.optional(), send: z.number().int().optional(), sendCurrency: z.string().optional(), sendCurrencyDivisor: z.number().int().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 a BillPay bill for a specific offer * * GET /billpay/offers/{offerId}/bill */ export const getApiBillpayOffersOfferIdBill = async (args: GetApiBillpayOffersOfferIdBillArgs, options: GetApiBillpayOffersOfferIdBillOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.billIdentifier != null) query.append("billIdentifier", String(args.billIdentifier)) if (args.accountNumber != null) query.append("accountNumber", String(args.accountNumber)) if (ApiKey !== undefined) headers.set("Authorization", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/billpay/offers/${encodeURIComponent(String(args.offerId))}/bill${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(dtoBillPayBillResponse, await res.json()) };