import {z} from 'zod' export type GetApiV4OrganizationsOrganizationIdEventsArgs = {page?: number | undefined, perPage?: number | undefined, sortBy?: (Array<'timestamp' | 'severity'>) | undefined, sortDirection?: ('asc' | 'desc') | undefined, userIds?: Array | undefined, clusterIds?: Array | undefined, projectIds?: Array | undefined, severityLevels?: (Array<'debug' | 'info' | 'warning' | 'critical'>) | undefined, tags?: (Array<'availability' | 'billing' | 'maintenance' | 'performance' | 'security' | 'alert'>) | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiV4OrganizationsOrganizationIdEventsOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const getEventResponse = z.object({id: z.string(), source: z.string(), key: z.string(), severity: z.string(), timestamp: z.string(), projectId: z.string().optional(), projectName: z.string().optional(), clusterId: z.string().optional(), clusterName: z.string().optional(), appServiceId: z.string().optional(), appServiceName: z.string().optional(), userId: z.string().optional(), userName: z.string().optional(), userEmail: z.string().optional(), sessionId: z.string().optional(), requestId: z.string().optional(), kv: z.object({}).optional(), summary: z.string().optional(), incidentIds: z.array(z.string()).optional(), occurrenceCount: z.number().int().optional(), imageURL: z.string().optional(), alertKey: z.string().optional()}); export const pages = z.object({page: z.number().int(), next: z.number().int().optional(), previous: z.number().int().optional(), last: z.number().int(), perPage: z.number().int(), totalItems: z.number().int()}); export const hrefs = z.object({first: z.string(), last: z.string(), previous: z.string(), next: z.string()}); export const cursor = z.object({pages: pages, hrefs: hrefs}); export const getEventsResponse = z.object({data: z.array(getEventResponse), cursor: cursor}); 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 Events * * GET /v4/organizations/{organizationId}/events */ export const getApiV4OrganizationsOrganizationIdEvents = async (args: GetApiV4OrganizationsOrganizationIdEventsArgs, options?: GetApiV4OrganizationsOrganizationIdEventsOptions) => { const {baseUrl = "https://cloudapi.cloud.couchbase.com", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) for (const item of args.sortBy ?? []) query.append("sortBy", String(item)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) for (const item of args.userIds ?? []) query.append("userIds", String(item)) for (const item of args.clusterIds ?? []) query.append("clusterIds", String(item)) for (const item of args.projectIds ?? []) query.append("projectIds", String(item)) for (const item of args.severityLevels ?? []) query.append("severityLevels", String(item)) for (const item of args.tags ?? []) query.append("tags", String(item)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/${encodeURIComponent(String(args.organizationId))}/events${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(getEventsResponse, await res.json()) };