import {z} from 'zod' export type GetApiApiWidgetTypeArgs = {isSystem: boolean, bundleAlias: string, alias: string, fqn: string}; export type GetApiApiWidgetTypeOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; 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 jsonNode = z.unknown(); export const widgetType = z.object({id: widgetTypeId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), fqn: z.string().optional(), name: z.string().optional(), deprecated: z.boolean().optional(), scada: z.boolean().optional(), version: z.number().int().optional(), descriptor: jsonNode.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 }; /** * Get Widget Type (getWidgetType) * * GET /api/widgetType */ export const getApiApiWidgetType = async (args: GetApiApiWidgetTypeArgs, options?: GetApiApiWidgetTypeOptions) => { const {baseUrl = "https://demo.thingsboard.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.isSystem != null) query.append("isSystem", String(args.isSystem)) if (args.bundleAlias != null) query.append("bundleAlias", String(args.bundleAlias)) if (args.alias != null) query.append("alias", String(args.alias)) if (args.fqn != null) query.append("fqn", String(args.fqn)) const url = `${baseUrl.replace(/\/+$/, '')}/api/widgetType${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(widgetType, await res.json()) };