import {z} from 'zod' export type GetApiV1MessagesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const messageItemReactionsResponseDto = z.object({counts: z.record(z.string(), z.number()), userReacted: z.array(z.string()).optional()}); export const messageQueryUserResponseDto = z.object({fullName: z.string(), profileImage: z.string().nullable()}); export const messageItemResponseDto = z.object({content: z.string(), createdAt: z.string(), deletedAt: z.string().nullable(), flagCount: z.number(), id: z.string(), messageId: z.string(), reactions: messageItemReactionsResponseDto, tokenImage: z.string().nullable(), tokenName: z.string(), updatedAt: z.string().nullable(), user: messageQueryUserResponseDto, userAddress: z.string(), userProfile: messageQueryUserResponseDto}); export const messagePageInfoResponseDto = z.object({endCursor: z.string().nullable(), hasNextPage: z.boolean(), hasPreviousPage: z.boolean(), startCursor: z.string().nullable()}); export const messageListDataResponseDto = z.object({messages: z.array(messageItemResponseDto), pageInfo: messagePageInfoResponseDto}); export const messageListSuccessEnvelopeDto = z.object({data: messageListDataResponseDto, error: z.literal(false), message: z.string(), status: z.number()}); 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 }; /** * Query unified messages (chat and comments) * * GET /v1/messages */ export const getApiV1Messages = async (options?: GetApiV1MessagesOptions) => { const {baseUrl = "https://lpad-backend.stage.defi.ovh.gala.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/messages${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(messageListSuccessEnvelopeDto, await res.json()) };