import {z} from 'zod' export type GithubComEtkeccMrsInternalModelRoomDirectoryFilter = {generic_search_term?: string | undefined, room_types?: Array | undefined}; export type GithubComEtkeccMrsInternalModelRoomDirectoryRequest = {filter?: GithubComEtkeccMrsInternalModelRoomDirectoryFilter | undefined, limit?: number | undefined, since?: string | undefined}; export type CreateApiMatrixFederationV1PublicRoomsArgs = {body: GithubComEtkeccMrsInternalModelRoomDirectoryRequest}; export type CreateApiMatrixFederationV1PublicRoomsOptions = {baseUrl: string, FederationAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const githubComEtkeccMrsInternalModelRoomDirectoryRoom = z.object({avatar_url: z.string().optional(), canonical_alias: z.string().optional(), guest_can_join: z.boolean().optional(), join_rule: z.string().optional(), name: z.string().optional(), num_joined_members: z.number().int().optional(), room_id: z.string().optional(), room_type: z.string().optional(), topic: z.string().optional(), world_readable: z.boolean().optional()}); export const githubComEtkeccMrsInternalModelRoomDirectoryResponse = z.object({chunk: z.array(githubComEtkeccMrsInternalModelRoomDirectoryRoom).optional(), 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 }; /** * Public rooms directory * * POST /_matrix/federation/v1/publicRooms */ export const createApiMatrixFederationV1PublicRooms = async (args: CreateApiMatrixFederationV1PublicRoomsArgs, options: CreateApiMatrixFederationV1PublicRoomsOptions) => { const {baseUrl, fetch: fetchFn = fetch, FederationAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (FederationAuth !== undefined) headers.set("Authorization", FederationAuth) const url = `${baseUrl.replace(/\/+$/, '')}/_matrix/federation/v1/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(githubComEtkeccMrsInternalModelRoomDirectoryResponse, await res.json()) };