import {z} from 'zod' export type GetApiDeviceprofilesArgs = {Authorization: unknown, 'X-ST-Organization'?: unknown | undefined, profileId?: Array | undefined}; export type GetApiDeviceprofilesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const deviceProfileMetadata = z.unknown(); export const deviceRestriction = z.object({tier: z.number().int(), historyRetentionTTLDays: z.number().int().optional(), visibleWhenRestricted: z.boolean().optional()}); export const deviceProfileStatus = z.enum(["DEVELOPMENT", "PUBLISHED"]); export const preferenceType = z.enum(["integer", "number", "boolean", "string", "enumeration"]); export const preferenceResponse = z.object({preferenceId: z.string().optional(), name: z.string().optional(), title: z.string(), description: z.string().optional(), required: z.boolean().optional(), explicit: z.boolean().optional(), preferenceType: preferenceType}); export const capabilityConfigurationValue = z.object({key: z.string(), range: z.array(z.number()).optional(), enabledValues: z.array(z.string()).optional(), step: z.number().optional()}); export const capabilityConfiguration = z.object({values: z.array(capabilityConfigurationValue)}); export const restriction = z.object({tier: z.number().int(), historyRetentionTTLDays: z.number().int().optional(), visibleWhenRestricted: z.boolean().optional()}); export const attributeState = z.object({value: z.object({}).optional(), unit: z.string().optional(), data: z.record(z.string(), z.object({})).optional(), timestamp: z.string().optional()}); export const capabilityStatus = z.record(z.string(), attributeState); export const deviceCapabilityReference = z.object({id: z.string(), version: z.number().int().optional(), optional: z.boolean().optional(), config: capabilityConfiguration.optional(), restrictions: restriction.optional(), ephemeral: z.boolean().optional(), status: capabilityStatus.optional()}); export const deviceCategory = z.object({name: z.string(), categoryType: z.enum(["manufacturer", "user"])}); export const deviceComponent = z.object({id: z.string(), label: z.string().optional(), capabilities: z.array(deviceCapabilityReference), categories: z.array(deviceCategory), restrictions: restriction.optional(), optional: z.boolean().optional()}); export const deviceProfileResponse = z.object({id: z.string(), name: z.string(), metadata: deviceProfileMetadata.optional(), restrictions: deviceRestriction.optional(), presentationId: z.string().optional(), migrationStatus: z.enum(["MIGRATED", "NOT_MIGRATED"]), status: deviceProfileStatus, preferences: z.array(preferenceResponse).optional(), components: z.array(deviceComponent)}); export const link = z.object({href: z.string().optional()}); export const links = z.object({next: link.optional(), previous: link.optional()}); export const pagedDeviceProfiles = z.object({items: z.array(deviceProfileResponse).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 all Device Profiles for a User * * GET /deviceprofiles */ export const getApiDeviceprofiles = async (args: GetApiDeviceprofilesArgs, options?: GetApiDeviceprofilesOptions) => { 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['X-ST-Organization'] != null) headers.set("X-ST-Organization", String(args['X-ST-Organization'])) for (const item of args.profileId ?? []) query.append("profileId", String(item)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/deviceprofiles${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(pagedDeviceProfiles, await res.json()) };