import {z} from 'zod' export type CreateApiApiV1AnalyticsQueryExportArgs = {body: {query: string, settings?: ({delimiter?: string | undefined, escapeSpecialCharacters?: boolean | undefined, disableQuotes?: boolean | undefined, exportFormat?: ('csv' | 'xlsx' | 'json' | 'txt' | 'parquet' | 'pdf') | undefined, numberFormattingMode?: ('locale' | 'raw') | undefined} | null) | undefined, parameters?: (Array<{name: string, type: string, defaultValue: string, valueMap?: Array<{label: string, value: string}> | undefined}> | null) | undefined}}; export type CreateApiApiV1AnalyticsQueryExportOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1AnalyticsQueryExportResponse = z.object({data: z.array(z.object({id: z.string(), status: z.string(), query: z.string(), url: z.string(), error: z.string(), exportFormat: z.string().optional(), createdAt: z.string(), updatedAt: z.string()})).nullable()}); 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 query export * * POST /api/v1/analytics/query/export */ export const createApiApiV1AnalyticsQueryExport = async (args: CreateApiApiV1AnalyticsQueryExportArgs, options?: CreateApiApiV1AnalyticsQueryExportOptions) => { const {baseUrl = "https://xentral.xentral.biz", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/analytics/query/export${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(createApiApiV1AnalyticsQueryExportResponse, await res.json()) };