import {z} from 'zod' export type GetApiInstalledappsArgs = {Authorization: unknown, locationId?: unknown | undefined, installedAppStatus?: ('PENDING' | 'AUTHORIZED' | 'REVOKED' | 'DISABLED') | undefined, installedAppType?: ('LAMBDA_SMART_APP' | 'WEBHOOK_SMART_APP') | undefined, tag?: unknown | undefined, appId?: unknown | undefined, modeId?: unknown | undefined, deviceId?: unknown | undefined}; export type GetApiInstalledappsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const installedAppType = z.enum(["LAMBDA_SMART_APP", "WEBHOOK_SMART_APP"]); export const installedAppStatus = z.enum(["PENDING", "AUTHORIZED", "REVOKED", "DISABLED"]); export const owner = z.object({ownerType: z.enum(["USER", "SYSTEM", "IMPLICIT", "ACCOUNT"]), ownerId: z.string()}); export const noticeCode = z.enum(["USER_PAUSE", "REVISIT", "RECONFIGURE"]); export const noticeAction = z.enum(["DISMISS", "EDIT", "NONE"]); export const notice = z.object({code: noticeCode.optional(), badgeUrl: z.string().optional(), message: z.string().optional(), actions: z.array(noticeAction).optional()}); export const installedApp = z.object({installedAppId: z.string(), installedAppType: installedAppType, installedAppStatus: installedAppStatus, displayName: z.string().max(100).optional(), appId: z.string(), referenceId: z.string().optional(), locationId: z.string().optional(), owner: owner, notices: z.array(notice), createdDate: z.string(), lastUpdatedDate: z.string(), ui: z.object({pluginId: z.string().optional(), pluginUri: z.string().optional(), dashboardCardsEnabled: z.boolean(), preInstallDashboardCardsEnabled: z.boolean()}).optional(), iconImage: z.object({url: z.string().optional()}).optional(), classifications: z.array(z.enum(["AUTOMATION", "SERVICE", "DEVICE", "CONNECTED_SERVICE", "HIDDEN", "LABS"])), principalType: z.enum(["LOCATION", "USER_LEVEL", "ACCOUNT_LEVEL"]), restrictionTier: z.number().int().optional(), singleInstance: z.boolean(), allowed: z.array(z.string()).optional()}); export const link = z.object({href: z.string().optional()}); export const links = z.object({next: link.optional(), previous: link.optional()}); export const pagedInstalledApps = z.object({items: z.array(installedApp).optional(), _links: links.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 }; /** * List Installed Apps * * GET /installedapps */ export const getApiInstalledapps = async (args: GetApiInstalledappsArgs, options?: GetApiInstalledappsOptions) => { const {baseUrl = "https://api.smartthings.com/v1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.installedAppStatus != null) query.append("installedAppStatus", String(args.installedAppStatus)) if (args.installedAppType != null) query.append("installedAppType", String(args.installedAppType)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.appId != null) query.append("appId", String(args.appId)) if (args.modeId != null) query.append("modeId", String(args.modeId)) if (args.deviceId != null) query.append("deviceId", String(args.deviceId)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/installedapps${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(pagedInstalledApps, await res.json()) };