import {z} from 'zod' export type GetApiRecipientsRecipientIdArgs = {recipientId: string, accountId: string}; export type GetApiRecipientsRecipientIdOptions = {baseUrl?: string | undefined, CommonAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRecipientsRecipientIdResponse = z.object({id: z.string().nullable().optional(), accountId: z.string().nullable().optional(), accountIdentifier: z.string().max(34).nullable().optional(), bankIdentifier: z.string().max(20).nullable().optional(), schemeName: z.enum(["UK.OBIE.IBAN", "UK.OBIE.SortCodeAccountNumber"]).nullable().optional(), name: z.string().max(256).nullable().optional(), friendlyName: z.string().max(256).nullable().optional(), intermediaryBankIdentifier: z.string().nullable().optional(), paymentNetwork: z.enum(["SWIFT", "SEPA.INSTANT", "SEPA.CREDITTRANSFER", "CHAPS", "UKFPS", "ACH", "WIRETRANSFER", "OFF_PLATFORM", "NIP"]).nullable(), purposeCode: z.string().nullable().optional(), email: z.string().max(256).nullable().optional(), defaultPurpose: z.string().max(256).nullable().optional(), defaultReference: z.string().nullable().optional(), status: z.enum(["UNVERIFIED", "PENDING", "AUTHORISED", "REJECTED"]), sepa: z.boolean().nullable().optional(), currency: z.string(), bankCode: z.string().nullable().optional(), createdBy: z.string().nullable().optional(), recipientType: z.string().nullable().optional(), verificationMethod: z.enum(["phone", "sms_otp", "biometrics", "email", "other"]).nullable().optional(), recipientAddress: z.object({country: z.string().nullable(), address: z.string().max(256).nullable().optional(), city: z.string().max(256).nullable().optional(), postcode: z.string().max(36).nullable().optional()}).optional(), bankAddress: z.object({bankName: z.string().nullable().optional(), fullBankName: z.string().nullable().optional(), city: z.string().max(256).nullable().optional(), postcode: z.string().max(36).nullable().optional(), country: z.string().nullable(), address: z.string().max(256).nullable().optional()}).optional(), validation: z.object({nameMatch: z.boolean().optional(), reason: z.string().optional(), reasonCode: z.enum(["N000", "N100", "N101", "N102", "N103", "N104", "N200", "N201", "N202", "N203", "N204", "N205", "N206", "N207", "N300", "N400", "N500"]), actualName: z.string().optional()}).optional(), bankDetailsFormat: z.enum(["bic-accountnumber", "usroutingnumber-accountnumber", "bic-caroutingnumber-accountnumber", "bic-bsb-accountnumber", "bic-ifsc-accountnumber", "sortcode-accountnumber", "bic-clabe", "bic-iban"]).nullable().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 recipient * * GET /recipients/{recipientId} */ export const getApiRecipientsRecipientId = async (args: GetApiRecipientsRecipientIdArgs, options?: GetApiRecipientsRecipientIdOptions) => { const {baseUrl = "https://api.equalsmoney.com/v2", fetch: fetchFn = fetch, CommonAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.accountId != null) query.append("accountId", String(args.accountId)) if (CommonAuth !== undefined) headers.set("Authorization", CommonAuth) const url = `${baseUrl.replace(/\/+$/, '')}/recipients/${encodeURIComponent(String(args.recipientId))}${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(getApiRecipientsRecipientIdResponse, await res.json()) };