import {z} from 'zod' export type GetApiDriversDriverIdVersionsVersionOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const driverId = z.string(); export const driverName = z.string(); export const packageKey = z.string(); export const deviceIntegrationProfileKey = z.object({id: z.string().optional(), majorVersion: z.number().int().optional()}); export const permissionAttributes = z.record(z.string(), z.object({})); export const driverPermission = z.object({name: z.string(), attributes: permissionAttributes}); export const driverPermissions = z.array(driverPermission); export const driverVersion = z.string(); export const fingerprintId = z.string(); export const fingerprintType = z.enum(["DTH", "ZIGBEE_GENERIC", "ZIGBEE_MANUFACTURER", "ZWAVE_MANUFACTURER", "ZWAVE_GENERIC"]); export const fingerprintDeviceLabel = z.string(); export const clientClusters = z.array(z.number().int().gte(0).lte(65535)); export const serverClusters = z.array(z.number().int().gte(0).lte(65535)); export const clusters = z.object({client: clientClusters.optional(), server: serverClusters.optional()}); export const deviceIdentifiers = z.array(z.number().int()); export const zigbeeProfiles = z.array(z.number().int()); export const zigbeeGenericFingerprint = z.object({clusters: clusters.optional(), deviceIdentifiers: deviceIdentifiers.optional(), zigbeeProfiles: zigbeeProfiles.optional(), deviceIntegrationProfileKey: deviceIntegrationProfileKey.optional()}); export const zigbeeManufacturer = z.string().max(32); export const zigbeeModel = z.string().max(32); export const zigbeeManufacturerFingerprint = z.object({manufacturer: zigbeeManufacturer.optional(), model: zigbeeModel.optional(), deviceIntegrationProfileKey: deviceIntegrationProfileKey.optional()}); export const zWaveManufacturerId = z.number().int().gte(0).lte(65535); export const zWaveProductId = z.number().int().gte(0).lte(65535); export const zWaveProductType = z.number().int().gte(0).lte(65535); export const zWaveManufacturerFingerprint = z.object({manufacturerId: zWaveManufacturerId.optional(), productId: zWaveProductId.optional(), productType: zWaveProductType, deviceIntegrationProfileKey: deviceIntegrationProfileKey.optional()}); export const zWaveGenericType = z.number().int().gte(0).lte(255); export const zWaveSpecificType = z.number().int().gte(0).lte(255); export const commandClasses = z.object({either: z.array(z.number().int().gte(0).lte(255)).optional(), controlled: z.array(z.number().int().gte(0).lte(255)).optional(), supported: z.array(z.number().int().gte(0).lte(255)).optional()}); export const zWaveGenericFingerprint = z.object({genericType: zWaveGenericType.optional(), specificType: z.array(zWaveSpecificType).optional(), commandClasses: commandClasses.optional(), deviceIntegrationProfileKey: deviceIntegrationProfileKey.optional()}); export const driverFingerprint = z.object({id: fingerprintId, type: fingerprintType, deviceLabel: fingerprintDeviceLabel.optional(), zigbeeGeneric: zigbeeGenericFingerprint.optional(), zigbeeManfacturer: zigbeeManufacturerFingerprint.optional(), zwaveManufacturer: zWaveManufacturerFingerprint.optional(), zwaveGeneric: zWaveGenericFingerprint.optional()}); export const driver = z.object({driverId: driverId, name: driverName, packageKey: packageKey, deviceIntegrationProfiles: z.array(deviceIntegrationProfileKey).optional(), permissions: driverPermissions.optional(), version: driverVersion.optional(), fingerprints: z.array(driverFingerprint).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 Driver Revision * * GET /drivers/{driverId}/versions/{version} */ export const getApiDriversDriverIdVersionsVersion = async (options?: GetApiDriversDriverIdVersionsVersionOptions) => { 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 (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/drivers/${encodeURIComponent(String(args.driverId))}/versions/${encodeURIComponent(String(args.version))}${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(driver, await res.json()) };