import {z} from 'zod' export type GetApiV1UsersAddressSummaryArgs = {address: string}; export type GetApiV1UsersAddressSummaryOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const userBadgeDataResponseDto = z.object({engagement: z.array(z.string()), launch: z.array(z.string()), success: z.array(z.string()), volume: z.array(z.string())}); export const managedUserProfileInfoResponseDto = z.object({address: z.string(), badges: userBadgeDataResponseDto, bio: z.string().nullable(), createdAt: z.string(), fullName: z.string().nullable(), profileImage: z.string().nullable()}); export const userActivityStatsResponseDto = z.object({activeBans: z.number(), chatMessagesCount: z.number(), commentsCount: z.number(), flagsPending: z.number(), flagsReceived: z.number(), globalBan: z.boolean(), tokensCreated: z.number(), tokensHeld: z.number()}); export const userSummaryResponseDto = z.object({profile: z.union([managedUserProfileInfoResponseDto, z.unknown()]), stats: userActivityStatsResponseDto}); export const getApiV1UsersAddressSummaryResponse = z.object({error: z.literal(false), message: z.string(), status: z.number(), data: userSummaryResponseDto}); 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 user summary * * GET /v1/users/{address}/summary * * Also accepts auth: bearer */ export const getApiV1UsersAddressSummary = async (args: GetApiV1UsersAddressSummaryArgs, options?: GetApiV1UsersAddressSummaryOptions) => { const {baseUrl = "https://lpad-backend.stage.defi.ovh.gala.com", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/${encodeURIComponent(String(args.address))}/summary${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(getApiV1UsersAddressSummaryResponse, await res.json()) };