import {z} from 'zod' export type CreateApiReadPaymentInformationArgs = {body: BodyInit}; export type CreateApiReadPaymentInformationOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const outputAmount = z.string(); export const currencyCode = z.string(); export const countryCode = z.string(); export const qrReference = z.string(); export const structuredCreditorReference = z.string(); export const createApiReadPaymentInformationResponse = z.object({amount: outputAmount.optional(), bic_swift: z.string().optional(), currency: currencyCode.optional(), iban: z.string().optional(), owner: z.union([z.object({name: z.string().optional(), address: z.string().optional(), address_complement: z.string().optional(), city: z.string().optional(), country: countryCode.optional(), state: z.string().optional(), zip: z.string().optional()}), z.object({name: z.string().optional(), building_name: z.string().optional(), building_number: z.string().optional(), country: countryCode.optional(), country_subdivision: z.string().optional(), department: z.string().optional(), district_name: z.string().optional(), floor: z.string().optional(), post_box: z.string().optional(), post_code: z.string().optional(), room: z.string().optional(), street_name: z.string().optional(), subdepartment: z.string().optional(), town_location_name: z.string().optional(), town_name: z.string().optional()})]).optional(), qr_reference: qrReference.optional(), reference: z.string().optional(), structured_creditor_reference: structuredCreditorReference.optional(), type: z.literal("bank_account"), ultimate_debtor: z.union([z.object({name: z.string().optional(), address: z.string().optional(), address_complement: z.string().optional(), city: z.string().optional(), country: countryCode.optional(), state: z.string().optional(), zip: z.string().optional()}), z.object({name: z.string().optional(), building_name: z.string().optional(), building_number: z.string().optional(), country: countryCode.optional(), country_subdivision: z.string().optional(), department: z.string().optional(), district_name: z.string().optional(), floor: z.string().optional(), post_box: z.string().optional(), post_code: z.string().optional(), room: z.string().optional(), street_name: z.string().optional(), subdepartment: z.string().optional(), town_location_name: z.string().optional(), town_name: z.string().optional()})]).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 }; /** * Read payment information * * POST /read_payment_information */ export const createApiReadPaymentInformation = async (args: CreateApiReadPaymentInformationArgs, options?: CreateApiReadPaymentInformationOptions) => { const {baseUrl = "https://exchange.api.bity.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "text/plain") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/read_payment_information${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiReadPaymentInformationResponse, await res.json()) };