import {z} from 'zod' export type PatchLocationRequest = {latitude?: ({value?: number | undefined, toNull?: boolean | undefined}) | undefined, longitude?: ({value?: number | undefined, toNull?: boolean | undefined}) | undefined, regionRadius?: ({value?: number | undefined, toNull?: boolean | undefined}) | undefined}; export type PatchApiLocationsLocationIdArgs = {body: PatchLocationRequest}; export type PatchApiLocationsLocationIdOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const locationParentTypes = z.enum(["LOCATIONGROUP", "ACCOUNT"]).min(7).max(14); export const locationParent = z.object({type: locationParentTypes.optional(), id: z.string().min(36).max(36).optional()}); export const updateLocationResponse = z.object({locationId: z.string(), name: z.string(), countryCode: z.string(), latitude: z.number().optional(), longitude: z.number().optional(), regionRadius: z.number().int().optional(), temperatureScale: z.string().optional(), timeZoneId: z.string().optional(), locale: z.string().optional(), backgroundImage: z.string().optional(), additionalProperties: z.record(z.string(), z.string()).optional(), parent: locationParent.optional(), created: z.string().optional(), lastModified: z.string().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 }; /** * Patch a location * * PATCH /locations/{locationId} */ export const patchApiLocationsLocationId = async (args: PatchApiLocationsLocationIdArgs, options?: PatchApiLocationsLocationIdOptions) => { 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 (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/locations/${encodeURIComponent(String(args.locationId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(updateLocationResponse, await res.json()) };