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