import {z} from 'zod' export type MentionType = 'Mention' | 'Forward'; export type GetApiV1ChannelMentionsArgs = {internal_id: string, from_date?: number | undefined, to_date?: number | undefined, kind?: MentionType | undefined, short_info?: boolean | undefined, cursor?: string | undefined}; export type GetApiV1ChannelMentionsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const mentionType = z.enum(["Mention", "Forward"]); export const channelMentionShort = z.object({source_chat_id: z.string(), kind: mentionType, message_id: z.number().int().gte(0), date: z.string()}); export const peerType = z.enum(["Group", "Channel"]); export const chatShort = z.object({internal_id: z.string(), peer: peerType, title: z.string(), photo_url: z.string().nullable().optional(), country: z.string().nullable().optional(), language: z.string().nullable().optional(), category: z.string().nullable().optional(), members_count: z.number().int().gte(0), verified: z.boolean()}); export const mentions = z.object({total: z.number().int().gte(0), mentions: z.array(channelMentionShort), chats: z.array(chatShort), cursor: z.string().nullable().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 }; /** * Mentions * * GET /v1/channel/mentions */ export const getApiV1ChannelMentions = async (args: GetApiV1ChannelMentionsArgs, options?: GetApiV1ChannelMentionsOptions) => { const {baseUrl = "https://api.tlmtr.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.internal_id != null) query.append("internal_id", String(args.internal_id)) if (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (args.kind != null) query.append("kind", String(args.kind)) if (args.short_info != null) query.append("short_info", String(args.short_info)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/channel/mentions${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(mentions, await res.json()) };