import {z} from 'zod' export type CreateApiExternalTransfersArgs = {body: {organizationId: string, teamId?: string | undefined, beneficiaryId: string, beneficiaryPaymentDetailId?: string | undefined, sourceCardAccountId?: string | undefined, transferAmount?: ({value?: number | undefined, currency?: string | undefined}) | undefined, transferMethod?: ('SEPA_CREDIT_TRANSFER' | 'INSTANT_CREDIT_TRANSFER') | undefined, purpose?: string | undefined, invoiceNumber?: string | undefined, invoiceDate?: string | undefined, dueDate?: string | undefined, transferTime?: string | undefined, vopId?: string | undefined}}; export type CreateApiExternalTransfersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiExternalTransfersResponse = z.object({id: z.string().optional(), organizationId: z.string().optional(), teamId: z.string().optional(), memberId: z.string().optional(), beneficiaryId: z.string().optional(), beneficiaryPaymentDetailId: z.string().optional(), sourceCardAccountId: z.string().optional(), transferAmount: z.object({value: z.number().optional(), currency: z.string().optional()}).optional(), transferMethod: z.enum(["SEPA_CREDIT_TRANSFER", "INSTANT_CREDIT_TRANSFER"]).optional(), status: z.enum(["DRAFT", "NEEDS_APPROVAL", "APPROVED", "COMPLIANCE_CHECK", "COMPLIANCE_PASSED", "COMPLIANCE_REVIEW", "PAYMENT_IN_PROGRESS", "PAID", "FAILED"]).optional(), purpose: z.string().optional(), invoiceNumber: z.string().optional(), invoiceDate: z.string().optional(), dueDate: z.string().optional(), transferTime: z.string().optional(), approvedAt: z.string().optional(), createdAt: z.string().optional(), vop: z.object({vopResult: z.string().optional(), vopExternalVerificationId: z.string().optional(), vopExpiresAt: z.string().optional()}).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 }; /** * Create External Transfer * * POST /external-transfers */ export const createApiExternalTransfers = async (args: CreateApiExternalTransfersArgs, options?: CreateApiExternalTransfersOptions) => { const {baseUrl = "https://partner-api.getpliant.com/api", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/external-transfers${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(createApiExternalTransfersResponse, await res.json()) };