import {z} from 'zod' export type GetApiItemsItemnameArgs = {'Accept-Language'?: string | undefined, metadata?: string | undefined, recursive?: boolean | undefined, itemname: string}; export type GetApiItemsItemnameOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const stateOption = z.object({value: z.string().optional(), label: z.string().optional()}); export const stateDescription = z.object({minimum: z.number().optional(), maximum: z.number().optional(), step: z.number().optional(), pattern: z.string().optional(), readOnly: z.boolean().optional(), options: z.array(stateOption).optional()}); export const commandOption = z.object({command: z.string().optional(), label: z.string().optional()}); export const commandDescription = z.object({commandOptions: z.array(commandOption).optional()}); export const enrichedItemDTO = z.object({type: z.string().optional(), name: z.string().optional(), label: z.string().optional(), category: z.string().optional(), tags: z.array(z.string()).optional(), groupNames: z.array(z.string()).optional(), link: z.string().optional(), state: z.string().optional(), transformedState: z.string().optional(), stateDescription: stateDescription.optional(), unitSymbol: z.string().optional(), commandDescription: commandDescription.optional(), metadata: z.record(z.string(), z.object({})).optional(), editable: z.boolean().optional(), groupType: 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 }; /** * Gets a single item. * * GET /items/{itemname} */ export const getApiItemsItemname = async (args: GetApiItemsItemnameArgs, options: GetApiItemsItemnameOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Accept-Language'] != null) headers.set("Accept-Language", String(args['Accept-Language'])) if (args.metadata != null) query.append("metadata", String(args.metadata)) if (args.recursive != null) query.append("recursive", String(args.recursive)) const url = `${baseUrl.replace(/\/+$/, '')}/items/${encodeURIComponent(String(args.itemname))}${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(enrichedItemDTO, await res.json()) };