import {z} from 'zod' export type MobileAppId = {id: string, entityType: '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 type TenantId = {id: string, entityType: 'TENANT'}; export type MobileAppVersionInfo = {minVersion?: string | undefined, minVersionReleaseNotes?: string | undefined, latestVersion?: string | undefined, latestVersionReleaseNotes?: string | undefined}; export type StoreInfo = {appId?: string | undefined, sha256CertFingerprints?: string | undefined, storeLink?: string | undefined}; export type MobileApp = {id?: MobileAppId | undefined, createdTime?: number | undefined, tenantId?: TenantId | undefined, pkgName: string, title?: string | undefined, appSecret: string, platformType: 'WEB' | 'ANDROID' | 'IOS', status: 'DRAFT' | 'PUBLISHED' | 'DEPRECATED' | 'SUSPENDED', versionInfo?: MobileAppVersionInfo | undefined, storeInfo?: StoreInfo | undefined, name?: string | undefined}; export type CreateApiApiMobileAppArgs = {body: MobileApp}; export type CreateApiApiMobileAppOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const mobileAppId = 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 mobileAppVersionInfo = z.object({minVersion: z.string().optional(), minVersionReleaseNotes: z.string().optional(), latestVersion: z.string().optional(), latestVersionReleaseNotes: z.string().optional()}); export const storeInfo = z.object({appId: z.string().optional(), sha256CertFingerprints: z.string().optional(), storeLink: z.string().optional()}); export const mobileApp = z.object({id: mobileAppId.optional(), createdTime: z.number().int().optional(), tenantId: tenantId.optional(), pkgName: z.string().min(1), title: z.string().optional(), appSecret: z.string().min(1), platformType: z.enum(["WEB", "ANDROID", "IOS"]), status: z.enum(["DRAFT", "PUBLISHED", "DEPRECATED", "SUSPENDED"]), versionInfo: mobileAppVersionInfo.optional(), storeInfo: storeInfo.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 }; /** * Save Or update Mobile app (saveMobileApp) * * POST /api/mobile/app */ export const createApiApiMobileApp = async (args: CreateApiApiMobileAppArgs, options?: CreateApiApiMobileAppOptions) => { 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/mobile/app${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(mobileApp, await res.json()) };