import {z} from 'zod' export type RecordId = string; export type GetApiFundingsIdArgs = {id: RecordId}; export type GetApiFundingsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const recordId = z.string(); export const externalMerchantId = z.string().max(30); export const merchantTradeName = z.string(); export const allianceCode = z.string().min(1).max(50).nullable(); export const funding = z.object({id: recordId, funded: z.string(), posted: z.string(), status: z.enum(["RELEASED", "HELD", "UNKNOWN"]), type: z.object({code: z.string(), description: z.string()}), merchant: z.object({id: externalMerchantId, name: merchantTradeName, allianceCode: allianceCode, partnerCode: z.string().min(1).max(50).nullable()}), account: z.object({id: z.string(), reference: z.string().nullable(), iban: z.string().nullable(), currencyCode: z.string().min(3).max(3).nullable(), type: z.object({code: z.string().nullable(), description: z.string()})}), financial: z.object({netAmount: z.string()}), meta: z.object({paymentReference: z.string().nullable()}), _links: z.object({transactions: z.object({href: z.string().optional()}).optional(), "funding-details": z.object({href: z.string().optional()}).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 }; /** * Retrieve a single funding * * GET /fundings/{id} */ export const getApiFundingsId = async (args: GetApiFundingsIdArgs, options?: GetApiFundingsIdOptions) => { const {baseUrl = "https://prod.emea.api.fiservapps.com/ipp/omnipay/acquirerprofile/v1.0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/fundings/${encodeURIComponent(String(args.id))}${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(funding, await res.json()) };