import {z} from 'zod' export type GetApiV1EventsEndpointNameArgs = {endpoint_name: string, begin_time?: string | undefined, end_time?: string | undefined, max_lines?: number | undefined, replica_id?: string | undefined}; export type GetApiV1EventsEndpointNameOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const event = z.object({level: z.enum(["normal", "warning"]).optional(), type: z.string(), endpoint: z.string().optional(), tenant_id: z.string().optional(), account_key: z.string().optional(), instance_type: z.string().optional(), details: z.object({}).optional()}); export const eventEntry = z.object({timestamp: z.string(), replica_id: z.string().optional(), event: event}); export const getApiV1EventsEndpointNameResponse = z.array(eventEntry); 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 }; /** * Return endpoint events * * GET /v1/events/{endpoint_name} */ export const getApiV1EventsEndpointName = async (args: GetApiV1EventsEndpointNameArgs, options?: GetApiV1EventsEndpointNameOptions) => { const {baseUrl = "https://api.octoai.cloud", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.begin_time != null) query.append("begin_time", String(args.begin_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.max_lines != null) query.append("max_lines", String(args.max_lines)) if (args.replica_id != null) query.append("replica_id", String(args.replica_id)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/events/${encodeURIComponent(String(args.endpoint_name))}${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(getApiV1EventsEndpointNameResponse, await res.json()) };