import {z} from 'zod' export type PeerType = 'Group' | 'Channel'; export type GetApiV1ChannelsSearchArgs = {term?: string | undefined, search_in_about?: boolean | undefined, peer_type?: PeerType | undefined, country?: string | undefined, language?: string | undefined, category?: string | undefined, limit?: number | undefined, skip?: number | undefined}; export type GetApiV1ChannelsSearchOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; 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 getApiV1ChannelsSearchResponse = z.array(chatShort); 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 }; /** * Search * * GET /v1/channels/search */ export const getApiV1ChannelsSearch = async (args: GetApiV1ChannelsSearchArgs, options?: GetApiV1ChannelsSearchOptions) => { 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.term != null) query.append("term", String(args.term)) if (args.search_in_about != null) query.append("search_in_about", String(args.search_in_about)) if (args.peer_type != null) query.append("peer_type", String(args.peer_type)) if (args.country != null) query.append("country", String(args.country)) if (args.language != null) query.append("language", String(args.language)) if (args.category != null) query.append("category", String(args.category)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/channels/search${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(getApiV1ChannelsSearchResponse, await res.json()) };