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 MetricsRequestWithOptions = {endTime: string, mpl?: string | undefined, startTime: string}; export type CreateApiQueryBatchArgs = {maxConcurrency?: number | undefined, nocache?: boolean | undefined, 'apl-source'?: string | undefined, 'apl-source-id'?: string | undefined, body: Array<{aplRequest?: APLRequestWithOptions | undefined, mplRequest?: MetricsRequestWithOptions | undefined, queryId: string}>}; export type CreateApiQueryBatchOptions = {baseUrl: string, Auth?: string | undefined, fetch?: typeof fetch | undefined}; /** * Runs several APL and MPL queries in one request and streams each result as a server-sent event when it completes. Each item sets exactly one of `aplRequest` or `mplRequest`; per-item failures, including query limit errors, arrive as `error` events. * * POST /query/batch */ export const createApiQueryBatch = async (args: CreateApiQueryBatchArgs, options: CreateApiQueryBatchOptions) => { const {baseUrl, fetch: fetchFn = fetch, Auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (args.maxConcurrency != null) query.append("maxConcurrency", String(args.maxConcurrency)) if (args.nocache != null) query.append("nocache", String(args.nocache)) 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 (Auth !== undefined) headers.set('authorization', `Bearer ${Auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/query/batch${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 undefined };