import {z} from 'zod' export type UpdateApiUsernameDashboardsDashboardIdBlocksIdArgs = {username: string, dashboard_id: string, id: string, body: {name?: string | undefined, description?: string | undefined, key?: string | undefined, dashboard_id?: number | undefined, visual_type?: string | undefined, column?: number | undefined, row?: number | undefined, size_x?: number | undefined, size_y?: number | undefined, block_feeds?: (Array<{feed_id?: string | undefined, group_id?: string | undefined}>) | undefined, properties?: Record | undefined}}; export type UpdateApiUsernameDashboardsDashboardIdBlocksIdOptions = {baseUrl?: string | undefined, HeaderKey?: string | undefined, fetch?: typeof fetch | undefined}; export const shallowGroup = z.object({id: z.number().optional(), name: z.string().optional(), description: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const data = z.object({id: z.string().optional(), value: z.string().optional(), feed_id: z.number().optional(), group_id: z.number().optional(), expiration: z.string().optional(), lat: z.number().optional(), lon: z.number().optional(), ele: z.number().optional(), completed_at: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), created_epoch: z.number().optional()}); export const feed = z.object({id: z.number().optional(), name: z.string().optional(), key: z.string().optional(), group: z.record(z.string(), shallowGroup).optional(), groups: z.array(shallowGroup).optional(), description: z.string().optional(), details: z.object({shared_with: z.array(z.object({})).optional(), data: z.object({first: z.record(z.string(), data).optional(), last: z.record(z.string(), data).optional(), count: z.number().int().optional()}).optional()}).optional(), unit_type: z.string().optional(), unit_symbol: z.string().optional(), history: z.boolean().optional(), visibility: z.enum(["private", "public"]).optional(), license: z.string().optional(), enabled: z.boolean().optional(), last_value: z.string().optional(), status: z.string().optional(), status_notify: z.boolean().optional(), status_timeout: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const group = z.object({id: z.number().optional(), name: z.string().optional(), description: z.string().optional(), feeds: z.array(feed).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const blockFeed = z.object({id: z.string().optional(), feed: feed.optional(), group: group.optional()}); export const block = z.object({name: z.string().optional(), description: z.string().optional(), key: z.string().optional(), visual_type: z.string().optional(), column: z.number().optional(), row: z.number().optional(), size_x: z.number().optional(), size_y: z.number().optional(), block_feeds: z.array(blockFeed).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 }; /** * Replace an existing Block * * PUT /{username}/dashboards/{dashboard_id}/blocks/{id} * * Also accepts auth: HeaderSignature, QueryKey */ export const updateApiUsernameDashboardsDashboardIdBlocksId = async (args: UpdateApiUsernameDashboardsDashboardIdBlocksIdArgs, options?: UpdateApiUsernameDashboardsDashboardIdBlocksIdOptions) => { const {baseUrl = "https://io.adafruit.com/api/v2", fetch: fetchFn = fetch, HeaderKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HeaderKey !== undefined) headers.set("X-AIO-Key", HeaderKey) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.username))}/dashboards/${encodeURIComponent(String(args.dashboard_id))}/blocks/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(block, await res.json()) };