import {z} from 'zod' export type NotificationTargetId = {id: string, entityType: 'NOTIFICATION_TARGET'}; export type TenantId = {id: string, entityType: 'TENANT'}; export type MicrosoftTeamsNotificationTargetConfig = {description?: string | undefined, type: string, webhookUrl: string, channelName: string, useOldApi?: boolean | undefined, id: unknown, title?: string | undefined, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined}; export type AffectedTenantAdministratorsFilter = {type: string}; export type AffectedUserFilter = {type: string}; export type AllUsersFilter = {type: string}; export type CustomerUsersFilter = {type: string, customerId: string}; export type OriginatorEntityOwnerUsersFilter = {type: string}; export type SystemAdministratorsFilter = {type: string}; export type TenantAdministratorsFilter = {type: string, tenantsIds?: Array | undefined, tenantProfilesIds?: Array | undefined}; export type UserListFilter = {type: string, usersIds: Array}; export type PlatformUsersNotificationTargetConfig = {description?: string | undefined, type: string, usersFilter: AffectedTenantAdministratorsFilter | AffectedUserFilter | AllUsersFilter | CustomerUsersFilter | OriginatorEntityOwnerUsersFilter | SystemAdministratorsFilter | TenantAdministratorsFilter | UserListFilter}; export type SlackConversation = {type: 'DIRECT' | 'PUBLIC_CHANNEL' | 'PRIVATE_CHANNEL', id: string, name: string, wholeName?: string | undefined, email?: string | undefined, title?: string | undefined}; export type SlackNotificationTargetConfig = {description?: string | undefined, type: string, conversationType?: ('DIRECT' | 'PUBLIC_CHANNEL' | 'PRIVATE_CHANNEL') | undefined, conversation: SlackConversation}; export type NotificationTarget = {id?: NotificationTargetId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, name: string, configuration: MicrosoftTeamsNotificationTargetConfig | PlatformUsersNotificationTargetConfig | SlackNotificationTargetConfig}; export type CreateApiApiNotificationTargetArgs = {body: NotificationTarget}; export type CreateApiApiNotificationTargetOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const notificationTargetId = z.object({id: z.string(), entityType: z.literal("NOTIFICATION_TARGET")}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const microsoftTeamsNotificationTargetConfig = z.object({description: z.string().optional(), type: z.string(), webhookUrl: z.string().min(1), channelName: z.string().min(1), useOldApi: z.boolean().optional(), id: z.unknown(), title: z.string().optional(), email: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional()}); export const affectedTenantAdministratorsFilter = z.object({type: z.string()}); export const affectedUserFilter = z.object({type: z.string()}); export const allUsersFilter = z.object({type: z.string()}); export const customerUsersFilter = z.object({type: z.string(), customerId: z.string()}); export const originatorEntityOwnerUsersFilter = z.object({type: z.string()}); export const systemAdministratorsFilter = z.object({type: z.string()}); export const tenantAdministratorsFilter = z.object({type: z.string(), tenantsIds: z.array(z.string()).optional(), tenantProfilesIds: z.array(z.string()).optional()}); export const userListFilter = z.object({type: z.string(), usersIds: z.array(z.string())}); export const platformUsersNotificationTargetConfig = z.object({description: z.string().optional(), type: z.string(), usersFilter: z.union([affectedTenantAdministratorsFilter, affectedUserFilter, allUsersFilter, customerUsersFilter, originatorEntityOwnerUsersFilter, systemAdministratorsFilter, tenantAdministratorsFilter, userListFilter])}); export const slackConversation = z.object({type: z.enum(["DIRECT", "PUBLIC_CHANNEL", "PRIVATE_CHANNEL"]), id: z.string().min(1), name: z.string().min(1), wholeName: z.string().optional(), email: z.string().optional(), title: z.string().optional()}); export const slackNotificationTargetConfig = z.object({description: z.string().optional(), type: z.string(), conversationType: z.enum(["DIRECT", "PUBLIC_CHANNEL", "PRIVATE_CHANNEL"]).optional(), conversation: slackConversation}); export const notificationTarget = z.object({id: notificationTargetId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), name: z.string().min(1), configuration: z.union([microsoftTeamsNotificationTargetConfig, platformUsersNotificationTargetConfig, slackNotificationTargetConfig])}); 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 }; /** * Save notification target (saveNotificationTarget) * * POST /api/notification/target */ export const createApiApiNotificationTarget = async (args: CreateApiApiNotificationTargetArgs, options?: CreateApiApiNotificationTargetOptions) => { const {baseUrl = "https://demo.thingsboard.io", 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/notification/target${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(notificationTarget, await res.json()) };