import {z} from 'zod' export type AccountId = string; export type ConsentId = string; export type GetApiV1AccountsAccountIdArgs = {'account-id': AccountId, withBalance?: boolean | undefined, 'X-Request-ID': string, Digest?: string | undefined, Signature?: string | undefined, 'TPP-Signature-Certificate'?: string | undefined, 'Consent-ID': ConsentId, 'PSU-IP-Address'?: string | undefined, 'PSU-IP-Port'?: string | undefined, 'PSU-Accept'?: string | undefined, 'PSU-Accept-Charset'?: string | undefined, 'PSU-Accept-Encoding'?: string | undefined, 'PSU-Accept-Language'?: string | undefined, 'PSU-User-Agent'?: string | undefined, 'PSU-Http-Method'?: ('GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE') | undefined, 'PSU-Device-ID'?: string | undefined, 'PSU-Geo-Location'?: string | undefined}; export type GetApiV1AccountsAccountIdOptions = {baseUrl?: string | undefined, BearerAuthOAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const iban = z.string(); export const bban = z.string(); export const msisdn = z.string().max(35); export const currencyCode = z.string(); export const displayName = z.string().max(70); export const cashAccountType = z.string(); export const accountStatus = z.enum(["enabled", "deleted", "blocked"]); export const bicfi = z.string(); export const amountValue = z.string(); export const amount = z.object({currency: currencyCode, amount: amountValue}); export const balanceType = z.enum(["closingBooked", "expected", "openingBooked", "interimAvailable", "interimBooked", "forwardAvailable", "nonInvoiced"]); export const balance = z.object({balanceAmount: amount, balanceType: balanceType, creditLimitIncluded: z.boolean().optional(), lastChangeDateTime: z.string().optional(), referenceDate: z.string().optional(), lastCommittedTransaction: z.string().max(35).optional()}); export const balanceList = z.array(balance); export const hrefEntry = z.string(); export const hrefType = z.object({href: hrefEntry.optional()}); export const linksAccountDetails = z.object({balances: hrefType.optional(), transactions: hrefType.optional()}).and(z.record(z.string(), hrefType)); export const ownerName = z.string().max(140); export const accountDetails = z.object({resourceId: z.string().optional(), iban: iban.optional(), bban: bban.optional(), msisdn: msisdn.optional(), currency: currencyCode, name: z.string().max(70).optional(), displayName: displayName.optional(), product: z.string().max(35).optional(), cashAccountType: cashAccountType.optional(), status: accountStatus.optional(), bic: bicfi.optional(), linkedAccounts: z.string().max(70).optional(), usage: z.enum(["PRIV", "ORGA"]).max(4).optional(), details: z.string().max(500).optional(), balances: balanceList.optional(), _links: linksAccountDetails.optional(), ownerName: ownerName.optional()}); export const getApiV1AccountsAccountIdResponse = z.object({account: accountDetails}); 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 }; /** * Read account details * * GET /v1/accounts/{account-id} */ export const getApiV1AccountsAccountId = async (args: GetApiV1AccountsAccountIdArgs, options?: GetApiV1AccountsAccountIdOptions) => { const {baseUrl = "https://api.dev.openbankingproject.ch", fetch: fetchFn = fetch, BearerAuthOAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.withBalance != null) query.append("withBalance", String(args.withBalance)) if (args['X-Request-ID'] != null) headers.set("X-Request-ID", String(args['X-Request-ID'])) if (args.Digest != null) headers.set("Digest", String(args.Digest)) if (args.Signature != null) headers.set("Signature", String(args.Signature)) if (args['TPP-Signature-Certificate'] != null) headers.set("TPP-Signature-Certificate", String(args['TPP-Signature-Certificate'])) if (args['Consent-ID'] != null) headers.set("Consent-ID", String(args['Consent-ID'])) if (args['PSU-IP-Address'] != null) headers.set("PSU-IP-Address", String(args['PSU-IP-Address'])) if (args['PSU-IP-Port'] != null) headers.set("PSU-IP-Port", String(args['PSU-IP-Port'])) if (args['PSU-Accept'] != null) headers.set("PSU-Accept", String(args['PSU-Accept'])) if (args['PSU-Accept-Charset'] != null) headers.set("PSU-Accept-Charset", String(args['PSU-Accept-Charset'])) if (args['PSU-Accept-Encoding'] != null) headers.set("PSU-Accept-Encoding", String(args['PSU-Accept-Encoding'])) if (args['PSU-Accept-Language'] != null) headers.set("PSU-Accept-Language", String(args['PSU-Accept-Language'])) if (args['PSU-User-Agent'] != null) headers.set("PSU-User-Agent", String(args['PSU-User-Agent'])) if (args['PSU-Http-Method'] != null) headers.set("PSU-Http-Method", String(args['PSU-Http-Method'])) if (args['PSU-Device-ID'] != null) headers.set("PSU-Device-ID", String(args['PSU-Device-ID'])) if (args['PSU-Geo-Location'] != null) headers.set("PSU-Geo-Location", String(args['PSU-Geo-Location'])) if (BearerAuthOAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuthOAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts/${encodeURIComponent(String(args['account-id']))}${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(getApiV1AccountsAccountIdResponse, await res.json()) };