import {z} from 'zod' export type GetApiStatusArgs = {last_n_hours?: number | undefined, id?: number | undefined, terminate_pid?: number | undefined, authorization?: string | undefined, mode?: string | undefined}; export type GetApiStatusOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiStatusResponse = z.object({}); 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 }; /** * Obtain status information about the endpoint * * GET /status */ export const getApiStatus = async (args: GetApiStatusArgs, options?: GetApiStatusOptions) => { const {baseUrl = "https://arax.transltr.io/api/arax/v1.4", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.last_n_hours != null) query.append("last_n_hours", String(args.last_n_hours)) if (args.id != null) query.append("id", String(args.id)) if (args.terminate_pid != null) query.append("terminate_pid", String(args.terminate_pid)) if (args.authorization != null) query.append("authorization", String(args.authorization)) if (args.mode != null) query.append("mode", String(args.mode)) 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(getApiStatusResponse, await res.json()) };