import {z} from 'zod' export type QueryOptions = {against?: string | undefined, againstStart?: string | undefined, againstTimestamp?: string | undefined, aggChartOpts?: string | undefined, caseSensitive?: string | undefined, containsTimeFilter?: string | undefined, datasets?: string | undefined, displayNull?: string | undefined, editorContent?: string | undefined, endColumn?: string | undefined, endLineNumber?: string | undefined, endTime?: string | undefined, integrationsFilter?: string | undefined, nanosecondPrecision?: string | undefined, openIntervals?: string | undefined, overlayCharts?: string | undefined, queryObject?: string | undefined, quickRange?: string | undefined, resolution?: string | undefined, resultsHistogram?: string | undefined, selection?: string | undefined, shownColumns?: string | undefined, startColumn?: string | undefined, startLineNumber?: string | undefined, startTime?: string | undefined, timeSeriesVariant?: string | undefined, timeSeriesView?: string | undefined}; export type APLRequestWithOptions = {apl: string, cursor?: string | undefined, defaultLimit?: number | undefined, defaultOrder?: (Array<{desc?: boolean | undefined, field?: string | undefined}>) | undefined, endTime?: string | undefined, includeCursor?: boolean | undefined, includeCursorField?: boolean | undefined, libraries?: Array | undefined, maxBinAutoGroups?: number | undefined, queryOptions?: QueryOptions | undefined, startTime?: string | undefined, variables?: {[key: string]: Record} | undefined}; export type CreateApiQueryAplFormatTabularArgs = {format: 'legacy' | 'tabular' | 'tabular-rows', nocache?: boolean | undefined, saveAsKind?: string | undefined, id?: string | undefined, 'streaming-duration'?: string | undefined, 'apl-source'?: string | undefined, 'apl-source-id'?: string | undefined, totals?: boolean | undefined, body: APLRequestWithOptions}; export type CreateApiQueryAplFormatTabularOptions = {baseUrl: string, Auth?: string | undefined, fetch?: typeof fetch | undefined}; export const aplResult = z.object({}); 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 }; /** * Runs an APL query against datasets in this edge deployment and returns the result in the requested `format`. Returns `429` when an organization, token, or user query cost limit is exhausted. * * POST /query/_apl?format=tabular */ export const createApiQueryAplFormatTabular = async (args: CreateApiQueryAplFormatTabularArgs, options: CreateApiQueryAplFormatTabularOptions) => { const {baseUrl, fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.format != null) query.append("format", String(args.format)) if (args.nocache != null) query.append("nocache", String(args.nocache)) if (args.saveAsKind != null) query.append("saveAsKind", String(args.saveAsKind)) if (args.id != null) query.append("id", String(args.id)) if (args['streaming-duration'] != null) query.append("streaming-duration", String(args['streaming-duration'])) if (args['apl-source'] != null) query.append("apl-source", String(args['apl-source'])) if (args['apl-source-id'] != null) query.append("apl-source-id", String(args['apl-source-id'])) if (args.totals != null) query.append("totals", String(args.totals)) if (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/query/_apl?format=tabular${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(aplResult, await res.json()) };