import {z} from 'zod' export type GetApiDevicepreferencesArgs = {namespace?: string | undefined, pageSize?: number | undefined, startKey?: string | undefined}; export type GetApiDevicepreferencesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; 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 link = z.object({href: z.string().optional()}); export const links = z.object({next: link.optional(), previous: link.optional()}); export const pagedPreferences = z.object({items: z.array(preferenceResponse).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 Preferences * * GET /devicepreferences */ export const getApiDevicepreferences = async (args: GetApiDevicepreferencesArgs, options?: GetApiDevicepreferencesOptions) => { 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.namespace != null) query.append("namespace", String(args.namespace)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.startKey != null) query.append("startKey", String(args.startKey)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/devicepreferences${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(pagedPreferences, await res.json()) };