Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<b>Signature:</b>

```typescript
export interface IKibanaSearchResponse<RawResponse = any>
export interface IKibanaSearchResponse<RawResponse = any> extends BatchResultBase
```

## Properties
Expand Down
7 changes: 6 additions & 1 deletion examples/bfetch_explorer/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -24,12 +25,16 @@ export interface BfetchExplorerStartPlugins {
bfetch: BfetchPublicStart;
}

interface ExampleResponse extends BatchResultBase {
num: number;
}

export class BfetchExplorerPlugin implements Plugin {
public setup(
core: CoreSetup<BfetchExplorerStartPlugins, void>,
{ bfetch, developerExamples }: BfetchExplorerSetupPlugins
) {
const double = bfetch.batchedFunction<{ num: number }, { num: number }>({
const double = bfetch.batchedFunction<{ num: number }, ExampleResponse>({
url: '/bfetch_explorer/double',
});

Expand Down
11 changes: 10 additions & 1 deletion src/plugins/bfetch/common/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ export interface ErrorLike {
message: string;
}

export interface BatchResultBase {
meta?: {
size: number;
};
}

export interface BatchRequestData<Item> {
batch: Item[];
}

export interface BatchResponseItem<Result extends object, Error extends ErrorLike = ErrorLike> {
export interface BatchResponseItem<
Result extends BatchResultBase,
Error extends ErrorLike = ErrorLike
> {
id: number;
result?: Result;
error?: Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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 } });
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
createBatchedFunction,
BatchResponseItem,
ErrorLike,
BatchResultBase,
} from '../../common';
import { fetchStreaming, split } from '../streaming';
import { normalizeError } from '../../common';
Expand All @@ -22,7 +23,7 @@ export interface BatchedFunctionProtocolError extends ErrorLike {
code: string;
}

export interface StreamingBatchedFunctionParams<Payload, Result> {
export interface StreamingBatchedFunctionParams<Payload, Result extends BatchResultBase> {
/**
* 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
Expand Down Expand Up @@ -56,7 +57,7 @@ export interface StreamingBatchedFunctionParams<Payload, Result> {
* 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 = <Payload, Result extends object>(
export const createStreamingBatchedFunction = <Payload, Result extends BatchResultBase>(
params: StreamingBatchedFunctionParams<Payload, Result>
): BatchedFunc<Payload, Result> => {
const {
Expand Down Expand Up @@ -134,6 +135,11 @@ export const createStreamingBatchedFunction = <Payload, Result extends object>(
if (response.error) {
items[response.id].future.reject(response.error);
} else if (response.result !== undefined) {
if (typeof response.result === 'object') {
response.result.meta = {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't really like it that we are adding things to actual response, i would preffer to se meta information separate from actual responses.

also, i think this information could be useful on the server as well, and i would really prefer to keep our server/client API as similar as possible.

size: json.length,
};
}
items[response.id].future.resolve(response.result);
}
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/bfetch/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -23,7 +23,7 @@ export interface BfetchPublicStartDependencies {}

export interface BfetchPublicContract {
fetchStreaming: (params: FetchStreamingParams) => ReturnType<typeof fetchStreamingStatic>;
batchedFunction: <Payload, Result extends object>(
batchedFunction: <Payload, Result extends BatchResultBase>(
params: StreamingBatchedFunctionParams<Payload, Result>
) => BatchedFunc<Payload, Result>;
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/common/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -37,7 +38,7 @@ export interface ISearchClient {
extend: ISearchExtendGeneric;
}

export interface IKibanaSearchResponse<RawResponse = any> {
export interface IKibanaSearchResponse<RawResponse = any> extends BatchResultBase {
/**
* Some responses may contain a unique id to identify the request this response came from.
*/
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1262,10 +1262,11 @@ export interface IKibanaSearchRequest<Params = any> {
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<RawResponse = any> {
export interface IKibanaSearchResponse<RawResponse = any> extends BatchResultBase {
id?: string;
isPartial?: boolean;
isRunning?: boolean;
Expand Down
9 changes: 9 additions & 0 deletions test/examples/bfetch_explorer/batched_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down