import {z} from 'zod' export type GetApiApiDomainInfosArgs = {pageSize: number, page: number, textSearch?: string | undefined, sortProperty?: string | undefined, sortOrder?: string | undefined}; export type GetApiApiDomainInfosOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const domainId = z.object({id: z.string(), entityType: z.enum(["TENANT", "CUSTOMER", "USER", "DASHBOARD", "ASSET", "DEVICE", "ALARM", "RULE_CHAIN", "RULE_NODE", "ENTITY_VIEW", "WIDGETS_BUNDLE", "WIDGET_TYPE", "TENANT_PROFILE", "DEVICE_PROFILE", "ASSET_PROFILE", "API_USAGE_STATE", "TB_RESOURCE", "OTA_PACKAGE", "EDGE", "RPC", "QUEUE", "NOTIFICATION_TARGET", "NOTIFICATION_TEMPLATE", "NOTIFICATION_REQUEST", "NOTIFICATION", "NOTIFICATION_RULE", "QUEUE_STATS", "OAUTH2_CLIENT", "DOMAIN", "MOBILE_APP", "MOBILE_APP_BUNDLE", "CALCULATED_FIELD", "JOB", "ADMIN_SETTINGS", "AI_MODEL", "API_KEY"])}); export const tenantId = z.object({id: z.string(), entityType: z.literal("TENANT")}); export const oAuth2ClientId = z.object({id: z.string(), entityType: z.enum(["TENANT", "CUSTOMER", "USER", "DASHBOARD", "ASSET", "DEVICE", "ALARM", "RULE_CHAIN", "RULE_NODE", "ENTITY_VIEW", "WIDGETS_BUNDLE", "WIDGET_TYPE", "TENANT_PROFILE", "DEVICE_PROFILE", "ASSET_PROFILE", "API_USAGE_STATE", "TB_RESOURCE", "OTA_PACKAGE", "EDGE", "RPC", "QUEUE", "NOTIFICATION_TARGET", "NOTIFICATION_TEMPLATE", "NOTIFICATION_REQUEST", "NOTIFICATION", "NOTIFICATION_RULE", "QUEUE_STATS", "OAUTH2_CLIENT", "DOMAIN", "MOBILE_APP", "MOBILE_APP_BUNDLE", "CALCULATED_FIELD", "JOB", "ADMIN_SETTINGS", "AI_MODEL", "API_KEY"])}); export const oAuth2ClientInfo = z.object({id: oAuth2ClientId.optional(), createdTime: z.number().int().optional(), title: z.string().optional(), providerName: z.string().optional(), platforms: z.array(z.enum(["WEB", "ANDROID", "IOS"])).optional(), name: z.string().optional()}); export const domainInfo = z.object({id: domainId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), name: z.string().min(1), oauth2Enabled: z.boolean().optional(), propagateToEdge: z.boolean().optional(), oauth2ClientInfos: z.array(oAuth2ClientInfo).optional()}); export const pageDataDomainInfo = z.object({data: z.array(domainInfo).optional(), totalPages: z.number().int().optional(), totalElements: z.number().int().optional(), hasNext: z.boolean().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 Domain infos (getTenantDomainInfos) * * GET /api/domain/infos */ export const getApiApiDomainInfos = async (args: GetApiApiDomainInfosArgs, options?: GetApiApiDomainInfosOptions) => { 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.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.textSearch != null) query.append("textSearch", String(args.textSearch)) if (args.sortProperty != null) query.append("sortProperty", String(args.sortProperty)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) const url = `${baseUrl.replace(/\/+$/, '')}/api/domain/infos${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(pageDataDomainInfo, await res.json()) };