import {z} from 'zod' export type CreateApiThermostatsDailyProgramsCreateArgs = {body: {device_id: string, name: string, periods: Array<{climate_preset_key: string, starts_at_time: string}>}}; export type CreateApiThermostatsDailyProgramsCreateOptions = {baseUrl?: string | undefined, clientSession?: string | undefined, fetch?: typeof fetch | undefined}; export const thermostatDailyProgram = z.object({created_at: z.string(), device_id: z.string(), name: z.string().nullable(), periods: z.array(z.object({climate_preset_key: z.string(), starts_at_time: z.string()})), thermostat_daily_program_id: z.string(), workspace_id: z.string()}); export const createApiThermostatsDailyProgramsCreateResponse = z.object({thermostat_daily_program: thermostatDailyProgram}); 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 }; /** * Create a Thermostat Daily Program * * POST /thermostats/daily_programs/create * * Also accepts auth: pat_with_workspace, console_session_with_workspace, api_key */ export const createApiThermostatsDailyProgramsCreate = async (args: CreateApiThermostatsDailyProgramsCreateArgs, options?: CreateApiThermostatsDailyProgramsCreateOptions) => { const {baseUrl = "https://connect.getseam.com", fetch: fetchFn = fetch, clientSession} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (clientSession !== undefined) headers.set('authorization', `Bearer ${clientSession}`) const url = `${baseUrl.replace(/\/+$/, '')}/thermostats/daily_programs/create${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiThermostatsDailyProgramsCreateResponse, await res.json()) };