import {z} from 'zod' export type ExternalToken = string; export type BasicAuth = {user: string, password: string}; export type Exclude = {clusters?: Array | undefined, appServices?: Array | undefined}; export type WebhookCustomPayload = {payload: {[key: string]: unknown}}; export type RequestWebhook = {method: 'POST' | 'PUT', url: string, token?: ExternalToken | undefined, basicAuth?: BasicAuth | undefined, headers?: {[key: string]: string} | undefined, exclude?: Exclude | undefined, customPayloads?: {[key: string]: WebhookCustomPayload} | undefined}; export type SlackCustomPayload = {payload: {[key: string]: unknown}}; export type UpdateRequestSlack = {botToken?: (string | null) | undefined, channel?: (string | null) | undefined, clusterChannelMappings?: ({[key: string]: string | null}) | undefined, appServiceChannelMappings?: ({[key: string]: string | null}) | undefined, clusterWebhookChannelMappings?: ({[key: string]: string | null}) | undefined, appServiceWebhookChannelMappings?: ({[key: string]: string | null}) | undefined, channelWebhookUrlMappings?: ({[key: string]: string | null}) | undefined, customPayloads?: ({[key: string]: SlackCustomPayload | null}) | undefined}; export type TeamsCustomPayload = {payload: {[key: string]: unknown}}; export type UpdateRequestTeams = {webhookUrlMappings?: ({[key: string]: string | null}) | undefined, clusterWebhookMappings?: ({[key: string]: string | null}) | undefined, appServiceWebhookMappings?: ({[key: string]: string | null}) | undefined, customPayloads?: ({[key: string]: TeamsCustomPayload | null}) | undefined}; export type UpdateRequestConfig = {webhook: RequestWebhook, slack?: UpdateRequestSlack | undefined, teams?: UpdateRequestTeams | undefined} | {webhook?: RequestWebhook | undefined, slack: UpdateRequestSlack, teams?: UpdateRequestTeams | undefined} | {webhook?: RequestWebhook | undefined, slack?: UpdateRequestSlack | undefined, teams: UpdateRequestTeams}; export type UpdateAlertRequest = {kind?: ('webhook' | 'slack' | 'teams') | undefined, name?: (string | null) | undefined, enabled?: boolean | undefined, config?: UpdateRequestConfig | undefined}; export type UpdateApiV4OrganizationsOrganizationIdProjectsProjectIdAlertIntegrationsAlertIntegrationIdArgs = {body: UpdateAlertRequest}; export type UpdateApiV4OrganizationsOrganizationIdProjectsProjectIdAlertIntegrationsAlertIntegrationIdOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const exclude = z.object({clusters: z.array(z.string()).optional(), appServices: z.array(z.string()).optional()}); export const webhookCustomPayload = z.object({payload: z.record(z.string(), z.unknown())}); export const responseWebhook = z.object({method: z.enum(["POST", "PUT"]), url: z.string(), headers: z.record(z.string(), z.string()).optional(), exclude: exclude.optional(), customPayloads: z.record(z.string(), webhookCustomPayload).optional()}); export const slackCustomPayload = z.object({payload: z.record(z.string(), z.unknown())}); export const responseSlack = z.object({channel: z.string().optional(), clusterChannelMappings: z.record(z.string(), z.string()).optional(), appServiceChannelMappings: z.record(z.string(), z.string()).optional(), clusterWebhookChannelMappings: z.record(z.string(), z.string()).optional(), appServiceWebhookChannelMappings: z.record(z.string(), z.string()).optional(), customPayloads: z.record(z.string(), slackCustomPayload).optional()}); export const teamsCustomPayload = z.object({payload: z.record(z.string(), z.unknown())}); export const responseTeams = z.object({clusterWebhookMappings: z.record(z.string(), z.string()).optional(), appServiceWebhookMappings: z.record(z.string(), z.string()).optional(), customPayloads: z.record(z.string(), teamsCustomPayload).optional()}); export const responseConfig = z.union([z.object({webhook: responseWebhook, slack: responseSlack.optional(), teams: responseTeams.optional()}), z.object({webhook: responseWebhook.optional(), slack: responseSlack, teams: responseTeams.optional()}), z.object({webhook: responseWebhook.optional(), slack: responseSlack.optional(), teams: responseTeams})]); export const couchbaseAuditData = z.object({createdBy: z.string(), createdAt: z.string(), modifiedBy: z.string(), modifiedAt: z.string(), version: z.number().int()}); export const getAlertResponse = z.object({id: z.string(), name: z.string().max(1024), tenantId: z.string(), projectId: z.string().optional(), kind: z.enum(["webhook", "slack", "teams"]), configKey: z.string(), status: z.string(), enabled: z.boolean(), config: responseConfig, audit: couchbaseAuditData}); 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 }; /** * Update Alert Integration * * PUT /v4/organizations/{organizationId}/projects/{projectId}/alertIntegrations/{alertIntegrationId} */ export const updateApiV4OrganizationsOrganizationIdProjectsProjectIdAlertIntegrationsAlertIntegrationId = async (args: UpdateApiV4OrganizationsOrganizationIdProjectsProjectIdAlertIntegrationsAlertIntegrationIdArgs, options?: UpdateApiV4OrganizationsOrganizationIdProjectsProjectIdAlertIntegrationsAlertIntegrationIdOptions) => { const {baseUrl = "https://cloudapi.cloud.couchbase.com", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/${encodeURIComponent(String(args.organizationId))}/projects/${encodeURIComponent(String(args.projectId))}/alertIntegrations/${encodeURIComponent(String(args.alertIntegrationId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(getAlertResponse, await res.json()) };