import {z} from 'zod' export type CreateApiMatrixClientV3PublicRoomsArgs = {server?: unknown | undefined, body: {filter?: ({generic_search_term?: string | undefined, room_types?: (Array) | undefined}) | undefined, include_all_networks?: boolean | undefined, limit?: number | undefined, since?: string | undefined, third_party_instance_id?: string | undefined}}; export type CreateApiMatrixClientV3PublicRoomsOptions = {baseUrl?: string | undefined, accessTokenQuery?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiMatrixClientV3PublicRoomsResponse = z.object({chunk: z.array(z.object({avatar_url: z.string().optional(), canonical_alias: z.string().optional(), guest_can_join: z.boolean(), join_rule: z.string().optional(), name: z.string().optional(), num_joined_members: z.number().int(), room_id: z.string(), room_type: z.string().optional(), topic: z.string().optional(), world_readable: z.boolean()})), next_batch: z.string().optional(), prev_batch: z.string().optional(), total_room_count_estimate: z.number().int().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 }; /** * Lists a server's published room directory with an optional filter * * POST /_matrix/client/v3/publicRooms * * Also accepts auth: accessTokenBearer */ export const createApiMatrixClientV3PublicRooms = async (args: CreateApiMatrixClientV3PublicRoomsArgs, options?: CreateApiMatrixClientV3PublicRoomsOptions) => { const {baseUrl = "https://matrix.org", fetch: fetchFn = fetch, accessTokenQuery} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.server != null) query.append("server", String(args.server)) if (accessTokenQuery !== undefined) query.set("access_token", accessTokenQuery) const url = `${baseUrl.replace(/\/+$/, '')}/_matrix/client/v3/publicRooms${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(createApiMatrixClientV3PublicRoomsResponse, await res.json()) };