import {z} from 'zod' export type CreateApiV1WebsocketArgs = {body: {action: 'trade_executed' | 'balance_updated' | 'trade_update_notify' | 'stream_status' | 'viewer_count' | 'stream_countdown' | 'recording_status' | 'recordings_count_updated' | 'download_ready' | 'simulcast_status' | 'stream_language_updated' | 'chat_message' | 'chat_status' | 'typing_indicator' | 'stream_chat_sent' | 'stream_chat_updated' | 'stream_chat_deleted' | 'stream_chat_pinned' | 'stream_chat_unpinned' | 'stream_control_status_changed' | 'send_stream_reaction' | 'stream_reaction' | 'content_reaction_added' | 'content_reaction_removed' | 'comment_created' | 'comment_updated' | 'comment_deleted' | 'token_banned' | 'token_unbanned' | 'moderator_added' | 'moderator_removed' | 'moderator_invite_created' | 'moderator_role_updated' | 'overseer_added' | 'overseer_removed' | 'user_banned' | 'user_unbanned' | 'ban_enforcement' | 'user_profile_updated' | 'api_key_created' | 'api_key_updated' | 'api_key_revoked' | 'holder_count_updated' | 'pool_badge_assigned' | 'pool_created' | 'pool_metadata_updated' | 'pool_social_links_updated' | 'token_config_updated' | 'ai_moderation_complete' | 'ai_moderation_failed' | 'content_flagged' | 'flag_resolved' | 'site_config_changed' | 'feature_status' | 'chat_global_status' | 'streaming_global_status' | 'comments_global_status' | 'webhooks_global_status' | 'ai_moderation_global_status' | 'stats_updated' | 'event', broadcast?: boolean | undefined, payload: {[key: string]: unknown}, tokenName?: string | undefined, userAddress?: string | undefined}}; export type CreateApiV1WebsocketOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1WebsocketResponse = z.object({error: z.literal(false), message: z.string(), status: z.number(), data: z.object({})}); 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 }; /** * Emit WebSocket event to connected clients * * POST /v1/websocket * * Also accepts auth: bearer */ export const createApiV1Websocket = async (args: CreateApiV1WebsocketArgs, options?: CreateApiV1WebsocketOptions) => { 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('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("x-api-key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/websocket${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1WebsocketResponse, await res.json()) };