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 PatchApiV2InsightsInsightIdArgs = {'cloudzero-idempotency-key'?: string | undefined, 'cloudzero-test-key'?: string | undefined, insight_id: string, body: InsightNew}; export type PatchApiV2InsightsInsightIdOptions = {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 patchApiV2InsightsInsightIdResponse = 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 }; /** * Update a single Insight * * PATCH /v2/insights/{insight_id} */ export const patchApiV2InsightsInsightId = async (args: PatchApiV2InsightsInsightIdArgs, options?: PatchApiV2InsightsInsightIdOptions) => { 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/${encodeURIComponent(String(args.insight_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiV2InsightsInsightIdResponse, await res.json()) };