import {z} from 'zod' export type MCPEventsArguments = {all_projects?: boolean | undefined, for: string, issue_uid?: string | undefined, project_uids?: Array | undefined}; export type MCPEventsDelivery = {mode: string, secret?: string | undefined, url: string}; export type MCPEventsForwarderScope = {all?: boolean | undefined, projects?: Array | undefined}; export type MCPEventsSubscribeBody = {arguments: MCPEventsArguments, delivery: MCPEventsDelivery, forwarder_scope?: MCPEventsForwarderScope | undefined, name: string, no_expiry?: boolean | undefined, ttl_ms?: number | undefined}; export type CreateApiApiV1McpEventsSubscribeArgs = {body: MCPEventsSubscribeBody}; export type CreateApiApiV1McpEventsSubscribeOptions = {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 mCPEventsSubscribeResponseBody = z.object({delivery_status: mCPEventsDeliveryStatus, id: z.string(), refresh_before: z.string().nullable()}).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 }; /** * Create or refresh an MCP Events webhook subscription * * POST /api/v1/mcp-events/subscribe */ export const createApiApiV1McpEventsSubscribe = async (args: CreateApiApiV1McpEventsSubscribeArgs, options: CreateApiApiV1McpEventsSubscribeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/mcp-events/subscribe${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(mCPEventsSubscribeResponseBody, await res.json()) };