import {z} from 'zod' export type CubeQueryFilterSchema = {member?: (string | null) | undefined, dimension?: (string | null) | undefined, measure?: (string | null) | undefined, operator?: ('equals' | 'notEquals' | 'contains' | 'notContains' | 'gt' | 'gte' | 'lt' | 'lte' | 'set' | 'notSet' | 'inDateRange' | 'notInDateRange' | 'beforeDate' | 'afterDate' | 'startsWith' | 'endsWith') | undefined, values?: (Array | null) | undefined}; export type CubeQueryTimeDimensionSchema = {dimension: string, dateRange?: (string | Array | null) | undefined, compareDateRange?: (Array> | null) | undefined, granularity?: (string | null) | undefined}; export type CubeQueryOrderSchema = {id: string, desc?: boolean | undefined}; export type CubeQuerySchema = {dimensions?: (Array | null) | undefined, measures?: (Array | null) | undefined, filters?: (Array | null) | undefined, timeDimensions?: (Array | null) | undefined, segments?: (Array | null) | undefined, order?: (Array | null) | undefined, limit?: (number | null) | undefined, offset?: (number | null) | undefined, timezone?: (string | null) | undefined, ungrouped?: (boolean | null) | undefined}; export type SemanticModelQueryRequest = {query: CubeQuerySchema}; export type CreateApiV3TeamsTeamIdIntegrationsIntegrationIdSemanticModelsQueryArgs = {team_id: string, integration_id: string, body: SemanticModelQueryRequest}; export type CreateApiV3TeamsTeamIdIntegrationsIntegrationIdSemanticModelsQueryOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const semanticModelQueryResponse = z.object({sql: z.array(z.unknown()), aliasNameToMember: z.record(z.string(), z.unknown()).nullable().optional(), memberNames: z.array(z.string()).nullable().optional(), cacheKeyQueries: z.array(z.unknown()).nullable().optional(), canUseTransformedQuery: z.record(z.string(), z.unknown()).nullable().optional(), dataSource: z.string().nullable().optional(), external: z.boolean().nullable().optional(), lambdaQueries: z.record(z.string(), z.unknown()).nullable().optional(), order: z.record(z.string(), z.unknown()).nullable().optional(), preAggregations: z.array(z.unknown()).nullable().optional(), rollupMatchResults: z.array(z.unknown()).nullable().optional(), raw_schema: z.record(z.string(), z.unknown()).nullable().optional(), execution_time_ms: z.number().nullable().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 }; /** * Execute a query against a semantic model * * POST /v3/teams/{team_id}/integrations/{integration_id}/semantic-models/query */ export const createApiV3TeamsTeamIdIntegrationsIntegrationIdSemanticModelsQuery = async (args: CreateApiV3TeamsTeamIdIntegrationsIntegrationIdSemanticModelsQueryArgs, options: CreateApiV3TeamsTeamIdIntegrationsIntegrationIdSemanticModelsQueryOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v3/teams/${encodeURIComponent(String(args.team_id))}/integrations/${encodeURIComponent(String(args.integration_id))}/semantic-models/query${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(semanticModelQueryResponse, await res.json()) };