import {z} from 'zod' export type GetApiV1AccountsArgs = {customerUuid?: string | undefined, accountType?: (Array<'ACCOUNT_TYPE_UNKNOWN' | 'ACCOUNT_TYPE_DEBIT' | 'ACCOUNT_TYPE_CREDIT'>) | undefined}; export type GetApiV1AccountsOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const dtoAccountType = z.enum(["ACCOUNT_TYPE_UNKNOWN", "ACCOUNT_TYPE_DEBIT", "ACCOUNT_TYPE_CREDIT"]); export const dtoMemberAccount = z.object({accountUuid: z.string().optional(), customerUuid: z.string().optional(), memberId: z.string().optional(), shareId: z.string().optional(), description: z.string().optional(), holderType: z.string().optional(), accountType: dtoAccountType.optional(), openedAt: z.string().optional()}); export const v1ListAccountsResponse = z.object({accounts: z.array(dtoMemberAccount).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 }; /** * List Accounts * * GET /v1/accounts */ export const getApiV1Accounts = async (args: GetApiV1AccountsArgs, options: GetApiV1AccountsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.customerUuid != null) query.append("customerUuid", String(args.customerUuid)) for (const item of args.accountType ?? []) query.append("accountType", String(item)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts${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(v1ListAccountsResponse, await res.json()) };