import {z} from 'zod' export type AlarmId = {id: string, entityType: 'ALARM'}; export type UserId = {id: string, entityType: 'USER'}; export type JsonNode = unknown; export type AlarmCommentId = {id: string}; export type AlarmComment = {alarmId?: AlarmId | undefined, userId?: UserId | undefined, type?: ('SYSTEM' | 'OTHER') | undefined, comment?: JsonNode | undefined, id?: AlarmCommentId | undefined, createdTime?: number | undefined, name?: string | undefined}; export type CreateApiApiAlarmAlarmIdCommentArgs = {alarmId: string, body: AlarmComment}; export type CreateApiApiAlarmAlarmIdCommentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const alarmId = z.object({id: z.string(), entityType: z.literal("ALARM")}); export const userId = z.object({id: z.string(), entityType: z.literal("USER")}); export const jsonNode = z.unknown(); export const alarmCommentId = z.object({id: z.string()}); export const alarmComment = z.object({alarmId: alarmId.optional(), userId: userId.optional(), type: z.enum(["SYSTEM", "OTHER"]).optional(), comment: jsonNode.optional(), id: alarmCommentId.optional(), createdTime: z.number().int().optional(), name: z.string().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 }; /** * Create or update Alarm Comment * * POST /api/alarm/{alarmId}/comment */ export const createApiApiAlarmAlarmIdComment = async (args: CreateApiApiAlarmAlarmIdCommentArgs, options?: CreateApiApiAlarmAlarmIdCommentOptions) => { 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/alarm/${encodeURIComponent(String(args.alarmId))}/comment${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(alarmComment, await res.json()) };