import {z} from 'zod' export type GetApiV2BalancesBalanceIdArgs = {testmode?: boolean | undefined, 'idempotency-key'?: string | undefined}; export type GetApiV2BalancesBalanceIdOptions = {baseUrl?: string | undefined, advancedAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const balanceToken = z.string(); export const mode = z.enum(["live", "test"]); export const createdAt = z.string(); export const currencies = z.enum(["EUR", "GBP", "CHF", "DKK", "NOK", "PLN", "SEK", "USD", "CZK", "HUF", "AUD", "CAD", "RON"]); export const balanceStatus = z.enum(["active", "inactive"]); export const balanceTransferFrequency = z.enum(["every-day", "daily", "every-monday", "every-tuesday", "every-wednesday", "every-thursday", "every-friday", "monthly", "revenue-day", "never"]); export const balanceTransferDestinationType = z.literal("bank-account"); export const url = z.object({href: z.string(), type: z.string()}); export const entityBalance = z.object({resource: z.string(), id: balanceToken, mode: mode, createdAt: createdAt, currency: currencies, description: z.string(), status: balanceStatus, transferFrequency: balanceTransferFrequency.optional(), transferThreshold: z.object({currency: z.string(), value: z.string()}).optional(), transferReference: z.string().nullable().optional(), transferDestination: z.object({type: balanceTransferDestinationType.optional(), bankAccount: z.string().optional(), beneficiaryName: z.string().optional()}).nullable().optional(), availableAmount: z.object({currency: z.string(), value: z.string()}), pendingAmount: z.object({currency: z.string(), value: z.string()}), _links: z.object({self: url.optional(), documentation: url.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 }; /** * Get balance * * GET /v2/balances/{balanceId} * * Also accepts auth: oAuth */ export const getApiV2BalancesBalanceId = async (args: GetApiV2BalancesBalanceIdArgs, options?: GetApiV2BalancesBalanceIdOptions) => { const {baseUrl = "https://api.mollie.com", fetch: fetchFn = fetch, advancedAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/hal+json") if (args.testmode != null) query.append("testmode", String(args.testmode)) if (args['idempotency-key'] != null) headers.set("idempotency-key", String(args['idempotency-key'])) if (advancedAccessToken !== undefined) headers.set('authorization', `Bearer ${advancedAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/balances/${encodeURIComponent(String(args.balanceId))}${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(entityBalance, await res.json()) };