import {z} from 'zod' export type Currency = string; export type CurrencyIn = Currency | number; export type InsightNew = {effort?: ('not_set' | 'low' | 'medium' | 'high') | undefined, cost_impact?: CurrencyIn | undefined, status?: ('new' | 'in_progress' | 'on_hold' | 'addressed' | 'ignored') | undefined, category?: string | undefined, link?: string | undefined, description?: string | undefined, title?: string | undefined}; export type CreateApiV2InsightsArgs = {'cloudzero-idempotency-key'?: string | undefined, 'cloudzero-test-key'?: string | undefined, body: InsightNew}; export type CreateApiV2InsightsOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const epoch = z.string(); export const insight = z.object({effort: z.enum(["not_set", "low", "medium", "high"]).optional(), cost_impact: z.string().optional(), status: z.enum(["new", "in_progress", "on_hold", "addressed", "ignored"]).optional(), category: z.string().optional(), link: z.string().optional(), description: z.string().optional(), title: z.string().optional(), id: z.string(), created: epoch, last_updated: epoch}); export const createApiV2InsightsResponse = z.object({insight: insight.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 }; /** * Create a new Insight * * POST /v2/insights */ export const createApiV2Insights = async (args: CreateApiV2InsightsArgs, options?: CreateApiV2InsightsOptions) => { const {baseUrl = "https://api.cloudzero.com", fetch: fetchFn = fetch, APIKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/insights${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(createApiV2InsightsResponse, await res.json()) };