import {z} from 'zod' export type GetApiApiV1McpEventsSubscriptionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const mCPEventsDeliveryStatus = z.object({active: z.boolean(), failed_since: z.string().optional(), last_delivery_at: z.string().optional(), last_error: z.string().optional()}).and(z.record(z.string(), z.unknown())); export const mCPEventsSubscriptionStatus = z.object({dead_letter_count: z.number().int(), delivery: mCPEventsDeliveryStatus, end_reason: z.string().optional(), failed_attempts: z.number().int(), id: z.string(), issue_uid: z.string().optional(), name: z.string(), pending_control: z.string().optional(), project_uids: z.array(z.string()).optional(), recipient: z.string(), refresh_before: z.string().nullable(), state: z.string()}).and(z.record(z.string(), z.unknown())); export const mCPEventsSubscriptionsResponseBody = z.object({enabled: z.boolean(), reason: z.string().optional(), subscriptions: z.array(mCPEventsSubscriptionStatus)}).and(z.record(z.string(), z.unknown())); 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 }; /** * List the caller's MCP Events subscriptions and whether Events is running * * GET /api/v1/mcp-events/subscriptions */ export const getApiApiV1McpEventsSubscriptions = async (options: GetApiApiV1McpEventsSubscriptionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/mcp-events/subscriptions${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(mCPEventsSubscriptionsResponseBody, await res.json()) };