import {z} from 'zod' export type RoleNew = {name: string, description?: (string | null) | undefined, permission_set_ids: Array, data_access_filter?: ({filter_type?: ('NO_ACCESS' | 'LIMITED_ACCESS' | 'FULL_ACCESS') | undefined, filter_dimensions?: {[key: string]: Array} | undefined, dynamic_filter_dimensions?: ({[key: string]: string | Array} | null) | undefined, attribute_filters?: {[key: string]: Array} | undefined} | null) | undefined}; export type CreateApiV2RolesArgs = {'cloudzero-test-key'?: string | undefined, body: RoleNew}; export type CreateApiV2RolesOptions = {baseUrl?: string | undefined, APIKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const role = z.object({id: z.string(), name: z.string(), description: z.string().nullable().optional(), permission_set_ids: z.array(z.string()), data_access_filter: z.object({name: z.string().optional(), description: z.string().nullable().optional(), filter_type: z.string().optional(), filter_dimensions: z.record(z.string(), z.array(z.string())).optional(), dynamic_filter_dimensions: z.record(z.string(), z.union([z.string(), z.array(z.string())])).nullable().optional(), attribute_filters: z.record(z.string(), z.array(z.string())).optional()}).nullable().optional(), created_at: z.string().optional(), created_by: z.string().optional(), last_updated_at: z.string().optional(), last_updated_by: z.string().optional()}); export const createApiV2RolesResponse = z.object({role: role.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 Role * * POST /v2/roles */ export const createApiV2Roles = async (args: CreateApiV2RolesArgs, options?: CreateApiV2RolesOptions) => { 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-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/roles${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(createApiV2RolesResponse, await res.json()) };