import {z} from 'zod' export type GetApiUaasAnalyticsConnectorOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const nacRunHistoryDto = z.object({error_count: z.number().int().optional(), run_id: z.string(), unifx_version: z.string(), start_timestamp: z.string(), destination_type: z.string(), failed_file_count: z.number().int().optional(), elapsed_time: z.number().int(), source_type: z.string(), exported_files: z.number().int(), average_throughput: z.number().int(), end_time: z.number().int(), volume_name: z.string(), percent_complete: z.number().int(), exported_dirs: z.number().int(), volume_guid: z.string(), exported_bytes: z.number().int()}); export const analyticsConnectorDto = z.object({key: z.string(), nac_link: z.string(), launchable: z.boolean(), unlicensed: z.boolean(), nac_run_history: z.array(nacRunHistoryDto).nullable(), can_generate: z.boolean(), can_view: z.boolean(), unifx_aws_ids: z.string()}); 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 Analytics Connector Information * * GET /uaas/analytics_connector */ export const getApiUaasAnalyticsConnector = async (options?: GetApiUaasAnalyticsConnectorOptions) => { const {baseUrl = "https://am1.portal.api.nasuni.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/uaas/analytics_connector${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(analyticsConnectorDto, await res.json()) };