import {z} from 'zod' export type MandateToken = string; export type Sorting = 'asc' | 'desc'; export type MandateScopes = 'customer-present' | 'customer-not-present'; export type GetApiV2CustomersCustomerIdMandatesArgs = {from?: MandateToken | undefined, limit?: (number | null) | undefined, sort?: Sorting | undefined, scopes?: Array | undefined, testmode?: boolean | undefined, 'idempotency-key'?: string | undefined}; export type GetApiV2CustomersCustomerIdMandatesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const listCount = z.number().int().gte(0).lte(250); export const mandateToken = z.string(); export const mode = z.enum(["live", "test"]); export const mandateMethodResponse = z.enum(["creditcard", "directdebit", "paypal"]); export const mandateDetailsCardLabelResponse = z.enum(["American Express", "Carta Si", "Carte Bleue", "Cartes Bancaires", "Dankort", "Diners Club", "Discover", "JCB", "Laser", "Maestro", "Mastercard", "Unionpay", "Visa", "null"]).nullable(); export const mandateScopesResponse = z.enum(["customer-present", "customer-not-present"]); export const mandateStatusResponse = z.enum(["valid", "pending", "invalid"]); export const customerToken = z.string(); export const createdAt = z.string(); export const testmodeCreate = z.boolean().nullable(); export const url = z.object({href: z.string(), type: z.string()}); export const listMandateResponse = z.object({resource: z.string(), id: mandateToken, mode: mode, method: mandateMethodResponse, consumerName: z.string().optional(), consumerAccount: z.string().nullable().optional(), consumerBic: z.string().nullable().optional(), consumerEmail: z.string().nullable().optional(), details: z.object({consumerName: z.string().nullable().optional(), consumerAccount: z.string().nullable().optional(), consumerBic: z.string().nullable().optional(), cardHolder: z.string().nullable().optional(), cardNumber: z.string().nullable().optional(), cardExpiryDate: z.string().nullable().optional(), cardLabel: mandateDetailsCardLabelResponse.optional(), cardFingerprint: z.string().nullable().optional()}), signatureDate: z.string().nullable(), mandateReference: z.string().nullable(), paypalBillingAgreementId: z.string().nullable().optional(), payPalVaultId: z.string().nullable().optional(), scopes: z.array(mandateScopesResponse).nullable().optional(), status: mandateStatusResponse, customerId: customerToken, createdAt: createdAt, testmode: testmodeCreate.optional(), _links: z.object({self: url, customer: url})}); export const urlNullable = z.object({href: z.string().optional(), type: z.string().optional()}).nullable(); export const listLinks = z.object({self: url, previous: urlNullable, next: urlNullable, documentation: url}); export const getApiV2CustomersCustomerIdMandatesResponse = z.object({count: listCount, _embedded: z.object({mandates: z.array(listMandateResponse)}), _links: listLinks}); 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 mandates * * GET /v2/customers/{customerId}/mandates * * Also accepts auth: advancedAccessToken, oAuth */ export const getApiV2CustomersCustomerIdMandates = async (args: GetApiV2CustomersCustomerIdMandatesArgs, options?: GetApiV2CustomersCustomerIdMandatesOptions) => { const {baseUrl = "https://api.mollie.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/hal+json") if (args.from != null) query.append("from", String(args.from)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort != null) query.append("sort", String(args.sort)) for (const item of args.scopes ?? []) query.append("scopes", String(item)) if (args.testmode != null) query.append("testmode", String(args.testmode)) if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/customers/${encodeURIComponent(String(args.customerId))}/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(getApiV2CustomersCustomerIdMandatesResponse, await res.json()) };