import {z} from 'zod' export type IconImage = {url?: string | undefined}; export type SmartAppType = 'API_ONLY'; export type AppClassification = 'AUTOMATION' | 'SERVICE' | 'DEVICE' | 'CONNECTED_SERVICE' | 'HIDDEN' | 'LABS'; export type CreateOrUpdateApiOnlyRequest = {targetUrl?: string | undefined, privacyPolicyUrl?: string | undefined}; export type AppUISettings = {pluginId?: unknown | undefined, pluginUri?: string | undefined, dashboardCardsEnabled: boolean, preInstallDashboardCardsEnabled: boolean}; export type UpdateSmartAppRequest = {displayName: string, description: string, singleInstance?: boolean | undefined, iconImage?: IconImage | undefined, appType: SmartAppType, classifications: Array, apiOnly?: CreateOrUpdateApiOnlyRequest | undefined, ui?: AppUISettings | undefined}; export type UpdateApiSmartappsAppNameOrIdArgs = {Authorization: unknown, appNameOrId: string, signatureType?: ('APP_RSA' | 'ST_PADLOCK') | undefined, requireConfirmation?: boolean | undefined, body: UpdateSmartAppRequest}; export type UpdateApiSmartappsAppNameOrIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const appType = z.enum(["LAMBDA_SMART_APP", "WEBHOOK_SMART_APP", "API_ONLY"]); export const principalType = z.enum(["LOCATION", "USER_LEVEL", "ACCOUNT_LEVEL"]); export const appClassification = z.enum(["AUTOMATION", "SERVICE", "DEVICE", "CONNECTED_SERVICE", "HIDDEN", "LABS"]); export const iconImage = z.object({url: z.string().optional()}); export const owner = z.object({ownerType: z.enum(["USER", "SYSTEM", "IMPLICIT", "ACCOUNT"]), ownerId: z.string()}); export const lambdaSmartApp = z.object({functions: z.array(z.string()).optional()}); export const appTargetStatus = z.enum(["PENDING", "CONFIRMED"]); export const signatureType = z.enum(["APP_RSA", "ST_PADLOCK"]); export const webhookSmartApp = z.object({targetUrl: z.string().optional(), targetStatus: appTargetStatus.optional(), publicKey: z.string().optional(), signatureType: signatureType.optional()}); export const appUISettings = z.object({pluginId: z.string().optional(), pluginUri: z.string().optional(), dashboardCardsEnabled: z.boolean(), preInstallDashboardCardsEnabled: z.boolean()}); export const app = z.object({appName: z.string().optional(), appId: z.string().optional(), appType: appType.optional(), principalType: principalType.optional(), classifications: z.array(appClassification).optional(), displayName: z.string().max(75).optional(), description: z.string().max(250).optional(), singleInstance: z.boolean().optional(), iconImage: iconImage.optional(), installMetadata: z.record(z.string(), z.string()).optional(), owner: owner.optional(), createdDate: z.string().optional(), lastUpdatedDate: z.string().optional(), lambdaSmartApp: lambdaSmartApp.optional(), webhookSmartApp: webhookSmartApp.optional(), ui: appUISettings.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 }; /** * Update an App * * PUT /smartapps/{appNameOrId} * * Also accepts auth: Bearer */ export const updateApiSmartappsAppNameOrId = async (args: UpdateApiSmartappsAppNameOrIdArgs, options?: UpdateApiSmartappsAppNameOrIdOptions) => { const {baseUrl = "https://api.smartthings.com/v1", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.signatureType != null) query.append("signatureType", String(args.signatureType)) if (args.requireConfirmation != null) query.append("requireConfirmation", String(args.requireConfirmation)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/smartapps/${encodeURIComponent(String(args.appNameOrId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(app, await res.json()) };