import {z} from 'zod' export type GetApiV1ConfigScreenIdArgs = {id: string}; export type GetApiV1ConfigScreenIdOptions = {baseUrl?: string | undefined, PipelineServiceKey?: string | undefined, fetch?: typeof fetch | undefined}; export const screenResource = z.object({id: z.string(), account: z.string().optional(), title: z.string(), ratio: z.enum(["3:2", "4:3", "5:4", "16:9", "16:10", "21:9"]), rotate90Degrees: z.boolean(), background: z.enum(["dark", "light"]), categories: z.array(z.string()).optional(), widgets: z.array(z.object({id: z.string(), type: z.enum(["count", "gauge", "table", "time-shifted-graph"]), operation: z.enum(["count", "min", "max", "avg", "cum", "diff", "percentile-75", "percentile-85", "percentile-95", "percentile-99"]), duration: z.union([z.enum(["hourly-compare", "daily-compare", "weekly-compare", "iso-weekly-compare"]), z.enum(["hourly", "daily", "weekly", "iso-weekly", "1m", "30m", "1h", "2h", "4h", "8h", "12h", "1d", "2d", "4d", "1w", "2w"])]), field: z.string(), match: z.string().optional(), filter: z.string().optional(), label: z.string().optional(), position: z.object({start: z.array(z.unknown()), end: z.array(z.unknown())}), sort: z.enum(["asc", "desc"]).optional(), rows: z.union([z.literal(3), z.literal(5), z.literal(10), z.literal(15), z.literal(20), z.literal(25)]).optional(), leftLabel: z.string().optional(), rightLabel: z.string().optional(), showBarChart: z.boolean().optional(), groupBy: z.string().optional(), min: z.number().optional(), max: z.number().optional(), units: z.string().optional(), format: z.enum(["number-integer", "number-tenths", "number-ten-thousandths", "number-unformatted", "currency-prefix", "currency-postfix", "bytes-si", "percent-integer", "percent-thousandths", "time-HH:mm:ss"]).optional()})).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 }; /** * Retrieve a screen * * GET /v1/config/screen/{id} */ export const getApiV1ConfigScreenId = async (args: GetApiV1ConfigScreenIdArgs, options?: GetApiV1ConfigScreenIdOptions) => { const {baseUrl = "https://api.mezmo.com/v3", fetch: fetchFn = fetch, PipelineServiceKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (PipelineServiceKey !== undefined) headers.set("Authorization", PipelineServiceKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/config/screen/${encodeURIComponent(String(args.id))}${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(screenResource, await res.json()) };