import {z} from 'zod' export type GetApiAccountProductArgs = {account_type?: string | undefined, fbo_id?: string | undefined, name?: string | undefined, ownership?: string | undefined, page_cursor?: string | undefined, page_size?: number | undefined, from_date?: string | undefined, to_date?: string | undefined}; export type GetApiAccountProductOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiAccountProductResponse = z.object({data: z.array(z.object({account_type: z.string().optional(), apply_workflow: z.string().optional(), business_kyc_product_id: z.string().optional(), fbo_id: z.string().optional(), id: z.string().optional(), name: z.string().optional(), ownership: z.string().optional(), person_kyc_product_id: z.string().optional(), userdata: z.object({}).optional()})).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 Account Products * * GET /account_product * * Also accepts auth: apiKey */ export const getApiAccountProduct = async (args: GetApiAccountProductArgs, options?: GetApiAccountProductOptions) => { const {baseUrl = "https://api.treasuryprime.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account_type != null) query.append("account_type", String(args.account_type)) if (args.fbo_id != null) query.append("fbo_id", String(args.fbo_id)) if (args.name != null) query.append("name", String(args.name)) if (args.ownership != null) query.append("ownership", String(args.ownership)) if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/account_product${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(getApiAccountProductResponse, await res.json()) };