import {z} from 'zod' export type JsonNode = unknown; export type ResourceExportData = {link?: string | undefined, title?: string | undefined, type?: ('LWM2M_MODEL' | 'JKS' | 'PKCS_12' | 'JS_MODULE' | 'IMAGE' | 'DASHBOARD' | 'GENERAL') | undefined, subType?: ('IMAGE' | 'SCADA_SYMBOL' | 'EXTENSION' | 'MODULE') | undefined, resourceKey?: string | undefined, fileName?: string | undefined, publicResourceKey?: string | undefined, isPublic?: boolean | undefined, mediaType?: string | undefined, data?: string | undefined, public?: boolean | undefined}; export type WidgetTypeId = {id: string, entityType: 'WIDGET_TYPE'}; export type TenantId = {id: string, entityType: 'TENANT'}; export type WidgetTypeDetails = {fqn?: string | undefined, name?: string | undefined, deprecated?: boolean | undefined, image?: string | undefined, description?: string | undefined, descriptor?: JsonNode | undefined, resources?: Array | undefined, id?: WidgetTypeId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, scada?: boolean | undefined, version?: number | undefined, tags?: Array | undefined}; export type CreateApiApiWidgetTypeArgs = {updateExistingByFqn?: boolean | undefined, body: WidgetTypeDetails}; export type CreateApiApiWidgetTypeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const jsonNode = z.unknown(); export const resourceExportData = z.object({link: z.string().optional(), title: z.string().optional(), type: z.enum(["LWM2M_MODEL", "JKS", "PKCS_12", "JS_MODULE", "IMAGE", "DASHBOARD", "GENERAL"]).optional(), subType: z.enum(["IMAGE", "SCADA_SYMBOL", "EXTENSION", "MODULE"]).optional(), resourceKey: z.string().optional(), fileName: z.string().optional(), publicResourceKey: z.string().optional(), isPublic: z.boolean().optional(), mediaType: z.string().optional(), data: z.string().optional(), public: z.boolean().optional()}); export const widgetTypeId = z.object({id: z.string(), entityType: z.literal("WIDGET_TYPE")}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const widgetTypeDetails = z.object({fqn: z.string().optional(), name: z.string().optional(), deprecated: z.boolean().optional(), image: z.string().optional(), description: z.string().optional(), descriptor: jsonNode.optional(), resources: z.array(resourceExportData).optional(), id: widgetTypeId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), scada: z.boolean().optional(), version: z.number().int().optional(), tags: z.array(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 Widget Type (saveWidgetType) * * POST /api/widgetType */ export const createApiApiWidgetType = async (args: CreateApiApiWidgetTypeArgs, options?: CreateApiApiWidgetTypeOptions) => { 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") if (args.updateExistingByFqn != null) query.append("updateExistingByFqn", String(args.updateExistingByFqn)) const url = `${baseUrl.replace(/\/+$/, '')}/api/widgetType${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(widgetTypeDetails, await res.json()) };