import {z} from 'zod' export type GetApiV2ViewsViewIdArgs = {'cloudzero-idempotency-key'?: string | undefined, 'cloudzero-test-key'?: string | undefined, view_id: string}; export type GetApiV2ViewsViewIdOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const viewFilter = z.record(z.string(), z.array(z.string())); export const viewConnectionsEmail = z.object({addresses: z.array(z.string()).optional(), include_all_organizers: z.boolean().optional()}); export const viewConnectionsSlack = z.object({id: z.string(), name: z.string()}); export const viewConnections = z.object({email: viewConnectionsEmail.optional(), slack: z.array(viewConnectionsSlack).optional()}); export const viewPrincipalDimension = z.string(); export const viewAnomaliesThresholdType = z.enum(["Automatic", "Percent"]); export const viewAnomaliesThresholdAmount = z.number().int(); export const viewAnomalies = z.object({enabled: z.boolean().optional(), threshold_type: viewAnomaliesThresholdType.optional(), threshold_amount: viewAnomaliesThresholdAmount.optional(), min_cost_impact: z.number().gte(0).optional()}); export const epoch = z.string(); export const view = z.object({name: z.string(), filter: viewFilter, connections: viewConnections, principal_dimension: viewPrincipalDimension, anomalies: viewAnomalies.optional(), notifications: z.object({weekly: z.boolean(), monthly: z.boolean()}).optional(), id: z.string(), last_updated: epoch}); export const getApiV2ViewsViewIdResponse = z.object({view: view.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 }; /** * Get a single View * * GET /v2/views/{view_id} */ export const getApiV2ViewsViewId = async (args: GetApiV2ViewsViewIdArgs, options?: GetApiV2ViewsViewIdOptions) => { const {baseUrl = "https://api.cloudzero.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['cloudzero-idempotency-key'] != null) headers.set("cloudzero-idempotency-key", String(args['cloudzero-idempotency-key'])) if (args['cloudzero-test-key'] != null) headers.set("cloudzero-test-key", String(args['cloudzero-test-key'])) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/views/${encodeURIComponent(String(args.view_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(getApiV2ViewsViewIdResponse, await res.json()) };