import {z} from 'zod' export type PaymentInstrumentMandateStatus = 'canceled' | 'confirmed' | 'created' | 'rejected'; export type PaymentInstrumentMandateType = 'sepa_direct_debit'; export type GetApiAccountsAccountIdPaymentInstrumentMandatesArgs = {account_id: string, status?: (PaymentInstrumentMandateStatus | '*') | undefined, type?: PaymentInstrumentMandateType | undefined, mandate_reference?: string | undefined, cursor?: string | undefined, limit?: number | undefined, 'LMG-Data-Privacy-Access-Principal': string, 'LMG-Data-Privacy-Access-Justification': string}; export type GetApiAccountsAccountIdPaymentInstrumentMandatesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const paymentInstrumentMandateType = z.literal("sepa_direct_debit"); export const paymentInstrumentMandateDebtorResponse = z.object({iban: z.string(), bic: z.string().optional(), account_holder_name: z.string()}); export const paymentInstrumentMandateCreditorAddressResponse = z.object({line_1: z.string(), line_2: z.string().optional(), postal_code: z.string(), city: z.string(), country: z.string()}); export const paymentInstrumentMandateCreditorResponse = z.object({identifier: z.string(), name: z.string(), address: paymentInstrumentMandateCreditorAddressResponse}); export const paymentInstrumentMandateScaResponse = z.object({required: z.boolean(), challenge: z.string().optional()}); export const paymentInstrumentMandateStatus = z.enum(["canceled", "confirmed", "created", "rejected"]); export const statusChangePaymentInstrumentMandateStatus = z.object({status: paymentInstrumentMandateStatus, timestamp: z.string()}); export const paymentInstrumentMandateResponse = z.object({id: z.string(), type: paymentInstrumentMandateType, mandate_reference: z.string(), debtor: paymentInstrumentMandateDebtorResponse, creditor: paymentInstrumentMandateCreditorResponse, sca: paymentInstrumentMandateScaResponse.optional(), history: z.array(statusChangePaymentInstrumentMandateStatus)}); export const encodedPaginationResponse = z.object({next_cursor: z.string().optional()}); export const encodedPaginatedCollectionResponsePaymentInstrumentMandateResponse = z.object({data: z.array(paymentInstrumentMandateResponse), pagination: encodedPaginationResponse}); 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 }; /** * List Payment Instrument Mandates * * GET /accounts/{account_id}/payment_instrument_mandates */ export const getApiAccountsAccountIdPaymentInstrumentMandates = async (args: GetApiAccountsAccountIdPaymentInstrumentMandatesArgs, options?: GetApiAccountsAccountIdPaymentInstrumentMandatesOptions) => { const {baseUrl = "https://sandbox.api.lemon.markets/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (args.mandate_reference != null) query.append("mandate_reference", String(args.mandate_reference)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args['LMG-Data-Privacy-Access-Principal'] != null) headers.set("LMG-Data-Privacy-Access-Principal", String(args['LMG-Data-Privacy-Access-Principal'])) if (args['LMG-Data-Privacy-Access-Justification'] != null) headers.set("LMG-Data-Privacy-Access-Justification", String(args['LMG-Data-Privacy-Access-Justification'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.account_id))}/payment_instrument_mandates${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(encodedPaginatedCollectionResponsePaymentInstrumentMandateResponse, await res.json()) };