import {z} from 'zod' export type GetApiProfilesMeOptions = {baseUrl?: string | undefined, HmacAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const profileResponse = z.object({id: z.number().int().optional(), account: z.string().optional(), username: z.string().optional(), displayName: z.string().optional(), bio: z.string().nullable().optional(), client: z.string().optional(), pfpUrl: z.string().nullable().optional(), smartWallet: z.string().nullable().optional(), isCreator: z.boolean().optional(), isOnboarded: z.boolean().optional(), socialUrl: z.string().nullable().optional(), hasTraded: z.boolean().optional(), tradeWalletOption: z.enum(["eoa", "smartWallet"]).nullable().optional(), tradeWalletChoosen: z.boolean().optional(), embeddedAccount: z.string().nullable().optional(), mode: z.enum(["simple", "advanced"]).optional(), referralCode: z.string().nullable().optional(), points: z.number().optional(), accumulativePoints: z.number().optional(), rank: z.object({id: z.number().int().optional(), name: z.enum(["Bronze", "Silver", "Gold", "Platinum", "Diamond"]).optional(), feeRateBps: z.number().int().optional()}).optional(), referralData: z.array(z.object({createdAt: z.string().optional(), id: z.number().int().optional(), referredProfileId: z.number().int().optional(), pfpUrl: z.string().nullable().optional(), displayName: z.string().optional()})).optional(), referredUsersCount: z.number().int().optional(), enrolledInPointsProgram: z.boolean().nullable().optional(), leaderboardPosition: z.number().int().optional(), isTop100: z.boolean().optional(), isCaptain: z.boolean().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 }; /** * Get current profile * * GET /profiles/me */ export const getApiProfilesMe = async (options?: GetApiProfilesMeOptions) => { 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 (HmacAuth !== undefined) headers.set("lmts-api-key", HmacAuth) const url = `${baseUrl.replace(/\/+$/, '')}/profiles/me${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(profileResponse, await res.json()) };