import {z} from 'zod' export type GetApiStatusOptions = {baseUrl: string, AdminAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const githubComEtkeccMrsInternalModelIndexStatsTime = z.object({finished_at: z.string().optional(), started_at: z.string().optional()}); export const githubComEtkeccMrsInternalModelIndexStatsRooms = z.object({banned: z.number().int().optional(), indexed: z.number().int().optional(), parsed: z.number().int().optional(), reported: z.number().int().optional()}); export const githubComEtkeccMrsInternalModelIndexStatsServers = z.object({blocked: z.number().int().optional(), indexable: z.number().int().optional(), online: z.number().int().optional(), software: z.record(z.string(), z.number().int()).optional()}); export const githubComEtkeccMrsInternalModelIndexStats = z.object({discovery: githubComEtkeccMrsInternalModelIndexStatsTime.optional(), indexing: githubComEtkeccMrsInternalModelIndexStatsTime.optional(), parsing: githubComEtkeccMrsInternalModelIndexStatsTime.optional(), rooms: githubComEtkeccMrsInternalModelIndexStatsRooms.optional(), servers: githubComEtkeccMrsInternalModelIndexStatsServers.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 }; /** * Index status * * GET /-/status */ export const getApiStatus = async (options: GetApiStatusOptions) => { const {baseUrl, fetch: fetchFn = fetch, AdminAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (AdminAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${AdminAuth.username}:${AdminAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/-/status${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(githubComEtkeccMrsInternalModelIndexStats, await res.json()) };