import {z} from 'zod' export type CostformationNamespaceSortKeys = 'last_updated' | 'fqn'; export type SortOrder = 'asc' | 'desc'; export type GetApiV2CostformationNamespaceArgs = {'cloudzero-idempotency-key'?: string | undefined, 'cloudzero-test-key'?: string | undefined, limit?: number | undefined, cursor?: string | undefined, sort_key?: CostformationNamespaceSortKeys | undefined, sort_order?: SortOrder | undefined}; export type GetApiV2CostformationNamespaceOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const namespaceBody = z.object({id: z.string(), view_role_ids: z.array(z.string()).nullable().optional(), edit_role_ids: z.array(z.string()).nullable().optional(), display_name: z.string().nullable().optional()}); export const getApiV2CostformationNamespaceResponse = z.object({namespaces: z.array(namespaceBody).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 all namespaces * * GET /v2/costformation/namespace */ export const getApiV2CostformationNamespace = async (args: GetApiV2CostformationNamespaceArgs, options?: GetApiV2CostformationNamespaceOptions) => { 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 (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.sort_key != null) query.append("sort_key", String(args.sort_key)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (APIKeyAuth !== undefined) headers.set("Authorization", APIKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v2/costformation/namespace${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(getApiV2CostformationNamespaceResponse, await res.json()) };