import {z} from 'zod' export type GetApiProfilesPartnerAccountsArgs = {account?: string | undefined, limit?: number | undefined, page?: number | undefined}; export type GetApiProfilesPartnerAccountsOptions = {baseUrl?: string | undefined, HmacAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const partnerAccountListItem = z.object({profileId: z.number().int(), account: z.string(), displayName: z.string()}); export const listPartnerAccountsResponse = z.object({data: z.array(partnerAccountListItem), limit: z.number().int(), page: z.number().int(), hasMore: z.boolean()}); 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 partner sub-accounts * * GET /profiles/partner-accounts */ export const getApiProfilesPartnerAccounts = async (args: GetApiProfilesPartnerAccountsArgs, options?: GetApiProfilesPartnerAccountsOptions) => { const {baseUrl = "https://api.limitless.exchange", fetch: fetchFn = fetch, HmacAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account != null) query.append("account", String(args.account)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (HmacAuth !== undefined) headers.set("lmts-api-key", HmacAuth) const url = `${baseUrl.replace(/\/+$/, '')}/profiles/partner-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(listPartnerAccountsResponse, await res.json()) };