import {z} from 'zod' export type GetApiAccountsAccountIdArgs = {AccountId: string, 'x-fapi-financial-id': string, 'x-fapi-customer-last-logged-time'?: string | undefined, 'x-fapi-customer-ip-address'?: string | undefined, 'x-fapi-interaction-id'?: string | undefined, Authorization?: string | undefined, 'x-customer-user-agent'?: string | undefined}; export type GetApiAccountsAccountIdOptions = {baseUrl?: string | undefined, PSUOAuth2Security?: string | undefined, fetch?: typeof fetch | undefined}; export const accountId = z.string().min(1).max(40); export const oBExternalAccountType1Code = z.enum(["Business", "Personal"]); export const oBExternalAccountSubType1Code = z.enum(["ChargeCard", "CreditCard", "CurrentAccount", "EMoney", "Loan", "Mortgage", "PrePaidCard", "Savings"]); export const oBExternalAccountIdentification4Code = z.string().min(1).max(40); export const oBCashAccount5 = z.object({SchemeName: oBExternalAccountIdentification4Code, Identification: z.string().min(1).max(256), Name: z.string().min(1).max(70).optional(), SecondaryIdentification: z.string().min(1).max(34).optional()}); export const oBExternalFinancialInstitutionIdentification4Code = z.string().min(1).max(40); export const oBBranchAndFinancialInstitutionIdentification5 = z.object({SchemeName: oBExternalFinancialInstitutionIdentification4Code, Identification: z.string().min(1).max(35)}); export const oBAccount3 = z.object({AccountId: accountId, Currency: z.string(), AccountType: oBExternalAccountType1Code, AccountSubType: oBExternalAccountSubType1Code, Description: z.string().min(1).max(35).optional(), Nickname: z.string().min(1).max(70).optional(), Account: z.array(oBCashAccount5).optional(), Servicer: oBBranchAndFinancialInstitutionIdentification5.optional()}); export const links = z.object({Self: z.string(), First: z.string().optional(), Prev: z.string().optional(), Next: z.string().optional(), Last: z.string().optional()}); export const iSODateTime = z.string(); export const meta = z.object({TotalPages: z.number().int().optional(), FirstAvailableDateTime: iSODateTime.optional(), LastAvailableDateTime: iSODateTime.optional()}); export const oBReadAccount3 = z.object({Data: z.object({Account: z.array(oBAccount3).optional()}), Links: links, Meta: meta}); 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 Accounts * * GET /accounts/{AccountId} */ export const getApiAccountsAccountId = async (args: GetApiAccountsAccountIdArgs, options?: GetApiAccountsAccountIdOptions) => { const {baseUrl = "https://secure.tell.systems/algbra/open-banking/v3.1/aisp", fetch: fetchFn = fetch, PSUOAuth2Security} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=utf-8") if (args['x-fapi-financial-id'] != null) headers.set("x-fapi-financial-id", String(args['x-fapi-financial-id'])) if (args['x-fapi-customer-last-logged-time'] != null) headers.set("x-fapi-customer-last-logged-time", String(args['x-fapi-customer-last-logged-time'])) if (args['x-fapi-customer-ip-address'] != null) headers.set("x-fapi-customer-ip-address", String(args['x-fapi-customer-ip-address'])) if (args['x-fapi-interaction-id'] != null) headers.set("x-fapi-interaction-id", String(args['x-fapi-interaction-id'])) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['x-customer-user-agent'] != null) headers.set("x-customer-user-agent", String(args['x-customer-user-agent'])) if (PSUOAuth2Security !== undefined) headers.set('authorization', `Bearer ${PSUOAuth2Security}`) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/${encodeURIComponent(String(args.AccountId))}${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(oBReadAccount3, await res.json()) };