diff --git a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.ikibanasearchresponse.md b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.ikibanasearchresponse.md index 1d3e0c08dfc18..6c87457123bc4 100644 --- a/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.ikibanasearchresponse.md +++ b/docs/development/plugins/data/public/kibana-plugin-plugins-data-public.ikibanasearchresponse.md @@ -7,7 +7,7 @@ Signature: ```typescript -export interface IKibanaSearchResponse +export interface IKibanaSearchResponse extends BatchResultBase ``` ## Properties diff --git a/examples/bfetch_explorer/public/plugin.tsx b/examples/bfetch_explorer/public/plugin.tsx index f96a900063340..0d13326984ea2 100644 --- a/examples/bfetch_explorer/public/plugin.tsx +++ b/examples/bfetch_explorer/public/plugin.tsx @@ -8,6 +8,7 @@ import { Plugin, CoreSetup, AppNavLinkStatus } from '../../../src/core/public'; import { BfetchPublicSetup, BfetchPublicStart } from '../../../src/plugins/bfetch/public'; +import { BatchResultBase } from '../../../src/plugins/bfetch/common'; import { mount } from './mount'; import { DeveloperExamplesSetup } from '../../developer_examples/public'; @@ -24,12 +25,16 @@ export interface BfetchExplorerStartPlugins { bfetch: BfetchPublicStart; } +interface ExampleResponse extends BatchResultBase { + num: number; +} + export class BfetchExplorerPlugin implements Plugin { public setup( core: CoreSetup, { bfetch, developerExamples }: BfetchExplorerSetupPlugins ) { - const double = bfetch.batchedFunction<{ num: number }, { num: number }>({ + const double = bfetch.batchedFunction<{ num: number }, ExampleResponse>({ url: '/bfetch_explorer/double', }); diff --git a/src/plugins/bfetch/common/batch.ts b/src/plugins/bfetch/common/batch.ts index a84d94b541ae5..ddb002b7b402f 100644 --- a/src/plugins/bfetch/common/batch.ts +++ b/src/plugins/bfetch/common/batch.ts @@ -10,11 +10,20 @@ export interface ErrorLike { message: string; } +export interface BatchResultBase { + meta?: { + size: number; + }; +} + export interface BatchRequestData { batch: Item[]; } -export interface BatchResponseItem { +export interface BatchResponseItem< + Result extends BatchResultBase, + Error extends ErrorLike = ErrorLike +> { id: number; result?: Result; error?: Error; diff --git a/src/plugins/bfetch/public/batching/create_streaming_batched_function.test.ts b/src/plugins/bfetch/public/batching/create_streaming_batched_function.test.ts index 1c7c44d7c9aa5..d2340fca459ee 100644 --- a/src/plugins/bfetch/public/batching/create_streaming_batched_function.test.ts +++ b/src/plugins/bfetch/public/batching/create_streaming_batched_function.test.ts @@ -310,8 +310,8 @@ describe('createStreamingBatchedFunction()', () => { expect(await isPending(promise1)).toBe(true); expect(await isPending(promise2)).toBe(false); expect(await isPending(promise3)).toBe(false); - expect(await promise2).toEqual({ foo: 'bar' }); - expect(await promise3).toEqual({ foo: 'bar 2' }); + expect(await promise2).toEqual({ foo: 'bar', meta: { size: 31 } }); + expect(await promise3).toEqual({ foo: 'bar 2', meta: { size: 33 } }); }); test('resolves falsy results', async () => { @@ -429,9 +429,9 @@ describe('createStreamingBatchedFunction()', () => { const [, error2] = await promise2; const [result3] = await promise3; - expect(result1).toEqual({ b: '1' }); + expect(result1).toEqual({ b: '1', meta: { size: 27 } }); expect(error2).toEqual({ b: '2' }); - expect(result3).toEqual({ b: '3' }); + expect(result3).toEqual({ b: '3', meta: { size: 27 } }); }); describe('when requests are aborted', () => { @@ -497,7 +497,7 @@ describe('createStreamingBatchedFunction()', () => { await new Promise((r) => setTimeout(r, 1)); const [result2] = await of(promise2); - expect(result2).toEqual({ b: '2' }); + expect(result2).toEqual({ b: '2', meta: { size: 27 } }); }); }); diff --git a/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts b/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts index 2d81331f10a88..2d7e98781c4d4 100644 --- a/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts +++ b/src/plugins/bfetch/public/batching/create_streaming_batched_function.ts @@ -13,6 +13,7 @@ import { createBatchedFunction, BatchResponseItem, ErrorLike, + BatchResultBase, } from '../../common'; import { fetchStreaming, split } from '../streaming'; import { normalizeError } from '../../common'; @@ -22,7 +23,7 @@ export interface BatchedFunctionProtocolError extends ErrorLike { code: string; } -export interface StreamingBatchedFunctionParams { +export interface StreamingBatchedFunctionParams { /** * URL endpoint that will receive a batch of requests. This endpoint is expected * to receive batch as a serialized JSON array. It should stream responses back @@ -56,7 +57,7 @@ export interface StreamingBatchedFunctionParams { * server using `params.fetchStreaming` in a POST request. Responses are streamed back * and each batch item is resolved once corresponding response is received. */ -export const createStreamingBatchedFunction = ( +export const createStreamingBatchedFunction = ( params: StreamingBatchedFunctionParams ): BatchedFunc => { const { @@ -134,6 +135,11 @@ export const createStreamingBatchedFunction = ( if (response.error) { items[response.id].future.reject(response.error); } else if (response.result !== undefined) { + if (typeof response.result === 'object') { + response.result.meta = { + size: json.length, + }; + } items[response.id].future.resolve(response.result); } } catch (e) { diff --git a/src/plugins/bfetch/public/plugin.ts b/src/plugins/bfetch/public/plugin.ts index ed97d468eec0b..45d93e3b2e488 100644 --- a/src/plugins/bfetch/public/plugin.ts +++ b/src/plugins/bfetch/public/plugin.ts @@ -8,7 +8,7 @@ import { CoreStart, PluginInitializerContext, CoreSetup, Plugin } from 'src/core/public'; import { fetchStreaming as fetchStreamingStatic, FetchStreamingParams } from './streaming'; -import { removeLeadingSlash } from '../common'; +import { removeLeadingSlash, BatchResultBase } from '../common'; import { createStreamingBatchedFunction, StreamingBatchedFunctionParams, @@ -23,7 +23,7 @@ export interface BfetchPublicStartDependencies {} export interface BfetchPublicContract { fetchStreaming: (params: FetchStreamingParams) => ReturnType; - batchedFunction: ( + batchedFunction: ( params: StreamingBatchedFunctionParams ) => BatchedFunc; } diff --git a/src/plugins/data/common/search/types.ts b/src/plugins/data/common/search/types.ts index d77a2ea62bb9a..dec6b41b5ba96 100644 --- a/src/plugins/data/common/search/types.ts +++ b/src/plugins/data/common/search/types.ts @@ -9,6 +9,7 @@ import { Observable } from 'rxjs'; import { IEsSearchRequest, IEsSearchResponse } from './es_search'; import { IndexPattern } from '..'; +import { BatchResultBase } from '../../../bfetch/common'; export type ISearchGeneric = < SearchStrategyRequest extends IKibanaSearchRequest = IEsSearchRequest, @@ -37,7 +38,7 @@ export interface ISearchClient { extend: ISearchExtendGeneric; } -export interface IKibanaSearchResponse { +export interface IKibanaSearchResponse extends BatchResultBase { /** * Some responses may contain a unique id to identify the request this response came from. */ diff --git a/src/plugins/data/public/public.api.md b/src/plugins/data/public/public.api.md index e4085abe14050..1a53f47cfa594 100644 --- a/src/plugins/data/public/public.api.md +++ b/src/plugins/data/public/public.api.md @@ -1262,10 +1262,11 @@ export interface IKibanaSearchRequest { params?: Params; } +// Warning: (ae-forgotten-export) The symbol "BatchResultBase" needs to be exported by the entry point index.d.ts // Warning: (ae-missing-release-tag) "IKibanaSearchResponse" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) -export interface IKibanaSearchResponse { +export interface IKibanaSearchResponse extends BatchResultBase { id?: string; isPartial?: boolean; isRunning?: boolean; diff --git a/test/examples/bfetch_explorer/batched_function.ts b/test/examples/bfetch_explorer/batched_function.ts index 2354d5e60d74b..d509829c87738 100644 --- a/test/examples/bfetch_explorer/batched_function.ts +++ b/test/examples/bfetch_explorer/batched_function.ts @@ -38,18 +38,27 @@ export default function ({ getService }: FtrProviderContext) { { num: 300, result: { + meta: { + size: 29, + }, num: 600, }, }, { num: 1000, result: { + meta: { + size: 30, + }, num: 2000, }, }, { num: 2000, result: { + meta: { + size: 30, + }, num: 4000, }, },