import {z} from 'zod' export type GetApiNotificationsChannelsArgs = {filter?: Array | undefined, fields?: Array | undefined, skip?: unknown | undefined, sort?: Array | undefined, limit?: unknown | undefined}; export type GetApiNotificationsChannelsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jumpcloudNotificationsEmailRecipientConfigRole = z.object({objectId: z.string().optional(), roleObjectId: z.string().optional()}); export const jumpcloudNotificationsEmailRecipientConfigUser = z.object({address: z.string().optional(), name: z.string().optional(), objectId: z.string().optional()}); export const jumpcloudNotificationsEmailRecipientConfig = z.object({role: z.array(jumpcloudNotificationsEmailRecipientConfigRole).optional(), user: z.array(jumpcloudNotificationsEmailRecipientConfigUser).optional()}); export const jumpcloudNotificationsSlackChannelConfig = z.object({objectId: z.string().optional(), slackChannelId: z.string().optional(), slackTeamId: z.string().optional(), slackWorkspaceName: z.string().optional()}); export const jumpcloudNotificationsSlackConfig = z.object({slackChannel: z.array(jumpcloudNotificationsSlackChannelConfig).optional()}); export const jumpcloudNotificationsWebhookAuthType = z.enum(["WEBHOOK_AUTH_TYPE_UNSPECIFIED", "WEBHOOK_AUTH_TYPE_BASIC", "WEBHOOK_AUTH_TYPE_BEARER"]); export const jumpcloudNotificationsCustomHeaders = z.object({key: z.string().optional(), masked: z.boolean().optional(), objectId: z.string().optional(), value: z.string().optional()}); export const jumpcloudNotificationsMaskedURLPortion = z.object({key: z.string().optional(), objectId: z.string().optional(), value: z.string().optional()}); export const jumpcloudNotificationsWebhookConfig = z.object({authPassword: z.string().optional(), authToken: z.string().optional(), authType: jumpcloudNotificationsWebhookAuthType.optional(), authUsername: z.string().optional(), customHeaders: z.array(jumpcloudNotificationsCustomHeaders).optional(), maskedUrlPortion: z.array(jumpcloudNotificationsMaskedURLPortion).optional(), objectId: z.string().optional(), sslVerification: z.boolean().optional(), url: z.string().optional(), urlMasked: z.boolean().optional()}); export const jumpcloudNotificationsChannelTypeConfig = z.object({email: jumpcloudNotificationsEmailRecipientConfig.optional(), slack: jumpcloudNotificationsSlackConfig.optional(), webhook: jumpcloudNotificationsWebhookConfig.optional()}); export const jumpcloudNotificationsChannelType = z.enum(["CHANNEL_TYPE_UNSPECIFIED", "CHANNEL_TYPE_EMAIL", "CHANNEL_TYPE_WEBHOOK", "CHANNEL_TYPE_SLACK"]); export const jumpcloudNotificationsChannel = z.object({config: jumpcloudNotificationsChannelTypeConfig.optional(), createdAt: z.string().optional(), createdBy: z.string().optional(), description: z.string().optional(), enabled: z.boolean().optional(), name: z.string().optional(), objectId: z.string().optional(), organizationObjectId: z.string().optional(), type: jumpcloudNotificationsChannelType.optional(), updatedAt: z.string().optional(), updatedBy: z.string().optional()}); export const jumpcloudNotificationsListChannelsResponse = z.object({channels: z.array(jumpcloudNotificationsChannel).optional(), count: z.number().int().optional()}); 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 }; /** * This endpoint is used for listing the notification channels. * * GET /notifications/channels */ export const getApiNotificationsChannels = async (args: GetApiNotificationsChannelsArgs, options?: GetApiNotificationsChannelsOptions) => { const {baseUrl = "https://console.jumpcloud.com/api/v2", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.filter ?? []) query.append("filter", String(item)) for (const item of args.fields ?? []) query.append("fields", String(item)) if (args.skip != null) query.append("skip", String(args.skip)) for (const item of args.sort ?? []) query.append("sort", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/notifications/channels${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(jumpcloudNotificationsListChannelsResponse, await res.json()) };