import {z} from 'zod' export type VoucherInfoInputIntegration = {test?: (boolean | null) | undefined, pointOfInteraction?: ('ON_THE_MOVE' | 'ECOMMERCE' | 'IN_PERSON' | 'INVOICE' | 'DEBT_COLLECTION' | 'FUNDING' | 'PAYMENT_REQUEST' | 'RECURRING' | 'UNATTENDED' | 'MOTO' | 'PAYOUT' | 'null' | null) | undefined}; export type VoucherInfoInputVoucher = {number: string, pincode?: (string | null) | undefined}; export type VoucherInfoInput = {serviceId?: (string | null) | undefined, integration?: (VoucherInfoInputIntegration | null) | undefined, voucher: VoucherInfoInputVoucher}; export type CreateApiV1VouchersInfoArgs = {body: VoucherInfoInput}; export type CreateApiV1VouchersInfoOptions = {baseUrl?: string | undefined, BasicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const voucherInfoOutputIntegration = z.object({test: z.boolean().optional(), pointOfInteraction: z.enum(["ON_THE_MOVE", "ECOMMERCE", "IN_PERSON", "INVOICE", "DEBT_COLLECTION", "FUNDING", "PAYMENT_REQUEST", "RECURRING", "UNATTENDED", "MOTO", "PAYOUT"]).optional()}); export const voucherInfoOutputAmount = z.object({value: z.number().int().optional(), currency: z.string().optional()}); export const voucherInfoOutputVoucherBrand = z.object({id: z.number().int().optional(), name: z.string().optional(), iconUrl: z.string().optional()}); export const voucherInfoOutputVoucher = z.object({amount: voucherInfoOutputAmount.optional(), expiresAt: z.string().nullable().optional(), brand: voucherInfoOutputVoucherBrand.optional()}); export const voucherInfoOutput = z.object({integration: voucherInfoOutputIntegration.optional(), voucher: voucherInfoOutputVoucher.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 }; /** * Voucher:info * * POST /v1/vouchers/info * * Also accepts auth: BearerAuth */ export const createApiV1VouchersInfo = async (args: CreateApiV1VouchersInfoArgs, options?: CreateApiV1VouchersInfoOptions) => { const {baseUrl = "https://connect.pay.nl", fetch: fetchFn = fetch, BasicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BasicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${BasicAuth.username}:${BasicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/vouchers/info${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(voucherInfoOutput, await res.json()) };